ContentMoleculeCombat.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. }
  10. public getSay(specificMolecule : ContentMolecule) {
  11. this.saidTimes++;
  12. if (this.say instanceof Say) {
  13. return this.say;
  14. } else {
  15. let combatAtom = <ContentAtomCombat>(specificMolecule.getAtoms()[0]);
  16. return this.say(combatAtom.attacker, combatAtom.target, combatAtom.weapons.getNouns(), combatAtom.markers.getNouns());
  17. }
  18. }
  19. public getWeight () {
  20. // TODO: Check if this implementation is good enough.
  21. let weight = -this.saidTimes;
  22. this.atoms.forEach(value => {
  23. weight += value.getAtomPriority()
  24. });
  25. return weight;
  26. }
  27. protected static MOLECULES = [];
  28. /**
  29. * Returns an array of ContentMoleculeCombat that fulfill ContentMolecule given.
  30. * @param specificMolecule
  31. */
  32. public static getSay (specificMolecule : ContentMolecule) {
  33. // Randomize the array to prevent the same message from showing up every time
  34. let many : Array<ContentMoleculeCombat> = new Shuffler(ContentMoleculeCombat.MOLECULES).getShuffled();
  35. // Order the array placing higher priority messages on top, but messages with same priority are still randomized
  36. many.sort((a, b) => {
  37. return b.getWeight() - a.getWeight()
  38. });
  39. let matches = specificMolecule.findMatches(many);
  40. if (matches.length == 0) {
  41. let atom = <ContentAtomCombat> specificMolecule.getAtoms()[0];
  42. Elements.CurrentTurnHandler.printAsError(
  43. new Say(
  44. "Unable to find match for this combat passage. Attacker: ", atom.attacker,
  45. ". Target: ", atom.target,
  46. ". Weapons used:", ...(atom.weapons.getNouns()),
  47. ". Markers: ", ...(atom.markers.getNouns()),
  48. ". Please report for fixing."
  49. )
  50. );
  51. } else {
  52. let finalSay = new Say();
  53. matches.forEach((value : ContentMoleculeCombat) => {
  54. finalSay.add(value.getSay(specificMolecule));
  55. finalSay.add(" ");
  56. });
  57. return finalSay;
  58. }
  59. }
  60. }
  61. // new ContentMoleculeCombat(
  62. // (attacker, target, weapons, markers) => {
  63. // return new Say("Woohoo");
  64. // }
  65. // ).addAtom(
  66. // new ContentAtomCombat(
  67. // Person,
  68. // Person,
  69. // [Thing],
  70. // [new ContentNoun(ContentAtomCombat.HIT, ContentAtomCombat.CRITICAL).setType(ContentNounType.FULLY_ADAPTIVE), ContentAtomCombat.KNOCKED_OFF]
  71. // )
  72. // );
  73. // Definitely tons saner than the current implementation