1
1

HyperlinkHandler.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. if (availableActions.length > 0) {
  42. currentActionTarget.nodeValue = thing.getPrintedName() + ": ";
  43. for (let i = 0, value = availableActions[i]; value != undefined; value = availableActions[++i]) {
  44. let link = createLink(value);
  45. link.classList.add("columnLink");
  46. Controls.KeyHandler.applyCode(link, Controls.KeyHandler.getSecondKeyCode());
  47. linkedActionsTab.appendChild(link);
  48. }
  49. }
  50. }
  51. }
  52. function createLink (value : Array<any>) {
  53. let link = document.createElement("a");
  54. link.appendChild(document.createTextNode(value[0]));
  55. Controls.Links.makeLink(link, value[1]);
  56. return link;
  57. }
  58. export async function hyperlinkCommonActions () {
  59. resetCommonActions();
  60. await CommonActionsRulebook.execute({});
  61. for (let i = 0, value = commonActions[i]; value != undefined; value = commonActions[++i]) {
  62. let link = createLink(value);
  63. link.classList.add("lineLink");
  64. Controls.KeyHandler.applyCode(link, Controls.KeyHandler.getThirdKeyCode());
  65. commonActionsTab.appendChild(link);
  66. }
  67. }
  68. /**
  69. * The hyperlinking rulebook is ran over the last thing the player interacted with.
  70. * It populates the hyperlinks tab with information on the thing that was interacted with.
  71. * @type {Rulebook}
  72. */
  73. export var HyperlinkingRulebook = new Rulebook<Thing>("Hyperlinking something");
  74. /**
  75. * The common actions rulebook serves to set up all the available common actions for the player.
  76. * It should check the player's current state to see if a command is applicable and
  77. * then ADD said command to the HyperlinkHandler.
  78. * @type {Rulebook}
  79. */
  80. export var CommonActionsRulebook = new Rulebook<void>("Common Actions Rulebook");
  81. }