refactor: clear the code of SceneWithStory and add comments to it

This commit is contained in:
Renge 2022-04-03 18:48:52 -04:00
parent a3009dcbbf
commit 11dffbd718
2 changed files with 40 additions and 23 deletions

View File

@ -11,7 +11,7 @@ import Color from "../../Wolfie2D/Utils/Color";
import Layer from "../../Wolfie2D/Scene/Layer"; import Layer from "../../Wolfie2D/Scene/Layer";
import Label from "../../Wolfie2D/Nodes/UIElements/Label"; import Label from "../../Wolfie2D/Nodes/UIElements/Label";
import MainMenu from "./MainMenu"; import MainMenu from "./MainMenu";
import StorySceneTester from "./StorySceneTester"; import SceneWithStory from "./SceneWithStory";
export default class Levels extends Scene { export default class Levels extends Scene {
@ -110,7 +110,7 @@ export default class Levels extends Scene {
} }
if(event.type === "StorySceneTester"){ if(event.type === "StorySceneTester"){
this.sceneManager.changeToScene(StorySceneTester, {}); this.sceneManager.changeToScene(SceneWithStory, {});
} }
if(event.type === "back"){ if(event.type === "back"){

View File

@ -1,4 +1,3 @@
import Input from "../../Wolfie2D/Input/Input";
import Scene from "../../Wolfie2D/Scene/Scene"; import Scene from "../../Wolfie2D/Scene/Scene";
import Label, { HAlign } from "../../Wolfie2D/Nodes/UIElements/Label"; import Label, { HAlign } from "../../Wolfie2D/Nodes/UIElements/Label";
import Story from "../Tools/DataTypes/Story"; import Story from "../Tools/DataTypes/Story";
@ -7,7 +6,6 @@ import Vec2 from "../../Wolfie2D/DataTypes/Vec2";
import Color from "../../Wolfie2D/Utils/Color"; import Color from "../../Wolfie2D/Utils/Color";
import Layer from "../../Wolfie2D/Scene/Layer"; import Layer from "../../Wolfie2D/Scene/Layer";
import Sprite from "../../Wolfie2D/Nodes/Sprites/Sprite"; import Sprite from "../../Wolfie2D/Nodes/Sprites/Sprite";
import MapTemplate from "../Tools/DataTypes/MapTemplate";
import { GameEventType } from "../../Wolfie2D/Events/GameEventType"; import { GameEventType } from "../../Wolfie2D/Events/GameEventType";
enum Mode { enum Mode {
@ -15,11 +13,12 @@ enum Mode {
STORY_MODE = "StoryMode", STORY_MODE = "StoryMode",
PAUSE_MODE = "PauseMode", PAUSE_MODE = "PauseMode",
} }
export default class StorySceneTester extends Scene {
export default class SceneWithStory extends Scene {
private currentMode: Mode = Mode.GAME_MODE; private currentMode: Mode = Mode.GAME_MODE;
private storytextLabel: Label; private storytextLabel: Label;
private storyLayer: Layer; private storyLayer: Layer;
private primary: Layer; // private primary: Layer;
private story: Story; private story: Story;
private storyProgress: number; private storyProgress: number;
private storySprites: Array<Sprite>; private storySprites: Array<Sprite>;
@ -28,24 +27,32 @@ export default class StorySceneTester extends Scene {
private currentContent: string; private currentContent: string;
startScene(): void { startScene(): void {
this.primary = this.addUILayer("primary");
this.storyLayer = this.addUILayer("story");
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");
// 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.receiver.subscribe("loadStory");
} }
/**
* This function load a story JSON from storyPath and auto display it to storyLayer
* @param storyPath The path to the story JSON
*/
async storyLoader(storyPath: string) { async storyLoader(storyPath: string) {
this.storyLayer = this.addUILayer("story");
const response = await (await fetch(storyPath)).json(); const response = await (await fetch(storyPath)).json();
this.story = <Story>response; this.story = <Story>response;
if (this.story.bgm) { if (this.story.bgm) {
@ -74,10 +81,16 @@ export default class StorySceneTester extends Scene {
this.updateStory(); this.updateStory();
} }
/**
* @returns True if the story has next sentence False otherwise
*/
hasNextStory(): boolean { hasNextStory(): boolean {
return this.currentMode === Mode.STORY_MODE && this.storyProgress + 1 < this.story.texts.length; return this.currentMode === Mode.STORY_MODE && this.storyProgress + 1 < this.story.texts.length;
} }
/**
* Go to the next sentence of current story if there is one or clear the storyLayer and exit storyMode
*/
updateStory() { updateStory() {
if (this.currentMode === Mode.STORY_MODE && this.hasNextStory()) { if (this.currentMode === Mode.STORY_MODE && this.hasNextStory()) {
this.storyProgress++; this.storyProgress++;
@ -147,22 +160,26 @@ export default class StorySceneTester extends Scene {
console.log("sound stopped:", bgm); console.log("sound stopped:", bgm);
}); });
} }
this.storyLayer.disable();
this.storyBGMs = undefined; this.storyBGMs = undefined;
this.storySprites = undefined; this.storySprites = undefined;
this.story = undefined; this.story = undefined;
this.storytextLabel = undefined; this.storytextLabel = undefined;
this.storyLayer = undefined;
} }
} }
updateScene(deltaT: number): void { updateScene(deltaT: number): void {
while (this.receiver.hasNextEvent()) { while (this.receiver.hasNextEvent()) {
let event = this.receiver.getNextEvent(); let event = this.receiver.getNextEvent();
if (event.type === "loadStory" && this.currentMode === Mode.GAME_MODE) { // Testing code
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");
} // }
if (Input.isMouseJustPressed() && this.currentMode === Mode.STORY_MODE) { }
this.updateStory(); // Testing code
} // if (Input.isMouseJustPressed() && this.currentMode === Mode.STORY_MODE) {
// this.updateStory();
// }
} }
} }