feat: 修复了找不到comTrans的问题

This commit is contained in:
honmono
2022-05-15 11:30:36 +08:00
parent 8a2274fb5b
commit fdb002d00a
9 changed files with 252 additions and 37 deletions

View File

@@ -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);

View File

@@ -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;

View File

@@ -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<any>[] = [];
private _fillters = new Map<string, ECSFilter>();
private _entitiesToDelete: Set<EntityIndex> = new Set();
export class ECSWorld {
private _systems: ECSSystem[] = []; // world内所有的system
private _entityIdxPools: number[] = []; // 回收entity
private _entityToComponents: number[][] = []; // entity component 二维表
private _entitiesToDelete: Set<EntityIndex> = new Set(); // 统一删除entity
private _componentPools: ECSComponentPool<any>[] = []; // component pools
private _filters = new Map<string, ECSFilter>(); // filter
/** 获取ComponentPool */
public getComponentPool<T>(typeOrFunc: ComType | {prototype: T}): ECSComponentPool<T> {
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<T>(entityIndex: EntityIndex, com: {prototype: T} | ComType) {
public getComponentPoolIdx<T>(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<entity.length; i++) {
if(entity[i] == -1) continue;
this.getComponentPool(i).free(entity[i]);
entity[i] = -1;
}
entity.fill(-1);
dirty && this.setEntityDirty(entityIndex);
}
public getSingletonComponent<T>(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._entityToComponents.length; i++) {
if(fillter.isAccept(i)) {
@@ -179,11 +176,10 @@ export class ECSWorld {
private _realRemoveEntity() {
this._entitiesToDelete.forEach((value) => {
this.removeAllComponents(value);
this._reservedIds.push(value);
this._entityIdxPools.push(value);
});
this._entitiesToDelete.clear();
}
}
export function GenFillterKey(accepts: ECSComConstructor[], rejects?: ECSComConstructor[]) {

View File

@@ -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 {
/** 连接 */

View File

@@ -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;
}

View File

@@ -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() {