From c62bd6b2099efcbe3062086fdbb945741f1e95ff Mon Sep 17 00:00:00 2001 From: Renge Date: Wed, 6 Apr 2022 13:24:06 -0400 Subject: [PATCH] feat: tested load single audio/image --- .../ResourceManager/ResourceManager.ts | 4 +- src/shattered_sword/Scenes/SceneWithStory.ts | 60 +++++++++++-------- 2 files changed, 37 insertions(+), 27 deletions(-) diff --git a/src/Wolfie2D/ResourceManager/ResourceManager.ts b/src/Wolfie2D/ResourceManager/ResourceManager.ts index d0d14f5..96b749b 100644 --- a/src/Wolfie2D/ResourceManager/ResourceManager.ts +++ b/src/Wolfie2D/ResourceManager/ResourceManager.ts @@ -1044,7 +1044,7 @@ export default class ResourceManager { } } - public loadSingleImage(key: string, path: string, isDependency: boolean, callbackIfLast: Function): void { + public singleImage(key: string, path: string, callbackIfLast: Function): void { var image = new Image(); image.onload = () => { @@ -1064,7 +1064,7 @@ export default class ResourceManager { image.src = path; } - public loadSingleAudio(key: string, path: string, callbackIfLast: Function): void { + public singleAudio(key: string, path: string, callbackIfLast: Function): void { let audioCtx = AudioManager.getInstance().getAudioContext(); let request = new XMLHttpRequest(); diff --git a/src/shattered_sword/Scenes/SceneWithStory.ts b/src/shattered_sword/Scenes/SceneWithStory.ts index 9e93123..77fed97 100644 --- a/src/shattered_sword/Scenes/SceneWithStory.ts +++ b/src/shattered_sword/Scenes/SceneWithStory.ts @@ -7,6 +7,7 @@ import Color from "../../Wolfie2D/Utils/Color"; import Layer from "../../Wolfie2D/Scene/Layer"; import Sprite from "../../Wolfie2D/Nodes/Sprites/Sprite"; import { GameEventType } from "../../Wolfie2D/Events/GameEventType"; +import Input from "../../Wolfie2D/Input/Input"; enum Mode { GAME_MODE = "GameMode", @@ -18,7 +19,7 @@ export default class SceneWithStory extends Scene { private currentMode: Mode = Mode.GAME_MODE; private storytextLabel: Label; private storyLayer: Layer; - // private primary: Layer; + private primary: Layer; private story: Story; private storyProgress: number; private storySprites: Array; @@ -27,22 +28,22 @@ export default class SceneWithStory extends Scene { private currentContent: string; startScene(): void { - + // The code below are for testing only. Please comment them when submit - // this.primary = this.addUILayer("primary"); - // const center = this.viewport.getCenter(); - // const loadStory = this.add.uiElement(UIElementType.BUTTON, "primary", { position: new Vec2(center.x, center.y), text: "LoadStory" }); - // loadStory.size.set(200, 50); - // loadStory.borderWidth = 2; - // loadStory.borderColor = Color.WHITE; - // loadStory.backgroundColor = Color.TRANSPARENT; - // loadStory.onClickEventId = "loadStory"; + this.primary = this.addUILayer("primary"); + const center = this.viewport.getCenter(); + const loadStory = this.add.uiElement(UIElementType.BUTTON, "primary", { position: new Vec2(center.x, center.y), text: "LoadStory" }); + loadStory.size.set(200, 50); + loadStory.borderWidth = 2; + loadStory.borderColor = Color.WHITE; + loadStory.backgroundColor = Color.TRANSPARENT; + loadStory.onClickEventId = "loadStory"; - // this.receiver.subscribe("loadStory"); + this.receiver.subscribe("loadStory"); } @@ -58,12 +59,15 @@ export default class SceneWithStory extends Scene { if (this.story.bgm) { this.storyBGMs = new Array; this.story.bgm.forEach((bgm) => { - this.load.audio(bgm.key, bgm.path); - console.log("audio:", bgm.key, "path:", bgm.path); - this.load.loadResourcesFromQueue(() => { - console.log("finished loading audio"); + // this.load.audio(bgm.key, bgm.path); + // console.log("audio:", bgm.key, "path:", bgm.path); + // this.load.loadResourcesFromQueue(() => { + // console.log("finished loading audio"); + // this.emitter.fireEvent(GameEventType.PLAY_SOUND, { key: bgm.key, loop: false, holdReference: true }); + // }); + this.load.singleAudio(bgm.key, bgm.path, () => { this.emitter.fireEvent(GameEventType.PLAY_SOUND, { key: bgm.key, loop: false, holdReference: true }); - }); + }) this.storyBGMs.push(bgm.key); }) } @@ -99,13 +103,19 @@ export default class SceneWithStory extends Scene { this.story.texts[this.storyProgress].actions.forEach(action => { switch (action.type) { case "loadSprite": - this.load.image(action.key, action.path); - this.load.loadResourcesFromQueue(() => { + // this.load.image(action.key, action.path); + // this.load.loadResourcesFromQueue(() => { + // tmp = this.add.sprite(action.key, "story"); + // tmp.position.set(action.positon[0], action.positon[1]); + // tmp.scale.set(action.scale[0], action.scale[1]); + // this.storySprites.push(tmp); + // }); + this.load.singleImage(action.key, action.path, () => { tmp = this.add.sprite(action.key, "story"); tmp.position.set(action.positon[0], action.positon[1]); tmp.scale.set(action.scale[0], action.scale[1]); this.storySprites.push(tmp); - }); + }) break; case "loadAnimatedSprite": this.load.spritesheet(action.key, action.path); @@ -173,13 +183,13 @@ export default class SceneWithStory extends Scene { while (this.receiver.hasNextEvent()) { let event = this.receiver.getNextEvent(); // Testing code - // if (event.type === "loadStory" && this.currentMode === Mode.GAME_MODE) { - // this.storyLoader("shattered_sword_assets/jsons/samplestory.json"); - // } + if (event.type === "loadStory" && this.currentMode === Mode.GAME_MODE) { + this.storyLoader("shattered_sword_assets/jsons/samplestory.json"); + } } // Testing code - // if (Input.isMouseJustPressed() && this.currentMode === Mode.STORY_MODE) { - // this.updateStory(); - // } + if (Input.isMouseJustPressed() && this.currentMode === Mode.STORY_MODE) { + this.updateStory(); + } } } \ No newline at end of file