CreditsState.js 3.3 KB

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