Home Reference Source

src/jg/gameformat/game.js

  1. /**
  2. * The object you pass to the {@link jumbogrove} function.
  3. *
  4. * @example
  5. * const game = {
  6. * id: 'jg-example',
  7. * version: 1,
  8. * globalState: {
  9. * aliensHaveInvaded: false,
  10. * },
  11. * willEnter: (model, ui, previousId, nextId) => {
  12. * console.log("Transitioning from", previousId, "to", nextId);
  13. * },
  14. * };
  15. * jumbogrove('#app', game);
  16. */
  17. class game {
  18. /** @ignore */
  19. constructor() {
  20. /**
  21. * An ID unique to your game. Used to determine save location.
  22. * @type {string}
  23. */
  24. this.id = null;
  25.  
  26. /**
  27. * The current version of your game. Used to determine save location.
  28. * @type {number}
  29. */
  30. this.version = null;
  31.  
  32. /**
  33. * The ID of the first situation you want to show the player.
  34. * @type {string}
  35. */
  36. this.initialSituation = 'start';
  37.  
  38. /**
  39. * If you set this to `false`, the left sidebar will not be shown.
  40. * Default `true`.
  41. * @type {Boolean}
  42. */
  43. this.showNav = true;
  44.  
  45. /**
  46. * If you set this to `false`, the right sidebar will not be shown.
  47. * Default `true`.
  48. * @type {Boolean}
  49. */
  50. this.showAside = true;
  51.  
  52. /**
  53. * If you set this to `false`, fewer styles will be applied to
  54. * the HTML.
  55. * Default `true`.
  56. * @type {Boolean}
  57. */
  58. this.defaultStylesheet = true;
  59.  
  60. /**
  61. * If you set this to `false`, the browser will not scroll as new text
  62. * is added.
  63. * Default `true`.
  64. * @type {Boolean}
  65. */
  66. this.autoScroll = true;
  67.  
  68. /**
  69. * The Markdown string you want to show at the top of the left sidebar.
  70. * @type {string}
  71. */
  72. this.navHeader = '';
  73.  
  74. /**
  75. * The Markdown string you want to show at the top of the right sidebar.
  76. * This string is processed by the template engine before being displayed.
  77. * @type {string}
  78. */
  79. this.asideHeader = '';
  80.  
  81. /**
  82. * The Markdown string you want to show whenever the game is saved.
  83. * @type {string}
  84. */
  85. this.gameSaveMessage = null;
  86.  
  87. /**
  88. * The initial value of {@link model.globalState}. Must be JSON-safe.
  89. * @type {*}
  90. */
  91. this.globalState = {};
  92.  
  93. /**
  94. * List of {@link character} definitions.
  95. * **Note:** this is not the same as the {@link Character} class!
  96. * There's a difference between what you write in your game definition, and what gets passed to the
  97. * various callbacks.
  98. * @type {character}
  99. */
  100. this.characters = [];
  101.  
  102. /**
  103. * List of {@link situation} definitions.
  104. * **Note:** this is not the same as the {@link Situation} class!
  105. * There's a difference between what you write in your game definition, and what gets passed to the
  106. * various callbacks.
  107. * @type {situation}
  108. */
  109. this.situations = [];
  110. }
  111.  
  112. /**
  113. * Called immediately after all initial objects have been created, but before the
  114. * first situation has been entered. This is where you would add Markdown plugins
  115. * and Nunjucks filters.
  116. * @param {model} model
  117. * @param {ui} ui
  118. */
  119. init(model, ui) { }
  120.  
  121. /**
  122. * Called when the game has requested to enter a new situation. This function returns
  123. * a truthy value (`true`) if this should be allowed, `false` if not.
  124. *
  125. * It is safe to call `model.do()` or `model.goTo()` from `willEnter()`, as long as
  126. * you return `false` afterward.
  127. *
  128. * @param {model} model
  129. * @param {ui} ui
  130. * @param {string} previousId
  131. * @param {string} nextId
  132. * @returns {Boolean}
  133. */
  134. willEnter(model, ui, previousId, nextId) { }
  135. /**
  136. * Called when the game has just entered a new situation.
  137. * @param {model} model
  138. * @param {ui} ui
  139. * @param {string} previousId
  140. * @param {string} nextId
  141. */
  142. didEnter(model, ui, previousId, nextId) { }
  143.  
  144. /**
  145. * Called when the game is about to exit a situation.
  146. * @param {model} model
  147. * @param {ui} ui
  148. * @param {string} thisId
  149. * @param {string} nextId
  150. */
  151. willExit(model, ui, thisId, nextId) { }
  152.  
  153. /**
  154. * Called when the game just exited a situation.
  155. * @param {model} model
  156. * @param {ui} ui
  157. * @param {string} thisId
  158. * @param {string} nextId
  159. */
  160. didExit(model, ui, thisId, nextId) { }
  161.  
  162. /**
  163. * Called when the game is about to execute the given action on the given sitaution.
  164. * @param {model} model
  165. * @param {ui} ui
  166. * @param {Situation} situation
  167. * @param {string} action
  168. */
  169. willAct(model, ui, situation, action) { }
  170.  
  171. /**
  172. * Called when the game is just executed the given action on the given sitaution.
  173. * @param {model} model
  174. * @param {ui} ui
  175. * @param {Situation} situation
  176. * @param {string} action
  177. */
  178. didAct(model, ui, situation, action) { }
  179. };
  180. export default game;