From fdb002d00a3fbcd8a7d04c8f2d2d84114f46a929 Mon Sep 17 00:00:00 2001 From: honmono <1099263878@qq.com> Date: Sun, 15 May 2022 11:30:36 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BF=AE=E5=A4=8D=E4=BA=86=E6=89=BE?= =?UTF-8?q?=E4=B8=8D=E5=88=B0comTrans=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/Scene/helloworld.fire | 202 +++++++++++++++++++++- assets/Script/Common/BehaviorTree.ts | 4 + assets/Script/Core/RoleEventProcess.ts | 10 +- assets/Script/ECS/lib/ECSWorld.ts | 40 ++--- assets/Script/ECS/systems/SysAttack.ts | 2 +- assets/Script/ECS/systems/SysCocosView.ts | 12 +- assets/Script/Main.ts | 15 +- assets/resources/Biker/Biker.prefab | 2 +- assets/resources/Cyborg/Cyborg.prefab | 2 +- 9 files changed, 252 insertions(+), 37 deletions(-) diff --git a/assets/Scene/helloworld.fire b/assets/Scene/helloworld.fire index baba273..fb6c6e7 100755 --- a/assets/Scene/helloworld.fire +++ b/assets/Scene/helloworld.fire @@ -85,18 +85,24 @@ }, { "__id__": 26 + }, + { + "__id__": 34 + }, + { + "__id__": 36 } ], "_active": true, "_components": [ { - "__id__": 34 + "__id__": 38 }, { - "__id__": 35 + "__id__": 39 }, { - "__id__": 36 + "__id__": 40 } ], "_prefab": null, @@ -1644,6 +1650,196 @@ "handler": "onClick3", "customEventData": "" }, + { + "__type__": "cc.Node", + "_name": "a_role_count", + "_objFlags": 0, + "_parent": { + "__id__": 2 + }, + "_children": [], + "_active": true, + "_components": [ + { + "__id__": 35 + } + ], + "_prefab": null, + "_opacity": 255, + "_color": { + "__type__": "cc.Color", + "r": 255, + "g": 255, + "b": 255, + "a": 255 + }, + "_contentSize": { + "__type__": "cc.Size", + "width": 97.87, + "height": 50.4 + }, + "_anchorPoint": { + "__type__": "cc.Vec2", + "x": 0.5, + "y": 0.5 + }, + "_trs": { + "__type__": "TypedArray", + "ctor": "Float64Array", + "array": [ + -504.601, + 294.479, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 1 + ] + }, + "_eulerAngles": { + "__type__": "cc.Vec3", + "x": 0, + "y": 0, + "z": 0 + }, + "_skewX": 0, + "_skewY": 0, + "_is3DNode": false, + "_groupIndex": 0, + "groupIndex": 0, + "_id": "a4JCp5+xFOA4s8fngj8szD" + }, + { + "__type__": "cc.Label", + "_name": "", + "_objFlags": 0, + "node": { + "__id__": 34 + }, + "_enabled": true, + "_materials": [ + { + "__uuid__": "eca5d2f2-8ef6-41c2-bbe6-f9c79d09c432" + } + ], + "_srcBlendFactor": 770, + "_dstBlendFactor": 771, + "_string": "Label", + "_N$string": "Label", + "_fontSize": 40, + "_lineHeight": 40, + "_enableWrapText": true, + "_N$file": null, + "_isSystemFontUsed": true, + "_spacingX": 0, + "_batchAsBitmap": false, + "_styleFlags": 0, + "_underlineHeight": 0, + "_N$horizontalAlign": 1, + "_N$verticalAlign": 1, + "_N$fontFamily": "Arial", + "_N$overflow": 0, + "_N$cacheMode": 0, + "_id": "4417a7mtlNhbcCkscLMkuR" + }, + { + "__type__": "cc.Node", + "_name": "b_role_count", + "_objFlags": 0, + "_parent": { + "__id__": 2 + }, + "_children": [], + "_active": true, + "_components": [ + { + "__id__": 37 + } + ], + "_prefab": null, + "_opacity": 255, + "_color": { + "__type__": "cc.Color", + "r": 255, + "g": 255, + "b": 255, + "a": 255 + }, + "_contentSize": { + "__type__": "cc.Size", + "width": 97.87, + "height": 50.4 + }, + "_anchorPoint": { + "__type__": "cc.Vec2", + "x": 0.5, + "y": 0.5 + }, + "_trs": { + "__type__": "TypedArray", + "ctor": "Float64Array", + "array": [ + -349.693, + 294.479, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 1 + ] + }, + "_eulerAngles": { + "__type__": "cc.Vec3", + "x": 0, + "y": 0, + "z": 0 + }, + "_skewX": 0, + "_skewY": 0, + "_is3DNode": false, + "_groupIndex": 0, + "groupIndex": 0, + "_id": "80NAtDFhBOxrIjst58bHAy" + }, + { + "__type__": "cc.Label", + "_name": "", + "_objFlags": 0, + "node": { + "__id__": 36 + }, + "_enabled": true, + "_materials": [ + { + "__uuid__": "eca5d2f2-8ef6-41c2-bbe6-f9c79d09c432" + } + ], + "_srcBlendFactor": 770, + "_dstBlendFactor": 771, + "_string": "Label", + "_N$string": "Label", + "_fontSize": 40, + "_lineHeight": 40, + "_enableWrapText": true, + "_N$file": null, + "_isSystemFontUsed": true, + "_spacingX": 0, + "_batchAsBitmap": false, + "_styleFlags": 0, + "_underlineHeight": 0, + "_N$horizontalAlign": 1, + "_N$verticalAlign": 1, + "_N$fontFamily": "Arial", + "_N$overflow": 0, + "_N$cacheMode": 0, + "_id": "7a9iiLwS1KqqiX/+3BS+Bg" + }, { "__type__": "cc.Canvas", "_name": "", diff --git a/assets/Script/Common/BehaviorTree.ts b/assets/Script/Common/BehaviorTree.ts index fad8295..31bceed 100644 --- a/assets/Script/Common/BehaviorTree.ts +++ b/assets/Script/Common/BehaviorTree.ts @@ -627,6 +627,10 @@ export namespace BT { let comTrans = context.world.getComponent(context.entity, ComTransform); let comMonitor = context.world.getComponent(context.entity, ComMonitor); let comMovable = context.world.getComponent(context.entity, ComMovable); + if(comMonitor.others.length <=0){ + node.state = BT.NodeState.Fail; + return ; + } if(comMovable.points.length == 0 || comMovable.pointIdx < 0 || comMovable.pointIdx >= comMovable.points.length) { let target = context.world.getComponent(comMonitor.others[0], ComTransform); diff --git a/assets/Script/Core/RoleEventProcess.ts b/assets/Script/Core/RoleEventProcess.ts index eb02598..c45210e 100644 --- a/assets/Script/Core/RoleEventProcess.ts +++ b/assets/Script/Core/RoleEventProcess.ts @@ -59,11 +59,11 @@ export default class RoleEventProcess extends EventProcess { break; case EventType.GraphicsDraw: - if(cc.debug.isDisplayStats()) { - this._graphicsDraw(event as EventGraphicsDraw); - }else { - this._graphicsDraw(null); - } + // if(cc.debug.isDisplayStats()) { + // this._graphicsDraw(event as EventGraphicsDraw); + // }else { + // this._graphicsDraw(null); + // } break; diff --git a/assets/Script/ECS/lib/ECSWorld.ts b/assets/Script/ECS/lib/ECSWorld.ts index 6abf904..593581b 100755 --- a/assets/Script/ECS/lib/ECSWorld.ts +++ b/assets/Script/ECS/lib/ECSWorld.ts @@ -4,17 +4,15 @@ import { ECSSystem } from "./ECSSystem"; import { ComPoolIndex, ComType, EntityIndex } from "./Const"; import { ECSComponentPool } from "./ECSComponentPool"; -/** - * - */ -export class ECSWorld { - private _systems: ECSSystem[] = []; // world内所有的system - private _reservedIds: number[] = []; // 缓存 - private _entityToComponents: number[][] = []; - private _componentPools: ECSComponentPool[] = []; - private _fillters = new Map(); - private _entitiesToDelete: Set = new Set(); +export class ECSWorld { + private _systems: ECSSystem[] = []; // world内所有的system + private _entityIdxPools: number[] = []; // 回收entity + private _entityToComponents: number[][] = []; // entity component 二维表 + private _entitiesToDelete: Set = new Set(); // 统一删除entity + private _componentPools: ECSComponentPool[] = []; // component pools + private _filters = new Map(); // filter + /** 获取ComponentPool */ public getComponentPool(typeOrFunc: ComType | {prototype: T}): ECSComponentPool { let type = typeof typeOrFunc == "number" ? typeOrFunc : GetComConstructorType(typeOrFunc); @@ -49,8 +47,8 @@ export class ECSWorld { /** 创建实体 */ public createEntity(): number { let index = -1; - if(this._reservedIds.length > 0) { - index = this._reservedIds.pop(); + if(this._entityIdxPools.length > 0) { + index = this._entityIdxPools.pop(); this._entityToComponents[index].fill(-1); }else { index = this._entityToComponents.length; @@ -70,7 +68,7 @@ export class ECSWorld { return false; } - this._fillters.forEach((fillter, key) => { + this._filters.forEach((fillter, key) => { fillter.isContains(entityIndex) && fillter.onEntityLeave(entityIndex); }); for(let system of this._systems) { @@ -81,7 +79,7 @@ export class ECSWorld { return true; } - public getComponentPoolIdx(entityIndex: EntityIndex, com: {prototype: T} | ComType) { + public getComponentPoolIdx(entityIndex: EntityIndex, com: {prototype: T} | ComType): ComPoolIndex { let entity = this._entityToComponents[entityIndex]; if(!entity) return -1; let type = typeof com == 'number' ? com : GetComConstructorType(com); @@ -125,12 +123,11 @@ export class ECSWorld { for(let i=0; i(com: {prototype: T}): T { let component = this.getComponent(0, com); if(!component) { @@ -140,7 +137,7 @@ export class ECSWorld { } public setEntityDirty(entityIndex: EntityIndex): void { - this._fillters.forEach((fillter, key) => { + this._filters.forEach((fillter, key) => { let accept = !this._entitiesToDelete.has(entityIndex) && fillter.isAccept(entityIndex); if(accept != fillter.isContains(entityIndex)) { accept ? fillter.onEntityEnter(entityIndex) : fillter.onEntityLeave(entityIndex); @@ -150,14 +147,14 @@ export class ECSWorld { public getFilter(fillterKey: string): ECSFilter { - if(this._fillters.has(fillterKey)) { - return this._fillters.get(fillterKey); + if(this._filters.has(fillterKey)) { + return this._filters.get(fillterKey); } let [acceptStr, rejectStr] = fillterKey.split("-"); let accept = acceptStr && acceptStr.length > 0 ? acceptStr.split(',').map(Number) : null; let reject = rejectStr && rejectStr.length > 0 ? rejectStr.split(',').map(Number) : null; let fillter = new ECSFilter(this, accept, reject); - this._fillters.set(fillterKey, fillter); + this._filters.set(fillterKey, fillter); // 将当期的entity放入fillter for(let i=1; i { this.removeAllComponents(value); - this._reservedIds.push(value); + this._entityIdxPools.push(value); }); this._entitiesToDelete.clear(); } - } export function GenFillterKey(accepts: ECSComConstructor[], rejects?: ECSComConstructor[]) { diff --git a/assets/Script/ECS/systems/SysAttack.ts b/assets/Script/ECS/systems/SysAttack.ts index bf1ea3e..026df53 100644 --- a/assets/Script/ECS/systems/SysAttack.ts +++ b/assets/Script/ECS/systems/SysAttack.ts @@ -6,7 +6,7 @@ import { ComTransform } from "../components/ComTransform"; import { ECSSystem } from "../lib/ECSSystem"; import { ECSWorld, GenFillterKey } from "../lib/ECSWorld"; -const FILTER_ATTACKABLE = GenFillterKey([ComAttackable]); +const FILTER_ATTACKABLE = GenFillterKey([ComTransform, ComAttackable, ComRoleConfig]); const FILTER_BEATTACKED = GenFillterKey([ComBeAttacked]); export class SysAttack extends ECSSystem { /** 连接 */ diff --git a/assets/Script/ECS/systems/SysCocosView.ts b/assets/Script/ECS/systems/SysCocosView.ts index 0c7ad43..e0d4291 100755 --- a/assets/Script/ECS/systems/SysCocosView.ts +++ b/assets/Script/ECS/systems/SysCocosView.ts @@ -54,8 +54,7 @@ export class SysCocosView extends ECSSystem implements ITouchProcessor { let comRoleConfig = world.getComponent(entity, ComRoleConfig); this._loadView(world, entity, comNodeConfig).then((node: cc.Node) => { - console.log('load view success'); - + console.log('load view success',comNodeConfig.prefabUrl); }); return false; }); @@ -75,6 +74,15 @@ export class SysCocosView extends ECSSystem implements ITouchProcessor { return true; }); + let layer = cc.find('Canvas/Layers/0'); + let count1 = 0, count2 = 0; + for(const node of layer.children) { + node.zIndex = node.y * -1; + if(node.name == 'Biker') count1++; + if(node.name == 'Cyborg') count2++; + } + cc.find('Canvas/a_role_count').getComponent(cc.Label).string = '' + count1; + cc.find('Canvas/b_role_count').getComponent(cc.Label).string = '' + count2; } diff --git a/assets/Script/Main.ts b/assets/Script/Main.ts index 51e72ae..6b8281b 100644 --- a/assets/Script/Main.ts +++ b/assets/Script/Main.ts @@ -16,6 +16,11 @@ export default class Main extends cc.Component { private _touchHandler: ITouchProcessor[] = []; private ecsController = new ECSController(); + + protected onLoad(): void { + cc.dynamicAtlasManager.enabled = true; + } + start () { this.ecsController.world = this._world = new WorldCocosView(); this._world.createEntity(); // 创建0号实体 @@ -30,14 +35,20 @@ export default class Main extends cc.Component { //this.regiestTouchHandler(); + + } onClick1() { - this.ecsController.createRoleEntity("Biker"); + for(let i=0; i<10; i++) { + this.ecsController.createRoleEntity("Biker"); + } } onClick2() { - this.ecsController.createRoleEntity("Cyborg"); + for(let i=0; i<10; i++) { + this.ecsController.createRoleEntity("Cyborg"); + } } onClick3() { diff --git a/assets/resources/Biker/Biker.prefab b/assets/resources/Biker/Biker.prefab index 5a713b2..5d2bb0e 100644 --- a/assets/resources/Biker/Biker.prefab +++ b/assets/resources/Biker/Biker.prefab @@ -488,7 +488,7 @@ "node": { "__id__": 1 }, - "_enabled": true, + "_enabled": false, "_materials": [ { "__uuid__": "a153945d-2511-4c14-be7b-05d242f47d57" diff --git a/assets/resources/Cyborg/Cyborg.prefab b/assets/resources/Cyborg/Cyborg.prefab index a178ec2..16a423f 100644 --- a/assets/resources/Cyborg/Cyborg.prefab +++ b/assets/resources/Cyborg/Cyborg.prefab @@ -488,7 +488,7 @@ "node": { "__id__": 1 }, - "_enabled": true, + "_enabled": false, "_materials": [ { "__uuid__": "a153945d-2511-4c14-be7b-05d242f47d57"