123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- ///<reference path="../ContentMolecule.ts"/>
- class ContentMoleculeCombat extends ContentMolecule {
- protected say : Say | ((attacker : any, target : any, weapons : Array<any>, markers : Array<ContentNounSimple>) => Say);
- protected saidTimes = 0;
- public constructor (description : Say | ((attacker : any, target : any, weapons : Array<any>, markers : Array<ContentNounSimple>) => Say)) {
- super();
- this.say = description;
- ContentMoleculeCombat.MOLECULES.push(this);
- console.warn(this);
- }
- public getSay(specificMolecule : ContentMolecule) {
- this.saidTimes++;
- if (this.say instanceof Say) {
- return this.say;
- } else {
- let combatAtom = <ContentAtomCombat>(specificMolecule.getAtoms()[0]);
- return this.say(combatAtom.attacker, combatAtom.target, combatAtom.weapons.getNouns(), combatAtom.markers.getNouns());
- }
- }
- public getWeight () {
- // TODO: Check if this implementation is good enough.
- let weight = -this.saidTimes;
- this.atoms.forEach(value => {
- weight += value.getAtomPriority()
- });
- return weight;
- }
- protected static MOLECULES : Array<ContentMoleculeCombat> = [];
- /**
- * Searches for Combat molecules that describe the specificMolecule given. Assumes Atoms are of the Combat variety. Assumes there is always a single Atom.
- * @param specificMolecule
- */
- public static getSay (specificMolecule : ContentMolecule) {
- // Randomize the array to prevent the same message from showing up every time
- let many : Array<ContentMoleculeCombat> = new Shuffler(ContentMoleculeCombat.MOLECULES).getShuffled();
- // Order the array placing higher priority messages on top, but messages with same priority are still randomized
- many.sort((a, b) => {
- return b.getWeight() - a.getWeight()
- });
- let matches = specificMolecule.findMatches(many);
- if (matches.length == 0) {
- let atom = <ContentAtomCombat> specificMolecule.getAtoms()[0];
- Elements.CurrentTurnHandler.printAsError(
- new Say(
- "Unable to find match for this combat passage. Attacker: ", atom.attacker,
- ". Target: ", atom.target,
- ". Weapons used:", ...(atom.weapons.getNouns()),
- ". Markers: ", ...(atom.markers.getNouns()),
- ". Please report for fixing."
- )
- );
- return new Say();
- } else {
- let finalSay = new Say();
- matches.forEach((idx : number) => {
- finalSay.add(ContentMoleculeCombat.MOLECULES[idx].getSay(specificMolecule));
- finalSay.add(" ");
- });
- return finalSay;
- }
- }
- }
|