From 45e1570701f966169f5c530a4a8b1bd609a72340 Mon Sep 17 00:00:00 2001 From: helloyork Date: Mon, 9 Sep 2024 19:18:32 -0700 Subject: [PATCH 1/3] sync --- src/game/game.ts | 13 +- src/game/nlcore/action/action.ts | 17 +- src/game/nlcore/action/actions.ts | 50 ++-- src/game/nlcore/action/constructable.ts | 11 +- src/game/nlcore/action/tree/actionTree.ts | 65 ++--- src/game/nlcore/common/Utils.ts | 4 +- src/game/nlcore/elements/condition.ts | 4 +- src/game/nlcore/elements/control.ts | 4 +- src/game/nlcore/elements/image.ts | 54 ++--- src/game/nlcore/elements/menu.ts | 4 +- src/game/nlcore/elements/scene.ts | 115 ++++----- src/game/nlcore/elements/script.ts | 4 +- src/game/nlcore/elements/sound.ts | 10 +- src/game/nlcore/elements/story.ts | 6 +- src/game/nlcore/elements/text.ts | 4 +- .../nlcore/elements/transform/position.ts | 2 +- .../nlcore/elements/transform/transform.ts | 55 +++-- .../nlcore/elements/transition/dissolve.ts | 4 +- src/game/nlcore/elements/transition/fade.ts | 2 +- src/game/nlcore/elements/transition/fadeIn.ts | 2 +- src/game/nlcore/game.ts | 61 +++-- src/game/nlcore/gameTypes.d.ts | 14 +- src/game/nlcore/save/storable.ts | 225 ++++++------------ src/game/nlcore/save/type.ts | 26 ++ src/game/player/elements/scene/Background.tsx | 7 +- .../elements/scene/BackgroundTransition.tsx | 2 +- src/game/player/gameState.ts | 32 ++- src/game/player/provider/game-state.tsx | 7 +- src/game/player/provider/ratio.tsx | 2 +- src/util/data.ts | 12 +- tsconfig.json | 7 +- 31 files changed, 362 insertions(+), 463 deletions(-) create mode 100644 src/game/nlcore/save/type.ts diff --git a/src/game/game.ts b/src/game/game.ts index 2ddf567..8f222cc 100644 --- a/src/game/game.ts +++ b/src/game/game.ts @@ -2,7 +2,9 @@ import {deepMerge} from "@lib/util/data"; import {Game} from "@core/game"; export type ClientGameConfig = {}; -export type ClientRequirement = {}; +export type ClientRequirement = { + game: Game; +}; export type ClientGamePreference = { afm: boolean; }; @@ -37,14 +39,7 @@ export class ClientGame extends BaseGame { return this.preference[key]; }, }; + this.game = requirement.game; } - - // init(window: Window) { - // this.game = new Game({ - // clientGame: this, - // }); - // this.game.createLiveGame(); - // return this; - // } } diff --git a/src/game/nlcore/action/action.ts b/src/game/nlcore/action/action.ts index fe71213..36e0a97 100644 --- a/src/game/nlcore/action/action.ts +++ b/src/game/nlcore/action/action.ts @@ -23,10 +23,6 @@ export class Action { this._id = Game.getIdManager().prefix("action", Game.getIdManager().getStringId(), "-"); } - static isAction(action: any): action is Action { - return action instanceof Action; - } - public executeAction(state: GameState): CalledActionResult | Awaitable { return { type: this.type as any, @@ -38,19 +34,8 @@ export class Action { return this._id; } - toData() { - return { - type: this.type, - content: this.contentNode.toData(), - } - } - getFutureActions(): LogicAction.Actions[] { const action = this.contentNode.child; - return action ? [action.action] : []; - } - - undo() { - this.contentNode.action.undo(); + return (action && action.action) ? [action.action] : []; } } \ No newline at end of file diff --git a/src/game/nlcore/action/actions.ts b/src/game/nlcore/action/actions.ts index d116001..512433b 100644 --- a/src/game/nlcore/action/actions.ts +++ b/src/game/nlcore/action/actions.ts @@ -46,6 +46,10 @@ export class TypedAction< this.callee = callee; this.contentNode.action = this; } + + unknownType() { + throw new Error("Unknown action type: " + this.type); + } } export class CharacterAction @@ -76,7 +80,7 @@ export class SceneAction).getContent()[0]; + this.callee.state.background = (this.contentNode as ContentNode).getContent()![0]; return super.executeAction(state); } else if (this.type === SceneActionTypes.sleep) { const awaitable = new Awaitable(v => v); @@ -89,7 +93,7 @@ export class SceneAction { @@ -103,7 +107,7 @@ export class SceneAction).getContent()[0] + (this.contentNode as ContentNode).getContent()![0] ); return super.executeAction(state); } else if (this.type === SceneActionTypes.applyTransition) { @@ -180,7 +184,7 @@ export class SceneAction).getContent(); - this.callee.events.emit("event:scene.setBackgroundMusic", sound, fade); + this.callee.events.emit("event:scene.setBackgroundMusic", sound, fade || 0); return super.executeAction(state); } else if (this.type === SceneActionTypes.preUnmount) { @@ -301,6 +305,8 @@ export class ImageAction { - let lastChild = state.clientGame.game.getLiveGame().getCurrentAction().contentNode.child; + let lastChild = state.clientGame.game.getLiveGame().getCurrentAction()?.contentNode.getChild() || null; if (lastChild) { v.action[v.action.length - 1]?.contentNode.addChild(lastChild); } @@ -382,10 +388,12 @@ export class SoundAction { if (this.type === SoundActionTypes.play) { SoundAction.initSound(state, this.callee); - console.log("[sound] played", this.callee.$getHowl()); + if (!this.callee.$getHowl()) { + throw new Error("Howl is not initialized"); + } if (this.callee.config.sync && !this.callee.config.loop) { const awaitable = new Awaitable(v => v); - const token = state.playSound(this.callee.$getHowl(), () => { + const token = state.playSound(this.callee.$getHowl()!, () => { this.callee.$stop(); awaitable.resolve({ type: this.type as any, @@ -395,7 +403,7 @@ export class SoundAction { + const token = state.playSound(this.callee.$getHowl()!, () => { this.callee.$stop(); }); this.callee.$setToken(token); @@ -403,7 +411,7 @@ export class SoundAction).getContent(); if (this.callee.$getHowl()) { - this.callee.$getHowl().fade(start, end, duration, this.callee.$getToken()); + this.callee.$getHowl()!.fade(start, end, duration, this.callee.$getToken()); } return super.executeAction(state); } else if (this.type === SoundActionTypes.setVolume) { const [volume] = (this.contentNode as ContentNode).getContent(); if (this.callee.$getHowl()) { - this.callee.$getHowl().volume(volume, this.callee.$getToken()); + this.callee.$getHowl()!.volume(volume, this.callee.$getToken()); } return super.executeAction(state); } else if (this.type === SoundActionTypes.setRate) { const [rate] = (this.contentNode as ContentNode).getContent(); if (this.callee.$getHowl()) { - this.callee.$getHowl().rate(rate, this.callee.$getToken()); + this.callee.$getHowl()!.rate(rate, this.callee.$getToken()); } return super.executeAction(state); } + + throw super.unknownType(); } } @@ -443,8 +453,8 @@ export class ControlAction { awaitable.resolve({ type: this.type, - node: this.contentNode.child + node: this.contentNode.getChild() }); }); return awaitable; diff --git a/src/game/nlcore/action/constructable.ts b/src/game/nlcore/action/constructable.ts index 75eb6cb..34e8694 100644 --- a/src/game/nlcore/action/constructable.ts +++ b/src/game/nlcore/action/constructable.ts @@ -48,7 +48,7 @@ export class Constructable< queue.push(sceneAction); while (queue.length > 0) { - const action = queue.shift(); + const action = queue.shift()!; if (action.type === SceneActionTypes.jumpTo) { if (!includeJumpTo || seen.includes(action.getId())) { continue; @@ -76,7 +76,7 @@ export class Constructable< queue.push(...futureActions); while (queue.length > 0) { - const action = queue.shift(); + const action = queue.shift()!; if (action.getId() === id) { return action; } @@ -136,7 +136,7 @@ export class Constructable< findElementsByIds(ids: string[], elements: LogicAction.GameElement[]): LogicAction.GameElement[] { const map = new Map(); elements.forEach(element => map.set(element.id, element)); - return ids.map(id => map.get(id)).filter(Boolean); + return ids.map(id => map.get(id)).filter(Boolean) as LogicAction.GameElement[]; } /** @@ -153,10 +153,7 @@ export class Constructable< const content = actions.flat(2) as TAction[]; this.actions.push(...content); const constructed = this.construct(); - const sceneRoot = new ContentNode( - Game.getIdManager().getStringId(), - constructed || void 0 - ).setContent(this); + const sceneRoot = new ContentNode(Game.getIdManager().getStringId(), undefined, undefined, constructed || void 0).setContent(this); constructed?.setParent(sceneRoot); const thisConstructor = this.constructor as T; diff --git a/src/game/nlcore/action/tree/actionTree.ts b/src/game/nlcore/action/tree/actionTree.ts index 4198a61..c05bf16 100644 --- a/src/game/nlcore/action/tree/actionTree.ts +++ b/src/game/nlcore/action/tree/actionTree.ts @@ -1,5 +1,5 @@ -import {LogicAction} from "@lib/game/game/logicAction"; -import {Game} from "@lib/game/game/game"; +import {LogicAction} from "@core/action/logicAction"; +import {Game} from "@core/game"; export enum NodeType { TreeNode = "TreeNode", @@ -27,8 +27,8 @@ export class Node { return this; } - getContent() { - return this.content; + getContent(): C { + return this.content!; } } @@ -43,45 +43,45 @@ export type ContentNodeData = { export class ContentNode extends Node { static forEachParent(node: RenderableNode, callback: (node: RenderableNode) => void) { const seen: Set = new Set(); - let current = node; + let current: RenderableNode | null = node; while (current) { if (seen.has(current)) { break; } seen.add(current); callback(current); - current = current.parent; + current = current.getParent(); } } static forEachChild(node: RenderableNode, callback: (node: RenderableNode) => void) { const seen: Set = new Set(); - let current = node; + let current: RenderableNode | null = node; while (current) { if (seen.has(current)) { break; } seen.add(current); callback(current); - current = current.child; + current = current.getChild(); } } child?: RenderableNode | null; initChild?: RenderableNode | null; parent: RenderableNode | null; - action: LogicAction.Actions; + action: LogicAction.Actions | null; constructor( id: string, - child?: RenderableNode, + callee?: LogicAction.Actions, parent?: RenderableNode | null, - callee?: LogicAction.Actions + child?: RenderableNode ) { super(Game.getIdManager().prefix("node", id, "-"), NodeType.ContentNode); this.child = child || null; this.parent = parent || null; - this.action = callee + this.action = callee || null; } setParent(parent: RenderableNode | null) { @@ -92,7 +92,7 @@ export class ContentNode extends Node { return this; } - setChild(child: RenderableNode) { + setChild(child: RenderableNode | null) { if (child === this) { throw new Error('Cannot set child to itself'); } @@ -103,6 +103,14 @@ export class ContentNode extends Node { return this; } + getChild(): RenderableNode | null { + return this.child || null; + } + + getParent(): RenderableNode | null { + return this.parent || null; + } + /** * To track the changes of the child * should only be called when constructing the tree @@ -116,7 +124,7 @@ export class ContentNode extends Node { * Public method for setting the content of the node * should only be called when changing the state in-game */ - public addChild(child: RenderableNode) { + public addChild(child: RenderableNode | null) { this.setChild(child); return this; } @@ -139,31 +147,6 @@ export class ContentNode extends Node { return this; } - toData(): RenderableNodeData { - const content = this.getContent(); - return { - id: this.id, - - // use toData if it exists - data: (typeof content === 'object' && content !== null) ? - (typeof content["toData"] === "function" ? content["toData"] : content) : content, - }; - } - - fromData(data: RenderableNodeData) { - const content = this.getContent(); - if ( - typeof content === 'object' - && content !== null - && typeof content["fromData"] === "function" - ) { - content["fromData"](content); - } else { - this.setContent(data.data); - } - return this; - } - hasChild() { return !!this.child; } @@ -195,9 +178,7 @@ export class RootNode extends ContentNode { } seen.add(node); callback(node); - if (node instanceof ContentNode) { - queue.push(node.child); - } + queue.push(node.child); } } } diff --git a/src/game/nlcore/common/Utils.ts b/src/game/nlcore/common/Utils.ts index 362b340..acd44a3 100644 --- a/src/game/nlcore/common/Utils.ts +++ b/src/game/nlcore/common/Utils.ts @@ -26,7 +26,7 @@ export class Utils { public static backgroundToSrc(background: Background["background"]) { return Utils.isStaticImageData(background) ? background.src : ( - background["url"] || null + (background as any)?.["url"] || null ); } @@ -90,7 +90,7 @@ export class StaticChecker { this.checkAction(action, imageStates, seen); const child = action.contentNode.child; - if (child) { + if (child && child.action) { queue.push(child.action); } } diff --git a/src/game/nlcore/elements/condition.ts b/src/game/nlcore/elements/condition.ts index f0c9cd1..528c16b 100644 --- a/src/game/nlcore/elements/condition.ts +++ b/src/game/nlcore/elements/condition.ts @@ -128,9 +128,7 @@ export class Condition extends Actionable { Reflect.construct(ConditionAction, [ this, ConditionAction.ActionTypes.action, - new ContentNode( - Game.getIdManager().getStringId(), - ).setContent(this.conditions) + new ContentNode(Game.getIdManager().getStringId()).setContent(this.conditions) ]) as ConditionAction ]; this.conditions = Condition.getInitialState(); diff --git a/src/game/nlcore/elements/control.ts b/src/game/nlcore/elements/control.ts index 9abeb2c..6600488 100644 --- a/src/game/nlcore/elements/control.ts +++ b/src/game/nlcore/elements/control.ts @@ -94,9 +94,7 @@ export class Control extends Actionable { const action = new ControlAction( this, type, - new ContentNode( - Game.getIdManager().getStringId() - ).setContent([this.construct(flatted), ...args]) + new ContentNode(Game.getIdManager().getStringId()).setContent([this.construct(flatted), ...args]) ); this.actions.push(action); return this; diff --git a/src/game/nlcore/elements/image.ts b/src/game/nlcore/elements/image.ts index fdf33b2..05058b0 100644 --- a/src/game/nlcore/elements/image.ts +++ b/src/game/nlcore/elements/image.ts @@ -33,12 +33,6 @@ export type ImageDataRaw = { state: Record; }; -const ImageTransactionTypes = { - set: "set", - show: "show", - hide: "hide", -} as const; - export type ImageEventTypes = { "event:image.show": [Transform]; "event:image.hide": [Transform]; @@ -51,7 +45,7 @@ export type ImageEventTypes = { "event:image.setTransition": [ITransition | null]; }; -export class Image extends Actionable { +export class Image extends Actionable { static EventTypes: { [K in keyof ImageEventTypes]: K } = { "event:image.show": "event:image.show", "event:image.hide": "event:image.hide", @@ -88,7 +82,7 @@ export class Image extends Actionable { super(Actionable.IdPrefixes.Image); if (typeof arg0 === "string") { this.name = arg0; - this.config = deepMerge(Image.defaultConfig, config); + this.config = deepMerge(Image.defaultConfig, config || {}); } else { this.name = ""; this.config = deepMerge(Image.defaultConfig, arg0); @@ -160,9 +154,7 @@ export class Image extends Actionable { const action = new ImageAction( this, ImageAction.ActionTypes.setSrc, - new ContentNode<[string]>( - Game.getIdManager().getStringId() - ).setContent([ + new ContentNode<[string]>(Game.getIdManager().getStringId()).setContent([ typeof src === "string" ? src : Utils.staticImageDataToSrc(src) ]) ); @@ -174,9 +166,7 @@ export class Image extends Actionable { const action = new ImageAction( this, ImageAction.ActionTypes.applyTransform, - new ContentNode( - Game.getIdManager().getStringId() - ).setContent([ + new ContentNode(Game.getIdManager().getStringId()).setContent([ void 0, transform, getCallStack() @@ -208,15 +198,13 @@ export class Image extends Actionable { props: { opacity: 1, }, - options: options + options: options || {} } ]); const action = new ImageAction( this, ImageAction.ActionTypes.show, - new ContentNode( - Game.getIdManager().getStringId() - ).setContent([ + new ContentNode(Game.getIdManager().getStringId()).setContent([ void 0, trans ]) @@ -240,20 +228,18 @@ export class Image extends Actionable { public hide(transform: Partial): this; - public hide(transform?: Transform | Partial): this { + public hide(arg0?: Transform | Partial): this { const action = new ImageAction( this, ImageAction.ActionTypes.hide, - new ContentNode( - Game.getIdManager().getStringId() - ).setContent([ + new ContentNode(Game.getIdManager().getStringId()).setContent([ void 0, - (transform instanceof Transform) ? transform : new Transform([ + (arg0 instanceof Transform) ? arg0 : new Transform([ { props: { opacity: 0, }, - options: transform + options: arg0 || {} } ]) ]) @@ -284,7 +270,7 @@ export class Image extends Actionable { return this; } - getScope(): React.RefObject { + getScope(): React.RefObject | undefined { return this.ref; } @@ -292,7 +278,7 @@ export class Image extends Actionable { return new Image(this.name, this.config); } - public toData(): ImageDataRaw { + public toData(): ImageDataRaw | null { if (this.state.disposed || _.isEqual(this.state, this.config)) { return null; } @@ -316,9 +302,7 @@ export class Image extends Actionable { this.actions.push(new ImageAction( this, ImageAction.ActionTypes.setTransition, - new ContentNode<[ITransition | null]>( - Game.getIdManager().getStringId() - ).setContent([ + new ContentNode<[ITransition | null]>(Game.getIdManager().getStringId()).setContent([ transition ]) )); @@ -329,9 +313,7 @@ export class Image extends Actionable { this.actions.push(new ImageAction<"image:applyTransition">( this, "image:applyTransition", - new ContentNode<[ITransition]>( - Game.getIdManager().getStringId() - ).setContent([ + new ContentNode<[ITransition]>(Game.getIdManager().getStringId()).setContent([ transition ]) )); @@ -348,9 +330,7 @@ export class Image extends Actionable { this.actions.push(new ImageAction( this, ImageAction.ActionTypes.dispose, - new ContentNode( - Game.getIdManager().getStringId() - ) + new ContentNode(Game.getIdManager().getStringId()) )); return this; } @@ -359,9 +339,7 @@ export class Image extends Actionable { this.actions.push(new ImageAction( this, ImageAction.ActionTypes.init, - new ContentNode<[Scene?]>( - Game.getIdManager().getStringId() - ).setContent([ + new ContentNode<[Scene?]>(Game.getIdManager().getStringId()).setContent([ scene ]) )); diff --git a/src/game/nlcore/elements/menu.ts b/src/game/nlcore/elements/menu.ts index 31ea0de..69369f4 100644 --- a/src/game/nlcore/elements/menu.ts +++ b/src/game/nlcore/elements/menu.ts @@ -80,9 +80,7 @@ export class Menu extends Actionable { new MenuAction( this, MenuAction.ActionTypes.action, - new ContentNode( - Game.getIdManager().getStringId() - ).setContent({ + new ContentNode(Game.getIdManager().getStringId()).setContent({ prompt: this.prompt, choices: this.constructChoices() }) diff --git a/src/game/nlcore/elements/scene.ts b/src/game/nlcore/elements/scene.ts index b4e390d..f16d04c 100644 --- a/src/game/nlcore/elements/scene.ts +++ b/src/game/nlcore/elements/scene.ts @@ -1,6 +1,6 @@ import {Constructable} from "../action/constructable"; import {Game} from "../game"; -import {Awaitable, deepMerge, EventDispatcher, safeClone} from "@lib/util/data"; +import {Awaitable, deepMerge, DeepPartial, EventDispatcher, safeClone} from "@lib/util/data"; import {Background, CommonImage} from "@core/types"; import {ContentNode} from "@core/action/tree/actionTree"; import {LogicAction} from "@core/action/logicAction"; @@ -19,15 +19,15 @@ import { SceneActionTypes } from "@core/action/actionTypes"; import {Image} from "@core/elements/image"; +import {Utils} from "@core/common/core"; import Actions = LogicAction.Actions; import ImageTransformProps = TransformDefinitions.ImageTransformProps; -import {Utils} from "@core/common/core"; export type SceneConfig = { - invertY?: boolean; - invertX?: boolean; - backgroundMusic?: Sound | null; - backgroundMusicFade?: number; + invertY: boolean; + invertX: boolean; + backgroundMusic: Sound | null; + backgroundMusicFade: number; } & Background; export type SceneState = { backgroundMusic?: Sound | null; @@ -79,6 +79,7 @@ export class Scene extends Constructable< static defaultConfig: SceneConfig = { background: null, invertY: false, + invertX: false, backgroundMusic: null, backgroundMusicFade: 0, }; @@ -94,10 +95,10 @@ export class Scene extends Constructable< _liveState = { active: false, } - _sceneRoot: SceneAction<"scene:action">; + _sceneRoot?: SceneAction<"scene:action">; private _actions: SceneAction[] = []; - constructor(name: string, config: SceneConfig = Scene.defaultConfig) { + constructor(name: string, config: DeepPartial = Scene.defaultConfig) { super(); this.id = Game.getIdManager().getStringId(); this.name = name; @@ -130,9 +131,7 @@ export class Scene extends Constructable< this._actions.push(new SceneAction( this, "scene:setBackground", - new ContentNode<[Background["background"]]>( - Game.getIdManager().getStringId(), - ).setContent([ + new ContentNode<[Background["background"]]>(Game.getIdManager().getStringId()).setContent([ background, ]) )); @@ -147,9 +146,7 @@ export class Scene extends Constructable< this._actions.push(new SceneAction( this, "scene:applyTransform", - new ContentNode( - Game.getIdManager().getStringId(), - ).setContent([transform]) + new ContentNode(Game.getIdManager().getStringId()).setContent([transform]) )); return this; } @@ -163,9 +160,7 @@ export class Scene extends Constructable< this._actions.push(new SceneAction( this, "scene:preUnmount", - new ContentNode( - Game.getIdManager().getStringId(), - ).setContent([]) + new ContentNode(Game.getIdManager().getStringId()).setContent([]) )); const jumpConfig: Partial = config || {}; @@ -189,9 +184,7 @@ export class Scene extends Constructable< this._actions.push(new SceneAction( this, "scene:sleep", - new ContentNode( - Game.getIdManager().getStringId(), - ).setContent(content) + new ContentNode(Game.getIdManager().getStringId()).setContent(content) )); return this; } @@ -202,12 +195,10 @@ export class Scene extends Constructable< * @param fade 毫秒数,如果设置,则会将渐出效果应用于上一首音乐,将渐入效果应用于当前音乐,时长为 {@link fade} 毫秒 */ public setBackgroundMusic(sound: Sound, fade?: number): this { - this._actions.push(new SceneAction( + this._actions.push(new SceneAction( this, - "scene:setBackgroundMusic", - new ContentNode( - Game.getIdManager().getStringId(), - ).setContent([sound, fade]) + SceneActionTypes["setBackgroundMusic"], + new ContentNode(Game.getIdManager().getStringId()).setContent([sound, fade]) )); return this; } @@ -222,7 +213,7 @@ export class Scene extends Constructable< return this.state.backgroundMusic; } - toData(): SceneDataRaw { + toData(): SceneDataRaw | null { if (_.isEqual(this.state, this.config)) { return null; } @@ -241,6 +232,8 @@ export class Scene extends Constructable< if (data.state.backgroundMusic) { this.state.backgroundMusic = new Sound().fromData(data.state.backgroundMusic); this.state.background = data.state.background; + } + if (data.state.backgroundImageState) { this.backgroundImageState = Image.deserializeImageState(data.state.backgroundImageState); } return this; @@ -250,9 +243,7 @@ export class Scene extends Constructable< this._actions.push(new SceneAction( this, "scene:setTransition", - new ContentNode( - Game.getIdManager().getStringId(), - ).setContent([transition]) + new ContentNode(Game.getIdManager().getStringId()).setContent([transition]) )); return this; } @@ -261,9 +252,7 @@ export class Scene extends Constructable< this._actions.push(new SceneAction( this, "scene:applyTransition", - new ContentNode( - Game.getIdManager().getStringId(), - ).setContent([transition]) + new ContentNode(Game.getIdManager().getStringId()).setContent([transition]) )); return this; } @@ -279,10 +268,6 @@ export class Scene extends Constructable< ]); } - protected getSceneActions() { - return this.toActions(); - } - registerSrc(seen: Set = new Set()) { if (!this.getActions().length) { return; @@ -293,7 +278,7 @@ export class Scene extends Constructable< const futureScene = new Set(); while (queue.length) { - const action = queue.shift(); + const action = queue.shift()!; if (action instanceof SceneAction) { if (action.type === SceneActionTypes.jumpTo) { const jumpTo = action as SceneAction; @@ -307,14 +292,14 @@ export class Scene extends Constructable< futureScene.add(scene); seen.add(scene); } else if (action.type === SceneActionTypes.setBackground) { - const content = (action.contentNode as ContentNode).content[0]; + const content = (action.contentNode as ContentNode).getContent()[0]; this.srcManager.register(new Image({src: Utils.backgroundToSrc(content)})); } } else if (action instanceof ImageAction) { const imageAction = action as ImageAction; this.srcManager.register(imageAction.callee); if (action.type === ImageActionTypes.setSrc) { - const content = (action.contentNode as ContentNode).content[0]; + const content = (action.contentNode as ContentNode).getContent()[0]; this.srcManager.register(new Image({src: content})); } } else if (action instanceof SoundAction) { @@ -334,13 +319,33 @@ export class Scene extends Constructable< }); } + public action(actions: (Actions | Actions[])[]): this; + + public action(actions: ((scene: Scene) => Actions[])): this; + + public action(actions: (Actions | Actions[])[] | ((scene: Scene) => Actions[])): this { + const userActions = Array.isArray(actions) ? actions.flat(2) : actions(this).flat(2); + const images = this.getAllElements(this.getAllActions(false, userActions)) + .filter(element => element instanceof Image); + const futureActions = [ + ...this.activate().toActions(), + ...images.map(image => (image as Image).init().toActions()).flat(2), + ...userActions, + ]; + + this._sceneRoot = this._action(futureActions); + return this; + } + + protected getSceneActions() { + return this.toActions(); + } + private _jumpTo(scene: Scene) { this._actions.push(new SceneAction( this, "scene:jumpTo", - new ContentNode<[Scene]>( - Game.getIdManager().getStringId(), - ).setContent([ + new ContentNode<[Scene]>(Game.getIdManager().getStringId()).setContent([ scene ]) )); @@ -351,9 +356,7 @@ export class Scene extends Constructable< this._actions.push(new SceneAction( this, "scene:exit", - new ContentNode( - Game.getIdManager().getStringId(), - ).setContent([]) + new ContentNode(Game.getIdManager().getStringId()).setContent([]) )); return this; } @@ -361,7 +364,7 @@ export class Scene extends Constructable< private _transitionToScene(scene?: Scene, transition?: ITransition): this { if (transition) { const copy = transition.copy(); - if(scene) copy.setSrc(Utils.backgroundToSrc(scene.config.background)); + if (scene) copy.setSrc(Utils.backgroundToSrc(scene.config.background)); this._setTransition(copy) ._applyTransition(copy); } @@ -377,27 +380,9 @@ export class Scene extends Constructable< this._actions.push(new SceneAction( this, "scene:init", - new ContentNode( - Game.getIdManager().getStringId(), - ).setContent([]) + new ContentNode(Game.getIdManager().getStringId()).setContent([]) )); return this; } - - public action(actions: (Actions | Actions[])[]): this; - public action(actions: ((scene: Scene) => Actions[])): this; - public action(actions: (Actions | Actions[])[] | ((scene: Scene) => Actions[])): this { - const userActions = Array.isArray(actions) ? actions.flat(2) : actions(this).flat(2); - const images = this.getAllElements(this.getAllActions(false, userActions)) - .filter(element => element instanceof Image); - const futureActions = [ - ...this.activate().toActions(), - ...images.map(image => (image as Image).init().toActions()).flat(2), - ...userActions, - ]; - - this._sceneRoot = this._action(futureActions); - return this; - } } diff --git a/src/game/nlcore/elements/script.ts b/src/game/nlcore/elements/script.ts index ba479a6..e6bbc38 100644 --- a/src/game/nlcore/elements/script.ts +++ b/src/game/nlcore/elements/script.ts @@ -45,9 +45,7 @@ export class Script extends Actionable { new ScriptAction( this, ScriptAction.ActionTypes.action, - new ContentNode