CreditsState.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 { Picture } from "../../libraries/CanvasArtist.js";
  6. export class CreditsState {
  7. constructor(view) {
  8. this.stateMachine = view.stateMachine
  9. this.camera = new Camera()
  10. this.camera.scale = new Point(1, 1)
  11. this.textWrapper = new TextWrapper()
  12. this.headerFontSize = 48
  13. this.normalFontSize = 24
  14. this.debounce = 50
  15. }
  16. init(scaledCanvas) {
  17. this.canvasBounds = scaledCanvas.bounds
  18. }
  19. draw(ctx, scaledCanvas) {
  20. this.camera.draw(ctx, scaledCanvas, () => {
  21. ctx.fillStyle = Theme.Colors.ivory
  22. ctx.font = `${this.headerFontSize}px ${Theme.Fonts.Header}`
  23. ctx.textAlign = "center"
  24. this.textWrapper.fontSize = this.headerFontSize
  25. this.textWrapper.draw(ctx, `You Found
  26. The Diamond!`, {width: this.canvasBounds.width})
  27. ctx.save()
  28. ctx.translate(0, this.headerFontSize * 2)
  29. this.textWrapper.fontSize = this.normalFontSize
  30. ctx.font = `${this.normalFontSize}px ${Theme.Fonts.Header}`
  31. this.textWrapper.draw(ctx, `Developed By
  32. Midas
  33. and
  34. Nusha
  35. Thanks for Playing!
  36. js13k Games 2022 Entry
  37. `, {width: this.canvasBounds.width})
  38. ctx.restore()
  39. ctx.save()
  40. ctx.translate(-Theme.Game.TileSize.width /2, 400)
  41. Picture.Diamond(ctx, {width: Theme.Game.TileSize.width, height: Theme.Game.TileSize.height * 0.8})
  42. ctx.restore()
  43. })
  44. // this.playButton.setPosition(this.canvasBounds.width / 2, this.canvasBounds.height * (7 / 8))
  45. // this.playButton.draw(ctx, scaledCanvas)
  46. }
  47. update(delta) {
  48. if(this.debounce > 0) {
  49. this.debounce--
  50. }
  51. this.camera.targetPosition.y = (this.canvasBounds.height / 2) - 100
  52. let fontScale = this.canvasBounds.width / 500
  53. this.headerFontSize = Math.min(Math.floor(48 * fontScale), 48)
  54. this.normalFontSize = Math.min(Math.floor(24 * fontScale), 24)
  55. this.camera.update(delta)
  56. if(!this.ending && this.camera.position.equals(this.camera.targetPosition)) {
  57. this.ending = true
  58. this.endWait = setTimeout(() => {
  59. this.stateMachine.transitionTo("mainmenu")
  60. }, 8000)
  61. }
  62. }
  63. enter() {
  64. this.registeredEvents = {}
  65. this.registeredEvents["keyup"] = this.onKeyUp.bind(this)
  66. this.registeredEvents["mouseup"] = this.onMouseUp.bind(this)
  67. for (let index in this.registeredEvents) {
  68. window.addEventListener(index, this.registeredEvents[index])
  69. }
  70. this.debounce = 50
  71. this.camera.targetPosition.y = 500
  72. this.camera.position.y = -500
  73. }
  74. leave() {
  75. for (let index in this.registeredEvents) {
  76. window.removeEventListener(index, this.registeredEvents[index])
  77. }
  78. clearTimeout(this.endWait)
  79. }
  80. onKeyUp(event) {
  81. if(this.debounce > 0) {
  82. return
  83. }
  84. this.stateMachine.transitionTo("mainmenu")
  85. }
  86. onMouseUp(event) {
  87. if(this.debounce > 0) {
  88. return
  89. }
  90. this.stateMachine.transitionTo("mainmenu")
  91. }
  92. }