Files
esengine/extensions/cocos/cocos-ecs/assets/scripts/ecs/components/PositionComponent.ts

62 lines
1.7 KiB
TypeScript
Raw Normal View History

2025-06-17 00:32:16 +08:00
import { Component } from '@esengine/ecs-framework';
import { Vec3 } from 'cc';
/**
* -
*
* ECS组件的设计原则
* 1.
* 2.
* 3. 使
*/
export class PositionComponent extends Component {
/** 3D位置坐标 */
public position: Vec3 = new Vec3();
/** 上一帧的位置(用于计算移动距离) */
public lastPosition: Vec3 = new Vec3();
constructor(x: number = 0, y: number = 0, z: number = 0) {
super();
this.position.set(x, y, z);
this.lastPosition.set(x, y, z);
}
/**
*
*/
setPosition(x: number, y: number, z: number = 0) {
this.lastPosition.set(this.position);
this.position.set(x, y, z);
}
/**
*
*/
move(deltaX: number, deltaY: number, deltaZ: number = 0) {
this.lastPosition.set(this.position);
this.position.x += deltaX;
this.position.y += deltaY;
this.position.z += deltaZ;
}
/**
*
*/
distanceTo(other: PositionComponent): number {
return Vec3.distance(this.position, other.position);
}
/**
*
*/
getMovementDistance(): number {
return Vec3.distance(this.position, this.lastPosition);
}
/**
*
*/
isWithinRange(target: PositionComponent, range: number): boolean {
return this.distanceTo(target) <= range;
}
}