1
1

RoomHandler.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /// <reference path="../Elements.ts" />
  2. /// <reference path="../../World/Classes/Action/ActionExamine.ts" />
  3. /// <reference path="../../World/Classes/Action/ActionGo.ts" />
  4. module Elements.RoomHandler {
  5. export var currentRoomTab = document.getElementById("currentRoomTab");
  6. export var currentRoomDescription = document.getElementById("roomDescription");
  7. export var currentRoomExits = document.getElementById("roomExits");
  8. export var currentRoomName : Text = document.createTextNode("");
  9. document.getElementById("roomName").appendChild(currentRoomName);
  10. export function linkObjects () {
  11. let objs = currentRoomTab.getElementsByClassName("roomObject");
  12. for (let i = 0; i < objs.length; i++) {
  13. let linkKeyCode = Controls.KeyHandler.getFirstKeyCode();
  14. Controls.KeyHandler.applyCode(<HTMLElement> objs[i], linkKeyCode);
  15. }
  16. }
  17. export function emptyRoom () {
  18. while (currentRoomDescription.firstChild) {
  19. currentRoomDescription.removeChild(currentRoomDescription.firstChild);
  20. }
  21. while (currentRoomExits.firstChild) {
  22. currentRoomExits.removeChild(currentRoomExits.firstChild);
  23. }
  24. currentRoomName.nodeValue = "";
  25. }
  26. export async function updateRoom () {
  27. emptyRoom();
  28. let room = WorldState.player.getRoom();
  29. if (room != undefined) {
  30. currentRoomName.nodeValue = room.getPrintedName();
  31. let description = room.description.getHTML("p", ["roomDescription"]);
  32. await description.then(value => {
  33. for (let i = 0, p = value[i]; p != undefined; p = value[++i]) {
  34. currentRoomDescription.appendChild(p);
  35. }
  36. });
  37. let things = room.getContainedAndVisible();
  38. if (things.length > 0) {
  39. let thingList = document.createElement("p");
  40. thingList.classList.add("roomDescription");
  41. await PrintingVisibleThingsRulebook.execute({
  42. noun : <VisibleThingsOptions> {
  43. things : things,
  44. container : thingList
  45. }
  46. });
  47. currentRoomDescription.appendChild(thingList);
  48. }
  49. for (let index = 0, value = room.connections[index]; index < room.connections.length; value = room.connections[++index]) {
  50. if (value != undefined) {
  51. let p = document.createElement("p");
  52. p.classList.add("roomExit");
  53. let link = document.createElement("a");
  54. link.classList.add("roomDirection");
  55. link.appendChild(document.createTextNode(DirectionNames[Direction[index]]));
  56. Controls.Links.makeLink(link, new ActionGo(WorldState.player, index));
  57. Controls.KeyHandler.applyCode(link, Controls.KeyHandler.getDirectionCodeByIndex(index));
  58. p.appendChild(link);
  59. let directionResult = ": ";
  60. if (WorldState.isRoomRemembered(value)) {
  61. directionResult += value.getPrintedName();
  62. } else {
  63. directionResult += "A new place";
  64. }
  65. p.appendChild(document.createTextNode(directionResult));
  66. currentRoomExits.appendChild(p);
  67. }
  68. }
  69. }
  70. }
  71. export interface VisibleThingsOptions {
  72. container : HTMLElement,
  73. things : Array<Thing>
  74. }
  75. /**
  76. * Noun = VisibleThingsOptions
  77. * @type {Rulebook}
  78. */
  79. export var PrintingVisibleThingsRulebook = new Rulebook("Printing the name of visible things in a room");
  80. export var PrintIntroToVisibleThingsRule = new Rule({
  81. name : "Print \"You can see\" text",
  82. firstPriority : Rule.PRIORITY_HIGH,
  83. code : (rulebook : RulebookRunner<VisibleThingsOptions>) => {
  84. let noun = <VisibleThingsOptions> rulebook.noun;
  85. noun.container.appendChild(document.createTextNode("You can see "));
  86. }
  87. });
  88. PrintingVisibleThingsRulebook.addRule(PrintIntroToVisibleThingsRule);
  89. export var PrintVisibleThingsRule = new Rule({
  90. name : "Print all visible things",
  91. code : async (rulebook : RulebookRunner<VisibleThingsOptions>) => {
  92. let noun = <VisibleThingsOptions> rulebook.noun;
  93. for (let i = 0; i < noun.things.length; i++) {
  94. let value = noun.things[i];
  95. let link = document.createElement("a");
  96. link.classList.add("roomObject");
  97. let say : Say
  98. if (value instanceof Thing && value.unique) {
  99. say = new Say(new SayThe(false), value);
  100. } else {
  101. say = new Say(new SayAn(false), value);
  102. }
  103. await say.getPureElements().then(value2 => {
  104. for (let i = 0, element = value2[i]; element != undefined; element = value2[++i]) {
  105. link.appendChild(element);
  106. }
  107. });
  108. Controls.Links.makeLink(link, new ActionExamine(WorldState.player, value));
  109. Controls.KeyHandler.applyCode(link, Controls.KeyHandler.getFirstKeyCode());
  110. noun.container.appendChild(link);
  111. if ((i + 1) < noun.things.length) {
  112. noun.container.appendChild(document.createTextNode(", "));
  113. }
  114. }
  115. }
  116. });
  117. PrintingVisibleThingsRulebook.addRule(PrintVisibleThingsRule);
  118. export var PrintOutroToVisibleThingsRule = new Rule({
  119. name : "Print \"... here\" text",
  120. firstPriority : Rule.PRIORITY_LOW,
  121. code : (rulebook : RulebookRunner<VisibleThingsOptions>) => {
  122. let noun = <VisibleThingsOptions> rulebook.noun;
  123. noun.container.appendChild(document.createTextNode(" here."));
  124. }
  125. });
  126. PrintingVisibleThingsRulebook.addRule(PrintOutroToVisibleThingsRule);
  127. }