HyperlinkHandler.ts 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /// <reference path="../Elements.ts" />
  2. /// <reference path="../../World/Classes/Rulebook.ts" />
  3. module Elements.HyperlinkHandler {
  4. export var linkedActionsTab = document.getElementById("linkActions");
  5. export var commonActionsTab = document.getElementById("commonActionsTab");
  6. var currentCommand = document.createTextNode("");
  7. document.getElementById("currentCommand").appendChild(currentCommand);
  8. var currentActionTarget = document.createTextNode("");
  9. document.getElementById("linkTarget").appendChild(currentActionTarget);
  10. var commonActions = [];
  11. var availableActions = [];
  12. export function resetCommonActions () {
  13. commonActions.splice(0, commonActions.length);
  14. while (commonActionsTab.firstChild) {
  15. commonActionsTab.removeChild(commonActionsTab.firstChild);
  16. }
  17. }
  18. export function addCommonAction (name : string, action : Action) {
  19. commonActions.push([name, action]);
  20. }
  21. function resetAvailableActions () {
  22. availableActions = [];
  23. currentActionTarget.nodeValue = "";
  24. while (linkedActionsTab.firstChild) {
  25. linkedActionsTab.removeChild(linkedActionsTab.firstChild);
  26. }
  27. }
  28. export function addAvailableAction (name : string, action : Action) {
  29. availableActions.push([name, action]);
  30. }
  31. export function hoverAction (action : Action) {
  32. currentCommand.nodeValue = action.getCommandText().toLowerCase();
  33. }
  34. export function unhoverAction () {
  35. currentCommand.nodeValue = "";
  36. }
  37. export async function hyperlinkObject (thing? : any) {
  38. resetAvailableActions();
  39. if (thing instanceof Thing && thing != WorldState.player) {
  40. await HyperlinkingRulebook.execute({noun: thing});
  41. currentActionTarget.nodeValue = thing.getPrintedName() + ": ";
  42. for (let i = 0, value = availableActions[i]; value != undefined; value = availableActions[++i]) {
  43. let link = createLink(value);
  44. link.classList.add("columnLink");
  45. Controls.KeyHandler.applyCode(link, Controls.KeyHandler.getSecondKeyCode());
  46. linkedActionsTab.appendChild(link);
  47. }
  48. }
  49. }
  50. function createLink (value : Array<any>) {
  51. let link = document.createElement("a");
  52. link.appendChild(document.createTextNode(value[0]));
  53. Controls.Links.makeLink(link, value[1]);
  54. return link;
  55. }
  56. export async function hyperlinkCommonActions () {
  57. resetCommonActions();
  58. await CommonActionsRulebook.execute({});
  59. for (let i = 0, value = commonActions[i]; value != undefined; value = commonActions[++i]) {
  60. let link = createLink(value);
  61. link.classList.add("lineLink");
  62. Controls.KeyHandler.applyCode(link, Controls.KeyHandler.getThirdKeyCode());
  63. commonActionsTab.appendChild(link);
  64. }
  65. }
  66. /**
  67. * The hyperlinking rulebook is ran over the last thing the player interacted with.
  68. * It populates the hyperlinks tab with information on the thing that was interacted with.
  69. * @type {Rulebook}
  70. */
  71. export var HyperlinkingRulebook = new Rulebook<Thing>("Hyperlinking something");
  72. /**
  73. * The common actions rulebook serves to set up all the available common actions for the player.
  74. * It should check the player's current state to see if a command is applicable and
  75. * then ADD said command to the HyperlinkHandler.
  76. * @type {Rulebook}
  77. */
  78. export var CommonActionsRulebook = new Rulebook<void>("Common Actions Rulebook");
  79. }