UpgradeState.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 { DomButton } from "../../libraries/components/DomButton.js";
  5. import { Picture } from "../../libraries/CanvasArtist.js";
  6. import UpgradeButton from "../../libraries/UpgradeButton.js";
  7. import { Save } from "../../libraries/Save.js";
  8. export class UpgradeState {
  9. constructor(view) {
  10. this.stateMachine = view.stateMachine
  11. this.camera = new Camera()
  12. this.camera.scale = new Point(1, 1)
  13. this.continueButton = new DomButton(50, 50, view.element, "continue", "ContinueButton")
  14. this.save = new Save()
  15. this.upgrades = []
  16. }
  17. init(scaledCanvas) {
  18. this.canvasBounds = scaledCanvas.bounds
  19. }
  20. draw(ctx, scaledCanvas) {
  21. let fontScale = this.canvasBounds.width / 500
  22. let fontSize = Math.min(Math.floor(48 * fontScale), 48)
  23. this.camera.draw(ctx, scaledCanvas, () => {
  24. ctx.fillStyle = "gray"
  25. ctx.beginPath()
  26. ctx.rect(-this.canvasBounds.width / 2, -this.canvasBounds.height / 2, this.canvasBounds.width, 128)
  27. ctx.fill()
  28. ctx.fillStyle = Theme.Colors.ivory
  29. ctx.font = `${fontSize}px ${Theme.Fonts.Header}`
  30. ctx.textAlign = "center"
  31. ctx.fillText("Upgrade", 0, -this.canvasBounds.height / 2 + fontSize + 24)
  32. ctx.save()
  33. ctx.translate(- Theme.Game.TileSize.width / 2, -this.canvasBounds.height / 2 + 128 + Theme.Game.TileSize.height - Theme.Game.TileSize.height / 2)
  34. this.upgrades.forEach((row, index) => {
  35. ctx.save()
  36. ctx.translate((index * this.canvasBounds.width / this.upgrades.length) - this.canvasBounds.width / this.upgrades.length, 24)
  37. this.drawUpgradeRow(ctx, scaledCanvas, row)
  38. ctx.restore()
  39. })
  40. ctx.fillStyle = Theme.Colors.darkgreen
  41. ctx.textAlign = "right"
  42. ctx.fillText(`$${this.getDollars()}`, (this.canvasBounds.width / 2) - 48, -48)
  43. ctx.restore()
  44. }) //end camera
  45. this.continueButton.setPosition(this.canvasBounds.width / 2, Math.max(this.canvasBounds.height * (7 / 8), (this.incrementRow()) * 5 + 128))
  46. this.continueButton.draw(ctx, scaledCanvas)
  47. }
  48. getDollars() {
  49. return this.save.getKey("money")
  50. }
  51. incrementRow() {
  52. return Math.max(this.canvasBounds.height / 7, Theme.Game.TileSize.width + Theme.Game.TileSize.spacing * 4)
  53. }
  54. drawUpgradeRow(ctx, scaledCanvas, row) {
  55. ctx.textAlign = "center"
  56. ctx.fillStyle = Theme.Colors.ivory
  57. let fontScale = this.canvasBounds.width / 500
  58. let fontSize = Math.min(Math.floor(24 * fontScale), 24)
  59. ctx.font = `${fontSize}px ${Theme.Fonts.Header}`
  60. ctx.fillText(row.header, Theme.Game.TileSize.width / 2, -fontSize)
  61. ctx.strokeStyle = Theme.Colors.ivory
  62. ctx.lineWidth = 4
  63. ctx.save()
  64. ctx.translate(Theme.Game.TileSize.width / 2, Theme.Game.TileSize.height / 2)
  65. for (let index = 0; index < row.length - 1; index++) {
  66. ctx.beginPath()
  67. ctx.moveTo(0, this.incrementRow() * index)
  68. ctx.lineTo(0, this.incrementRow() * (index + 1))
  69. ctx.stroke()
  70. }
  71. ctx.restore()
  72. row.buttons.forEach((button, index) => {
  73. ctx.save()
  74. ctx.translate(0, this.incrementRow() * index)
  75. button.draw(ctx, scaledCanvas)
  76. ctx.restore()
  77. })
  78. }
  79. update(delta) {
  80. this.camera.update(delta)
  81. this.continueButton.update(delta)
  82. }
  83. enter() {
  84. this.registeredEvents = {}
  85. this.registeredEvents["resize"] = this.onResize.bind(this)
  86. this.registeredEvents["keydown"] = this.onKeyDown.bind(this)
  87. this.registeredEvents["keyup"] = this.onKeyUp.bind(this)
  88. this.registeredEvents["touchstart"] = this.onTouchStart.bind(this)
  89. this.registeredEvents["touchmove"] = this.onTouchMove.bind(this)
  90. this.registeredEvents["touchend"] = this.onTouchEnd.bind(this)
  91. this.registeredEvents["mousedown"] = this.onMouseDown.bind(this)
  92. this.registeredEvents["mousemove"] = this.onMouseMove.bind(this)
  93. this.registeredEvents["mouseup"] = this.onMouseUp.bind(this)
  94. for (let index in this.registeredEvents) {
  95. window.addEventListener(index, this.registeredEvents[index])
  96. }
  97. this.continueButton.attach()
  98. this.continueButton.onClick(() => {
  99. this.stateMachine.transitionTo("storyupdate")
  100. })
  101. this.upgrades = [
  102. {header: "shovel speed", buttons: [new UpgradeButton(Picture.Shovel, "I", "shovel-1", 10), new UpgradeButton(Picture.Shovel, "II", "shovel-2", 50), new UpgradeButton(Picture.Shovel, "III", "shovel-3", 100)]},
  103. {header: "bone rarity", buttons: [new UpgradeButton(Picture.Bone, "I", "bone-1", 50), new UpgradeButton(Picture.Bone, "II", "bone-2", 100), new UpgradeButton(Picture.Bone, "III", "bone-3", 200)]},
  104. {header: "new area", buttons: [new UpgradeButton(Picture.Tombstone, "I", "graveyard-1", 100), new UpgradeButton(Picture.Tombstone, "II", "graveyard-2", 200), new UpgradeButton(Picture.Tombstone, "III", "graveyard-3", 400)]},
  105. ]
  106. this.upgrades.forEach(row => {
  107. row.buttons.forEach((button, index) => {
  108. if (index == 0) {
  109. button.locked = false
  110. }
  111. if (index > 0 && row.buttons[index - 1].upgraded) {
  112. button.locked = false
  113. }
  114. })
  115. })
  116. }
  117. leave() {
  118. for (let index in this.registeredEvents) {
  119. window.removeEventListener(index, this.registeredEvents[index])
  120. }
  121. this.continueButton.remove()
  122. }
  123. onResize() {
  124. }
  125. onKeyDown(event) {
  126. }
  127. onKeyUp(event) {
  128. switch (event.code) {
  129. case "Enter":
  130. this.stateMachine.transitionTo("dig")
  131. break
  132. }
  133. }
  134. onTouchStart(event) {
  135. }
  136. onTouchMove(event) {
  137. }
  138. onTouchEnd(event) {
  139. }
  140. onMouseDown(event) {
  141. }
  142. onMouseMove(event) {
  143. let point = this.camera.screenToWorld(new Point(event.clientX, event.clientY))
  144. point.offset(new Point(0, (this.canvasBounds.height / 2 - 152) - Theme.Game.TileSize.height))
  145. document.body.style.cursor = "default"
  146. this.upgrades.forEach((row, columnIndex) => {
  147. row.buttons.forEach((button, rowIndex) => {
  148. button.hovered = false
  149. let buttonX = (columnIndex * this.canvasBounds.width / this.upgrades.length) - (this.canvasBounds.width / this.upgrades.length) - Theme.Game.TileSize.width / 2
  150. if (point.x > buttonX && point.x < buttonX + Theme.Game.TileSize.width) {
  151. let buttonY = (this.incrementRow() * rowIndex) - Theme.Game.TileSize.height / 2
  152. if (point.y > buttonY && point.y < buttonY + Theme.Game.TileSize.height) {
  153. document.body.style.cursor = "pointer"
  154. button.hovered = true
  155. }
  156. }
  157. })
  158. })
  159. }
  160. onMouseUp(event) {
  161. this.upgrades.forEach((row, columnIndex) => {
  162. row.buttons.forEach((button, rowIndex) => {
  163. if (button.hovered) {
  164. if (button.purchaseUpgrade()) {
  165. if (row.buttons[rowIndex + 1]) {
  166. row.buttons[rowIndex + 1].locked = false
  167. }
  168. }
  169. }
  170. })
  171. })
  172. }
  173. }