72 lines
2.1 KiB
TypeScript
72 lines
2.1 KiB
TypeScript
import { ecs } from "../../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
|
|
import { HeroModel } from "../../component/hero/HeroModel";
|
|
import { HeroViewComp } from "../../component/hero/HeroView";
|
|
import { Movement } from "../../component/hero/Movement";
|
|
import { Combat } from "../../component/hero/Combat";
|
|
import type { HeroConfig } from "../../config/HeroConfig";
|
|
import { HeroConfigs } from "../../config/HeroConfig";
|
|
import { GameConstants } from "../../common/Constants";
|
|
import { HeroStateMachine } from "../../component/hero/HeroStateMachine";
|
|
import { HeroAnimState } from "../../component/hero/HeroAnimState";
|
|
const HERO_PREFAB_PATH = GameConstants.HERO_PREFAB_PATH;
|
|
|
|
/**
|
|
* 英雄实体
|
|
* 实现功能:
|
|
* 1. 角色数据管理
|
|
* 2. 视图加载与更新
|
|
* 3. 移动控制
|
|
* 4. 战斗行为
|
|
*/
|
|
@ecs.register('Hero')
|
|
export class Hero extends ecs.Entity {
|
|
HeroModel!: HeroModel;
|
|
HeroView!: HeroViewComp;
|
|
Movement!: Movement;
|
|
Combat!: Combat;
|
|
|
|
private _stateMachine = new HeroStateMachine();
|
|
|
|
protected init() {
|
|
// 数据层初始化
|
|
this.addComponents<ecs.Comp>(
|
|
HeroModel,
|
|
HeroViewComp,
|
|
Movement,
|
|
Combat
|
|
);
|
|
}
|
|
|
|
/** 初始化英雄实体 */
|
|
initHero(config: HeroConfig) {
|
|
// 数据层初始化
|
|
this.HeroModel.init(config);
|
|
|
|
// 视图层初始化
|
|
const spinePath = `${GameConstants.SPINE.HERO}/${config.assetPath}`;
|
|
this.HeroView.load(spinePath);
|
|
|
|
// 设置初始动画
|
|
this._stateMachine.changeState(HeroAnimState.IDLE, this.HeroView);
|
|
|
|
// 移动组件初始化
|
|
this.Movement.speed = config.moveSpeed;
|
|
|
|
// 战斗组件初始化
|
|
this.Combat.init(config.baseAttack, config.attackRange);
|
|
}
|
|
|
|
/** 销毁实体 */
|
|
destroyHero() {
|
|
this.remove(HeroViewComp);
|
|
this.remove(Movement);
|
|
this.remove(Combat);
|
|
this.destroy();
|
|
}
|
|
|
|
update() {
|
|
if (this.Movement.speed > 0) {
|
|
this._stateMachine.changeState(HeroAnimState.WALKING, this.HeroView);
|
|
}
|
|
}
|
|
}
|