使用ecs系统进行重构
This commit is contained in:
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));
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user