更新性能分析器及更改部分注释
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
import { EntitySystem, Entity, Matcher, Time } from '@esengine/ecs-framework';
|
||||
import { Health } from '../components';
|
||||
|
||||
/**
|
||||
* 生命值系统
|
||||
* 处理生命值回复、死亡检测等逻辑
|
||||
*/
|
||||
export class HealthSystem extends EntitySystem {
|
||||
|
||||
constructor() {
|
||||
// 处理具有Health组件的实体
|
||||
super(Matcher.empty().all(Health));
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理所有实体
|
||||
*/
|
||||
protected process(entities: Entity[]): void {
|
||||
const deltaTime = Time.deltaTime;
|
||||
|
||||
for (const entity of entities) {
|
||||
this.processEntity(entity, deltaTime);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理单个实体
|
||||
*/
|
||||
private processEntity(entity: Entity, deltaTime: number): void {
|
||||
const health = entity.getComponent(Health);
|
||||
|
||||
if (!health) return;
|
||||
|
||||
// 如果实体已死亡,跳过处理
|
||||
if (health.isDead) return;
|
||||
|
||||
// 处理生命值回复
|
||||
if (health.regenRate > 0) {
|
||||
const regenAmount = health.regenRate * deltaTime;
|
||||
health.heal(regenAmount);
|
||||
}
|
||||
|
||||
// 检查是否需要标记为死亡
|
||||
if (health.currentHealth <= 0 && !health.isDead) {
|
||||
health.isDead = true;
|
||||
this.onEntityDied(entity);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 当实体死亡时调用
|
||||
*/
|
||||
private onEntityDied(entity: Entity): void {
|
||||
console.log(`💀 实体 ${entity.name} 已死亡`);
|
||||
|
||||
// 这里可以添加死亡相关的逻辑
|
||||
// 比如播放死亡动画、掉落物品等
|
||||
}
|
||||
|
||||
/**
|
||||
* 系统初始化时调用
|
||||
*/
|
||||
public initialize(): void {
|
||||
super.initialize();
|
||||
console.log('❤️ 生命值系统已启动');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "074b3e3a-351e-4d95-b502-5a7dab8efc8d",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
import { EntitySystem, Entity, Matcher, Time } from '@esengine/ecs-framework';
|
||||
import { Transform, Velocity } from '../components';
|
||||
|
||||
/**
|
||||
* 移动系统
|
||||
* 处理具有Transform和Velocity组件的实体移动
|
||||
*/
|
||||
export class MovementSystem extends EntitySystem {
|
||||
|
||||
constructor() {
|
||||
// 使用Matcher设置系统处理的组件类型
|
||||
super(Matcher.empty().all(Transform, Velocity));
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理所有实体
|
||||
*/
|
||||
protected process(entities: Entity[]): void {
|
||||
const deltaTime = Time.deltaTime;
|
||||
|
||||
for (const entity of entities) {
|
||||
this.processEntity(entity, deltaTime);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理单个实体
|
||||
*/
|
||||
private processEntity(entity: Entity, deltaTime: number): void {
|
||||
const transform = entity.getComponent(Transform);
|
||||
const velocity = entity.getComponent(Velocity);
|
||||
|
||||
if (!transform || !velocity) return;
|
||||
|
||||
// 应用摩擦力
|
||||
velocity.applyFriction();
|
||||
|
||||
// 根据速度更新位置
|
||||
const deltaX = velocity.velocity.x * deltaTime;
|
||||
const deltaY = velocity.velocity.y * deltaTime;
|
||||
const deltaZ = velocity.velocity.z * deltaTime;
|
||||
|
||||
transform.move(deltaX, deltaY, deltaZ);
|
||||
|
||||
// 简单的边界检查 (假设游戏世界是 -500 到 500)
|
||||
const bounds = 500;
|
||||
if (transform.position.x > bounds) {
|
||||
transform.position.x = bounds;
|
||||
velocity.velocity.x = -Math.abs(velocity.velocity.x) * 0.5; // 反弹并减速
|
||||
} else if (transform.position.x < -bounds) {
|
||||
transform.position.x = -bounds;
|
||||
velocity.velocity.x = Math.abs(velocity.velocity.x) * 0.5;
|
||||
}
|
||||
|
||||
if (transform.position.y > bounds) {
|
||||
transform.position.y = bounds;
|
||||
velocity.velocity.y = -Math.abs(velocity.velocity.y) * 0.5;
|
||||
} else if (transform.position.y < -bounds) {
|
||||
transform.position.y = -bounds;
|
||||
velocity.velocity.y = Math.abs(velocity.velocity.y) * 0.5;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 系统初始化时调用
|
||||
*/
|
||||
public initialize(): void {
|
||||
super.initialize();
|
||||
console.log('🏃 移动系统已启动');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "5e556e6d-ddd5-415c-b074-3cbdb59ed503",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
import { EntitySystem, Entity, Matcher, Time } from '@esengine/ecs-framework';
|
||||
import { Transform, Velocity } from '../components';
|
||||
|
||||
/**
|
||||
* 随机移动系统
|
||||
* 让实体随机改变移动方向
|
||||
*/
|
||||
export class RandomMovementSystem extends EntitySystem {
|
||||
|
||||
/** 每个实体的下次方向改变时间 */
|
||||
private nextDirectionChangeTime: Map<number, number> = new Map();
|
||||
|
||||
constructor() {
|
||||
// 处理具有Transform和Velocity组件的实体
|
||||
super(Matcher.empty().all(Transform, Velocity));
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理所有实体
|
||||
*/
|
||||
protected process(entities: Entity[]): void {
|
||||
const currentTime = Time.totalTime;
|
||||
|
||||
for (const entity of entities) {
|
||||
this.processEntity(entity, currentTime);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理单个实体
|
||||
*/
|
||||
private processEntity(entity: Entity, currentTime: number): void {
|
||||
const velocity = entity.getComponent(Velocity);
|
||||
|
||||
if (!velocity) return;
|
||||
|
||||
// 检查是否需要改变方向
|
||||
const nextChangeTime = this.nextDirectionChangeTime.get(entity.id) || 0;
|
||||
|
||||
if (currentTime >= nextChangeTime) {
|
||||
// 随机生成新的移动方向
|
||||
const angle = Math.random() * Math.PI * 2; // 0-360度
|
||||
const speed = 50 + Math.random() * 100; // 50-150的随机速度
|
||||
|
||||
const newVelocityX = Math.cos(angle) * speed;
|
||||
const newVelocityY = Math.sin(angle) * speed;
|
||||
|
||||
velocity.setVelocity(newVelocityX, newVelocityY);
|
||||
|
||||
// 设置下次改变方向的时间(1-3秒后)
|
||||
const nextInterval = 1 + Math.random() * 2;
|
||||
this.nextDirectionChangeTime.set(entity.id, currentTime + nextInterval);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 当实体被添加到系统时
|
||||
*/
|
||||
protected onAdded(entity: Entity): void {
|
||||
// 为新实体设置初始方向改变时间
|
||||
const initialDelay = Math.random() * 2; // 0-2秒的初始延迟
|
||||
this.nextDirectionChangeTime.set(entity.id, Time.totalTime + initialDelay);
|
||||
}
|
||||
|
||||
/**
|
||||
* 当实体从系统中移除时
|
||||
*/
|
||||
protected onRemoved(entity: Entity): void {
|
||||
// 清理实体的时间记录
|
||||
this.nextDirectionChangeTime.delete(entity.id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 系统初始化时调用
|
||||
*/
|
||||
public initialize(): void {
|
||||
super.initialize();
|
||||
console.log('🎲 随机移动系统已启动');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "ed1688a1-b44f-4588-ae6a-080a6af38a94",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// 导出所有系统
|
||||
export { MovementSystem } from './MovementSystem';
|
||||
export { HealthSystem } from './HealthSystem';
|
||||
export { RandomMovementSystem } from './RandomMovementSystem';
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "586d2e9b-054b-457b-b44c-dafda0a73b6e",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
Reference in New Issue
Block a user