ContentMoleculeCombat.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. });
  56. return finalSay;
  57. }
  58. }
  59. }
  60. // new ContentMoleculeCombat(
  61. // (attacker, target, weapons, markers) => {
  62. // return new Say("Woohoo");
  63. // }
  64. // ).addAtom(
  65. // new ContentAtomCombat(
  66. // Person,
  67. // Person,
  68. // [Thing],
  69. // [new ContentNoun(ContentAtomCombat.HIT, ContentAtomCombat.CRITICAL).setType(ContentNounType.FULLY_ADAPTIVE), ContentAtomCombat.KNOCKED_OFF]
  70. // )
  71. // );
  72. // Definitely tons saner than the current implementation