使用ecs系统进行重构
This commit is contained in:
50
assets/script/game/heros/HeroComponent.ts
Normal file
50
assets/script/game/heros/HeroComponent.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import { ecs } from "../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
|
||||
|
||||
import { _decorator, Node } from 'cc';
|
||||
const { ccclass } = _decorator;
|
||||
|
||||
// 继承框架的ECComponent
|
||||
|
||||
// 基础属性组件
|
||||
@ecs.register('HeroBase')
|
||||
export class HeroBase extends ecs.Comp {
|
||||
// 定义需要序列化的字段
|
||||
static serializeFields = ['hp', 'attack', 'node'];
|
||||
|
||||
hp: number = 100;
|
||||
attack: number = 10;
|
||||
node: Node = null;
|
||||
|
||||
reset() {
|
||||
this.hp = 100;
|
||||
this.attack = 10;
|
||||
this.node = null;
|
||||
}
|
||||
}
|
||||
|
||||
// 技能组件
|
||||
@ecs.register('HeroSkill')
|
||||
export class HeroSkill extends ecs.Comp {
|
||||
static serializeFields = ['skillId'];
|
||||
|
||||
skillId: string = "";
|
||||
cooldown: number = 0;
|
||||
|
||||
reset() {
|
||||
this.skillId = "";
|
||||
this.cooldown = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// 状态组件
|
||||
@ecs.register('HeroState')
|
||||
export class HeroState extends ecs.Comp {
|
||||
current: 'idle' | 'attack' | 'die' = 'idle';
|
||||
previous: 'idle' | 'attack' | 'die' = 'idle';
|
||||
|
||||
reset() {
|
||||
this.current = 'idle';
|
||||
this.previous = 'idle';
|
||||
}
|
||||
}
|
||||
|
||||
9
assets/script/game/heros/HeroComponent.ts.meta
Normal file
9
assets/script/game/heros/HeroComponent.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "dbeb1c15-5c41-49bd-b633-728335443a38",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
22
assets/script/game/heros/HeroSystem.ts
Normal file
22
assets/script/game/heros/HeroSystem.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
@ecs.registerSystem()
|
||||
export class HeroMoveSystem extends ecs.System {
|
||||
filter(): ecs.IMatcher {
|
||||
return ecs.allOf(HeroModelComp, HeroViewComp, MoveToComp);
|
||||
}
|
||||
|
||||
update(entities: Hero[]) {
|
||||
const deltaTime = oops.timer.delta;
|
||||
entities.forEach(hero => {
|
||||
// 每个英雄独立计算移动
|
||||
const move = hero.get(MoveToComp);
|
||||
const view = hero.get(HeroViewComp);
|
||||
|
||||
// 计算移动向量(单个英雄逻辑)
|
||||
const dir = move.target.subtract(view.node.position).normalize();
|
||||
const speed = view.speed * deltaTime;
|
||||
|
||||
// 更新位置(独立操作)
|
||||
view.node.position = view.node.position.add(dir.multiplyScalar(speed));
|
||||
});
|
||||
}
|
||||
}
|
||||
28
assets/script/game/heros/HeroTypeSystem.ts
Normal file
28
assets/script/game/heros/HeroTypeSystem.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
@ecs.registerSystem()
|
||||
export class HeroTypeSystem extends ecs.System {
|
||||
filter() {
|
||||
return ecs.allOf(HeroTypeComp);
|
||||
}
|
||||
|
||||
update(entities: Hero[]) {
|
||||
entities.forEach(hero => {
|
||||
const type = hero.get(HeroTypeComp).type;
|
||||
switch(type) {
|
||||
case HeroType.MELEE:
|
||||
this.processMelee(hero);
|
||||
break;
|
||||
case HeroType.RANGED:
|
||||
this.processRanged(hero);
|
||||
break;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private processMelee(hero: Hero) {
|
||||
// 近战英雄特有逻辑
|
||||
}
|
||||
|
||||
private processRanged(hero: Hero) {
|
||||
// 远程英雄特有逻辑
|
||||
}
|
||||
}
|
||||
0
assets/script/game/heros/Heros.ts
Normal file
0
assets/script/game/heros/Heros.ts
Normal file
9
assets/script/game/heros/Heros.ts.meta
Normal file
9
assets/script/game/heros/Heros.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "3d62e7a3-a90b-47f3-bf88-85745af0b8b4",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
Reference in New Issue
Block a user