loading.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import * as audio from '../audio';
  2. import * as level from '../level';
  3. import * as util from '../util';
  4. import { GameContext, Screen } from './common';
  5. import { SelectScreen } from './select';
  6. export class LoadingScreen implements Screen {
  7. readonly name: string = 'loading';
  8. constructor(
  9. private context: GameContext,
  10. private configUrl: string,
  11. private fromEditor: boolean
  12. ) {}
  13. enter(): void {
  14. console.log('Loading assets...');
  15. let configPromise: Promise<level.Config>;
  16. if (this.configUrl.endsWith('.json')) {
  17. configPromise = level.loadFromJson(this.configUrl);
  18. } else {
  19. configPromise = level.loadFromTM(this.configUrl);
  20. }
  21. let editorConfigPromise: Promise<level.Config | null>;
  22. if (this.fromEditor) {
  23. editorConfigPromise = level.loadFromLocalStorage();
  24. } else {
  25. editorConfigPromise = Promise.resolve(null);
  26. }
  27. Promise.all([configPromise, editorConfigPromise]).then(
  28. ([config, editorConfig]) => {
  29. if (editorConfig !== null) {
  30. console.log('Using editor levels');
  31. const [result, context] = util.deepEqual(config, editorConfig);
  32. if (!result) {
  33. console.log(`Editor levels differ: ${context}`);
  34. }
  35. this.context.config = editorConfig;
  36. } else {
  37. this.context.config = config;
  38. }
  39. this.loadAssets();
  40. }
  41. );
  42. }
  43. loadAssets(): void {
  44. let config = this.context.config!;
  45. Promise.all([
  46. util.loadBackground(config.background),
  47. this.loadTrack(config.selectSound),
  48. this.loadTrack(config.decideSound),
  49. ]).then((v) => {
  50. console.log('Loaded assets.');
  51. let [background, selectSound, decideSound] = v;
  52. this.context.assets = {
  53. selectSound,
  54. decideSound,
  55. };
  56. this.finishLoading();
  57. });
  58. }
  59. finishLoading(): void {
  60. let loadingElement: HTMLElement = util.getElement(
  61. this.context.container,
  62. '#loading'
  63. );
  64. loadingElement.addEventListener('transitionend', (event) => {
  65. loadingElement.style.display = 'none';
  66. this.switchToSelect();
  67. });
  68. loadingElement.classList.add('finished');
  69. }
  70. loadTrack(url: string): Promise<audio.FileTrack | null> {
  71. if (url == null) {
  72. return Promise.resolve(null);
  73. } else {
  74. return this.context.audioManager.loadTrack(url);
  75. }
  76. }
  77. switchToSelect(): void {
  78. let selectScreen = new SelectScreen(this.context);
  79. this.context.switchScreen(selectScreen);
  80. }
  81. handleInput(key: string): void {}
  82. exit(): void {
  83. let config = this.context.config!;
  84. let containerStyle = this.context.container.style;
  85. containerStyle.setProperty('--base-color', config.baseColor);
  86. containerStyle.setProperty('--highlight-color', config.highlightColor);
  87. containerStyle.setProperty('--contrast-color', config.contrastColor);
  88. }
  89. transitionExit(): void {}
  90. }