MainMenu.re.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import * as RE from 'rogue-engine';
  2. import AsyncLoader from './Library/AsyncLoader';
  3. import { TweenManager, SimpleTween } from './Library/SimpleTweens';
  4. import Easing from './Library/Easings';
  5. import GameData from './Library/GameDataPersistent';
  6. export default class MainMenu extends RE.Component {
  7. loader = new AsyncLoader()
  8. tweenManager = new TweenManager()
  9. uiStyleValues = { opacity: 1, levelSelectOpacity: 0 }
  10. isFading = false
  11. levelData = {
  12. 1: {x: 2, y: 2, c: 2, g: 100},
  13. 2: {x: 2, y: 2, c: 3, g: 100},
  14. 3: {x: 2, y: 2, c: 4, g: 100},
  15. 4: {x: 2, y: 2, c: 5, g: 100},
  16. 5: {x: 4, y: 4, c: 2, g: 100},
  17. 6: {x: 4, y: 4, c: 3, g: 100},
  18. 7: {x: 4, y: 4, c: 4, g: 100},
  19. 8: {x: 4, y: 4, c: 5, g: 100},
  20. 9: {x: 6, y: 6, c: 2, g: 100},
  21. 10: {x: 6, y: 6, c: 3, g: 100},
  22. 11: {x: 6, y: 6, c: 4, g: 100},
  23. 12: {x: 6, y: 6, c: 5, g: 100},
  24. 13: {x: 8, y: 8, c: 2, g: 100},
  25. 14: {x: 8, y: 8, c: 3, g: 100},
  26. 15: {x: 8, y: 8, c: 4, g: 100},
  27. 16: {x: 8, y: 8, c: 5, g: 100},
  28. 17: {x: 10, y: 10, c: 2, g: 100},
  29. 18: {x: 10, y: 10, c: 3, g: 100},
  30. 19: {x: 10, y: 10, c: 4, g: 100},
  31. 20: {x: 10, y: 10, c: 5, g: 100},
  32. }
  33. awake() {
  34. }
  35. start() {
  36. GameData.set("level-data", this.levelData)
  37. this.loader.loadStaticText("mainmenu.html").then(html => {
  38. RE.Runtime.uiContainer.innerHTML = html
  39. document.getElementById("play-button").addEventListener("touchend", this.transitionToLevelSelect.bind(this))
  40. document.getElementById("play-button").addEventListener("mouseup", this.transitionToLevelSelect.bind(this))
  41. this.generateLevelButtons()
  42. }, error => RE.Debug.logError(error))
  43. this.tweenManager.start()
  44. }
  45. transitionToLevelSelect() {
  46. document.getElementById("play-button").disabled = true
  47. this.tweenManager.add(new SimpleTween(this.uiStyleValues, {opacity: 0}, 350, Easing.Linear.EaseNone, () => {
  48. document.getElementById("main-menu").style.display = "none"
  49. this.tweenManager.add(new SimpleTween(this.uiStyleValues, {levelSelectOpacity: 1}, 350, Easing.Linear.EaseNone, () => {
  50. document.getElementById("level-select").style.display = "block"
  51. }))
  52. }))
  53. }
  54. generateLevelButtons() {
  55. let existingButtons = [...document.getElementById("level-select").getElementsByTagName("button")]
  56. existingButtons.forEach(button => {
  57. let level = parseInt(button.getAttribute("data-level"))
  58. button.addEventListener("touchend", () => { this.levelSelect(level) })
  59. button.addEventListener("mouseup", () => { this.levelSelect(level) })
  60. })
  61. }
  62. levelSelect(level) {
  63. GameData.set("active-level", level)
  64. this.disabled = true
  65. this.transitionToMain()
  66. }
  67. transitionToMain() {
  68. this.tweenManager.add(new SimpleTween(this.uiStyleValues, {levelSelectOpacity: 0}, 500, Easing.Linear.EaseNone, () => {
  69. document.getElementById("main-menu").style.opacity = 0
  70. RE.App.loadScene("Main")
  71. }))
  72. }
  73. update() {
  74. this.tweenManager.update()
  75. if(this.uiStyleValues.opacity != 1 && this.uiStyleValues.opacity != 0) {
  76. document.getElementById("main-menu").style.opacity = this.uiStyleValues.opacity
  77. }
  78. if(this.uiStyleValues.levelSelectOpacity != 1 && this.uiStyleValues.levelSelectOpacity != 0) {
  79. document.getElementById("level-select").style.opacity = this.uiStyleValues.levelSelectOpacity
  80. }
  81. }
  82. }
  83. RE.registerComponent(MainMenu);