ActionFollow.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. if (cRoom instanceof RoomRandom && tRoom instanceof RoomRandom) {
  19. let distance = tRoom.getDistanceTo(cRoom);
  20. if (distance == 1 || distance < (actor.getStat(Attributes.Intelligence)/2)) {
  21. return true;
  22. }
  23. }
  24. return false;
  25. }
  26. public checkFollowClose = ActionFollow.check.createAndAddRule({
  27. name : "Follow - Is the stalked close enough?",
  28. code : (rulebook) => {
  29. let action = <ActionFollow> rulebook.noun;
  30. let actor = <Person> action.actor;
  31. let stalked = action.getNoun(0);
  32. let cRoom = <RoomRandom> actor.getRoom();
  33. let tRoom = <RoomRandom> stalked.getRoom();
  34. if (ActionFollow.isCloseEnough(actor, stalked)) {
  35. let direction = cRoom.getAStarBestDirectionTo(tRoom);
  36. return new ActionGo(actor, direction);
  37. } else {
  38. action.stop();
  39. }
  40. }
  41. })
  42. }
  43. Elements.HyperlinkHandler.HyperlinkingRulebook.addRule(new Rule(
  44. {
  45. name : "Hyperlink - Follow",
  46. firstPriority : Rule.PRIORITY_HIGHEST,
  47. code : (rulebook : RulebookRunner<Thing>) => {
  48. let thing = <Thing> rulebook.noun;
  49. if (thing instanceof Person && thing.getRoom() != WorldState.player.getRoom() && ActionFollow.isCloseEnough(WorldState.player, thing)) {
  50. Elements.HyperlinkHandler.addAvailableAction("Follow", new ActionFollow(WorldState.player, thing));
  51. }
  52. }
  53. }
  54. ));