mirror of
https://gitee.com/dgflash/oops-framework.git
synced 2026-05-18 00:16:21 +08:00
扩展ECSEntity可添加子实体对象与访问父实体对象的功能;CC3.6引擎API兼容处理
This commit is contained in:
@@ -2,10 +2,10 @@
|
||||
* @Author: dgflash
|
||||
* @Date: 2022-07-14 10:57:43
|
||||
* @LastEditors: dgflash
|
||||
* @LastEditTime: 2022-07-20 14:42:13
|
||||
* @LastEditTime: 2022-07-22 17:27:13
|
||||
*/
|
||||
import { _decorator } from 'cc';
|
||||
import { Timer } from '../../../extensions/oops-framework/assets/core/manager/TimerManager';
|
||||
import { Timer } from '../../../extensions/oops-framework/assets/core/common/manager/TimerManager';
|
||||
import { Root } from '../../../extensions/oops-framework/assets/core/Root';
|
||||
import { BehaviorTree, BTreeNode, Decorator, Sequence, Task } from '../../../extensions/oops-framework/assets/libs/behavior-tree';
|
||||
|
||||
|
||||
@@ -227,7 +227,7 @@
|
||||
"_priority": 1073741824,
|
||||
"_fov": 45,
|
||||
"_fovAxis": 0,
|
||||
"_orthoHeight": 375,
|
||||
"_orthoHeight": 382.0585048754063,
|
||||
"_near": 1,
|
||||
"_far": 2000,
|
||||
"_color": {
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
* @Author: dgflash
|
||||
* @Date: 2021-07-03 16:13:17
|
||||
* @LastEditors: dgflash
|
||||
* @LastEditTime: 2022-07-21 14:48:35
|
||||
* @LastEditTime: 2022-07-22 15:52:11
|
||||
*/
|
||||
import { dynamicAtlasManager, macro, profiler, _decorator } from 'cc';
|
||||
import { DEBUG, JSB } from 'cc/env';
|
||||
@@ -24,7 +24,7 @@ export class Main extends CommonEnter {
|
||||
if (DEBUG) profiler.showStats();
|
||||
}
|
||||
|
||||
protected async run() {
|
||||
protected async run() {this.node.children
|
||||
smc.initialize = ecs.getEntity<Initialize>(Initialize);
|
||||
if (JSB) {
|
||||
oops.gui.toast("热更新后新程序的提示");
|
||||
|
||||
@@ -2,10 +2,11 @@
|
||||
* @Author: dgflash
|
||||
* @Date: 2021-07-03 16:13:17
|
||||
* @LastEditors: dgflash
|
||||
* @LastEditTime: 2022-06-14 19:53:59
|
||||
* @LastEditTime: 2022-07-22 17:12:05
|
||||
*/
|
||||
import { ecs } from "../../../../../extensions/oops-framework/assets/libs/ecs/ECS";
|
||||
import { EcsAccountSystem } from "../../account/Account";
|
||||
import { EcsInitializeSystem } from "../../initialize/Initialize";
|
||||
import { EcsRoleSystem } from "../../role/Role";
|
||||
import { EcsPositionSystem } from "./position/EcsPositionSystem";
|
||||
|
||||
@@ -17,5 +18,6 @@ export class CommonSystem extends ecs.System {
|
||||
this.add(new EcsPositionSystem());
|
||||
this.add(new EcsAccountSystem());
|
||||
this.add(new EcsRoleSystem());
|
||||
this.add(new EcsInitializeSystem());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
* @Author: dgflash
|
||||
* @Date: 2021-11-18 14:20:46
|
||||
* @LastEditors: dgflash
|
||||
* @LastEditTime: 2022-06-14 19:54:06
|
||||
* @LastEditTime: 2022-07-22 17:38:46
|
||||
*/
|
||||
|
||||
import { ecs } from "../../../../../extensions/oops-framework/assets/libs/ecs/ECS";
|
||||
@@ -15,7 +15,9 @@ export class SingletonModuleComp extends ecs.Comp {
|
||||
/** 游戏初始化模块 */
|
||||
initialize: Initialize = null!;
|
||||
/** 游戏账号模块 */
|
||||
account: Account = null!;
|
||||
get account(): Account {
|
||||
return this.initialize.account;
|
||||
}
|
||||
|
||||
reset() { }
|
||||
}
|
||||
|
||||
@@ -2,15 +2,11 @@
|
||||
* @Author: dgflash
|
||||
* @Date: 2021-11-11 17:45:23
|
||||
* @LastEditors: dgflash
|
||||
* @LastEditTime: 2022-06-14 19:55:36
|
||||
* @LastEditTime: 2022-07-22 17:44:32
|
||||
*/
|
||||
import { resLoader } from "../../../../extensions/oops-framework/assets/core/common/loader/ResLoader";
|
||||
import { AsyncQueue, NextFunction } from "../../../../extensions/oops-framework/assets/core/common/queue/AsyncQueue";
|
||||
import { oops } from "../../../../extensions/oops-framework/assets/core/Oops";
|
||||
import { ecs } from "../../../../extensions/oops-framework/assets/libs/ecs/ECS";
|
||||
import { config } from "../common/config/Config";
|
||||
import { UIID } from "../common/config/GameUIConfig";
|
||||
import { LoadingViewComp } from "./view/LoadingViewComp";
|
||||
import { Account } from "../account/Account";
|
||||
import { InitResComp, InitResSystem } from "./bll/InitRes";
|
||||
|
||||
/**
|
||||
* 游戏进入初始化模块
|
||||
@@ -18,64 +14,23 @@ import { LoadingViewComp } from "./view/LoadingViewComp";
|
||||
* 2、加载默认资源
|
||||
*/
|
||||
export class Initialize extends ecs.Entity {
|
||||
LoadingView!: LoadingViewComp;
|
||||
/** 帐号管理 */
|
||||
account: Account = null!;
|
||||
|
||||
protected init() {
|
||||
var queue: AsyncQueue = new AsyncQueue();
|
||||
// 帐号模块为初始化模块的子实体对象
|
||||
this.account = ecs.getEntity<Account>(Account);
|
||||
this.addChild(this.account);
|
||||
|
||||
// 加载自定义资源
|
||||
this.loadCustom(queue);
|
||||
// 加载多语言包
|
||||
this.loadLanguage(queue);
|
||||
// 加载公共资源
|
||||
this.loadCommon(queue);
|
||||
// 加载游戏内容加载进度提示界面
|
||||
this.onComplete();
|
||||
|
||||
queue.play();
|
||||
// 初始化游戏公共资源
|
||||
this.add(InitResComp);
|
||||
}
|
||||
}
|
||||
|
||||
export class EcsInitializeSystem extends ecs.System {
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
/** 加载自定义内容(可选) */
|
||||
private loadCustom(queue: AsyncQueue) {
|
||||
queue.push(async (next: NextFunction, params: any, args: any) => {
|
||||
// 设置渠道号
|
||||
// if (config.query.channelId) SDKPlatform.setChannelId(config.query.channelId);
|
||||
|
||||
// 加载多语言对应字体
|
||||
resLoader.load("language/font/" + oops.language.current, next);
|
||||
});
|
||||
}
|
||||
|
||||
/** 加载化语言包(可选) */
|
||||
private loadLanguage(queue: AsyncQueue) {
|
||||
queue.push((next: NextFunction, params: any, args: any) => {
|
||||
// 设置默认语言
|
||||
let lan = oops.storage.get("language");
|
||||
if (lan == null) {
|
||||
// lan = SDKPlatform.getLanguage();
|
||||
lan = "zh";
|
||||
oops.storage.set("language", lan!);
|
||||
}
|
||||
|
||||
// 设置语言包路径
|
||||
oops.language.setAssetsPath(config.game.languagePathJson, config.game.languagePathTexture);
|
||||
|
||||
// 加载语言包资源
|
||||
oops.language.setLanguage(lan!, next);
|
||||
});
|
||||
}
|
||||
|
||||
/** 加载公共资源(必备) */
|
||||
private loadCommon(queue: AsyncQueue) {
|
||||
queue.push((next: NextFunction, params: any, args: any) => {
|
||||
resLoader.loadDir("common", next);
|
||||
});
|
||||
}
|
||||
|
||||
/** 加载完成进入游戏内容加载界面 */
|
||||
private async onComplete() {
|
||||
var node = await oops.gui.openAsync(UIID.Loading);
|
||||
if (node) this.add(node.getComponent(LoadingViewComp) as ecs.Comp);
|
||||
this.add(new InitResSystem());
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
"ver": "1.1.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "3403e996-9714-47ce-aa50-85dc701bb21d",
|
||||
"uuid": "de5401a8-45c8-4249-aa4c-933102551227",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
87
assets/script/game/initialize/bll/InitRes.ts
Normal file
87
assets/script/game/initialize/bll/InitRes.ts
Normal file
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* @Author: dgflash
|
||||
* @Date: 2022-07-22 17:06:22
|
||||
* @LastEditors: dgflash
|
||||
* @LastEditTime: 2022-07-22 17:09:30
|
||||
*/
|
||||
import { resLoader } from "../../../../../extensions/oops-framework/assets/core/common/loader/ResLoader";
|
||||
import { AsyncQueue, NextFunction } from "../../../../../extensions/oops-framework/assets/core/common/queue/AsyncQueue";
|
||||
import { oops } from "../../../../../extensions/oops-framework/assets/core/Oops";
|
||||
import { ecs } from "../../../../../extensions/oops-framework/assets/libs/ecs/ECS";
|
||||
import { config } from "../../common/config/Config";
|
||||
import { UIID } from "../../common/config/GameUIConfig";
|
||||
import { Initialize } from "../Initialize";
|
||||
import { LoadingViewComp } from "../view/LoadingViewComp";
|
||||
|
||||
/** 初始化游戏公共资源 */
|
||||
@ecs.register('InitRes')
|
||||
export class InitResComp extends ecs.Comp {
|
||||
reset() { }
|
||||
}
|
||||
|
||||
export class InitResSystem extends ecs.ComblockSystem implements ecs.IEntityEnterSystem {
|
||||
filter(): ecs.IMatcher {
|
||||
return ecs.allOf(InitResComp);
|
||||
}
|
||||
|
||||
entityEnter(e: Initialize): void {
|
||||
var queue: AsyncQueue = new AsyncQueue();
|
||||
|
||||
// 加载自定义资源
|
||||
this.loadCustom(queue);
|
||||
// 加载多语言包
|
||||
this.loadLanguage(queue);
|
||||
// 加载公共资源
|
||||
this.loadCommon(queue);
|
||||
// 加载游戏内容加载进度提示界面
|
||||
this.onComplete(queue, e);
|
||||
|
||||
queue.play();
|
||||
}
|
||||
|
||||
/** 加载自定义内容(可选) */
|
||||
private loadCustom(queue: AsyncQueue) {
|
||||
queue.push(async (next: NextFunction, params: any, args: any) => {
|
||||
// 设置渠道号
|
||||
// if (config.query.channelId) SDKPlatform.setChannelId(config.query.channelId);
|
||||
|
||||
// 加载多语言对应字体
|
||||
resLoader.load("language/font/" + oops.language.current, next);
|
||||
});
|
||||
}
|
||||
|
||||
/** 加载化语言包(可选) */
|
||||
private loadLanguage(queue: AsyncQueue) {
|
||||
queue.push((next: NextFunction, params: any, args: any) => {
|
||||
// 设置默认语言
|
||||
let lan = oops.storage.get("language");
|
||||
if (lan == null) {
|
||||
// lan = SDKPlatform.getLanguage();
|
||||
lan = "zh";
|
||||
oops.storage.set("language", lan!);
|
||||
}
|
||||
|
||||
// 设置语言包路径
|
||||
oops.language.setAssetsPath(config.game.languagePathJson, config.game.languagePathTexture);
|
||||
|
||||
// 加载语言包资源
|
||||
oops.language.setLanguage(lan!, next);
|
||||
});
|
||||
}
|
||||
|
||||
/** 加载公共资源(必备) */
|
||||
private loadCommon(queue: AsyncQueue) {
|
||||
queue.push((next: NextFunction, params: any, args: any) => {
|
||||
resLoader.loadDir("common", next);
|
||||
});
|
||||
}
|
||||
|
||||
/** 加载完成进入游戏内容加载界面 */
|
||||
private onComplete(queue: AsyncQueue, e: Initialize) {
|
||||
queue.complete = async () => {
|
||||
var node = await oops.gui.openAsync(UIID.Loading);
|
||||
if (node) e.add(node.getComponent(LoadingViewComp) as ecs.Comp);
|
||||
e.remove(InitResComp);
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "b843ea76-f1d0-4ed5-b9e7-98b0509b6be8",
|
||||
"uuid": "2ee0cf3e-ed1f-4414-a41b-f8e3e7f66964",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
@@ -1,4 +1,4 @@
|
||||
import { error, log, sys } from "cc";
|
||||
import { error, log, native, sys } from "cc";
|
||||
import { resLoader } from "../../../../../extensions/oops-framework/assets/core/common/loader/ResLoader";
|
||||
|
||||
/** 热更参数 */
|
||||
@@ -66,7 +66,7 @@ export class Hot {
|
||||
this.showSearchPath();
|
||||
|
||||
this.manifest = res.nativeUrl;
|
||||
this.storagePath = `${jsb.fileUtils.getWritablePath()}/oops_framework_remote`;
|
||||
this.storagePath = `${native.fileUtils.getWritablePath()}/oops_framework_remote`;
|
||||
this.assetsMgr = new jsb.AssetsManager(this.manifest, this.storagePath, (versionA, versionB) => {
|
||||
console.log("【热更新】客户端版本: " + versionA + ', 当前最新版本: ' + versionB);
|
||||
this.options?.onVersionInfo && this.options.onVersionInfo({ local: versionA, server: versionB });
|
||||
@@ -116,7 +116,7 @@ export class Hot {
|
||||
|
||||
/** 删除热更所有存储文件 */
|
||||
clearHotUpdateStorage() {
|
||||
jsb.fileUtils.removeDirectory(this.storagePath);
|
||||
native.fileUtils.removeDirectory(this.storagePath);
|
||||
}
|
||||
|
||||
// 检查更新
|
||||
@@ -191,11 +191,11 @@ export class Hot {
|
||||
|
||||
private onUpdateFinished() {
|
||||
this.assetsMgr.setEventCallback(null!);
|
||||
let searchPaths = jsb.fileUtils.getSearchPaths();
|
||||
let searchPaths = native.fileUtils.getSearchPaths();
|
||||
let newPaths = this.assetsMgr.getLocalManifest().getSearchPaths();
|
||||
Array.prototype.unshift.apply(searchPaths, newPaths);
|
||||
localStorage.setItem('HotUpdateSearchPaths', JSON.stringify(searchPaths));
|
||||
jsb.fileUtils.setSearchPaths(searchPaths);
|
||||
native.fileUtils.setSearchPaths(searchPaths);
|
||||
|
||||
console.log('【热更新】更新成功');
|
||||
this.options?.onUpdateSucceed && this.options.onUpdateSucceed();
|
||||
@@ -203,7 +203,7 @@ export class Hot {
|
||||
|
||||
private showSearchPath() {
|
||||
console.log("========================搜索路径========================");
|
||||
let searchPaths = jsb.fileUtils.getSearchPaths();
|
||||
let searchPaths = native.fileUtils.getSearchPaths();
|
||||
for (let i = 0; i < searchPaths.length; i++) {
|
||||
console.log("[" + i + "]: " + searchPaths[i]);
|
||||
}
|
||||
|
||||
@@ -2,14 +2,13 @@
|
||||
* @Author: dgflash
|
||||
* @Date: 2021-07-03 16:13:17
|
||||
* @LastEditors: dgflash
|
||||
* @LastEditTime: 2022-06-21 15:30:55
|
||||
* @LastEditTime: 2022-07-22 17:44:57
|
||||
*/
|
||||
import { _decorator } from "cc";
|
||||
import { resLoader } from "../../../../../extensions/oops-framework/assets/core/common/loader/ResLoader";
|
||||
import { oops } from "../../../../../extensions/oops-framework/assets/core/Oops";
|
||||
import { JsonUtil } from "../../../../../extensions/oops-framework/assets/core/utils/JsonUtil";
|
||||
import { ecs } from "../../../../../extensions/oops-framework/assets/libs/ecs/ECS";
|
||||
import { Account } from "../../account/Account";
|
||||
import { GameEvent } from "../../common/config/GameEvent";
|
||||
import { UIID } from "../../common/config/GameUIConfig";
|
||||
import { CCVMParentComp } from "../../common/ecs/CCVMParentComp";
|
||||
@@ -114,7 +113,6 @@ export class LoadingViewComp extends CCVMParentComp {
|
||||
/** 加载完成事件 */
|
||||
private onCompleteCallback() {
|
||||
// 初始化帐号模块
|
||||
smc.account = ecs.getEntity<Account>(Account);
|
||||
smc.account.connect();
|
||||
}
|
||||
}
|
||||
12
extensions/oops-framework/assets/core/common/collection.meta
Normal file
12
extensions/oops-framework/assets/core/common/collection.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"ver": "1.1.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "d82fce5b-a08f-4601-9538-b5ff033cb5bf",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"compressionType": {},
|
||||
"isRemoteBundle": {}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* @Author: dgflash
|
||||
* @Date: 2022-07-22 15:54:51
|
||||
* @LastEditors: dgflash
|
||||
* @LastEditTime: 2022-07-22 16:23:42
|
||||
*/
|
||||
|
||||
/**
|
||||
* 集合对象
|
||||
* 1、Map与Array集合体
|
||||
*/
|
||||
export class Collection<K, V> extends Map<K, V>{
|
||||
private _array: V[] = [];
|
||||
|
||||
get array() {
|
||||
return this._array;
|
||||
}
|
||||
|
||||
set(key: K, value: V) {
|
||||
if (!this.has(key)) {
|
||||
this._array.push(value);
|
||||
}
|
||||
|
||||
return super.set(key, value);
|
||||
}
|
||||
|
||||
delete(key: K): boolean {
|
||||
var value = this.get(key);
|
||||
if (value) {
|
||||
var index = this._array.indexOf(value);
|
||||
if (index > -1) this._array.splice(index, 1);
|
||||
return super.delete(key);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "1dcf502d-4105-4adc-a43f-da31c70f9822",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
@@ -1,120 +0,0 @@
|
||||
/** 引擎 utils.ts 中有一些基础数学方法 */
|
||||
|
||||
/** 随机管理 */
|
||||
export class RandomManager {
|
||||
private static _instance: RandomManager;
|
||||
public static get instance(): RandomManager {
|
||||
if (this._instance == null) {
|
||||
this._instance = new RandomManager();
|
||||
}
|
||||
return this._instance;
|
||||
}
|
||||
|
||||
// constructor() {
|
||||
// this.setSeed(1);
|
||||
|
||||
// for (let index = 0; index < 10; index++) {
|
||||
// console.log(this.getRandomInt(0, 100));
|
||||
// }
|
||||
|
||||
// var a = this.getRandomByMinMaxList(50, 100, 5)
|
||||
// console.log("随机的数字", a);
|
||||
|
||||
// var b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
||||
// var r = this.getRandomByObjectList(b, 5);
|
||||
// console.log("原始的对象", b);
|
||||
// console.log("随机的对象", r);
|
||||
|
||||
// var c = this.getRandomBySumList(5, -100,);
|
||||
// console.log("定和随机分配", c);
|
||||
// }
|
||||
|
||||
private seedrandom!: any;
|
||||
private getRandom(): number {
|
||||
if (this.seedrandom)
|
||||
return this.seedrandom.quick();
|
||||
|
||||
return Math.random();
|
||||
}
|
||||
|
||||
/** 设置随机种子 */
|
||||
setSeed(seed: number) {
|
||||
//@ts-ignore
|
||||
this.seedrandom = new Math.seedrandom(seed);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成指定范围的随机整数
|
||||
* @param min 最小值
|
||||
* @param max 最大值
|
||||
* @param type 类型
|
||||
*/
|
||||
getRandomInt(min: number, max: number, type: number = 2): number {
|
||||
min = Math.ceil(min);
|
||||
max = Math.floor(max);
|
||||
switch (type) {
|
||||
case 1: // [min,max) 得到一个两数之间的随机整数,这个值不小于min(如果min不是整数的话,得到一个向上取整的 min),并且小于(但不等于)max
|
||||
return Math.floor(this.getRandom() * (max - min)) + min;
|
||||
case 2: // [min,max] 得到一个两数之间的随机整数,包括两个数在内,这个值比min大(如果min不是整数,那就不小于比min大的整数),但小于(但不等于)max
|
||||
return Math.floor(this.getRandom() * (max - min + 1)) + min;
|
||||
case 3: // (min,max) 得到一个两数之间的随机整数
|
||||
return Math.floor(this.getRandom() * (max - min - 1)) + min + 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据最大值,最小值范围 生成随机数数组
|
||||
* @param min 最小值
|
||||
* @param max 最大值
|
||||
* @param n 随机个数
|
||||
* @param type 类型
|
||||
* @returns
|
||||
*/
|
||||
getRandomByMinMaxList(min: number, max: number, n: number, type: number = 2): Array<number> {
|
||||
var result: Array<number> = [];
|
||||
for (let i = 0; i < n; i++) {
|
||||
result.push(this.getRandomInt(min, max))
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数组中随机对象
|
||||
* @param objects 对象数组
|
||||
* @param n 随机个数
|
||||
* @returns
|
||||
*/
|
||||
getRandomByObjectList<T>(objects: Array<T>, n: number): Array<T> {
|
||||
var temp: Array<T> = objects.slice();
|
||||
var result: Array<T> = [];
|
||||
for (let i = 0; i < n; i++) {
|
||||
let index = this.getRandomInt(0, n, 1);
|
||||
result.push(temp.splice(index, 1)[0]);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 定和随机分配
|
||||
* @param n 随机数量
|
||||
* @param sum 随机元素合
|
||||
* @returns
|
||||
*/
|
||||
getRandomBySumList(n: number, sum: number) {
|
||||
var residue = sum;
|
||||
var value = 0;
|
||||
var result: Array<number> = [];
|
||||
for (let i = 0; i < n; i++) {
|
||||
value = this.getRandomInt(0, residue, 3);
|
||||
if (i == n - 1) {
|
||||
value = residue;
|
||||
}
|
||||
else {
|
||||
residue -= value;
|
||||
}
|
||||
result.push(value);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -1,242 +0,0 @@
|
||||
import { Component } from "cc";
|
||||
import { EventDispatcher } from "../common/event/EventDispatcher";
|
||||
|
||||
export const guid = function () {
|
||||
let guid: string = "";
|
||||
for (let i = 1; i <= 32; i++) {
|
||||
let n = Math.floor(Math.random() * 16.0).toString(16);
|
||||
guid += n;
|
||||
if ((i == 8) || (i == 12) || (i == 16) || (i == 20))
|
||||
guid += "-";
|
||||
}
|
||||
return guid;
|
||||
}
|
||||
|
||||
export class TimerManager extends EventDispatcher {
|
||||
private static times: any = {};
|
||||
private schedules: any = {};
|
||||
private _scheduleCount: number = 1;
|
||||
|
||||
private initTime: number = (new Date()).getTime(); // 当前游戏进入的时间毫秒值
|
||||
private component: Component;
|
||||
|
||||
// 服务器时间与本地时间间隔
|
||||
private _$serverTimeElasped: number = 0;
|
||||
|
||||
constructor(component: Component) {
|
||||
super();
|
||||
this.component = component;
|
||||
this.schedule(this.onUpdate.bind(this), 1);
|
||||
}
|
||||
/**
|
||||
* 设置服务器时间与本地时间间隔
|
||||
* @param val
|
||||
*/
|
||||
public serverTimeElasped(val?: number): number {
|
||||
if (val) {
|
||||
this._$serverTimeElasped = val;
|
||||
}
|
||||
return this._$serverTimeElasped;
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化日期显示 format= "yyyy-MM-dd hh:mm:ss";
|
||||
* @param format
|
||||
* @param date
|
||||
*/
|
||||
public format(format: string, date: Date): string {
|
||||
let o: any = {
|
||||
"M+": date.getMonth() + 1, // month
|
||||
"d+": date.getDate(), // day
|
||||
"h+": date.getHours(), // hour
|
||||
"m+": date.getMinutes(), // minute
|
||||
"s+": date.getSeconds(), // second
|
||||
"q+": Math.floor((date.getMonth() + 3) / 3), // quarter
|
||||
"S": date.getMilliseconds() // millisecond
|
||||
}
|
||||
if (/(y+)/.test(format)) {
|
||||
format = format.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
|
||||
}
|
||||
|
||||
for (let k in o) {
|
||||
if (new RegExp("(" + k + ")").test(format)) {
|
||||
format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
|
||||
}
|
||||
}
|
||||
return format;
|
||||
}
|
||||
|
||||
/** 获取游戏开始到现在逝去的时间 */
|
||||
public getTime(): number {
|
||||
return this.getLocalTime() - this.initTime;
|
||||
}
|
||||
|
||||
/** 获取本地时间刻度 */
|
||||
public getLocalTime(): number {
|
||||
return Date.now();
|
||||
}
|
||||
|
||||
public schedule(callback: Function, interval: number): string {
|
||||
let UUID = `schedule_${this._scheduleCount++}`
|
||||
this.schedules[UUID] = callback;
|
||||
this.component.schedule(callback, interval);
|
||||
return UUID;
|
||||
}
|
||||
|
||||
public scheduleOnce(callback: Function, delay: number = 0): string {
|
||||
let UUID = `scheduleOnce_${this._scheduleCount++}`;
|
||||
this.schedules[UUID] = callback;
|
||||
this.component.scheduleOnce(() => {
|
||||
let cb = this.schedules[UUID];
|
||||
if (cb) {
|
||||
cb();
|
||||
}
|
||||
this.unschedule(UUID);
|
||||
}, Math.max(delay, 0));
|
||||
return UUID;
|
||||
}
|
||||
|
||||
public unschedule(uuid: string) {
|
||||
let cb = this.schedules[uuid];
|
||||
if (cb) {
|
||||
this.component.unschedule(cb);
|
||||
delete this.schedules[uuid];
|
||||
}
|
||||
}
|
||||
|
||||
public unscheduleAllCallbacks() {
|
||||
for (let k in this.schedules) {
|
||||
this.component.unschedule(this.schedules[k]);
|
||||
}
|
||||
this.schedules = {};
|
||||
}
|
||||
|
||||
onUpdate(dt: number) {
|
||||
// 后台管理倒计时完成事件
|
||||
for (let key in TimerManager.times) {
|
||||
let data = TimerManager.times[key];
|
||||
if (data.object[data.field] > 0) {
|
||||
data.object[data.field]--;
|
||||
|
||||
if (data.object[data.field] == 0) {
|
||||
this.timerComplete(data);
|
||||
}
|
||||
else { // 修改是否完成状态
|
||||
if (data.onSecond) {
|
||||
data.onSecond.call(data.object); // 触发每秒回调事件
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** 游戏最小划时记录时间数据 */
|
||||
public save() {
|
||||
for (let key in TimerManager.times) {
|
||||
TimerManager.times[key].startTime = this.getTime();
|
||||
}
|
||||
}
|
||||
|
||||
/** 游戏最大化时回复时间数据 */
|
||||
public load() {
|
||||
for (let key in TimerManager.times) {
|
||||
let interval = Math.floor((this.getTime() - (TimerManager.times[key].startTime || this.getTime())) / 1000);
|
||||
let data = TimerManager.times[key];
|
||||
data.object[data.field] = data.object[data.field] - interval;
|
||||
if (data.object[data.field] < 0) {
|
||||
data.object[data.field] = 0;
|
||||
this.timerComplete(data);
|
||||
}
|
||||
TimerManager.times[key].startTime = null;
|
||||
}
|
||||
}
|
||||
|
||||
/** 触发倒计时完成事件 */
|
||||
private timerComplete(data: any) {
|
||||
if (data.onComplete) data.onComplete.call(data.object);
|
||||
if (data.event) this.dispatchEvent(data.event);
|
||||
}
|
||||
|
||||
/** 注册指定对象的倒计时属性更新 */
|
||||
public registerObject(object: any, field: string, onSecond: Function, onComplete: Function) {
|
||||
let data: any = {};
|
||||
data.id = guid();
|
||||
data.object = object; // 管理对象
|
||||
data.field = field; // 时间字段
|
||||
data.onSecond = onSecond; // 每秒事件
|
||||
data.onComplete = onComplete; // 倒计时完成事件
|
||||
TimerManager.times[data.id] = data;
|
||||
return data.id;
|
||||
}
|
||||
|
||||
/** 注消指定对象的倒计时属性更新 */
|
||||
public unRegisterObject(id: string) {
|
||||
if (TimerManager.times[id])
|
||||
delete TimerManager.times[id];
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册事件需要管理的实时变化的时间对象
|
||||
* @param event(String) 时间为零时触发的事件
|
||||
* @param object(object) 需要管理的数据结构对象
|
||||
* @param field(Array) 需要管理的字段
|
||||
*/
|
||||
public register(event: string, object: any, field: Array<string>) {
|
||||
let data: any = {};
|
||||
data.id = event;
|
||||
data.event = event; // 倒计时完成事件
|
||||
data.object = object; // 管理对象
|
||||
data.field = field; // 时间字段
|
||||
TimerManager.times[data.id] = data;
|
||||
}
|
||||
|
||||
/** 注销定时器 */
|
||||
public unRegister(event: string) {
|
||||
if (TimerManager.times[event])
|
||||
delete TimerManager.times[event];
|
||||
}
|
||||
}
|
||||
|
||||
/** 定时跳动组件 */
|
||||
export class Timer {
|
||||
public callback: Function | null = null;
|
||||
|
||||
private _elapsedTime: number = 0;
|
||||
|
||||
public get elapsedTime(): number {
|
||||
return this._elapsedTime;
|
||||
}
|
||||
|
||||
private _step: number = 0;
|
||||
/** 触发间隔时间(秒) */
|
||||
get step(): number {
|
||||
return this._step;
|
||||
}
|
||||
set step(step: number) {
|
||||
this._step = step; // 每次修改时间
|
||||
this._elapsedTime = 0; // 逝去时间
|
||||
}
|
||||
|
||||
public get progress(): number {
|
||||
return this._elapsedTime / this._step;
|
||||
}
|
||||
|
||||
constructor(step: number = 0) {
|
||||
this.step = step;
|
||||
}
|
||||
|
||||
public update(dt: number) {
|
||||
this._elapsedTime += dt;
|
||||
|
||||
if (this._elapsedTime >= this._step) {
|
||||
this._elapsedTime -= this._step;
|
||||
this.callback?.call(this);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public reset() {
|
||||
this._elapsedTime = 0;
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "246133c2-f8da-42af-9a59-dda24e690801",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"moduleId": "project:///assets/script/core/utils/TimerManager.js",
|
||||
"importerSettings": 4,
|
||||
"simulateGlobals": []
|
||||
}
|
||||
}
|
||||
@@ -58,21 +58,53 @@ function destroyEntity(entity: ECSEntity) {
|
||||
//#endregion
|
||||
|
||||
export class ECSEntity {
|
||||
/**
|
||||
* 实体唯一标识,不要手动修改。
|
||||
*/
|
||||
/** 实体唯一标识,不要手动修改 */
|
||||
eid: number = -1;
|
||||
|
||||
/** 组件过滤数据 */
|
||||
private mask = new ECSMask();
|
||||
/** 当前实体身上附加的组件构造函数 */
|
||||
private compTid2Ctor: Map<number, ecs.CompType<ecs.IComp>> = new Map();
|
||||
/** 配合 entity.remove(Comp, false), 记录组件实例上的缓存数据,在添加时恢复原数据 */
|
||||
private compTid2Obj: Map<number, ecs.IComp> = new Map();
|
||||
|
||||
private _parent: ECSEntity | null = null;
|
||||
/** 父实体 */
|
||||
get parent(): ECSEntity | null {
|
||||
return this._parent;
|
||||
}
|
||||
|
||||
private _children: Map<number, ECSEntity> | null = null;
|
||||
/** 子实体集合 */
|
||||
get children(): Map<number, ECSEntity> {
|
||||
if (this._children == null) {
|
||||
this._children = new Map<number, ECSEntity>();
|
||||
}
|
||||
return this._children;
|
||||
}
|
||||
|
||||
/**
|
||||
* 当前实体身上附加的组件构造函数
|
||||
* 添加子实体
|
||||
* @param entity 被添加的实体对象
|
||||
*/
|
||||
private compTid2Ctor: Map<number, ecs.CompType<ecs.IComp>> = new Map();
|
||||
addChild(entity: ECSEntity) {
|
||||
entity._parent = this;
|
||||
this.children.set(entity.eid, entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* 配合 entity.remove(Comp, false), 记录组件实例上的缓存数据,在添加时恢复原数据
|
||||
* 移除子实体
|
||||
* @param entity 被移除的实体对象
|
||||
* @returns
|
||||
*/
|
||||
private compTid2Obj: Map<number, ecs.IComp> = new Map();
|
||||
removeChild(entity: ECSEntity) {
|
||||
if (this.children == null) return;
|
||||
|
||||
this.children.delete(entity.eid);
|
||||
|
||||
if (this.children.size == 0) {
|
||||
this._children = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据组件id动态创建组件,并通知关心的系统。
|
||||
|
||||
Reference in New Issue
Block a user