ContentMoleculeCombat.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. ///<reference path="../ContentMolecule.ts"/>
  2. class ContentMoleculeCombat extends ContentMolecule {
  3. protected say : Say | ((attacker : any, target : any, weapons : Array<any>, markers : Array<ContentNounSimple>) => Say);
  4. protected saidTimes = 0;
  5. public constructor (description : Say | ((attacker : any, target : any, weapons : Array<any>, markers : Array<ContentNounSimple>) => Say)) {
  6. super();
  7. this.say = description;
  8. ContentMoleculeCombat.MOLECULES.push(this);
  9. console.warn(this);
  10. }
  11. public getSay(specificMolecule : ContentMolecule) {
  12. this.saidTimes++;
  13. if (this.say instanceof Say) {
  14. return this.say;
  15. } else {
  16. let combatAtom = <ContentAtomCombat>(specificMolecule.getAtoms()[0]);
  17. return this.say(combatAtom.attacker, combatAtom.target, combatAtom.weapons.getNouns(), combatAtom.markers.getNouns());
  18. }
  19. }
  20. public getWeight () {
  21. // TODO: Check if this implementation is good enough.
  22. let weight = -this.saidTimes;
  23. this.atoms.forEach(value => {
  24. weight += value.getAtomPriority()
  25. });
  26. return weight;
  27. }
  28. protected static MOLECULES : Array<ContentMoleculeCombat> = [];
  29. /**
  30. * Searches for Combat molecules that describe the specificMolecule given. Assumes Atoms are of the Combat variety. Assumes there is always a single Atom.
  31. * @param specificMolecule
  32. */
  33. public static getSay (specificMolecule : ContentMolecule) {
  34. // Randomize the array to prevent the same message from showing up every time
  35. let many : Array<ContentMoleculeCombat> = new Shuffler(ContentMoleculeCombat.MOLECULES).getShuffled();
  36. // Order the array placing higher priority messages on top, but messages with same priority are still randomized
  37. many.sort((a, b) => {
  38. return b.getWeight() - a.getWeight()
  39. });
  40. let matches = specificMolecule.findMatches(many);
  41. if (matches.length == 0) {
  42. let atom = <ContentAtomCombat> specificMolecule.getAtoms()[0];
  43. Elements.CurrentTurnHandler.printAsError(
  44. new Say(
  45. "Unable to find match for this combat passage. Attacker: ", atom.attacker,
  46. ". Target: ", atom.target,
  47. ". Weapons used:", ...(atom.weapons.getNouns()),
  48. ". Markers: ", ...(atom.markers.getNouns()),
  49. ". Please report for fixing."
  50. )
  51. );
  52. return new Say();
  53. } else {
  54. let finalSay = new Say();
  55. matches.forEach((idx : number) => {
  56. finalSay.add(ContentMoleculeCombat.MOLECULES[idx].getSay(specificMolecule));
  57. finalSay.add(" ");
  58. });
  59. return finalSay;
  60. }
  61. }
  62. }