去掉碰撞系统
This commit is contained in:
17
assets/script/game/common/ecs/position/BattleMoveComp.ts
Normal file
17
assets/script/game/common/ecs/position/BattleMoveComp.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { ecs } from "../../../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
|
||||
@ecs.register('BattleMove')
|
||||
|
||||
export class BattleMoveComp extends ecs.Comp {
|
||||
/** 移动方向:1向右,-1向左 */
|
||||
direction: number = 1;
|
||||
/** 目标x坐标 */
|
||||
targetX: number = 0;
|
||||
/** 是否处于移动状态 */
|
||||
moving: boolean = true;
|
||||
|
||||
reset() {
|
||||
this.direction = 1;
|
||||
this.targetX = 0;
|
||||
this.moving = true;
|
||||
}
|
||||
}
|
||||
51
assets/script/game/common/ecs/position/BattleMoveSystem.ts
Normal file
51
assets/script/game/common/ecs/position/BattleMoveSystem.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { HeroViewComp } from "../../../hero/HeroViewComp";
|
||||
import { BattleMoveComp } from "./BattleMoveComp";
|
||||
import { ecs } from "../../../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
|
||||
|
||||
@ecs.register('BattleMoveSystem')
|
||||
export class BattleMoveSystem extends ecs.ComblockSystem implements ecs.ISystemUpdate {
|
||||
filter(): ecs.IMatcher {
|
||||
return ecs.allOf(BattleMoveComp, HeroViewComp);
|
||||
}
|
||||
|
||||
update(e: ecs.Entity) {
|
||||
const move = e.get(BattleMoveComp);
|
||||
const view = e.get(HeroViewComp);
|
||||
|
||||
if (!move.moving) return;
|
||||
|
||||
// 检测攻击范围内是否有敌人
|
||||
const hasEnemy = this.checkEnemiesInRange(e, view.dis);
|
||||
|
||||
if (!hasEnemy) {
|
||||
// 计算移动量
|
||||
const delta = view.speed * this.dt * move.direction;
|
||||
const newX = view.node.position.x + delta;
|
||||
|
||||
// 限制移动范围
|
||||
if (this.validatePosition(newX, move)) {
|
||||
view.node.setPosition(newX, view.node.position.y, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** 验证目标位置有效性 */
|
||||
private validatePosition(newX: number, move: BattleMoveComp): boolean {
|
||||
// 我方不能超过右边界,敌方不能超过左边界
|
||||
return move.direction === 1 ?
|
||||
newX <= move.targetX :
|
||||
newX >= move.targetX;
|
||||
}
|
||||
|
||||
/** 检测攻击范围内敌人 */
|
||||
private checkEnemiesInRange(entity: ecs.Entity, range: number): boolean {
|
||||
const currentPos = entity.get(HeroViewComp).node.position;
|
||||
const team = entity.get(HeroViewComp).fac;
|
||||
|
||||
return ecs.query(ecs.allOf(HeroViewComp)).some(e => {
|
||||
const view = e.get(HeroViewComp);
|
||||
return view.fac !== team &&
|
||||
Math.abs(currentPos.x - view.node.position.x) <= range;
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user