ActionFollow.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /// <reference path="../Action.ts" />
  2. /// <reference path="../Rule.ts" />
  3. /// <reference path="../Rulebook.ts" />
  4. class ActionFollow extends Action {
  5. public static check = new Rulebook<ActionFollow>("Check Follow");
  6. public static carry = new Rulebook<ActionFollow>("Carry out Follow");
  7. public constructor (actor : Thing, ...nouns : Array<any>) {
  8. super(actor, ...nouns);
  9. this.requiresNoun = true;
  10. this.requiresVisibility = false;
  11. }
  12. public getCommandText () {
  13. return "follow " + this.getNoun(0).getPrintedName();
  14. }
  15. public static isCloseEnough (actor : Person, stalked : Thing) {
  16. let cRoom = <RoomRandom> actor.getRoom();
  17. let tRoom = <RoomRandom> stalked.getRoom();
  18. let distance = tRoom.getDistanceTo(cRoom);
  19. if (distance == 1 || distance < (actor.getStat(Attributes.Intelligence)/2)) {
  20. return true;
  21. }
  22. return false;
  23. }
  24. public checkFollowClose = ActionFollow.check.createAndAddRule({
  25. name : "Follow - Is the stalked close enough?",
  26. code : (rulebook) => {
  27. let action = <ActionFollow> rulebook.noun;
  28. let actor = <Person> action.actor;
  29. let stalked = action.getNoun(0);
  30. let cRoom = <RoomRandom> actor.getRoom();
  31. let tRoom = <RoomRandom> stalked.getRoom();
  32. if (ActionFollow.isCloseEnough(actor, stalked)) {
  33. let direction = cRoom.getAStarBestDirectionTo(tRoom);
  34. return new ActionGo(actor, direction);
  35. } else {
  36. action.stop();
  37. }
  38. }
  39. })
  40. }
  41. Elements.HyperlinkHandler.HyperlinkingRulebook.addRule(new Rule(
  42. {
  43. name : "Hyperlink - Follow",
  44. firstPriority : Rule.PRIORITY_HIGHEST,
  45. code : (rulebook : RulebookRunner<Thing>) => {
  46. let thing = <Thing> rulebook.noun;
  47. if (thing instanceof Person && thing.getRoom() != WorldState.player.getRoom() && ActionFollow.isCloseEnough(WorldState.player, thing)) {
  48. Elements.HyperlinkHandler.addAvailableAction("Follow", new ActionFollow(WorldState.player, thing));
  49. }
  50. }
  51. }
  52. ));