Skip to content

Commit

Permalink
use scenemgr replace SceneRefCom
Browse files Browse the repository at this point in the history
  • Loading branch information
Alistairot committed Apr 9, 2024
1 parent 16048ff commit b4ce017
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 73 deletions.
11 changes: 5 additions & 6 deletions dist/moye.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -563,12 +563,6 @@ export declare class SceneFactory {
static createClientScene(): Scene;
static createCurrentScene(id: bigint, name: string): Scene;
}
/**
* manage client scene
*/
export declare class SceneRefCom extends Entity {
scene: Scene;
}
/**
* key对应value数组的map
*/
Expand All @@ -583,6 +577,11 @@ export declare class MultiMap<T, K> {
getOne(t: T): K | undefined;
contains(t: T, k: K): boolean;
}
export declare class SceneMgr extends Singleton {
process: Scene;
client: Scene;
current: Scene;
}
export interface Entity {
currentScene(): Scene;
clientScene(): Scene;
Expand Down
2 changes: 1 addition & 1 deletion dist/moye.min.mjs

Large diffs are not rendered by default.

61 changes: 29 additions & 32 deletions dist/moye.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1676,43 +1676,43 @@ class CoroutineLock extends Singleton {
}
}

/**
* manage client scene
*/
class SceneRefCom extends Entity {
class SceneMgr extends Singleton {
}

// import { SceneRefCom } from "./SceneRefCom";
class SceneFactory {
static createClientScene() {
const parent = Root.get().scene.getCom(SceneRefCom);
parent.scene?.dispose();
// const parent = Root.get().scene.getCom(SceneRefCom);
// parent.scene?.dispose();
const scene = new Scene();
scene.init({
id: 1n,
sceneType: SceneType.CLIENT,
name: "Game",
instanceId: IdGenerator.get().generateInstanceId(),
parent: parent
parent: SceneMgr.get().process
});
scene.addCom(SceneRefCom);
parent.scene = scene;
// scene.addCom(SceneRefCom);
SceneMgr.get().client = scene;
// parent.scene = scene;
EventSystem.get().publish(scene, AfterCreateClientScene.create());
return scene;
}
static createCurrentScene(id, name) {
const clientSceneRef = Root.get().scene.getCom(SceneRefCom);
const clientScene = clientSceneRef.scene;
const parent = clientScene.getCom(SceneRefCom);
parent.scene?.dispose();
// const clientSceneRef = Root.get().scene.getCom(SceneRefCom);
// const clientScene = clientSceneRef.scene;
// const parent = clientScene.getCom(SceneRefCom);
SceneMgr.get().current?.dispose();
const scene = new Scene();
scene.init({
id: id,
sceneType: SceneType.CURRENT,
name: name,
instanceId: IdGenerator.get().generateInstanceId(),
parent: parent
parent: SceneMgr.get().client
});
parent.scene = scene;
// parent.scene = scene;
SceneMgr.get().current = scene;
EventSystem.get().publish(scene, AfterCreateCurrentScene.create());
return scene;
}
Expand All @@ -1731,6 +1731,7 @@ class Program {
Game.addSingleton(EntityCenter);
Game.addSingleton(EntityLifiCycleMgr);
Game.addSingleton(Root);
Game.addSingleton(SceneMgr).process = Root.get().scene;
// add client runtime
rootNode.addComponent(MoyeRuntime);
MoyeEventCenter.inst.publish(new AfterProgramInit());
Expand All @@ -1744,7 +1745,7 @@ class Program {
MoyeEventCenter.inst.publish(new BeforeProgramStart());
MoyeEventCenter.inst.publish(new AfterProgramStart());
// create client scene
Root.get().scene.addCom(SceneRefCom);
// Root.get().scene.addCom(SceneRefCom);
SceneFactory.createClientScene();
}
}
Expand Down Expand Up @@ -1898,23 +1899,19 @@ class MultiMap {

Entity.prototype.clientScene = function () {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const self = this;
const domainScene = self.domainScene();
if (domainScene.sceneType == SceneType.CLIENT) {
return domainScene;
}
else if (domainScene.sceneType == SceneType.CURRENT) {
return domainScene.parent.parent;
}
else if (domainScene.sceneType == SceneType.PROCESS) {
return domainScene.getCom(SceneRefCom).scene;
}
// const self: Entity = this;
// const domainScene = self.domainScene();
// if(domainScene.sceneType == SceneType.CLIENT) {
// return domainScene;
// }else if(domainScene.sceneType == SceneType.CURRENT) {
// return domainScene.parent.parent as Scene;
// }else if(domainScene.sceneType == SceneType.PROCESS) {
// return domainScene.getCom(SceneRefCom).scene;
// }
return SceneMgr.get().client;
};
Entity.prototype.currentScene = function () {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const self = this;
const clientScene = self.clientScene();
return clientScene.getCom(SceneRefCom).scene;
return SceneMgr.get().current;
};

/**
Expand Down Expand Up @@ -6819,4 +6816,4 @@ class YYJJoystickListener extends Entity {
}
}

export { AEvent, AEventHandler, AMHandler, AMoyeView, AWait, AfterAddLoginCom, AfterCreateClientScene, AfterCreateCurrentScene, AfterProgramInit, AfterProgramStart, AfterSingletonAdd, AssetOperationHandle, AsyncButtonListener, BeforeProgramInit, BeforeProgramStart, BeforeSingletonAdd, BgAdapter, BundleAsset, CTWidget, CancellationToken, CancellationTokenTag, CenterLayout, CoroutineLock, CoroutineLockItem, CoroutineLockTag, DecoratorCollector, Entity, EntityCenter, EventCom, EventDecorator, EventDecoratorType, EventHandlerTag, EventSystem, Game, IPEndPoint, IdGenerator, IdStruct, InstanceIdStruct, JsHelper, LocalStorageHelper, Logger, LoginCom, MoyeAssets, MoyeLabel, MoyeViewMgr, MsgHandlerDecorator, MsgHandlerDecoratorType, MsgMgr, MsgSerializeMgr, MultiMap, NetCom, NetServices, NetworkErrorCode, NodeNotBuild, ObjectPool, ObjectWait, Program, RecycleObj, RichTextListener, Root, RoundBoxSprite, Scene, SceneFactory, SceneRefCom, SceneType, Session, SessionCom, Singleton, SizeFollow, SpeedType, Task, TimeHelper, TimeInfo, TimerMgr, UIController, UIControllerAttr, UIControllerIndex, UIControllerListener, ViewDecorator, ViewDecoratorType, ViewLayer, WChannel, WService, WaitError, YYJJoystick, YYJJoystickCom, YYJJoystickListener, YYJJoystickMoveEvent, YYJJoystickSpeedChangeEvent, debug, debugF, error, errorF, log, logF, safeCall, warn, warnF };
export { AEvent, AEventHandler, AMHandler, AMoyeView, AWait, AfterAddLoginCom, AfterCreateClientScene, AfterCreateCurrentScene, AfterProgramInit, AfterProgramStart, AfterSingletonAdd, AssetOperationHandle, AsyncButtonListener, BeforeProgramInit, BeforeProgramStart, BeforeSingletonAdd, BgAdapter, BundleAsset, CTWidget, CancellationToken, CancellationTokenTag, CenterLayout, CoroutineLock, CoroutineLockItem, CoroutineLockTag, DecoratorCollector, Entity, EntityCenter, EventCom, EventDecorator, EventDecoratorType, EventHandlerTag, EventSystem, Game, IPEndPoint, IdGenerator, IdStruct, InstanceIdStruct, JsHelper, LocalStorageHelper, Logger, LoginCom, MoyeAssets, MoyeLabel, MoyeViewMgr, MsgHandlerDecorator, MsgHandlerDecoratorType, MsgMgr, MsgSerializeMgr, MultiMap, NetCom, NetServices, NetworkErrorCode, NodeNotBuild, ObjectPool, ObjectWait, Program, RecycleObj, RichTextListener, Root, RoundBoxSprite, Scene, SceneFactory, SceneMgr, SceneType, Session, SessionCom, Singleton, SizeFollow, SpeedType, Task, TimeHelper, TimeInfo, TimerMgr, UIController, UIControllerAttr, UIControllerIndex, UIControllerListener, ViewDecorator, ViewDecoratorType, ViewLayer, WChannel, WService, WaitError, YYJJoystick, YYJJoystickCom, YYJJoystickListener, YYJJoystickMoveEvent, YYJJoystickSpeedChangeEvent, debug, debugF, error, errorF, log, logF, safeCall, warn, warnF };
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "moye-cocos",
"version": "2.0.1",
"version": "2.1.0",
"description": "ecs game framework of cocos creator",
"main": "dist/moye.mjs",
"types": "dist/moye.d.ts",
Expand Down
3 changes: 2 additions & 1 deletion src/Core/Core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ export * from "./Scene/SceneRefCom";
export * from "./Task/Task";
export * from "./Type/Action";
export * from "./Type/Type";
export * from "./DataStructure/MultiMap";
export * from "./DataStructure/MultiMap";
export * from "./Scene/SceneMgr";
7 changes: 5 additions & 2 deletions src/Core/Program/Program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import { EntityLifiCycleMgr } from "../Entity/EntityLifiCycleMgr";
import { Root } from "../Entity/Root";
import { TimerMgr } from "../Timer/TimerMgr";
import { CoroutineLock } from "../CoroutineLock/CoroutineLock";
import { SceneRefCom } from "../Scene/SceneRefCom";
// import { SceneRefCom } from "../Scene/SceneRefCom";
import { SceneFactory } from "../Scene/SceneFactory";
import { SceneMgr } from "../Scene/SceneMgr";

export class Program {
static init(rootNode: Node) {
Expand All @@ -31,6 +32,8 @@ export class Program {
Game.addSingleton(EntityLifiCycleMgr);
Game.addSingleton(Root);

Game.addSingleton(SceneMgr).process = Root.get().scene;

// add client runtime
rootNode.addComponent(MoyeRuntime);

Expand All @@ -47,7 +50,7 @@ export class Program {
MoyeEventCenter.inst.publish(new AfterProgramStart());

// create client scene
Root.get().scene.addCom(SceneRefCom);
// Root.get().scene.addCom(SceneRefCom);
SceneFactory.createClientScene();
}
}
29 changes: 17 additions & 12 deletions src/Core/Scene/SceneFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,54 @@ import { SceneType } from "../Entity/SceneType";
import { AfterCreateClientScene, AfterCreateCurrentScene } from "../EventSystem/EventCore";
import { EventSystem } from "../EventSystem/EventSystem";
import { IdGenerator } from "../IdGenerator/IdGenerator";
import { SceneRefCom } from "./SceneRefCom";
import { SceneMgr } from "./SceneMgr";
// import { SceneRefCom } from "./SceneRefCom";

export class SceneFactory {
static createClientScene(): Scene {
const parent = Root.get().scene.getCom(SceneRefCom);
// const parent = Root.get().scene.getCom(SceneRefCom);

parent.scene?.dispose();
// parent.scene?.dispose();

const scene = new Scene();
scene.init({
id: 1n,
sceneType: SceneType.CLIENT,
name: "Game",
instanceId: IdGenerator.get().generateInstanceId(),
parent: parent
parent: SceneMgr.get().process
});

scene.addCom(SceneRefCom);
// scene.addCom(SceneRefCom);

parent.scene = scene;
SceneMgr.get().client = scene;

// parent.scene = scene;

EventSystem.get().publish(scene, AfterCreateClientScene.create());

return scene;
}

static createCurrentScene(id: bigint, name: string): Scene {
const clientSceneRef = Root.get().scene.getCom(SceneRefCom);
const clientScene = clientSceneRef.scene;
const parent = clientScene.getCom(SceneRefCom);
// const clientSceneRef = Root.get().scene.getCom(SceneRefCom);
// const clientScene = clientSceneRef.scene;
// const parent = clientScene.getCom(SceneRefCom);

parent.scene?.dispose();
SceneMgr.get().current?.dispose();

const scene = new Scene();
scene.init({
id: id,
sceneType: SceneType.CURRENT,
name: name,
instanceId: IdGenerator.get().generateInstanceId(),
parent: parent
parent: SceneMgr.get().client
});

parent.scene = scene;
// parent.scene = scene;

SceneMgr.get().current = scene;

EventSystem.get().publish(scene, AfterCreateCurrentScene.create());

Expand Down
8 changes: 8 additions & 0 deletions src/Core/Scene/SceneMgr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Scene } from "../Entity/Scene";
import { Singleton } from "../Singleton/Singleton";

export class SceneMgr extends Singleton {
process: Scene;
client: Scene;
current: Scene;
}
6 changes: 3 additions & 3 deletions src/Core/Scene/SceneRefCom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import { Scene } from "../Entity/Scene";
/**
* manage client scene
*/
export class SceneRefCom extends Entity {
scene: Scene;
}
// export class SceneRefCom extends Entity {
// scene: Scene;
// }
29 changes: 14 additions & 15 deletions src/EntityExt/EntityExtension.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Entity, Scene, SceneRefCom, SceneType } from "../Core/Core";
import { Entity, Scene, SceneType } from "../Core/Core";
import { SceneMgr } from "../Core/Scene/SceneMgr";


declare module "../Core/Core" {
Expand All @@ -11,22 +12,20 @@ declare module "../Core/Core" {

Entity.prototype.clientScene = function () {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const self: Entity = this;
const domainScene = self.domainScene();
// const self: Entity = this;
// const domainScene = self.domainScene();

if(domainScene.sceneType == SceneType.CLIENT) {
return domainScene;
}else if(domainScene.sceneType == SceneType.CURRENT) {
return domainScene.parent.parent as Scene;
}else if(domainScene.sceneType == SceneType.PROCESS) {
return domainScene.getCom(SceneRefCom).scene;
}
// if(domainScene.sceneType == SceneType.CLIENT) {
// return domainScene;
// }else if(domainScene.sceneType == SceneType.CURRENT) {
// return domainScene.parent.parent as Scene;
// }else if(domainScene.sceneType == SceneType.PROCESS) {
// return domainScene.getCom(SceneRefCom).scene;
// }

return SceneMgr.get().client;
};

Entity.prototype.currentScene = function () {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const self: Entity = this;
const clientScene = self.clientScene();

return clientScene.getCom(SceneRefCom).scene;
return SceneMgr.get().current;
};

0 comments on commit b4ce017

Please sign in to comment.