StoryUpdateState.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import { Camera } from "../../libraries/Camera.js";
  2. import { Point } from "../../libraries/spatial/Point.js";
  3. import { Theme } from "../../libraries/components/Theme.js"
  4. import TextWrapper from "../../libraries/TextWrapper.js";
  5. import { DomButton } from "../../libraries/components/DomButton.js";
  6. import { Save } from "../../libraries/Save.js";
  7. import { Picture } from "../../libraries/CanvasArtist.js";
  8. export class StoryUpdateState {
  9. constructor(view) {
  10. this.stateMachine = view.stateMachine
  11. this.camera = new Camera()
  12. this.camera.scale = new Point(1, 1)
  13. this.textWrapper = new TextWrapper()
  14. this.continueButton = new DomButton(50, 50, view.element, "continue")
  15. this.stageIndex = 0
  16. this.storyStages = [
  17. {header: "Introduction", text: `You are a gentleman merchant, grave-robbing and selling disreputable goods to the upper class.
  18. After spending your nights gathering materials, sell them to your discerning patrons at high prices.
  19. You seek the legendary blue diamond, buried with one of the world's elite.`},
  20. {header: "The First Day", text: `Your first night was successful!
  21. You brought your goods to market and are gaining quite the notorious reputation.`},
  22. {header: "The Pauper's Grounds", text: `Your reputation grows as fast as your coin-purse.
  23. Keep your eyes on the prize and purchase access to better graveyards.
  24. `},
  25. {header: "Shady Oaks", text: `Your reputation grows as fast as your coin-purse.
  26. Keep your eyes on the prize and purchase access to better graveyards.
  27. `},
  28. {header: "Peaceful Glen", text: `Your reputation grows as fast as your coin-purse.
  29. Keep your eyes on the prize and purchase access to better graveyards.
  30. `},
  31. {header: "Guilded Repose", text: `Your reputation grows as fast as your coin-purse.
  32. This burial ground is rumored to contain the blue diamond! Good luck!
  33. `},
  34. ]
  35. this.save = new Save()
  36. }
  37. init(scaledCanvas) {
  38. this.canvasBounds = scaledCanvas.bounds
  39. }
  40. draw(ctx, scaledCanvas) {
  41. let fontScale = this.canvasBounds.width / 500
  42. this.camera.draw(ctx, scaledCanvas, () => {
  43. ctx.fillStyle = Theme.Colors.ivory
  44. let fontSize = Math.min(Math.floor(48 * fontScale), 48)
  45. ctx.font = `${fontSize}px ${Theme.Fonts.Header}`
  46. ctx.textAlign = "center"
  47. ctx.save()
  48. ctx.translate(0, -(this.canvasBounds.height / 2) + fontSize)
  49. ctx.fillText(this.storyStages[this.stageIndex].header, 0, 0)
  50. ctx.restore()
  51. ctx.fillStyle = Theme.Colors.ivory
  52. ctx.font = `${Math.min(Math.floor(20 * fontScale), 20)}px ${Theme.Fonts.Header}`
  53. ctx.save()
  54. ctx.translate(0, -this.canvasBounds.height /2 + 200)
  55. ctx.textAlign = "center"
  56. this.textWrapper.draw(ctx, this.storyStages[this.stageIndex].text, {width: Math.min(this.canvasBounds.width, 600)})
  57. ctx.restore()
  58. ctx.save()
  59. ctx.translate(-55, 100)
  60. Picture.Diamond(ctx, {width: 100, height: 80})
  61. ctx.restore()
  62. ctx.save()
  63. ctx.translate(0, -this.canvasBounds.height / 2 + 100)
  64. Picture.DashHead(ctx, {})
  65. ctx.restore()
  66. ctx.save()
  67. ctx.translate(-Math.min(this.canvasBounds.width /4, 200), -this.canvasBounds.height / 2 + 90)
  68. Picture.Skull(ctx, {width: 80, height: 80})
  69. ctx.restore()
  70. ctx.save()
  71. ctx.translate(Math.min(this.canvasBounds.width /4, 200)- 50, -this.canvasBounds.height / 2 + 60)
  72. Picture.Bone(ctx, {width: 80, height: 80})
  73. ctx.restore()
  74. })
  75. this.continueButton.setPosition(
  76. this.canvasBounds.width * (4 / 8),
  77. this.canvasBounds.height * (7 / 8)
  78. )
  79. this.continueButton.draw(ctx, scaledCanvas)
  80. }
  81. update(delta) {
  82. this.camera.update(delta)
  83. this.continueButton.update(delta)
  84. if(this.stageIndex > this.storyStages.length - 1) {
  85. this.stageIndex = 0
  86. }
  87. }
  88. enter() {
  89. this.registeredEvents = {}
  90. this.registeredEvents["resize"] = this.onResize.bind(this)
  91. this.registeredEvents["keydown"] = this.onKeyDown.bind(this)
  92. this.registeredEvents["keyup"] = this.onKeyUp.bind(this)
  93. this.registeredEvents["touchstart"] = this.onTouchStart.bind(this)
  94. this.registeredEvents["touchmove"] = this.onTouchMove.bind(this)
  95. this.registeredEvents["touchend"] = this.onTouchEnd.bind(this)
  96. this.registeredEvents["mousedown"] = this.onMouseDown.bind(this)
  97. this.registeredEvents["mousemove"] = this.onMouseMove.bind(this)
  98. this.registeredEvents["mouseup"] = this.onMouseUp.bind(this)
  99. for (let index in this.registeredEvents) {
  100. window.addEventListener(index, this.registeredEvents[index])
  101. }
  102. this.continueButton.attach()
  103. this.continueButton.onClick(this.goToGame.bind(this))
  104. this.stageIndex = this.save.getKey('story-stage') == null ? 0 : this.save.getKey('story-stage')
  105. if(this.save.getKey("graveyard-1") == true && this.save.getKey("story-stage") == 2) {
  106. this.stageIndex = 3
  107. }
  108. if(this.save.getKey("graveyard-2") == true && this.save.getKey("story-stage") == 3) {
  109. this.stageIndex = 4
  110. }
  111. if(this.save.getKey("graveyard-3") == true && this.save.getKey("story-stage") == 4) {
  112. this.stageIndex = 5
  113. }
  114. }
  115. leave() {
  116. for (let index in this.registeredEvents) {
  117. window.removeEventListener(index, this.registeredEvents[index])
  118. }
  119. this.continueButton.remove()
  120. }
  121. goToGame() {
  122. this.save.setKey("story-stage", this.stageIndex)
  123. if(this.save.getKey("story-stage") == 1) {
  124. this.save.setKey("story-stage", 2)
  125. }
  126. this.stateMachine.transitionTo("dig")
  127. }
  128. onResize() {
  129. }
  130. onKeyDown(event) {
  131. }
  132. onKeyUp(event) {
  133. switch (event.code) {
  134. case "Enter":
  135. this.goToGame()
  136. break
  137. }
  138. }
  139. onTouchStart(event) {
  140. }
  141. onTouchMove(event) {
  142. }
  143. onTouchEnd(event) {
  144. }
  145. onMouseDown(event) {
  146. }
  147. onMouseMove(event) {
  148. }
  149. onMouseUp(event) {
  150. }
  151. }