UpgradeButton.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { Theme } from "./components/Theme.js"
  2. import { Save } from "./Save.js"
  3. export default class UpgradeButton {
  4. constructor(picture, text, value, cost) {
  5. this.picture = picture
  6. this.text = text
  7. this.value = value
  8. this.cost = cost
  9. this.locked = true
  10. this.hovered = false
  11. this.upgraded = false
  12. this.save = new Save()
  13. if(this.save.getKey(this.value)) {
  14. this.upgraded = true
  15. this.locked = false
  16. }
  17. }
  18. update(delta) {
  19. }
  20. draw(ctx, scaledCanvas) {
  21. let fontScale = scaledCanvas.bounds.width / 500
  22. ctx.lineWidth = 4
  23. ctx.fillStyle = Theme.Colors.lilac
  24. ctx.strokeStyle = Theme.Colors.lilac
  25. if (this.locked) {
  26. ctx.fillStyle = Theme.Colors.umber
  27. ctx.strokeStyle = Theme.Colors.umber
  28. }
  29. if (!this.locked && this.hovered) {
  30. ctx.fillStyle = Theme.Colors.lilac
  31. ctx.strokeStyle = Theme.Colors.seagreen
  32. }
  33. if (this.upgraded) {
  34. ctx.fillStyle = Theme.Colors.seagreen
  35. ctx.strokeStyle = Theme.Colors.seagreen
  36. }
  37. ctx.beginPath()
  38. ctx.roundRect(0, 0, Theme.Game.TileSize.width, Theme.Game.TileSize.height, 8)
  39. ctx.fill()
  40. ctx.stroke()
  41. this.picture(ctx, { width: Theme.Game.TileSize.width, height: Theme.Game.TileSize.height })
  42. ctx.fillStyle = Theme.Colors.black
  43. if (!this.locked && this.hovered) {
  44. ctx.fillStyle = Theme.Colors.seagreen
  45. }
  46. if (this.upgraded) {
  47. ctx.fillStyle = Theme.Colors.ivory
  48. }
  49. ctx.font = `${Math.min(Math.floor(48 * fontScale), 48)}px ${Theme.Fonts.Header}`
  50. ctx.textAlign = "center"
  51. ctx.fillText(this.text, Theme.Game.TileSize.width / 2, Theme.Game.TileSize.height - 4)
  52. if(!this.upgraded) {
  53. let money = this.save.getKey("money")
  54. ctx.fillStyle = Theme.Colors.darkgreen
  55. if(this.cost > money) {
  56. ctx.fillStyle = Theme.Colors.darkred
  57. }
  58. ctx.font = `${Math.min(Math.floor(18 * fontScale), 18)}px ${Theme.Fonts.Header}`
  59. ctx.textAlign = "center"
  60. ctx.fillText(`$${this.cost}`, Theme.Game.TileSize.width / 2, 20)
  61. }
  62. }
  63. purchaseUpgrade() {
  64. if(this.upgraded) {
  65. return false
  66. }
  67. let money = this.save.getKey("money")
  68. if (money >= this.cost) {
  69. this.save.incrementKey("money", -this.cost)
  70. this.upgraded = true
  71. this.save.setKey(this.value, true)
  72. return true
  73. }
  74. return false
  75. }
  76. }