rts-demo
This commit is contained in:
236
extensions/cocos/cocos-ecs/assets/scripts/RTSDemo.ts
Normal file
236
extensions/cocos/cocos-ecs/assets/scripts/RTSDemo.ts
Normal file
@@ -0,0 +1,236 @@
|
||||
import { _decorator, Component, Node, Vec3, instantiate, Prefab, Camera } from 'cc';
|
||||
import { UnitController } from './components/UnitController';
|
||||
import { RTSCameraController } from './controllers/RTSCameraController';
|
||||
import { UIController } from './controllers/UIController';
|
||||
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
/**
|
||||
* RTS演示项目主控制器
|
||||
* 展示行为树在3D RTS游戏中的应用
|
||||
*/
|
||||
@ccclass('RTSDemo')
|
||||
export class RTSDemo extends Component {
|
||||
|
||||
@property(Prefab)
|
||||
unitPrefab: Prefab = null!;
|
||||
|
||||
@property(Prefab)
|
||||
buildingPrefab: Prefab = null!;
|
||||
|
||||
@property(Prefab)
|
||||
resourcePrefab: Prefab = null!;
|
||||
|
||||
@property(Node)
|
||||
gameWorld: Node = null!;
|
||||
|
||||
@property(Camera)
|
||||
mainCamera: Camera = null!;
|
||||
|
||||
@property(Node)
|
||||
uiRoot: Node = null!;
|
||||
|
||||
private cameraController: RTSCameraController = null!;
|
||||
private uiController: UIController = null!;
|
||||
|
||||
// 游戏状态
|
||||
private units: Node[] = [];
|
||||
private buildings: Node[] = [];
|
||||
private resources: Node[] = [];
|
||||
private selectedUnits: Node[] = [];
|
||||
|
||||
onLoad() {
|
||||
console.log('RTS Demo 初始化开始...');
|
||||
this.initializeControllers();
|
||||
this.setupScene();
|
||||
console.log('RTS Demo 初始化完成!');
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化控制器
|
||||
*/
|
||||
private initializeControllers() {
|
||||
// 相机控制器
|
||||
this.cameraController = this.mainCamera.getComponent(RTSCameraController) ||
|
||||
this.mainCamera.addComponent(RTSCameraController);
|
||||
|
||||
// UI控制器
|
||||
this.uiController = this.uiRoot.getComponent(UIController) ||
|
||||
this.uiRoot.addComponent(UIController);
|
||||
|
||||
// 设置UI回调
|
||||
this.uiController.onUnitSelected = this.onUnitSelected.bind(this);
|
||||
this.uiController.onCommandIssued = this.onCommandIssued.bind(this);
|
||||
|
||||
console.log('控制器初始化完成');
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置场景
|
||||
*/
|
||||
private setupScene() {
|
||||
this.createUnits();
|
||||
this.createBuildings();
|
||||
this.createResources();
|
||||
|
||||
// 设置初始相机位置
|
||||
this.mainCamera.node.setPosition(0, 20, 15);
|
||||
this.mainCamera.node.lookAt(Vec3.ZERO);
|
||||
|
||||
console.log('场景设置完成');
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建单位
|
||||
*/
|
||||
private createUnits() {
|
||||
const unitTypes = [
|
||||
{ name: 'Worker', behaviorTree: 'worker-ai', color: 'blue' },
|
||||
{ name: 'Soldier', behaviorTree: 'soldier-ai', color: 'red' },
|
||||
{ name: 'Scout', behaviorTree: 'scout-ai', color: 'green' }
|
||||
];
|
||||
|
||||
unitTypes.forEach((type, typeIndex) => {
|
||||
for (let i = 0; i < 3; i++) {
|
||||
const unit = instantiate(this.unitPrefab);
|
||||
unit.name = `${type.name}_${i + 1}`;
|
||||
|
||||
// 设置位置
|
||||
const angle = (i / 3) * Math.PI * 2;
|
||||
const radius = 3 + typeIndex * 2;
|
||||
const x = Math.cos(angle) * radius;
|
||||
const z = Math.sin(angle) * radius;
|
||||
unit.setPosition(x, 0, z);
|
||||
|
||||
// 添加到场景
|
||||
this.gameWorld.addChild(unit);
|
||||
this.units.push(unit);
|
||||
|
||||
// 配置单位组件
|
||||
const unitController = unit.getComponent(UnitController) || unit.addComponent(UnitController);
|
||||
unitController.setup({
|
||||
unitType: type.name.toLowerCase(),
|
||||
behaviorTreeName: type.behaviorTree,
|
||||
maxHealth: 100,
|
||||
moveSpeed: 3,
|
||||
attackRange: 2,
|
||||
attackDamage: 25,
|
||||
color: type.color
|
||||
});
|
||||
|
||||
console.log(`创建单位: ${unit.name} at (${x.toFixed(1)}, 0, ${z.toFixed(1)})`);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建建筑
|
||||
*/
|
||||
private createBuildings() {
|
||||
const buildingPositions = [
|
||||
{ pos: new Vec3(-10, 0, -10), name: 'MainBase' },
|
||||
{ pos: new Vec3(10, 0, 10), name: 'Barracks' },
|
||||
{ pos: new Vec3(-8, 0, 8), name: 'ResourceCenter' }
|
||||
];
|
||||
|
||||
buildingPositions.forEach((building, index) => {
|
||||
const buildingNode = instantiate(this.buildingPrefab);
|
||||
buildingNode.name = building.name;
|
||||
buildingNode.setPosition(building.pos);
|
||||
|
||||
this.gameWorld.addChild(buildingNode);
|
||||
this.buildings.push(buildingNode);
|
||||
|
||||
console.log(`创建建筑: ${building.name} at ${building.pos}`);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建资源
|
||||
*/
|
||||
private createResources() {
|
||||
const resourcePositions = [
|
||||
new Vec3(5, 0, -5),
|
||||
new Vec3(-5, 0, 5),
|
||||
new Vec3(8, 0, -8),
|
||||
new Vec3(-8, 0, -5),
|
||||
new Vec3(6, 0, 6)
|
||||
];
|
||||
|
||||
resourcePositions.forEach((pos, index) => {
|
||||
const resource = instantiate(this.resourcePrefab);
|
||||
resource.name = `Resource_${index + 1}`;
|
||||
resource.setPosition(pos);
|
||||
|
||||
this.gameWorld.addChild(resource);
|
||||
this.resources.push(resource);
|
||||
|
||||
console.log(`创建资源: ${resource.name} at ${pos}`);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 单位选择回调
|
||||
*/
|
||||
private onUnitSelected(units: Node[]) {
|
||||
// 取消之前的选择
|
||||
this.selectedUnits.forEach(unit => {
|
||||
const unitController = unit.getComponent(UnitController);
|
||||
if (unitController) {
|
||||
unitController.setSelected(false);
|
||||
}
|
||||
});
|
||||
|
||||
// 设置新选择
|
||||
this.selectedUnits = units;
|
||||
this.selectedUnits.forEach(unit => {
|
||||
const unitController = unit.getComponent(UnitController);
|
||||
if (unitController) {
|
||||
unitController.setSelected(true);
|
||||
}
|
||||
});
|
||||
|
||||
console.log(`选择了 ${units.length} 个单位`);
|
||||
this.uiController.setSelectedUnitsCount(units.length);
|
||||
}
|
||||
|
||||
/**
|
||||
* 命令发布回调
|
||||
*/
|
||||
private onCommandIssued(command: string, target?: Vec3 | Node) {
|
||||
if (this.selectedUnits.length === 0) {
|
||||
console.log('没有选择单位');
|
||||
return;
|
||||
}
|
||||
|
||||
this.selectedUnits.forEach(unit => {
|
||||
const unitController = unit.getComponent(UnitController);
|
||||
if (unitController) {
|
||||
unitController.issueCommand(command, target);
|
||||
}
|
||||
});
|
||||
|
||||
console.log(`发布命令: ${command} 给 ${this.selectedUnits.length} 个单位`);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有单位
|
||||
*/
|
||||
getAllUnits(): Node[] {
|
||||
return [...this.units];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有建筑
|
||||
*/
|
||||
getAllBuildings(): Node[] {
|
||||
return [...this.buildings];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有资源
|
||||
*/
|
||||
getAllResources(): Node[] {
|
||||
return [...this.resources];
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "0f20d48a-7b30-4081-a9de-709432b6737b",
|
||||
"uuid": "c3386f4a-9bef-416f-b770-fcec91cedbc4",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
@@ -2,7 +2,7 @@
|
||||
"ver": "1.2.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "3d8cbc91-5bc5-4d17-b53a-01fda26e4660",
|
||||
"uuid": "d946c8cb-cba5-46eb-8949-5a327bdd4367",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
@@ -0,0 +1,159 @@
|
||||
import { Node, resources, JsonAsset } from 'cc';
|
||||
import { BehaviorTree, BehaviorTreeBuilder, Blackboard, TaskStatus, BehaviorTreeJSONConfig } from '@esengine/ai';
|
||||
import { ECSComponent } from './UnitComponent';
|
||||
|
||||
/**
|
||||
* 行为树组件 - ECS组件,管理单个实体的行为树
|
||||
*/
|
||||
export class BehaviorTreeComponent extends ECSComponent {
|
||||
public behaviorTreeFile: string = '';
|
||||
public cocosNode: Node | null = null;
|
||||
|
||||
private behaviorTree: BehaviorTree<any> | null = null;
|
||||
private blackboard: Blackboard | null = null;
|
||||
private context: any = null;
|
||||
private isLoaded: boolean = false;
|
||||
private isRunning: boolean = false;
|
||||
private lastTickTime: number = 0;
|
||||
private tickInterval: number = 0.1; // 行为树更新间隔(秒)
|
||||
|
||||
/**
|
||||
* 初始化行为树
|
||||
*/
|
||||
async initialize() {
|
||||
if (!this.behaviorTreeFile) {
|
||||
console.error('行为树文件路径未设置');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await this.loadBehaviorTree();
|
||||
this.setupBlackboard();
|
||||
this.isLoaded = true;
|
||||
this.isRunning = true;
|
||||
console.log(`行为树组件初始化成功: ${this.behaviorTreeFile}`);
|
||||
} catch (error) {
|
||||
console.error(`行为树组件初始化失败: ${this.behaviorTreeFile}`, error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载行为树文件
|
||||
*/
|
||||
private async loadBehaviorTree(): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
// 移除.btree扩展名,使用.bt.json
|
||||
const jsonPath = this.behaviorTreeFile.replace('.btree', '.bt.json');
|
||||
|
||||
resources.load(jsonPath, JsonAsset, (err, asset) => {
|
||||
if (err) {
|
||||
console.error(`加载行为树文件失败: ${jsonPath}`, err);
|
||||
reject(err);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const treeData = asset.json as BehaviorTreeJSONConfig;
|
||||
this.buildBehaviorTree(treeData);
|
||||
resolve();
|
||||
} catch (buildError) {
|
||||
console.error(`构建行为树失败: ${jsonPath}`, buildError);
|
||||
reject(buildError);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建行为树
|
||||
*/
|
||||
private buildBehaviorTree(treeData: BehaviorTreeJSONConfig) {
|
||||
// 创建基础执行上下文
|
||||
const baseContext = {
|
||||
cocosNode: this.cocosNode,
|
||||
unitComponent: this
|
||||
};
|
||||
|
||||
// 使用@esengine/ai的BehaviorTreeBuilder构建行为树
|
||||
// 这会自动创建黑板并设置所有配置
|
||||
const result = BehaviorTreeBuilder.fromBehaviorTreeConfig(treeData, baseContext);
|
||||
this.behaviorTree = result.tree;
|
||||
this.blackboard = result.blackboard;
|
||||
this.context = result.context;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置黑板
|
||||
*/
|
||||
private setupBlackboard() {
|
||||
if (!this.blackboard || !this.cocosNode) return;
|
||||
|
||||
// 设置基础信息
|
||||
this.blackboard.setValue('entityName', this.cocosNode.name);
|
||||
this.blackboard.setValue('currentTime', Date.now() / 1000);
|
||||
this.blackboard.setValue('deltaTime', 0.016);
|
||||
this.blackboard.setValue('worldPosition', this.cocosNode.worldPosition);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新行为树
|
||||
*/
|
||||
update(deltaTime: number) {
|
||||
if (!this.isLoaded || !this.isRunning || !this.behaviorTree || !this.context) return;
|
||||
|
||||
// 控制更新频率
|
||||
this.lastTickTime += deltaTime;
|
||||
if (this.lastTickTime < this.tickInterval) return;
|
||||
|
||||
this.lastTickTime = 0;
|
||||
|
||||
// 更新黑板中的时间信息
|
||||
if (this.blackboard) {
|
||||
this.blackboard.setValue('deltaTime', deltaTime);
|
||||
this.blackboard.setValue('currentTime', Date.now() / 1000);
|
||||
if (this.cocosNode) {
|
||||
this.blackboard.setValue('worldPosition', this.cocosNode.worldPosition);
|
||||
}
|
||||
}
|
||||
|
||||
// 执行行为树
|
||||
try {
|
||||
this.behaviorTree.tick();
|
||||
} catch (error) {
|
||||
console.error(`行为树执行错误:`, error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取黑板
|
||||
*/
|
||||
getBlackboard(): Blackboard | null {
|
||||
return this.blackboard;
|
||||
}
|
||||
|
||||
/**
|
||||
* 暂停行为树
|
||||
*/
|
||||
pause() {
|
||||
this.isRunning = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 恢复行为树
|
||||
*/
|
||||
resume() {
|
||||
if (this.isLoaded) {
|
||||
this.isRunning = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 停止行为树
|
||||
*/
|
||||
stop() {
|
||||
this.isRunning = false;
|
||||
if (this.behaviorTree) {
|
||||
this.behaviorTree.reset();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "0c14be41-df1d-4bdc-88fd-b2e504ecdee6",
|
||||
"uuid": "8efd182b-9891-4903-bef2-eb07b5184263",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
@@ -0,0 +1,258 @@
|
||||
import { _decorator, Component, resources, JsonAsset } from 'cc';
|
||||
import { BehaviorTree, BehaviorTreeBuilder, Blackboard, BehaviorTreeJSONConfig } from '@esengine/ai';
|
||||
import { UnitController } from './UnitController';
|
||||
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
/**
|
||||
* 执行上下文接口
|
||||
*/
|
||||
interface GameExecutionContext {
|
||||
blackboard?: Blackboard;
|
||||
unitController: UnitController;
|
||||
gameObject: any;
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
/**
|
||||
* 行为树管理器 - 使用@esengine/ai包管理行为树
|
||||
*/
|
||||
@ccclass('BehaviorTreeManager')
|
||||
export class BehaviorTreeManager extends Component {
|
||||
|
||||
@property
|
||||
debugMode: boolean = true;
|
||||
|
||||
@property
|
||||
tickInterval: number = 0.1; // 行为树更新间隔(秒)
|
||||
|
||||
private behaviorTree: BehaviorTree<GameExecutionContext> | null = null;
|
||||
private blackboard: Blackboard | null = null;
|
||||
private context: GameExecutionContext | null = null;
|
||||
private isLoaded: boolean = false;
|
||||
private isRunning: boolean = false;
|
||||
private lastTickTime: number = 0;
|
||||
private unitController: UnitController | null = null;
|
||||
private currentBehaviorTreeName: string = '';
|
||||
|
||||
/**
|
||||
* 初始化行为树
|
||||
*/
|
||||
async initializeBehaviorTree(behaviorTreeName: string, unitController: UnitController) {
|
||||
this.currentBehaviorTreeName = behaviorTreeName;
|
||||
this.unitController = unitController;
|
||||
|
||||
try {
|
||||
await this.loadBehaviorTree(behaviorTreeName);
|
||||
this.setupBlackboard();
|
||||
this.isLoaded = true;
|
||||
this.isRunning = true;
|
||||
console.log(`行为树初始化成功: ${behaviorTreeName}`);
|
||||
} catch (error) {
|
||||
console.error(`行为树初始化失败: ${behaviorTreeName}`, error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载行为树文件
|
||||
*/
|
||||
private async loadBehaviorTree(behaviorTreeName: string): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const jsonPath = `${behaviorTreeName}.bt`;
|
||||
|
||||
resources.load(jsonPath, JsonAsset, (err, asset) => {
|
||||
if (err) {
|
||||
console.error(`加载行为树文件失败: ${jsonPath}`, err);
|
||||
reject(err);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const behaviorTreeData = asset.json as BehaviorTreeJSONConfig;
|
||||
|
||||
// 创建执行上下文
|
||||
this.blackboard = new Blackboard();
|
||||
this.context = {
|
||||
blackboard: this.blackboard,
|
||||
unitController: this.unitController!,
|
||||
gameObject: this.node
|
||||
};
|
||||
|
||||
// 从JSON数据创建行为树
|
||||
const buildResult = BehaviorTreeBuilder.fromBehaviorTreeConfig(behaviorTreeData, this.context);
|
||||
this.behaviorTree = buildResult.tree as BehaviorTree<GameExecutionContext>;
|
||||
this.blackboard = buildResult.blackboard;
|
||||
|
||||
resolve();
|
||||
} catch (parseError) {
|
||||
console.error(`创建行为树失败: ${jsonPath}`, parseError);
|
||||
reject(parseError);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置黑板基础信息
|
||||
*/
|
||||
private setupBlackboard() {
|
||||
if (!this.unitController || !this.blackboard) return;
|
||||
|
||||
// 设置单位基础信息
|
||||
this.blackboard.setValue('entityName', this.node.name);
|
||||
this.blackboard.setValue('unitType', this.unitController.unitType);
|
||||
this.blackboard.setValue('maxHealth', this.unitController.maxHealth);
|
||||
this.blackboard.setValue('currentHealth', this.unitController.currentHealth);
|
||||
this.blackboard.setValue('moveSpeed', this.unitController.moveSpeed);
|
||||
this.blackboard.setValue('attackRange', this.unitController.attackRange);
|
||||
this.blackboard.setValue('attackDamage', this.unitController.attackDamage);
|
||||
this.blackboard.setValue('attackCooldown', this.unitController.attackCooldown);
|
||||
|
||||
// 设置时间信息
|
||||
this.blackboard.setValue('currentTime', Date.now() / 1000);
|
||||
this.blackboard.setValue('deltaTime', 0.016);
|
||||
this.blackboard.setValue('worldPosition', this.node.worldPosition);
|
||||
|
||||
// 设置初始状态
|
||||
this.blackboard.setValue('currentCommand', 'idle');
|
||||
this.blackboard.setValue('hasTarget', false);
|
||||
this.blackboard.setValue('isSelected', false);
|
||||
|
||||
// 设置单位控制器引用,供行为树节点使用
|
||||
this.blackboard.setValue('unitController', this.unitController);
|
||||
this.blackboard.setValue('gameObject', this.node);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新黑板值
|
||||
*/
|
||||
updateBlackboardValue(key: string, value: any) {
|
||||
if (this.blackboard) {
|
||||
this.blackboard.setValue(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取黑板值
|
||||
*/
|
||||
getBlackboardValue(key: string): any {
|
||||
return this.blackboard?.getValue(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取黑板
|
||||
*/
|
||||
getBlackboard(): Blackboard | null {
|
||||
return this.blackboard;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取行为树
|
||||
*/
|
||||
getBehaviorTree(): BehaviorTree<GameExecutionContext> | null {
|
||||
return this.behaviorTree;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新行为树
|
||||
*/
|
||||
update(deltaTime: number) {
|
||||
if (!this.isLoaded || !this.isRunning || !this.behaviorTree || !this.blackboard) return;
|
||||
|
||||
// 控制更新频率
|
||||
this.lastTickTime += deltaTime;
|
||||
if (this.lastTickTime < this.tickInterval) return;
|
||||
|
||||
this.lastTickTime = 0;
|
||||
|
||||
// 更新黑板中的时间信息
|
||||
this.blackboard.setValue('deltaTime', deltaTime);
|
||||
this.blackboard.setValue('currentTime', Date.now() / 1000);
|
||||
this.blackboard.setValue('worldPosition', this.node.worldPosition);
|
||||
|
||||
// 更新单位状态信息
|
||||
if (this.unitController) {
|
||||
this.blackboard.setValue('currentHealth', this.unitController.currentHealth);
|
||||
this.blackboard.setValue('healthPercentage', this.unitController.currentHealth / this.unitController.maxHealth);
|
||||
this.blackboard.setValue('isLowHealth', this.unitController.currentHealth < this.unitController.maxHealth * 0.3);
|
||||
this.blackboard.setValue('currentCommand', this.unitController.currentCommand);
|
||||
this.blackboard.setValue('isSelected', this.unitController.isSelected);
|
||||
this.blackboard.setValue('targetPosition', this.unitController.targetPosition);
|
||||
this.blackboard.setValue('targetNode', this.unitController.targetNode);
|
||||
|
||||
// 更新距离信息
|
||||
if (this.unitController.targetPosition) {
|
||||
const distance = this.node.worldPosition.subtract(this.unitController.targetPosition).length();
|
||||
this.blackboard.setValue('distanceToTarget', distance);
|
||||
this.blackboard.setValue('isInAttackRange', distance <= this.unitController.attackRange);
|
||||
this.blackboard.setValue('isCloseToTarget', distance <= 1.0);
|
||||
}
|
||||
|
||||
// 更新状态标志
|
||||
this.blackboard.setValue('isIdle', this.unitController.currentCommand === 'idle');
|
||||
this.blackboard.setValue('isMoving', this.unitController.currentCommand === 'move');
|
||||
this.blackboard.setValue('isAttacking', this.unitController.currentCommand === 'attack');
|
||||
this.blackboard.setValue('isGathering', this.unitController.currentCommand === 'gather');
|
||||
this.blackboard.setValue('isPatrolling', this.unitController.currentCommand === 'patrol');
|
||||
}
|
||||
|
||||
// 执行行为树
|
||||
try {
|
||||
this.behaviorTree.tick(deltaTime);
|
||||
if (this.debugMode && Math.random() < 0.01) { // 1%的概率打印调试信息
|
||||
console.log(`行为树执行完成, 单位: ${this.node.name}`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`行为树执行错误:`, error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 暂停行为树
|
||||
*/
|
||||
pause() {
|
||||
this.isRunning = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 恢复行为树
|
||||
*/
|
||||
resume() {
|
||||
if (this.isLoaded) {
|
||||
this.isRunning = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 停止行为树
|
||||
*/
|
||||
stop() {
|
||||
this.isRunning = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 重新加载行为树
|
||||
*/
|
||||
async reloadBehaviorTree() {
|
||||
if (this.currentBehaviorTreeName && this.unitController) {
|
||||
this.stop();
|
||||
await this.initializeBehaviorTree(this.currentBehaviorTreeName, this.unitController);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 重置行为树
|
||||
*/
|
||||
reset() {
|
||||
if (this.behaviorTree) {
|
||||
this.behaviorTree.reset();
|
||||
}
|
||||
}
|
||||
|
||||
onDestroy() {
|
||||
this.stop();
|
||||
this.behaviorTree = null;
|
||||
this.blackboard = null;
|
||||
this.context = null;
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "3c3bf485-e1ab-44a4-8758-6594edc4c575",
|
||||
"uuid": "891c88fc-282d-4791-a961-8d85244bfee7",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
@@ -0,0 +1,345 @@
|
||||
import { _decorator, Component, Node, Vec3, Material, MeshRenderer, Color, tween } from 'cc';
|
||||
import { BehaviorTreeComponent } from './BehaviorTreeComponent';
|
||||
|
||||
// 简化的ECS组件基类
|
||||
export class ECSComponent {
|
||||
public entity: any = null;
|
||||
}
|
||||
|
||||
// 简化的Entity类
|
||||
export class Entity {
|
||||
public name: string = '';
|
||||
private components: Map<any, any> = new Map();
|
||||
|
||||
constructor(name: string) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
addComponent(component: any) {
|
||||
this.components.set(component.constructor, component);
|
||||
component.entity = this;
|
||||
}
|
||||
|
||||
getComponent<T>(componentClass: any): T | null {
|
||||
return this.components.get(componentClass) || null;
|
||||
}
|
||||
|
||||
hasComponent(componentClass: any): boolean {
|
||||
return this.components.has(componentClass);
|
||||
}
|
||||
}
|
||||
|
||||
// 简化的Core类
|
||||
export class Core {
|
||||
static entityManager = {
|
||||
entities: [] as Entity[],
|
||||
createEntity: (name: string) => {
|
||||
const entity = new Entity(name);
|
||||
Core.entityManager.entities.push(entity);
|
||||
return entity;
|
||||
},
|
||||
destroyEntity: (entity: Entity) => {
|
||||
const index = Core.entityManager.entities.indexOf(entity);
|
||||
if (index !== -1) {
|
||||
Core.entityManager.entities.splice(index, 1);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
static create(config?: any) {
|
||||
console.log('ECS Core initialized with config:', config);
|
||||
}
|
||||
|
||||
static update(deltaTime: number) {
|
||||
// 简化的更新逻辑
|
||||
}
|
||||
}
|
||||
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
/**
|
||||
* 单位配置接口
|
||||
*/
|
||||
export interface UnitConfig {
|
||||
unitType: string;
|
||||
behaviorTreeFile: string;
|
||||
maxHealth: number;
|
||||
moveSpeed: number;
|
||||
attackRange: number;
|
||||
attackDamage: number;
|
||||
color: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* 单位状态组件
|
||||
*/
|
||||
export class UnitStateComponent extends ECSComponent {
|
||||
public unitType: string = '';
|
||||
public maxHealth: number = 100;
|
||||
public currentHealth: number = 100;
|
||||
public moveSpeed: number = 3;
|
||||
public attackRange: number = 2;
|
||||
public attackDamage: number = 25;
|
||||
public isSelected: boolean = false;
|
||||
public currentCommand: string = 'idle';
|
||||
public targetPosition: Vec3 = Vec3.ZERO.clone();
|
||||
public targetNode: Node | null = null;
|
||||
public lastAttackTime: number = 0;
|
||||
public attackCooldown: number = 1.5;
|
||||
public color: string = 'white';
|
||||
}
|
||||
|
||||
/**
|
||||
* 单位组件 - Cocos Creator组件,管理单位的可视化和ECS实体
|
||||
*/
|
||||
@ccclass('UnitComponent')
|
||||
export class UnitComponent extends Component {
|
||||
|
||||
@property
|
||||
showDebugInfo: boolean = true;
|
||||
|
||||
private entity: Entity | null = null;
|
||||
private unitState: UnitStateComponent | null = null;
|
||||
private behaviorTreeComponent: BehaviorTreeComponent | null = null;
|
||||
private meshRenderer: MeshRenderer | null = null;
|
||||
private originalMaterial: Material | null = null;
|
||||
private selectionMaterial: Material | null = null;
|
||||
|
||||
onLoad() {
|
||||
this.meshRenderer = this.getComponent(MeshRenderer);
|
||||
if (this.meshRenderer && this.meshRenderer.material) {
|
||||
this.originalMaterial = this.meshRenderer.material;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置单位配置
|
||||
*/
|
||||
setup(config: UnitConfig) {
|
||||
// 创建ECS实体
|
||||
this.entity = Core.entityManager.createEntity(`Unit_${this.node.name}`);
|
||||
|
||||
// 添加单位状态组件
|
||||
this.unitState = new UnitStateComponent();
|
||||
this.unitState.unitType = config.unitType;
|
||||
this.unitState.maxHealth = config.maxHealth;
|
||||
this.unitState.currentHealth = config.maxHealth;
|
||||
this.unitState.moveSpeed = config.moveSpeed;
|
||||
this.unitState.attackRange = config.attackRange;
|
||||
this.unitState.attackDamage = config.attackDamage;
|
||||
this.unitState.color = config.color;
|
||||
this.entity.addComponent(this.unitState);
|
||||
|
||||
// 添加行为树组件
|
||||
this.behaviorTreeComponent = new BehaviorTreeComponent();
|
||||
this.behaviorTreeComponent.behaviorTreeFile = config.behaviorTreeFile;
|
||||
this.behaviorTreeComponent.cocosNode = this.node;
|
||||
this.entity.addComponent(this.behaviorTreeComponent);
|
||||
|
||||
// 设置材质颜色
|
||||
this.setUnitColor(config.color);
|
||||
|
||||
console.log(`单位 ${this.node.name} 设置完成 - 类型: ${config.unitType}, 行为树: ${config.behaviorTreeFile}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置单位颜色
|
||||
*/
|
||||
private setUnitColor(colorName: string) {
|
||||
if (!this.meshRenderer || !this.meshRenderer.material) return;
|
||||
|
||||
const colorMap: { [key: string]: Color } = {
|
||||
'red': Color.RED,
|
||||
'green': Color.GREEN,
|
||||
'blue': Color.BLUE,
|
||||
'yellow': Color.YELLOW,
|
||||
'white': Color.WHITE,
|
||||
'cyan': Color.CYAN,
|
||||
'magenta': Color.MAGENTA
|
||||
};
|
||||
|
||||
const color = colorMap[colorName] || Color.WHITE;
|
||||
this.meshRenderer.material.setProperty('mainColor', color);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置选择状态
|
||||
*/
|
||||
setSelected(selected: boolean) {
|
||||
if (!this.unitState) return;
|
||||
|
||||
this.unitState.isSelected = selected;
|
||||
|
||||
// 视觉效果
|
||||
if (selected) {
|
||||
this.showSelectionEffect();
|
||||
} else {
|
||||
this.hideSelectionEffect();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 显示选择效果
|
||||
*/
|
||||
private showSelectionEffect() {
|
||||
// 添加选择圈效果
|
||||
tween(this.node)
|
||||
.to(0.3, { scale: new Vec3(1.1, 1.1, 1.1) })
|
||||
.to(0.3, { scale: Vec3.ONE })
|
||||
.union()
|
||||
.repeatForever()
|
||||
.start();
|
||||
}
|
||||
|
||||
/**
|
||||
* 隐藏选择效果
|
||||
*/
|
||||
private hideSelectionEffect() {
|
||||
// 停止所有缩放动画
|
||||
tween(this.node).stop();
|
||||
this.node.setScale(Vec3.ONE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发布命令
|
||||
*/
|
||||
issueCommand(command: string, target?: Vec3 | Node) {
|
||||
if (!this.unitState || !this.behaviorTreeComponent) return;
|
||||
|
||||
this.unitState.currentCommand = command;
|
||||
|
||||
// 设置目标
|
||||
if (target instanceof Vec3) {
|
||||
this.unitState.targetPosition = target.clone();
|
||||
this.unitState.targetNode = null;
|
||||
} else if (target instanceof Node) {
|
||||
this.unitState.targetPosition = target.worldPosition.clone();
|
||||
this.unitState.targetNode = target;
|
||||
}
|
||||
|
||||
// 通过黑板更新行为树状态
|
||||
const blackboard = this.behaviorTreeComponent.getBlackboard();
|
||||
if (blackboard) {
|
||||
blackboard.setValue('currentCommand', command);
|
||||
blackboard.setValue('hasTarget', target !== undefined);
|
||||
blackboard.setValue('targetPosition', this.unitState.targetPosition);
|
||||
|
||||
if (target instanceof Node) {
|
||||
blackboard.setValue('targetType', target.name.includes('Resource') ? 'resource' :
|
||||
target.name.includes('Building') ? 'building' : 'unit');
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`单位 ${this.node.name} 接收命令: ${command}`, target);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取单位状态
|
||||
*/
|
||||
getUnitState(): UnitStateComponent | null {
|
||||
return this.unitState;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取行为树组件
|
||||
*/
|
||||
getBehaviorTreeComponent(): BehaviorTreeComponent | null {
|
||||
return this.behaviorTreeComponent;
|
||||
}
|
||||
|
||||
/**
|
||||
* 受到伤害
|
||||
*/
|
||||
takeDamage(damage: number) {
|
||||
if (!this.unitState) return;
|
||||
|
||||
this.unitState.currentHealth = Math.max(0, this.unitState.currentHealth - damage);
|
||||
|
||||
// 更新黑板
|
||||
const blackboard = this.behaviorTreeComponent?.getBlackboard();
|
||||
if (blackboard) {
|
||||
blackboard.setValue('currentHealth', this.unitState.currentHealth);
|
||||
blackboard.setValue('healthPercentage', this.unitState.currentHealth / this.unitState.maxHealth);
|
||||
blackboard.setValue('isLowHealth', this.unitState.currentHealth < this.unitState.maxHealth * 0.3);
|
||||
}
|
||||
|
||||
// 视觉效果
|
||||
this.showDamageEffect();
|
||||
|
||||
if (this.unitState.currentHealth <= 0) {
|
||||
this.die();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 显示受伤效果
|
||||
*/
|
||||
private showDamageEffect() {
|
||||
if (!this.meshRenderer || !this.meshRenderer.material) return;
|
||||
|
||||
// 闪红效果
|
||||
const originalColor = this.meshRenderer.material.getProperty('mainColor') as Color;
|
||||
this.meshRenderer.material.setProperty('mainColor', Color.RED);
|
||||
|
||||
this.scheduleOnce(() => {
|
||||
if (this.meshRenderer && this.meshRenderer.material) {
|
||||
this.meshRenderer.material.setProperty('mainColor', originalColor);
|
||||
}
|
||||
}, 0.2);
|
||||
}
|
||||
|
||||
/**
|
||||
* 单位死亡
|
||||
*/
|
||||
private die() {
|
||||
console.log(`单位 ${this.node.name} 死亡`);
|
||||
|
||||
// 从ECS系统中移除实体
|
||||
if (this.entity) {
|
||||
Core.entityManager.destroyEntity(this.entity);
|
||||
}
|
||||
|
||||
// 播放死亡动画后销毁节点
|
||||
tween(this.node)
|
||||
.to(0.5, { scale: Vec3.ZERO })
|
||||
.call(() => {
|
||||
this.node.destroy();
|
||||
})
|
||||
.start();
|
||||
}
|
||||
|
||||
update(deltaTime: number) {
|
||||
if (!this.unitState || !this.behaviorTreeComponent) return;
|
||||
|
||||
// 更新黑板中的时间相关变量
|
||||
const blackboard = this.behaviorTreeComponent.getBlackboard();
|
||||
if (blackboard) {
|
||||
blackboard.setValue('deltaTime', deltaTime);
|
||||
blackboard.setValue('currentTime', Date.now() / 1000);
|
||||
blackboard.setValue('worldPosition', this.node.worldPosition);
|
||||
}
|
||||
|
||||
// 调试信息显示
|
||||
if (this.showDebugInfo) {
|
||||
this.updateDebugInfo();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新调试信息
|
||||
*/
|
||||
private updateDebugInfo() {
|
||||
// 可以在这里添加调试信息的显示逻辑
|
||||
// 比如在单位上方显示状态文本等
|
||||
}
|
||||
|
||||
onDestroy() {
|
||||
// 清理ECS实体
|
||||
if (this.entity) {
|
||||
Core.entityManager.destroyEntity(this.entity);
|
||||
}
|
||||
|
||||
// 停止所有动画
|
||||
tween(this.node).stop();
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "c82e7909-01e6-49ca-b68d-9ee98837e238",
|
||||
"uuid": "61885e67-b7a2-4e51-857e-ccbea4fdea03",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
@@ -0,0 +1,306 @@
|
||||
import { _decorator, Component, Node, Vec3, Material, MeshRenderer, Color, tween } from 'cc';
|
||||
import { BehaviorTreeManager } from './BehaviorTreeManager';
|
||||
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
/**
|
||||
* 单位配置接口
|
||||
*/
|
||||
export interface UnitConfig {
|
||||
unitType: string;
|
||||
behaviorTreeName: string;
|
||||
maxHealth: number;
|
||||
moveSpeed: number;
|
||||
attackRange: number;
|
||||
attackDamage: number;
|
||||
color: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* 单位控制器 - 纯Cocos Creator组件,管理单位的行为和状态
|
||||
*/
|
||||
@ccclass('UnitController')
|
||||
export class UnitController extends Component {
|
||||
|
||||
@property
|
||||
showDebugInfo: boolean = true;
|
||||
|
||||
// 单位属性
|
||||
public unitType: string = '';
|
||||
public maxHealth: number = 100;
|
||||
public currentHealth: number = 100;
|
||||
public moveSpeed: number = 3;
|
||||
public attackRange: number = 2;
|
||||
public attackDamage: number = 25;
|
||||
public isSelected: boolean = false;
|
||||
public currentCommand: string = 'idle';
|
||||
public targetPosition: Vec3 = Vec3.ZERO.clone();
|
||||
public targetNode: Node | null = null;
|
||||
public lastAttackTime: number = 0;
|
||||
public attackCooldown: number = 1.5;
|
||||
public color: string = 'white';
|
||||
|
||||
private behaviorTreeManager: BehaviorTreeManager | null = null;
|
||||
private meshRenderer: MeshRenderer | null = null;
|
||||
|
||||
onLoad() {
|
||||
this.meshRenderer = this.getComponent(MeshRenderer);
|
||||
|
||||
// 创建行为树管理器
|
||||
this.behaviorTreeManager = this.addComponent(BehaviorTreeManager);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置单位配置
|
||||
*/
|
||||
setup(config: UnitConfig) {
|
||||
this.unitType = config.unitType;
|
||||
this.maxHealth = config.maxHealth;
|
||||
this.currentHealth = config.maxHealth;
|
||||
this.moveSpeed = config.moveSpeed;
|
||||
this.attackRange = config.attackRange;
|
||||
this.attackDamage = config.attackDamage;
|
||||
this.color = config.color;
|
||||
|
||||
// 设置材质颜色
|
||||
this.setUnitColor(config.color);
|
||||
|
||||
// 初始化行为树
|
||||
if (this.behaviorTreeManager) {
|
||||
this.behaviorTreeManager.initializeBehaviorTree(config.behaviorTreeName, this);
|
||||
}
|
||||
|
||||
console.log(`单位 ${this.node.name} 设置完成 - 类型: ${config.unitType}, 行为树: ${config.behaviorTreeName}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置单位颜色
|
||||
*/
|
||||
private setUnitColor(colorName: string) {
|
||||
if (!this.meshRenderer || !this.meshRenderer.material) return;
|
||||
|
||||
const colorMap: { [key: string]: Color } = {
|
||||
'red': Color.RED,
|
||||
'green': Color.GREEN,
|
||||
'blue': Color.BLUE,
|
||||
'yellow': Color.YELLOW,
|
||||
'white': Color.WHITE,
|
||||
'cyan': Color.CYAN,
|
||||
'magenta': Color.MAGENTA
|
||||
};
|
||||
|
||||
const color = colorMap[colorName] || Color.WHITE;
|
||||
this.meshRenderer.material.setProperty('mainColor', color);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置选择状态
|
||||
*/
|
||||
setSelected(selected: boolean) {
|
||||
this.isSelected = selected;
|
||||
|
||||
// 视觉效果
|
||||
if (selected) {
|
||||
this.showSelectionEffect();
|
||||
} else {
|
||||
this.hideSelectionEffect();
|
||||
}
|
||||
|
||||
// 更新行为树黑板
|
||||
if (this.behaviorTreeManager) {
|
||||
this.behaviorTreeManager.updateBlackboardValue('isSelected', selected);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 显示选择效果
|
||||
*/
|
||||
private showSelectionEffect() {
|
||||
// 添加选择圈效果
|
||||
tween(this.node)
|
||||
.to(0.3, { scale: new Vec3(1.1, 1.1, 1.1) })
|
||||
.to(0.3, { scale: Vec3.ONE })
|
||||
.union()
|
||||
.repeatForever()
|
||||
.start();
|
||||
}
|
||||
|
||||
/**
|
||||
* 隐藏选择效果
|
||||
*/
|
||||
private hideSelectionEffect() {
|
||||
// 停止所有缩放动画
|
||||
tween(this.node).stop();
|
||||
this.node.setScale(Vec3.ONE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发布命令
|
||||
*/
|
||||
issueCommand(command: string, target?: Vec3 | Node) {
|
||||
this.currentCommand = command;
|
||||
|
||||
// 设置目标
|
||||
if (target instanceof Vec3) {
|
||||
this.targetPosition = target.clone();
|
||||
this.targetNode = null;
|
||||
} else if (target instanceof Node) {
|
||||
this.targetPosition = target.worldPosition.clone();
|
||||
this.targetNode = target;
|
||||
}
|
||||
|
||||
// 更新行为树黑板
|
||||
if (this.behaviorTreeManager) {
|
||||
this.behaviorTreeManager.updateBlackboardValue('currentCommand', command);
|
||||
this.behaviorTreeManager.updateBlackboardValue('hasTarget', target !== undefined);
|
||||
this.behaviorTreeManager.updateBlackboardValue('targetPosition', this.targetPosition);
|
||||
|
||||
if (target instanceof Node) {
|
||||
this.behaviorTreeManager.updateBlackboardValue('targetType',
|
||||
target.name.includes('Resource') ? 'resource' :
|
||||
target.name.includes('Building') ? 'building' : 'unit');
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`单位 ${this.node.name} 接收命令: ${command}`, target);
|
||||
}
|
||||
|
||||
/**
|
||||
* 受到伤害
|
||||
*/
|
||||
takeDamage(damage: number) {
|
||||
this.currentHealth = Math.max(0, this.currentHealth - damage);
|
||||
|
||||
// 更新行为树黑板
|
||||
if (this.behaviorTreeManager) {
|
||||
this.behaviorTreeManager.updateBlackboardValue('currentHealth', this.currentHealth);
|
||||
this.behaviorTreeManager.updateBlackboardValue('healthPercentage', this.currentHealth / this.maxHealth);
|
||||
this.behaviorTreeManager.updateBlackboardValue('isLowHealth', this.currentHealth < this.maxHealth * 0.3);
|
||||
}
|
||||
|
||||
// 视觉效果
|
||||
this.showDamageEffect();
|
||||
|
||||
if (this.currentHealth <= 0) {
|
||||
this.die();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 显示受伤效果
|
||||
*/
|
||||
private showDamageEffect() {
|
||||
if (!this.meshRenderer || !this.meshRenderer.material) return;
|
||||
|
||||
// 闪红效果
|
||||
const originalColor = this.meshRenderer.material.getProperty('mainColor') as Color;
|
||||
this.meshRenderer.material.setProperty('mainColor', Color.RED);
|
||||
|
||||
this.scheduleOnce(() => {
|
||||
if (this.meshRenderer && this.meshRenderer.material) {
|
||||
this.meshRenderer.material.setProperty('mainColor', originalColor);
|
||||
}
|
||||
}, 0.2);
|
||||
}
|
||||
|
||||
/**
|
||||
* 单位死亡
|
||||
*/
|
||||
private die() {
|
||||
console.log(`单位 ${this.node.name} 死亡`);
|
||||
|
||||
// 播放死亡动画后销毁节点
|
||||
tween(this.node)
|
||||
.to(0.5, { scale: Vec3.ZERO })
|
||||
.call(() => {
|
||||
this.node.destroy();
|
||||
})
|
||||
.start();
|
||||
}
|
||||
|
||||
/**
|
||||
* 移动到目标位置
|
||||
*/
|
||||
moveToTarget(targetPos: Vec3, speed?: number): boolean {
|
||||
const currentPos = this.node.worldPosition;
|
||||
const distance = currentPos.subtract(targetPos).length();
|
||||
|
||||
if (distance < 0.5) {
|
||||
return true; // 已到达目标
|
||||
}
|
||||
|
||||
// 简单的移动逻辑
|
||||
const direction = targetPos.subtract(currentPos).normalize();
|
||||
const moveSpeed = speed || this.moveSpeed;
|
||||
const deltaTime = 1/60; // 假设60fps
|
||||
|
||||
const newPosition = currentPos.add(direction.multiplyScalar(moveSpeed * deltaTime));
|
||||
this.node.setWorldPosition(newPosition);
|
||||
|
||||
return false; // 还在移动中
|
||||
}
|
||||
|
||||
/**
|
||||
* 攻击目标
|
||||
*/
|
||||
attackTarget(): boolean {
|
||||
const currentTime = Date.now() / 1000;
|
||||
|
||||
if (currentTime - this.lastAttackTime < this.attackCooldown) {
|
||||
return false; // 冷却中
|
||||
}
|
||||
|
||||
// 执行攻击
|
||||
console.log(`${this.node.name} 执行攻击`);
|
||||
this.lastAttackTime = currentTime;
|
||||
|
||||
// 更新行为树黑板
|
||||
if (this.behaviorTreeManager) {
|
||||
this.behaviorTreeManager.updateBlackboardValue('lastAttackTime', currentTime);
|
||||
}
|
||||
|
||||
return true; // 攻击成功
|
||||
}
|
||||
|
||||
update(deltaTime: number) {
|
||||
// 更新行为树黑板中的时间相关变量
|
||||
if (this.behaviorTreeManager) {
|
||||
this.behaviorTreeManager.updateBlackboardValue('deltaTime', deltaTime);
|
||||
this.behaviorTreeManager.updateBlackboardValue('currentTime', Date.now() / 1000);
|
||||
this.behaviorTreeManager.updateBlackboardValue('worldPosition', this.node.worldPosition);
|
||||
|
||||
// 更新距离信息
|
||||
if (this.targetPosition) {
|
||||
const distance = this.node.worldPosition.subtract(this.targetPosition).length();
|
||||
this.behaviorTreeManager.updateBlackboardValue('distanceToTarget', distance);
|
||||
this.behaviorTreeManager.updateBlackboardValue('isInAttackRange', distance <= this.attackRange);
|
||||
this.behaviorTreeManager.updateBlackboardValue('isCloseToTarget', distance <= 1.0);
|
||||
}
|
||||
|
||||
// 更新状态标志
|
||||
this.behaviorTreeManager.updateBlackboardValue('isIdle', this.currentCommand === 'idle');
|
||||
this.behaviorTreeManager.updateBlackboardValue('isMoving', this.currentCommand === 'move');
|
||||
this.behaviorTreeManager.updateBlackboardValue('isAttacking', this.currentCommand === 'attack');
|
||||
this.behaviorTreeManager.updateBlackboardValue('isGathering', this.currentCommand === 'gather');
|
||||
this.behaviorTreeManager.updateBlackboardValue('isPatrolling', this.currentCommand === 'patrol');
|
||||
}
|
||||
|
||||
// 调试信息显示
|
||||
if (this.showDebugInfo) {
|
||||
this.updateDebugInfo();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新调试信息
|
||||
*/
|
||||
private updateDebugInfo() {
|
||||
// 可以在这里添加调试信息的显示逻辑
|
||||
// 比如在单位上方显示状态文本等
|
||||
}
|
||||
|
||||
onDestroy() {
|
||||
// 停止所有动画
|
||||
tween(this.node).stop();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "4ac64480-2d09-4de6-a22c-add022790676",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
"ver": "1.2.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "6f9217a1-dff6-4460-b5da-eb01cf29c03c",
|
||||
"uuid": "84a4754f-3fad-4031-8aeb-e699584cfb92",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
@@ -0,0 +1,203 @@
|
||||
import { _decorator, Component, Node, Camera, Vec3, input, Input, EventMouse, EventTouch, KeyCode, Quat } from 'cc';
|
||||
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
/**
|
||||
* RTS相机控制器
|
||||
* 提供RTS游戏常用的相机控制功能
|
||||
*/
|
||||
@ccclass('RTSCameraController')
|
||||
export class RTSCameraController extends Component {
|
||||
|
||||
@property
|
||||
moveSpeed: number = 10;
|
||||
|
||||
@property
|
||||
rotateSpeed: number = 60;
|
||||
|
||||
@property
|
||||
zoomSpeed: number = 5;
|
||||
|
||||
@property
|
||||
minZoom: number = 5;
|
||||
|
||||
@property
|
||||
maxZoom: number = 30;
|
||||
|
||||
@property
|
||||
boundarySize: number = 50;
|
||||
|
||||
private camera: Camera = null!;
|
||||
private isMouseDown: boolean = false;
|
||||
private lastMousePosition: Vec3 = Vec3.ZERO.clone();
|
||||
private currentZoom: number = 15;
|
||||
|
||||
// 键盘输入状态
|
||||
private keyStates: { [key: string]: boolean } = {};
|
||||
|
||||
onLoad() {
|
||||
this.camera = this.getComponent(Camera)!;
|
||||
this.currentZoom = this.node.position.y;
|
||||
|
||||
// 注册输入事件
|
||||
input.on(Input.EventType.MOUSE_DOWN, this.onMouseDown, this);
|
||||
input.on(Input.EventType.MOUSE_UP, this.onMouseUp, this);
|
||||
input.on(Input.EventType.MOUSE_MOVE, this.onMouseMove, this);
|
||||
input.on(Input.EventType.MOUSE_WHEEL, this.onMouseWheel, this);
|
||||
input.on(Input.EventType.KEY_DOWN, this.onKeyDown, this);
|
||||
input.on(Input.EventType.KEY_UP, this.onKeyUp, this);
|
||||
}
|
||||
|
||||
onDestroy() {
|
||||
// 取消注册输入事件
|
||||
input.off(Input.EventType.MOUSE_DOWN, this.onMouseDown, this);
|
||||
input.off(Input.EventType.MOUSE_UP, this.onMouseUp, this);
|
||||
input.off(Input.EventType.MOUSE_MOVE, this.onMouseMove, this);
|
||||
input.off(Input.EventType.MOUSE_WHEEL, this.onMouseWheel, this);
|
||||
input.off(Input.EventType.KEY_DOWN, this.onKeyDown, this);
|
||||
input.off(Input.EventType.KEY_UP, this.onKeyUp, this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 鼠标按下事件
|
||||
*/
|
||||
private onMouseDown(event: EventMouse) {
|
||||
if (event.getButton() === EventMouse.BUTTON_MIDDLE) {
|
||||
this.isMouseDown = true;
|
||||
this.lastMousePosition.set(event.getLocationX(), event.getLocationY(), 0);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 鼠标抬起事件
|
||||
*/
|
||||
private onMouseUp(event: EventMouse) {
|
||||
if (event.getButton() === EventMouse.BUTTON_MIDDLE) {
|
||||
this.isMouseDown = false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 鼠标移动事件
|
||||
*/
|
||||
private onMouseMove(event: EventMouse) {
|
||||
if (this.isMouseDown) {
|
||||
const deltaX = event.getLocationX() - this.lastMousePosition.x;
|
||||
const deltaY = event.getLocationY() - this.lastMousePosition.y;
|
||||
|
||||
// 相机平移
|
||||
const moveVector = new Vec3(-deltaX * 0.01, 0, deltaY * 0.01);
|
||||
this.node.translate(moveVector);
|
||||
|
||||
this.lastMousePosition.set(event.getLocationX(), event.getLocationY(), 0);
|
||||
|
||||
// 限制相机边界
|
||||
this.clampCameraBounds();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 鼠标滚轮事件
|
||||
*/
|
||||
private onMouseWheel(event: EventMouse) {
|
||||
const scrollY = event.getScrollY();
|
||||
this.currentZoom -= scrollY * this.zoomSpeed * 0.1;
|
||||
this.currentZoom = Math.max(this.minZoom, Math.min(this.maxZoom, this.currentZoom));
|
||||
|
||||
const pos = this.node.position;
|
||||
this.node.setPosition(pos.x, this.currentZoom, pos.z);
|
||||
}
|
||||
|
||||
/**
|
||||
* 键盘按下事件
|
||||
*/
|
||||
private onKeyDown(event: any) {
|
||||
this.keyStates[event.keyCode] = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 键盘抬起事件
|
||||
*/
|
||||
private onKeyUp(event: any) {
|
||||
this.keyStates[event.keyCode] = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新相机移动
|
||||
*/
|
||||
update(deltaTime: number) {
|
||||
this.handleKeyboardMovement(deltaTime);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理键盘移动
|
||||
*/
|
||||
private handleKeyboardMovement(deltaTime: number) {
|
||||
const moveDistance = this.moveSpeed * deltaTime;
|
||||
let moveVector = Vec3.ZERO.clone();
|
||||
|
||||
// WASD 移动
|
||||
if (this.keyStates[KeyCode.KEY_W] || this.keyStates[KeyCode.ARROW_UP]) {
|
||||
moveVector.z -= moveDistance;
|
||||
}
|
||||
if (this.keyStates[KeyCode.KEY_S] || this.keyStates[KeyCode.ARROW_DOWN]) {
|
||||
moveVector.z += moveDistance;
|
||||
}
|
||||
if (this.keyStates[KeyCode.KEY_A] || this.keyStates[KeyCode.ARROW_LEFT]) {
|
||||
moveVector.x -= moveDistance;
|
||||
}
|
||||
if (this.keyStates[KeyCode.KEY_D] || this.keyStates[KeyCode.ARROW_RIGHT]) {
|
||||
moveVector.x += moveDistance;
|
||||
}
|
||||
|
||||
// 应用移动
|
||||
if (!moveVector.equals(Vec3.ZERO)) {
|
||||
this.node.translate(moveVector);
|
||||
this.clampCameraBounds();
|
||||
}
|
||||
|
||||
// QE 旋转
|
||||
if (this.keyStates[KeyCode.KEY_Q]) {
|
||||
const rotateAngle = this.rotateSpeed * deltaTime * Math.PI / 180;
|
||||
const currentRotation = this.node.rotation.clone();
|
||||
const yRotation = Quat.fromAxisAngle(new Quat(), Vec3.UP, rotateAngle);
|
||||
Quat.multiply(currentRotation, currentRotation, yRotation);
|
||||
this.node.rotation = currentRotation;
|
||||
}
|
||||
if (this.keyStates[KeyCode.KEY_E]) {
|
||||
const rotateAngle = -this.rotateSpeed * deltaTime * Math.PI / 180;
|
||||
const currentRotation = this.node.rotation.clone();
|
||||
const yRotation = Quat.fromAxisAngle(new Quat(), Vec3.UP, rotateAngle);
|
||||
Quat.multiply(currentRotation, currentRotation, yRotation);
|
||||
this.node.rotation = currentRotation;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 限制相机边界
|
||||
*/
|
||||
private clampCameraBounds() {
|
||||
const pos = this.node.position;
|
||||
const clampedX = Math.max(-this.boundarySize, Math.min(this.boundarySize, pos.x));
|
||||
const clampedZ = Math.max(-this.boundarySize, Math.min(this.boundarySize, pos.z));
|
||||
|
||||
this.node.setPosition(clampedX, pos.y, clampedZ);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置相机位置
|
||||
*/
|
||||
setCameraPosition(position: Vec3) {
|
||||
this.node.setPosition(position);
|
||||
this.clampCameraBounds();
|
||||
}
|
||||
|
||||
/**
|
||||
* 聚焦到目标位置
|
||||
*/
|
||||
focusOnTarget(target: Vec3, duration: number = 1.0) {
|
||||
const targetPos = new Vec3(target.x, this.currentZoom, target.z);
|
||||
// 这里可以添加缓动动画
|
||||
this.setCameraPosition(targetPos);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "dabc6540-6e9f-450a-9d22-b9af94c20d6d",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
@@ -0,0 +1,225 @@
|
||||
import { _decorator, Component, Node, Vec3, Label, Button, EventHandler } from 'cc';
|
||||
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
/**
|
||||
* UI控制器 - 管理RTS演示的用户界面
|
||||
*/
|
||||
@ccclass('UIController')
|
||||
export class UIController extends Component {
|
||||
|
||||
@property(Label)
|
||||
selectedUnitsLabel: Label = null!;
|
||||
|
||||
@property(Button)
|
||||
moveButton: Button = null!;
|
||||
|
||||
@property(Button)
|
||||
attackButton: Button = null!;
|
||||
|
||||
@property(Button)
|
||||
gatherButton: Button = null!;
|
||||
|
||||
@property(Button)
|
||||
patrolButton: Button = null!;
|
||||
|
||||
@property(Label)
|
||||
infoLabel: Label = null!;
|
||||
|
||||
// 回调函数
|
||||
public onUnitSelected: ((units: Node[]) => void) | null = null;
|
||||
public onCommandIssued: ((command: string, target?: Vec3 | Node) => void) | null = null;
|
||||
|
||||
private selectedUnitsCount: number = 0;
|
||||
|
||||
onLoad() {
|
||||
this.setupUI();
|
||||
this.updateSelectedUnitsDisplay();
|
||||
this.updateInfoDisplay('RTS 行为树演示 - 点击单位选择,然后发布命令');
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置UI事件
|
||||
*/
|
||||
private setupUI() {
|
||||
// 移动命令按钮
|
||||
if (this.moveButton) {
|
||||
const moveHandler = new EventHandler();
|
||||
moveHandler.target = this.node;
|
||||
moveHandler.component = 'UIController';
|
||||
moveHandler.handler = 'onMoveCommand';
|
||||
this.moveButton.clickEvents.push(moveHandler);
|
||||
}
|
||||
|
||||
// 攻击命令按钮
|
||||
if (this.attackButton) {
|
||||
const attackHandler = new EventHandler();
|
||||
attackHandler.target = this.node;
|
||||
attackHandler.component = 'UIController';
|
||||
attackHandler.handler = 'onAttackCommand';
|
||||
this.attackButton.clickEvents.push(attackHandler);
|
||||
}
|
||||
|
||||
// 收集命令按钮
|
||||
if (this.gatherButton) {
|
||||
const gatherHandler = new EventHandler();
|
||||
gatherHandler.target = this.node;
|
||||
gatherHandler.component = 'UIController';
|
||||
gatherHandler.handler = 'onGatherCommand';
|
||||
this.gatherButton.clickEvents.push(gatherHandler);
|
||||
}
|
||||
|
||||
// 巡逻命令按钮
|
||||
if (this.patrolButton) {
|
||||
const patrolHandler = new EventHandler();
|
||||
patrolHandler.target = this.node;
|
||||
patrolHandler.component = 'UIController';
|
||||
patrolHandler.handler = 'onPatrolCommand';
|
||||
this.patrolButton.clickEvents.push(patrolHandler);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置选中单位数量
|
||||
*/
|
||||
setSelectedUnitsCount(count: number) {
|
||||
this.selectedUnitsCount = count;
|
||||
this.updateSelectedUnitsDisplay();
|
||||
this.updateButtonStates();
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新选中单位显示
|
||||
*/
|
||||
private updateSelectedUnitsDisplay() {
|
||||
if (this.selectedUnitsLabel) {
|
||||
this.selectedUnitsLabel.string = `选中单位: ${this.selectedUnitsCount}`;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新按钮状态
|
||||
*/
|
||||
private updateButtonStates() {
|
||||
const hasSelection = this.selectedUnitsCount > 0;
|
||||
|
||||
if (this.moveButton) {
|
||||
this.moveButton.interactable = hasSelection;
|
||||
}
|
||||
if (this.attackButton) {
|
||||
this.attackButton.interactable = hasSelection;
|
||||
}
|
||||
if (this.gatherButton) {
|
||||
this.gatherButton.interactable = hasSelection;
|
||||
}
|
||||
if (this.patrolButton) {
|
||||
this.patrolButton.interactable = hasSelection;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新信息显示
|
||||
*/
|
||||
updateInfoDisplay(message: string) {
|
||||
if (this.infoLabel) {
|
||||
this.infoLabel.string = message;
|
||||
}
|
||||
console.log(`[UI] ${message}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* 移动命令
|
||||
*/
|
||||
onMoveCommand() {
|
||||
if (this.selectedUnitsCount === 0) {
|
||||
this.updateInfoDisplay('请先选择单位');
|
||||
return;
|
||||
}
|
||||
|
||||
// 生成随机目标位置
|
||||
const targetPos = new Vec3(
|
||||
(Math.random() - 0.5) * 20,
|
||||
0,
|
||||
(Math.random() - 0.5) * 20
|
||||
);
|
||||
|
||||
this.onCommandIssued?.('move', targetPos);
|
||||
this.updateInfoDisplay(`发布移动命令到位置 (${targetPos.x.toFixed(1)}, ${targetPos.z.toFixed(1)})`);
|
||||
}
|
||||
|
||||
/**
|
||||
* 攻击命令
|
||||
*/
|
||||
onAttackCommand() {
|
||||
if (this.selectedUnitsCount === 0) {
|
||||
this.updateInfoDisplay('请先选择单位');
|
||||
return;
|
||||
}
|
||||
|
||||
// 生成随机攻击位置
|
||||
const targetPos = new Vec3(
|
||||
(Math.random() - 0.5) * 15,
|
||||
0,
|
||||
(Math.random() - 0.5) * 15
|
||||
);
|
||||
|
||||
this.onCommandIssued?.('attack', targetPos);
|
||||
this.updateInfoDisplay(`发布攻击命令到位置 (${targetPos.x.toFixed(1)}, ${targetPos.z.toFixed(1)})`);
|
||||
}
|
||||
|
||||
/**
|
||||
* 收集命令
|
||||
*/
|
||||
onGatherCommand() {
|
||||
if (this.selectedUnitsCount === 0) {
|
||||
this.updateInfoDisplay('请先选择单位');
|
||||
return;
|
||||
}
|
||||
|
||||
this.onCommandIssued?.('gather');
|
||||
this.updateInfoDisplay('发布收集资源命令');
|
||||
}
|
||||
|
||||
/**
|
||||
* 巡逻命令
|
||||
*/
|
||||
onPatrolCommand() {
|
||||
if (this.selectedUnitsCount === 0) {
|
||||
this.updateInfoDisplay('请先选择单位');
|
||||
return;
|
||||
}
|
||||
|
||||
this.onCommandIssued?.('patrol');
|
||||
this.updateInfoDisplay('发布巡逻命令');
|
||||
}
|
||||
|
||||
/**
|
||||
* 显示单位信息
|
||||
*/
|
||||
showUnitInfo(unitName: string, unitType: string, health: number, maxHealth: number) {
|
||||
const healthPercent = Math.round((health / maxHealth) * 100);
|
||||
this.updateInfoDisplay(`${unitName} (${unitType}) - 生命值: ${health}/${maxHealth} (${healthPercent}%)`);
|
||||
}
|
||||
|
||||
/**
|
||||
* 显示行为树状态
|
||||
*/
|
||||
showBehaviorTreeStatus(unitName: string, currentBehavior: string) {
|
||||
this.updateInfoDisplay(`${unitName} 当前行为: ${currentBehavior}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* 显示错误信息
|
||||
*/
|
||||
showError(message: string) {
|
||||
this.updateInfoDisplay(`错误: ${message}`);
|
||||
console.error(`[UI Error] ${message}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* 显示成功信息
|
||||
*/
|
||||
showSuccess(message: string) {
|
||||
this.updateInfoDisplay(`成功: ${message}`);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "00e982f3-dcf3-44b0-9b63-5e2877c1971e",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
@@ -1,270 +0,0 @@
|
||||
import { _decorator, Component, Node, Label, Button, resources, JsonAsset } from 'cc';
|
||||
import {
|
||||
BehaviorTreeBuilder,
|
||||
BehaviorTree,
|
||||
Blackboard
|
||||
} from '@esengine/ai';
|
||||
import type { BehaviorTreeJSONConfig } from '@esengine/ai';
|
||||
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
/**
|
||||
* 行为树测试示例
|
||||
* 使用 @esengine/ai 包的 BehaviorTreeBuilder API 自动加载和初始化行为树
|
||||
*
|
||||
* 特性:
|
||||
* - 自动从JSON配置文件加载行为树
|
||||
* - 自动初始化黑板变量
|
||||
* - 支持行为树的启动、停止、暂停、恢复
|
||||
* - 实时显示黑板变量状态
|
||||
*/
|
||||
@ccclass('BehaviorTreeExample')
|
||||
export class BehaviorTreeExample extends Component {
|
||||
|
||||
@property(Label)
|
||||
statusLabel: Label = null;
|
||||
|
||||
@property(Label)
|
||||
logLabel: Label = null;
|
||||
|
||||
@property(Button)
|
||||
startButton: Button = null;
|
||||
|
||||
@property(Button)
|
||||
stopButton: Button = null;
|
||||
|
||||
@property(Button)
|
||||
pauseButton: Button = null;
|
||||
|
||||
@property(Button)
|
||||
resumeButton: Button = null;
|
||||
|
||||
private behaviorTree: BehaviorTree<any> = null;
|
||||
private blackboard: Blackboard = null;
|
||||
private isRunning: boolean = false;
|
||||
private isPaused: boolean = false;
|
||||
private logs: string[] = [];
|
||||
private executionContext: any = null;
|
||||
private updateTimer: number = 0;
|
||||
|
||||
onLoad() {
|
||||
this.setupUI();
|
||||
this.loadBehaviorTree();
|
||||
}
|
||||
|
||||
private setupUI() {
|
||||
if (this.startButton) {
|
||||
this.startButton.node.on('click', this.startBehaviorTree, this);
|
||||
}
|
||||
if (this.stopButton) {
|
||||
this.stopButton.node.on('click', this.stopBehaviorTree, this);
|
||||
}
|
||||
if (this.pauseButton) {
|
||||
this.pauseButton.node.on('click', this.pauseBehaviorTree, this);
|
||||
}
|
||||
if (this.resumeButton) {
|
||||
this.resumeButton.node.on('click', this.resumeBehaviorTree, this);
|
||||
}
|
||||
|
||||
this.updateStatus('初始化中...');
|
||||
this.updateLog('行为树测试组件已加载');
|
||||
}
|
||||
|
||||
private async loadBehaviorTree() {
|
||||
try {
|
||||
this.updateStatus('加载行为树配置...');
|
||||
|
||||
// 从resources目录加载simple-example.bt.json文件
|
||||
resources.load('simple-example.bt', JsonAsset, (err, jsonAsset: JsonAsset) => {
|
||||
if (err) {
|
||||
console.error('加载行为树配置失败:', err);
|
||||
this.updateStatus('加载失败: ' + err.message);
|
||||
this.updateLog('❌ 加载simple-example.bt.json失败: ' + err.message);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const config = jsonAsset.json as BehaviorTreeJSONConfig;
|
||||
this.setupBehaviorTree(config);
|
||||
} catch (error) {
|
||||
console.error('解析行为树配置失败:', error);
|
||||
this.updateStatus('解析失败: ' + error.message);
|
||||
this.updateLog('❌ 解析行为树配置失败: ' + error.message);
|
||||
}
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error('行为树加载过程出错:', error);
|
||||
this.updateStatus('加载出错: ' + error.message);
|
||||
this.updateLog('❌ 行为树加载过程出错: ' + error.message);
|
||||
}
|
||||
}
|
||||
|
||||
private setupBehaviorTree(config: BehaviorTreeJSONConfig) {
|
||||
try {
|
||||
// 创建执行上下文
|
||||
this.executionContext = {
|
||||
node: this.node,
|
||||
component: this,
|
||||
// 添加日志方法供行为树节点使用
|
||||
log: (message: string, level: string = 'info') => {
|
||||
this.updateLog(`🤖 [${level.toUpperCase()}] ${message}`);
|
||||
}
|
||||
};
|
||||
|
||||
// 🎯 使用 @esengine/ai 的 BehaviorTreeBuilder API - 一行代码完成所有初始化!
|
||||
const result = BehaviorTreeBuilder.fromBehaviorTreeConfig(config, this.executionContext);
|
||||
|
||||
this.behaviorTree = result.tree;
|
||||
this.blackboard = result.blackboard;
|
||||
this.executionContext = result.context;
|
||||
|
||||
this.updateStatus('行为树加载完成,准备执行');
|
||||
this.updateLog('✅ 行为树创建成功(使用 @esengine/ai 包)');
|
||||
this.updateLog(`📊 节点总数: ${config.nodes ? config.nodes.length : 0}`);
|
||||
this.updateLog(`📋 变量总数: ${config.blackboard ? config.blackboard.length : 0}`);
|
||||
|
||||
// 显示黑板变量初始状态
|
||||
this.logBlackboardStatus();
|
||||
|
||||
// 启用开始按钮
|
||||
if (this.startButton) {
|
||||
this.startButton.interactable = true;
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('设置行为树失败:', error);
|
||||
this.updateStatus('设置失败: ' + error.message);
|
||||
this.updateLog('❌ 行为树设置失败: ' + error.message);
|
||||
}
|
||||
}
|
||||
|
||||
private startBehaviorTree() {
|
||||
if (!this.behaviorTree) {
|
||||
this.updateLog('❌ 行为树未准备好');
|
||||
return;
|
||||
}
|
||||
|
||||
this.isRunning = true;
|
||||
this.isPaused = false;
|
||||
// 重置行为树状态
|
||||
this.behaviorTree.reset();
|
||||
this.updateStatus('执行中...');
|
||||
this.updateLog('🚀 开始执行行为树(自动初始化黑板)');
|
||||
|
||||
// 更新按钮状态
|
||||
if (this.startButton) this.startButton.interactable = false;
|
||||
if (this.stopButton) this.stopButton.interactable = true;
|
||||
if (this.pauseButton) this.pauseButton.interactable = true;
|
||||
if (this.resumeButton) this.resumeButton.interactable = false;
|
||||
}
|
||||
|
||||
private stopBehaviorTree() {
|
||||
this.isRunning = false;
|
||||
this.isPaused = false;
|
||||
this.updateStatus('已停止');
|
||||
this.updateLog('⏹️ 行为树执行已停止');
|
||||
|
||||
// 重置行为树状态
|
||||
if (this.behaviorTree) {
|
||||
this.behaviorTree.reset();
|
||||
}
|
||||
|
||||
// 更新按钮状态
|
||||
if (this.startButton) this.startButton.interactable = true;
|
||||
if (this.stopButton) this.stopButton.interactable = false;
|
||||
if (this.pauseButton) this.pauseButton.interactable = false;
|
||||
if (this.resumeButton) this.resumeButton.interactable = false;
|
||||
}
|
||||
|
||||
private pauseBehaviorTree() {
|
||||
this.isPaused = true;
|
||||
this.updateStatus('已暂停');
|
||||
this.updateLog('⏸️ 行为树执行已暂停');
|
||||
|
||||
// 更新按钮状态
|
||||
if (this.pauseButton) this.pauseButton.interactable = false;
|
||||
if (this.resumeButton) this.resumeButton.interactable = true;
|
||||
}
|
||||
|
||||
private resumeBehaviorTree() {
|
||||
this.isPaused = false;
|
||||
this.updateStatus('执行中...');
|
||||
this.updateLog('▶️ 行为树执行已恢复');
|
||||
|
||||
// 更新按钮状态
|
||||
if (this.pauseButton) this.pauseButton.interactable = true;
|
||||
if (this.resumeButton) this.resumeButton.interactable = false;
|
||||
}
|
||||
|
||||
update(deltaTime: number) {
|
||||
if (!this.isRunning || this.isPaused || !this.behaviorTree) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.updateTimer += deltaTime;
|
||||
|
||||
// 每帧执行行为树
|
||||
try {
|
||||
this.behaviorTree.tick(deltaTime);
|
||||
|
||||
// 每2秒输出一次状态信息
|
||||
if (this.updateTimer >= 2.0) {
|
||||
this.updateTimer = 0;
|
||||
this.logBlackboardStatus();
|
||||
|
||||
// 检查行为树是否处于活动状态
|
||||
const isActive = this.behaviorTree.isActive();
|
||||
if (!isActive) {
|
||||
this.updateLog('✅ 行为树执行完成');
|
||||
// 注意:这里只是演示,实际上行为树会持续运行
|
||||
// 如果需要检查完成状态,需要通过黑板变量或其他逻辑判断
|
||||
}
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('行为树执行出错:', error);
|
||||
this.updateLog('❌ 执行出错: ' + error.message);
|
||||
this.stopBehaviorTree();
|
||||
}
|
||||
}
|
||||
|
||||
private logBlackboardStatus() {
|
||||
if (!this.blackboard) return;
|
||||
|
||||
// 获取所有黑板变量的当前值
|
||||
const variables = ['state', 'lastAction', 'defendActive', 'health', 'energy'];
|
||||
const status = variables.map(name => {
|
||||
const value = this.blackboard.getValue(name, 'undefined');
|
||||
return `${name}:${value}`;
|
||||
}).join(', ');
|
||||
|
||||
this.updateLog(`📊 状态: ${status}`);
|
||||
}
|
||||
|
||||
private updateStatus(status: string) {
|
||||
if (this.statusLabel) {
|
||||
this.statusLabel.string = status;
|
||||
}
|
||||
console.log('[BehaviorTree] 状态:', status);
|
||||
}
|
||||
|
||||
private updateLog(message: string) {
|
||||
this.logs.push(`[${new Date().toLocaleTimeString()}] ${message}`);
|
||||
|
||||
// 只保留最新的25条日志
|
||||
if (this.logs.length > 25) {
|
||||
this.logs.shift();
|
||||
}
|
||||
|
||||
if (this.logLabel) {
|
||||
this.logLabel.string = this.logs.join('\n');
|
||||
}
|
||||
|
||||
console.log('[BehaviorTree]', message);
|
||||
}
|
||||
|
||||
onDestroy() {
|
||||
this.stopBehaviorTree();
|
||||
}
|
||||
}
|
||||
@@ -1,202 +0,0 @@
|
||||
import { _decorator, Component, Node, Label, Button, Canvas, UITransform, Widget, Layout, Sprite, Color } from 'cc';
|
||||
import { BehaviorTreeExample } from './BehaviorTreeExample';
|
||||
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
/**
|
||||
* 行为树测试场景
|
||||
* 自动创建UI界面用于测试行为树
|
||||
*/
|
||||
@ccclass('BehaviorTreeTestScene')
|
||||
export class BehaviorTreeTestScene extends Component {
|
||||
|
||||
onLoad() {
|
||||
this.createTestUI();
|
||||
}
|
||||
|
||||
private createTestUI() {
|
||||
// 创建Canvas
|
||||
const canvasNode = new Node('BehaviorTreeTestCanvas');
|
||||
canvasNode.addComponent(Canvas);
|
||||
canvasNode.addComponent(UITransform);
|
||||
canvasNode.parent = this.node;
|
||||
|
||||
// 创建背景
|
||||
const backgroundNode = new Node('Background');
|
||||
const backgroundTransform = backgroundNode.addComponent(UITransform);
|
||||
backgroundTransform.setContentSize(1920, 1080);
|
||||
const backgroundSprite = backgroundNode.addComponent(Sprite);
|
||||
backgroundSprite.color = new Color(40, 40, 40, 255);
|
||||
backgroundNode.parent = canvasNode;
|
||||
|
||||
// 设置Widget组件使背景充满整个画布
|
||||
const backgroundWidget = backgroundNode.addComponent(Widget);
|
||||
backgroundWidget.isAlignTop = true;
|
||||
backgroundWidget.isAlignBottom = true;
|
||||
backgroundWidget.isAlignLeft = true;
|
||||
backgroundWidget.isAlignRight = true;
|
||||
|
||||
// 创建主容器
|
||||
const mainContainer = new Node('MainContainer');
|
||||
const mainTransform = mainContainer.addComponent(UITransform);
|
||||
mainTransform.setContentSize(1600, 900);
|
||||
mainContainer.parent = canvasNode;
|
||||
|
||||
// 设置主容器居中
|
||||
const mainWidget = mainContainer.addComponent(Widget);
|
||||
mainWidget.isAlignHorizontalCenter = true;
|
||||
mainWidget.isAlignVerticalCenter = true;
|
||||
|
||||
// 设置主容器的Layout
|
||||
const mainLayout = mainContainer.addComponent(Layout);
|
||||
mainLayout.type = Layout.Type.VERTICAL;
|
||||
mainLayout.spacingY = 20;
|
||||
mainLayout.paddingTop = 50;
|
||||
mainLayout.paddingBottom = 50;
|
||||
mainLayout.paddingLeft = 50;
|
||||
mainLayout.paddingRight = 50;
|
||||
|
||||
// 创建标题
|
||||
const titleNode = this.createText('简化API行为树测试器', 48, Color.WHITE);
|
||||
titleNode.parent = mainContainer;
|
||||
|
||||
// 创建状态显示区域
|
||||
const statusContainer = new Node('StatusContainer');
|
||||
const statusTransform = statusContainer.addComponent(UITransform);
|
||||
statusTransform.setContentSize(1500, 80);
|
||||
statusContainer.parent = mainContainer;
|
||||
|
||||
const statusLayout = statusContainer.addComponent(Layout);
|
||||
statusLayout.type = Layout.Type.HORIZONTAL;
|
||||
statusLayout.spacingX = 20;
|
||||
|
||||
// 状态标签
|
||||
const statusLabelNode = this.createText('状态: 初始化中...', 24, Color.YELLOW);
|
||||
statusLabelNode.parent = statusContainer;
|
||||
|
||||
// 创建控制按钮区域
|
||||
const buttonContainer = new Node('ButtonContainer');
|
||||
const buttonTransform = buttonContainer.addComponent(UITransform);
|
||||
buttonTransform.setContentSize(1500, 80);
|
||||
buttonContainer.parent = mainContainer;
|
||||
|
||||
const buttonLayout = buttonContainer.addComponent(Layout);
|
||||
buttonLayout.type = Layout.Type.HORIZONTAL;
|
||||
buttonLayout.spacingX = 20;
|
||||
|
||||
// 创建控制按钮
|
||||
const startButton = this.createButton('开始执行', Color.GREEN);
|
||||
const stopButton = this.createButton('停止', Color.RED);
|
||||
const pauseButton = this.createButton('暂停', Color.YELLOW);
|
||||
const resumeButton = this.createButton('恢复', Color.BLUE);
|
||||
|
||||
startButton.parent = buttonContainer;
|
||||
stopButton.parent = buttonContainer;
|
||||
pauseButton.parent = buttonContainer;
|
||||
resumeButton.parent = buttonContainer;
|
||||
|
||||
// 创建说明区域
|
||||
const infoContainer = new Node('InfoContainer');
|
||||
const infoTransform = infoContainer.addComponent(UITransform);
|
||||
infoTransform.setContentSize(1500, 60);
|
||||
infoContainer.parent = mainContainer;
|
||||
|
||||
const infoNode = this.createText('使用新的简化API - 一行代码完成所有初始化!', 18, Color.CYAN);
|
||||
infoNode.parent = infoContainer;
|
||||
|
||||
// 创建日志显示区域
|
||||
const logContainer = new Node('LogContainer');
|
||||
const logTransform = logContainer.addComponent(UITransform);
|
||||
logTransform.setContentSize(1500, 600);
|
||||
logContainer.parent = mainContainer;
|
||||
|
||||
// 日志背景
|
||||
const logBackground = new Node('LogBackground');
|
||||
const logBgTransform = logBackground.addComponent(UITransform);
|
||||
logBgTransform.setContentSize(1500, 600);
|
||||
const logBgSprite = logBackground.addComponent(Sprite);
|
||||
logBgSprite.color = new Color(20, 20, 20, 255);
|
||||
logBackground.parent = logContainer;
|
||||
|
||||
// 日志文本
|
||||
const logLabelNode = this.createText('等待行为树配置加载...', 16, Color.CYAN);
|
||||
const logLabel = logLabelNode.getComponent(Label);
|
||||
logLabel.overflow = Label.Overflow.RESIZE_HEIGHT;
|
||||
logLabel.verticalAlign = Label.VerticalAlign.TOP;
|
||||
logLabel.horizontalAlign = Label.HorizontalAlign.LEFT;
|
||||
const logTransformComp = logLabelNode.getComponent(UITransform);
|
||||
logTransformComp.setContentSize(1450, 550);
|
||||
logLabelNode.parent = logContainer;
|
||||
|
||||
// 设置日志文本位置
|
||||
const logWidget = logLabelNode.addComponent(Widget);
|
||||
logWidget.isAlignTop = true;
|
||||
logWidget.isAlignLeft = true;
|
||||
logWidget.top = 25;
|
||||
logWidget.left = 25;
|
||||
|
||||
// 添加BehaviorTreeExample组件
|
||||
const behaviorTreeExample = this.node.addComponent(BehaviorTreeExample);
|
||||
behaviorTreeExample.statusLabel = statusLabelNode.getComponent(Label);
|
||||
behaviorTreeExample.logLabel = logLabel;
|
||||
behaviorTreeExample.startButton = startButton.getComponent(Button);
|
||||
behaviorTreeExample.stopButton = stopButton.getComponent(Button);
|
||||
behaviorTreeExample.pauseButton = pauseButton.getComponent(Button);
|
||||
behaviorTreeExample.resumeButton = resumeButton.getComponent(Button);
|
||||
|
||||
// 初始化按钮状态
|
||||
stopButton.getComponent(Button).interactable = false;
|
||||
pauseButton.getComponent(Button).interactable = false;
|
||||
resumeButton.getComponent(Button).interactable = false;
|
||||
startButton.getComponent(Button).interactable = false; // 等待行为树配置加载完成
|
||||
|
||||
console.log('行为树测试UI创建完成');
|
||||
}
|
||||
|
||||
private createText(text: string, fontSize: number, color: Color): Node {
|
||||
const textNode = new Node('Text');
|
||||
const textTransform = textNode.addComponent(UITransform);
|
||||
|
||||
const label = textNode.addComponent(Label);
|
||||
label.string = text;
|
||||
label.fontSize = fontSize;
|
||||
label.color = color;
|
||||
label.lineHeight = fontSize + 4;
|
||||
|
||||
// 根据文本内容调整大小
|
||||
const estimatedWidth = Math.max(200, text.length * fontSize * 0.6);
|
||||
textTransform.setContentSize(estimatedWidth, fontSize + 10);
|
||||
|
||||
return textNode;
|
||||
}
|
||||
|
||||
private createButton(text: string, color: Color): Node {
|
||||
const buttonNode = new Node('Button');
|
||||
const buttonTransform = buttonNode.addComponent(UITransform);
|
||||
buttonTransform.setContentSize(180, 60);
|
||||
|
||||
// 按钮背景
|
||||
const buttonSprite = buttonNode.addComponent(Sprite);
|
||||
buttonSprite.color = color;
|
||||
|
||||
// 按钮组件
|
||||
const button = buttonNode.addComponent(Button);
|
||||
button.target = buttonNode;
|
||||
|
||||
// 按钮文本
|
||||
const labelNode = new Node('Label');
|
||||
const labelTransform = labelNode.addComponent(UITransform);
|
||||
labelTransform.setContentSize(170, 50);
|
||||
|
||||
const label = labelNode.addComponent(Label);
|
||||
label.string = text;
|
||||
label.fontSize = 20;
|
||||
label.color = Color.WHITE;
|
||||
label.horizontalAlign = Label.HorizontalAlign.CENTER;
|
||||
label.verticalAlign = Label.VerticalAlign.CENTER;
|
||||
|
||||
labelNode.parent = buttonNode;
|
||||
|
||||
return buttonNode;
|
||||
}
|
||||
}
|
||||
@@ -1,99 +0,0 @@
|
||||
import { Core } from '@esengine/ecs-framework';
|
||||
import { Component, _decorator } from 'cc';
|
||||
import { ExampleGameScene } from './scenes/ExampleGameScene';
|
||||
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
/**
|
||||
* ECS管理器 - Cocos Creator组件
|
||||
* 将此组件添加到场景中的任意节点上即可启动ECS框架
|
||||
*
|
||||
* 使用说明:
|
||||
* 1. 在Cocos Creator场景中创建一个空节点
|
||||
* 2. 将此ECSManager组件添加到该节点
|
||||
* 3. 运行场景即可自动启动ECS框架
|
||||
*/
|
||||
@ccclass('ECSManager')
|
||||
export class ECSManager extends Component {
|
||||
|
||||
@property({
|
||||
tooltip: '是否启用调试模式(建议开发阶段开启)'
|
||||
})
|
||||
public debugMode: boolean = true;
|
||||
|
||||
private isInitialized: boolean = false;
|
||||
|
||||
/**
|
||||
* 组件启动时初始化ECS
|
||||
*/
|
||||
start() {
|
||||
this.initializeECS();
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化ECS框架
|
||||
*/
|
||||
private initializeECS(): void {
|
||||
if (this.isInitialized) return;
|
||||
|
||||
console.log('🎮 正在初始化ECS框架...');
|
||||
|
||||
try {
|
||||
// 1. 创建Core实例,启用调试功能
|
||||
if (this.debugMode) {
|
||||
Core.create({
|
||||
debugConfig: {
|
||||
enabled: true,
|
||||
websocketUrl: 'ws://localhost:8080/ecs-debug',
|
||||
autoReconnect: true,
|
||||
updateInterval: 1000,
|
||||
channels: {
|
||||
entities: true,
|
||||
systems: true,
|
||||
performance: true,
|
||||
components: true,
|
||||
scenes: true
|
||||
}
|
||||
}
|
||||
});
|
||||
console.log('🔧 ECS调试模式已启用,可在Cocos Creator扩展面板中查看调试信息');
|
||||
} else {
|
||||
Core.create(false);
|
||||
}
|
||||
|
||||
// 2. 创建游戏场景
|
||||
const gameScene = new ExampleGameScene();
|
||||
|
||||
// 3. 设置为当前场景(会自动调用scene.begin())
|
||||
Core.scene = gameScene;
|
||||
|
||||
this.isInitialized = true;
|
||||
console.log('✅ ECS框架初始化成功!');
|
||||
console.log('📖 请查看 assets/scripts/ecs/README.md 了解如何添加组件和系统');
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ ECS框架初始化失败:', error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 每帧更新ECS框架
|
||||
*/
|
||||
update(deltaTime: number) {
|
||||
if (this.isInitialized) {
|
||||
// 更新ECS核心系统
|
||||
Core.update(deltaTime);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 组件销毁时清理ECS
|
||||
*/
|
||||
onDestroy() {
|
||||
if (this.isInitialized) {
|
||||
console.log('🧹 清理ECS框架...');
|
||||
// ECS框架会自动处理场景清理
|
||||
this.isInitialized = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,153 +0,0 @@
|
||||
# ECS框架启动模板
|
||||
|
||||
欢迎使用ECS框架!这是一个最基础的启动模板,帮助您快速开始ECS项目开发。
|
||||
|
||||
## 📁 项目结构
|
||||
|
||||
```
|
||||
ecs/
|
||||
├── components/ # 组件目录(请在此添加您的组件)
|
||||
├── systems/ # 系统目录(请在此添加您的系统)
|
||||
├── scenes/ # 场景目录
|
||||
│ └── GameScene.ts # 主游戏场景
|
||||
├── ECSManager.ts # ECS管理器组件
|
||||
└── README.md # 本文档
|
||||
```
|
||||
|
||||
## 🚀 快速开始
|
||||
|
||||
### 1. 启动ECS框架
|
||||
|
||||
ECS框架已经配置完成!您只需要:
|
||||
|
||||
1. 在Cocos Creator中打开您的场景
|
||||
2. 创建一个空节点(例如命名为"ECSManager")
|
||||
3. 将 `ECSManager` 组件添加到该节点
|
||||
4. 运行场景,ECS框架将自动启动
|
||||
|
||||
### 2. 查看控制台输出
|
||||
|
||||
如果一切正常,您将在控制台看到:
|
||||
|
||||
```
|
||||
🎮 正在初始化ECS框架...
|
||||
🔧 ECS调试模式已启用,可在Cocos Creator扩展面板中查看调试信息
|
||||
🎯 游戏场景已创建
|
||||
✅ ECS框架初始化成功!
|
||||
🚀 游戏场景已启动
|
||||
```
|
||||
|
||||
### 3. 使用调试面板
|
||||
|
||||
ECS框架已启用调试功能,您可以:
|
||||
|
||||
1. 在Cocos Creator编辑器菜单中选择 "扩展" → "ECS Framework" → "调试面板"
|
||||
2. 调试面板将显示实时的ECS运行状态:
|
||||
- 实体数量和状态
|
||||
- 系统执行信息
|
||||
- 性能监控数据
|
||||
- 组件统计信息
|
||||
|
||||
**注意**:调试功能会消耗一定性能,正式发布时建议关闭调试模式。
|
||||
|
||||
## 📚 下一步开发
|
||||
|
||||
### 创建您的第一个组件
|
||||
|
||||
在 `components/` 目录下创建组件:
|
||||
|
||||
```typescript
|
||||
// components/PositionComponent.ts
|
||||
import { Component } from '@esengine/ecs-framework';
|
||||
import { Vec3 } from 'cc';
|
||||
|
||||
export class PositionComponent extends Component {
|
||||
public position: Vec3 = new Vec3();
|
||||
|
||||
constructor(x: number = 0, y: number = 0, z: number = 0) {
|
||||
super();
|
||||
this.position.set(x, y, z);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 创建您的第一个系统
|
||||
|
||||
在 `systems/` 目录下创建系统:
|
||||
|
||||
```typescript
|
||||
// systems/MovementSystem.ts
|
||||
import { EntitySystem, Entity, Matcher } from '@esengine/ecs-framework';
|
||||
import { PositionComponent } from '../components/PositionComponent';
|
||||
|
||||
export class MovementSystem extends EntitySystem {
|
||||
constructor() {
|
||||
super(Matcher.empty().all(PositionComponent));
|
||||
}
|
||||
|
||||
protected process(entities: Entity[]): void {
|
||||
for (const entity of entities) {
|
||||
const position = entity.getComponent(PositionComponent);
|
||||
if (position) {
|
||||
// TODO: 在这里编写移动逻辑
|
||||
console.log(`实体 ${entity.name} 位置: ${position.position}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 在场景中注册系统
|
||||
|
||||
在 `scenes/GameScene.ts` 的 `initialize()` 方法中添加:
|
||||
|
||||
```typescript
|
||||
import { MovementSystem } from '../systems/MovementSystem';
|
||||
|
||||
public initialize(): void {
|
||||
super.initialize();
|
||||
this.name = "MainGameScene";
|
||||
|
||||
// 添加系统
|
||||
this.addEntityProcessor(new MovementSystem());
|
||||
|
||||
// 创建测试实体
|
||||
const testEntity = this.createEntity("TestEntity");
|
||||
testEntity.addComponent(new PositionComponent(0, 0, 0));
|
||||
}
|
||||
```
|
||||
|
||||
## 🔗 学习资源
|
||||
|
||||
- [ECS框架完整文档](https://github.com/esengine/ecs-framework)
|
||||
- [ECS概念详解](https://github.com/esengine/ecs-framework/blob/master/docs/concepts-explained.md)
|
||||
- [新手教程](https://github.com/esengine/ecs-framework/blob/master/docs/beginner-tutorials.md)
|
||||
- [组件设计指南](https://github.com/esengine/ecs-framework/blob/master/docs/component-design-guide.md)
|
||||
- [系统开发指南](https://github.com/esengine/ecs-framework/blob/master/docs/system-guide.md)
|
||||
|
||||
## 💡 开发提示
|
||||
|
||||
1. **组件只存储数据**:避免在组件中编写复杂逻辑
|
||||
2. **系统处理逻辑**:所有业务逻辑应该在系统中实现
|
||||
3. **使用Matcher过滤实体**:系统通过Matcher指定需要处理的实体类型
|
||||
4. **性能优化**:大量实体时考虑使用位掩码查询和组件索引
|
||||
|
||||
## ❓ 常见问题
|
||||
|
||||
### Q: 如何创建实体?
|
||||
A: 在场景中使用 `this.createEntity("实体名称")`
|
||||
|
||||
### Q: 如何给实体添加组件?
|
||||
A: 使用 `entity.addComponent(new YourComponent())`
|
||||
|
||||
### Q: 如何获取实体的组件?
|
||||
A: 使用 `entity.getComponent(YourComponent)`
|
||||
|
||||
### Q: 如何删除实体?
|
||||
A: 使用 `entity.destroy()` 或 `this.destroyEntity(entity)`
|
||||
|
||||
---
|
||||
|
||||
🎮 **开始您的ECS开发之旅吧!**
|
||||
|
||||
如有问题,请查阅官方文档或提交Issue。
|
||||
@@ -1,11 +0,0 @@
|
||||
{
|
||||
"ver": "1.0.1",
|
||||
"importer": "text",
|
||||
"imported": true,
|
||||
"uuid": "0932496e-f7fe-4cb9-86e2-ebd7d2a3d047",
|
||||
"files": [
|
||||
".json"
|
||||
],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
@@ -1,102 +0,0 @@
|
||||
import { Component } from '@esengine/ecs-framework';
|
||||
|
||||
/**
|
||||
* 生命值组件 - 管理实体的生命值和相关状态
|
||||
*
|
||||
* 展示游戏逻辑组件的设计:
|
||||
* 1. 包含生命值的核心数据
|
||||
* 2. 提供简单的查询方法
|
||||
* 3. 复杂的伤害处理逻辑留给系统处理
|
||||
*/
|
||||
export class HealthComponent extends Component {
|
||||
/** 最大生命值 */
|
||||
public maxHealth: number;
|
||||
/** 当前生命值 */
|
||||
public currentHealth: number;
|
||||
/** 生命值回复速度(每秒回复量) */
|
||||
public regenRate: number = 0;
|
||||
/** 最后受到伤害的时间(用于延迟回血等机制) */
|
||||
public lastDamageTime: number = 0;
|
||||
/** 是否无敌 */
|
||||
public invincible: boolean = false;
|
||||
/** 无敌持续时间 */
|
||||
public invincibleDuration: number = 0;
|
||||
|
||||
constructor(maxHealth: number = 100, regenRate: number = 0) {
|
||||
super();
|
||||
this.maxHealth = maxHealth;
|
||||
this.currentHealth = maxHealth;
|
||||
this.regenRate = regenRate;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否死亡
|
||||
*/
|
||||
isDead(): boolean {
|
||||
return this.currentHealth <= 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否满血
|
||||
*/
|
||||
isFullHealth(): boolean {
|
||||
return this.currentHealth >= this.maxHealth;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取生命值百分比(0-1)
|
||||
*/
|
||||
getHealthPercentage(): number {
|
||||
return this.currentHealth / this.maxHealth;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查生命值是否低于指定百分比
|
||||
*/
|
||||
isHealthBelowPercentage(percentage: number): boolean {
|
||||
return this.getHealthPercentage() < percentage;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置生命值(不超过最大值)
|
||||
*/
|
||||
setHealth(health: number) {
|
||||
this.currentHealth = Math.max(0, Math.min(health, this.maxHealth));
|
||||
}
|
||||
|
||||
/**
|
||||
* 增加生命值(治疗)
|
||||
*/
|
||||
heal(amount: number) {
|
||||
this.currentHealth = Math.min(this.currentHealth + amount, this.maxHealth);
|
||||
}
|
||||
|
||||
/**
|
||||
* 减少生命值(受伤)
|
||||
* 注意:这里只修改数据,具体的伤害逻辑(如死亡处理)应该在系统中实现
|
||||
*/
|
||||
takeDamage(damage: number) {
|
||||
if (this.invincible) return;
|
||||
|
||||
this.currentHealth = Math.max(0, this.currentHealth - damage);
|
||||
this.lastDamageTime = Date.now();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置无敌状态
|
||||
*/
|
||||
setInvincible(duration: number) {
|
||||
this.invincible = true;
|
||||
this.invincibleDuration = duration;
|
||||
}
|
||||
|
||||
/**
|
||||
* 重置到满血状态
|
||||
*/
|
||||
reset() {
|
||||
this.currentHealth = this.maxHealth;
|
||||
this.invincible = false;
|
||||
this.invincibleDuration = 0;
|
||||
this.lastDamageTime = 0;
|
||||
}
|
||||
}
|
||||
@@ -1,124 +0,0 @@
|
||||
import { Component } from '@esengine/ecs-framework';
|
||||
import { Vec2 } from 'cc';
|
||||
|
||||
/**
|
||||
* 玩家输入组件 - 存储玩家的输入状态
|
||||
*
|
||||
* 标记组件示例:
|
||||
* 1. 标识这是一个玩家控制的实体
|
||||
* 2. 存储输入状态数据
|
||||
* 3. 输入处理逻辑在InputSystem中实现
|
||||
*/
|
||||
export class PlayerInputComponent extends Component {
|
||||
/** 移动输入方向(-1到1) */
|
||||
public moveDirection: Vec2 = new Vec2();
|
||||
/** 按键状态 */
|
||||
public keys: { [key: string]: boolean } = {};
|
||||
/** 鼠标位置 */
|
||||
public mousePosition: Vec2 = new Vec2();
|
||||
/** 鼠标按键状态 */
|
||||
public mouseButtons: { left: boolean; right: boolean; middle: boolean } = {
|
||||
left: false,
|
||||
right: false,
|
||||
middle: false
|
||||
};
|
||||
|
||||
/** 是否启用输入 */
|
||||
public inputEnabled: boolean = true;
|
||||
/** 输入敏感度 */
|
||||
public sensitivity: number = 1.0;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置移动方向
|
||||
*/
|
||||
setMoveDirection(x: number, y: number) {
|
||||
this.moveDirection.set(x, y);
|
||||
// 标准化方向向量(对角线移动不应该更快)
|
||||
if (this.moveDirection.lengthSqr() > 1) {
|
||||
this.moveDirection.normalize();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置按键状态
|
||||
*/
|
||||
setKey(key: string, pressed: boolean) {
|
||||
this.keys[key] = pressed;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查按键是否按下
|
||||
*/
|
||||
isKeyPressed(key: string): boolean {
|
||||
return this.keys[key] || false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否有移动输入
|
||||
*/
|
||||
hasMovementInput(): boolean {
|
||||
return this.moveDirection.lengthSqr() > 0.01;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取标准化的移动方向
|
||||
*/
|
||||
getNormalizedMoveDirection(): Vec2 {
|
||||
const result = new Vec2(this.moveDirection);
|
||||
if (result.lengthSqr() > 0) {
|
||||
result.normalize();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置鼠标位置
|
||||
*/
|
||||
setMousePosition(x: number, y: number) {
|
||||
this.mousePosition.set(x, y);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置鼠标按键状态
|
||||
*/
|
||||
setMouseButton(button: 'left' | 'right' | 'middle', pressed: boolean) {
|
||||
this.mouseButtons[button] = pressed;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查鼠标按键是否按下
|
||||
*/
|
||||
isMouseButtonPressed(button: 'left' | 'right' | 'middle'): boolean {
|
||||
return this.mouseButtons[button];
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除所有输入状态
|
||||
*/
|
||||
clearInput() {
|
||||
this.moveDirection.set(0, 0);
|
||||
this.keys = {};
|
||||
this.mouseButtons.left = false;
|
||||
this.mouseButtons.right = false;
|
||||
this.mouseButtons.middle = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 禁用输入
|
||||
*/
|
||||
disableInput() {
|
||||
this.inputEnabled = false;
|
||||
this.clearInput();
|
||||
}
|
||||
|
||||
/**
|
||||
* 启用输入
|
||||
*/
|
||||
enableInput() {
|
||||
this.inputEnabled = true;
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
{
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "ab10dc4c-c8a3-4fd2-83d6-433d4195966b",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
{
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "e6ee57d6-d0eb-43f2-a601-9b7a2812de66",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
@@ -1,94 +0,0 @@
|
||||
import { Component } from '@esengine/ecs-framework';
|
||||
import { Vec3 } from 'cc';
|
||||
|
||||
/**
|
||||
* 速度组件 - 存储实体的运动速度信息
|
||||
*
|
||||
* 设计原则展示:
|
||||
* 1. 与PositionComponent分离:遵循单一职责原则
|
||||
* 2. 包含速度限制:避免无限加速
|
||||
* 3. 提供常用的速度操作方法
|
||||
*/
|
||||
export class VelocityComponent extends Component {
|
||||
/** 当前速度向量 */
|
||||
public velocity: Vec3 = new Vec3();
|
||||
/** 最大速度限制 */
|
||||
public maxSpeed: number = 100;
|
||||
/** 阻尼系数(0-1,1为无阻尼) */
|
||||
public damping: number = 1.0;
|
||||
|
||||
constructor(x: number = 0, y: number = 0, z: number = 0, maxSpeed: number = 100) {
|
||||
super();
|
||||
this.velocity.set(x, y, z);
|
||||
this.maxSpeed = maxSpeed;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置速度
|
||||
*/
|
||||
setVelocity(x: number, y: number, z: number = 0) {
|
||||
this.velocity.set(x, y, z);
|
||||
this.clampToMaxSpeed();
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加速度(加速度效果)
|
||||
*/
|
||||
addVelocity(x: number, y: number, z: number = 0) {
|
||||
this.velocity.x += x;
|
||||
this.velocity.y += y;
|
||||
this.velocity.z += z;
|
||||
this.clampToMaxSpeed();
|
||||
}
|
||||
|
||||
/**
|
||||
* 应用阻尼
|
||||
*/
|
||||
applyDamping(deltaTime: number) {
|
||||
if (this.damping < 1.0) {
|
||||
const dampingFactor = Math.pow(this.damping, deltaTime);
|
||||
this.velocity.multiplyScalar(dampingFactor);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 限制速度不超过最大值
|
||||
*/
|
||||
private clampToMaxSpeed() {
|
||||
const speed = this.velocity.length();
|
||||
if (speed > this.maxSpeed) {
|
||||
this.velocity.normalize();
|
||||
this.velocity.multiplyScalar(this.maxSpeed);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前速度大小
|
||||
*/
|
||||
getSpeed(): number {
|
||||
return this.velocity.length();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取速度方向(单位向量)
|
||||
*/
|
||||
getDirection(): Vec3 {
|
||||
const result = new Vec3();
|
||||
Vec3.normalize(result, this.velocity);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 停止移动
|
||||
*/
|
||||
stop() {
|
||||
this.velocity.set(0, 0, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否在移动
|
||||
*/
|
||||
isMoving(): boolean {
|
||||
return this.velocity.lengthSqr() > 0.01; // 避免浮点数精度问题
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
{
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "784d7c28-2b72-427c-8b04-da0fcf775acf",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
{
|
||||
"ver": "1.2.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "6a2d6231-acf9-47b8-a020-d45a7433a95d",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
{
|
||||
"ver": "1.2.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "80dcbaf5-21f7-4bb1-aff4-2cdbb0b5d364",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
{
|
||||
"ver": "1.2.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "879b4e07-dd6b-4445-adb2-a970b97c6d6f",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
@@ -1,316 +0,0 @@
|
||||
import { Scene } from '@esengine/ecs-framework';
|
||||
import { Entity } from '@esengine/ecs-framework';
|
||||
|
||||
// 导入组件
|
||||
import { PositionComponent } from '../components/PositionComponent';
|
||||
import { VelocityComponent } from '../components/VelocityComponent';
|
||||
import { HealthComponent } from '../components/HealthComponent';
|
||||
import { PlayerInputComponent } from '../components/PlayerInputComponent';
|
||||
|
||||
// 导入系统
|
||||
import { MovementSystem } from '../systems/MovementSystem';
|
||||
import { PlayerInputSystem } from '../systems/PlayerInputSystem';
|
||||
import { HealthSystem } from '../systems/HealthSystem';
|
||||
|
||||
/**
|
||||
* 示例游戏场景 - 完整的ECS应用示例
|
||||
*
|
||||
* 这个场景展示了:
|
||||
* 1. 如何创建和配置各种实体
|
||||
* 2. 如何添加和组织系统
|
||||
* 3. 如何实现完整的游戏逻辑
|
||||
* 4. 如何进行调试和监控
|
||||
*/
|
||||
export class ExampleGameScene extends Scene {
|
||||
// 场景中的重要实体引用
|
||||
private player: Entity | null;
|
||||
private enemies: Entity[];
|
||||
private gameConfig: {
|
||||
maxEnemies: number;
|
||||
enemySpawnInterval: number;
|
||||
gameArea: { width: number; height: number };
|
||||
};
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
// 在构造函数中初始化属性
|
||||
this.player = null;
|
||||
this.enemies = [];
|
||||
this.gameConfig = {
|
||||
maxEnemies: 5,
|
||||
enemySpawnInterval: 3000, // 3秒生成一个敌人
|
||||
gameArea: { width: 800, height: 600 }
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 场景初始化(构造时调用)
|
||||
*/
|
||||
public initialize(): void {
|
||||
super.initialize();
|
||||
this.name = "ExampleGameScene";
|
||||
console.log("📋 ExampleGameScene 构造完成");
|
||||
}
|
||||
|
||||
/**
|
||||
* 场景开始时的回调(所有构造函数执行完毕后调用)
|
||||
*/
|
||||
public onStart(): void {
|
||||
super.onStart();
|
||||
|
||||
console.log("🎮 开始初始化示例游戏场景...");
|
||||
|
||||
// 1. 添加系统(注意顺序很重要)
|
||||
this.setupSystems();
|
||||
|
||||
// 2. 创建游戏实体
|
||||
this.createGameEntities();
|
||||
|
||||
// 3. 设置定时器和事件
|
||||
this.setupGameLogic();
|
||||
|
||||
console.log("✅ 示例游戏场景初始化完成!");
|
||||
this.printSceneInfo();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置游戏系统
|
||||
*/
|
||||
private setupSystems(): void {
|
||||
console.log("🔧 添加游戏系统...");
|
||||
|
||||
// 输入系统(最先处理输入)
|
||||
this.addEntityProcessor(new PlayerInputSystem());
|
||||
|
||||
// 移动系统(处理所有移动逻辑)
|
||||
this.addEntityProcessor(new MovementSystem());
|
||||
|
||||
// 生命值系统(处理生命值、死亡等)
|
||||
this.addEntityProcessor(new HealthSystem());
|
||||
|
||||
console.log("✅ 系统添加完成");
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建游戏实体
|
||||
*/
|
||||
private createGameEntities(): void {
|
||||
console.log("🏗️ 创建游戏实体...");
|
||||
|
||||
// 创建玩家
|
||||
this.createPlayer();
|
||||
|
||||
// 创建初始敌人
|
||||
this.createInitialEnemies();
|
||||
|
||||
// 创建环境实体(可选)
|
||||
this.createEnvironmentEntities();
|
||||
|
||||
console.log("✅ 实体创建完成");
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建玩家实体
|
||||
*/
|
||||
private createPlayer(): void {
|
||||
this.player = this.createEntity("Player");
|
||||
|
||||
// 添加玩家组件
|
||||
this.player.addComponent(new PositionComponent(0, 0, 0));
|
||||
this.player.addComponent(new VelocityComponent(0, 0, 0, 250)); // 最大速度250
|
||||
this.player.addComponent(new HealthComponent(100, 5)); // 100血,每秒回5血
|
||||
this.player.addComponent(new PlayerInputComponent());
|
||||
|
||||
console.log("🎯 玩家创建完成 - 使用WASD或方向键移动,空格键攻击");
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建初始敌人
|
||||
*/
|
||||
private createInitialEnemies(): void {
|
||||
for (let i = 0; i < 3; i++) {
|
||||
this.createEnemy(i);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建单个敌人
|
||||
*/
|
||||
private createEnemy(index: number): Entity {
|
||||
const enemy = this.createEntity(`Enemy_${index}`);
|
||||
|
||||
// 随机位置
|
||||
const x = (Math.random() - 0.5) * this.gameConfig.gameArea.width;
|
||||
const y = (Math.random() - 0.5) * this.gameConfig.gameArea.height;
|
||||
|
||||
// 随机速度
|
||||
const velocityX = (Math.random() - 0.5) * 100;
|
||||
const velocityY = (Math.random() - 0.5) * 100;
|
||||
|
||||
// 添加敌人组件
|
||||
enemy.addComponent(new PositionComponent(x, y, 0));
|
||||
enemy.addComponent(new VelocityComponent(velocityX, velocityY, 0, 150));
|
||||
enemy.addComponent(new HealthComponent(50, 0)); // 50血,不回血
|
||||
|
||||
// 添加到敌人列表
|
||||
this.enemies.push(enemy);
|
||||
|
||||
return enemy;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建环境实体(演示不同类型的实体)
|
||||
*/
|
||||
private createEnvironmentEntities(): void {
|
||||
// 创建一些静态的环境对象
|
||||
for (let i = 0; i < 5; i++) {
|
||||
const obstacle = this.createEntity(`Obstacle_${i}`);
|
||||
|
||||
const x = (Math.random() - 0.5) * this.gameConfig.gameArea.width * 0.8;
|
||||
const y = (Math.random() - 0.5) * this.gameConfig.gameArea.height * 0.8;
|
||||
|
||||
// 只有位置,没有速度和生命值
|
||||
obstacle.addComponent(new PositionComponent(x, y, 0));
|
||||
}
|
||||
|
||||
console.log("🌲 环境实体创建完成");
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置游戏逻辑和定时器
|
||||
*/
|
||||
private setupGameLogic(): void {
|
||||
console.log("⚙️ 设置游戏逻辑...");
|
||||
|
||||
// 敌人生成定时器
|
||||
this.setupEnemySpawner();
|
||||
|
||||
// 游戏状态监控
|
||||
this.setupGameMonitoring();
|
||||
|
||||
console.log("✅ 游戏逻辑设置完成");
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置敌人生成器
|
||||
*/
|
||||
private setupEnemySpawner(): void {
|
||||
setInterval(() => {
|
||||
if (this.enemies.length < this.gameConfig.maxEnemies) {
|
||||
const newEnemy = this.createEnemy(this.enemies.length);
|
||||
}
|
||||
}, this.gameConfig.enemySpawnInterval);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置游戏监控
|
||||
*/
|
||||
private setupGameMonitoring(): void {
|
||||
// 每10秒清理已死亡的敌人引用
|
||||
setInterval(() => {
|
||||
this.cleanupDeadEnemies();
|
||||
}, 10000);
|
||||
}
|
||||
|
||||
/**
|
||||
* 打印游戏状态(按需调用)
|
||||
*/
|
||||
private printGameStatus(): void {
|
||||
const totalEntities = this.entities.count;
|
||||
const aliveEnemies = this.enemies.filter(e => !e.isDestroyed).length;
|
||||
|
||||
console.log("📊 游戏状态报告:");
|
||||
console.log(` - 总实体数: ${totalEntities}`);
|
||||
console.log(` - 存活敌人: ${aliveEnemies}`);
|
||||
|
||||
if (this.player && !this.player.isDestroyed) {
|
||||
const playerHealth = this.player.getComponent(HealthComponent);
|
||||
const playerPos = this.player.getComponent(PositionComponent);
|
||||
console.log(` - 玩家生命值: ${playerHealth?.currentHealth}/${playerHealth?.maxHealth}`);
|
||||
console.log(` - 玩家位置: (${playerPos?.position.x.toFixed(1)}, ${playerPos?.position.y.toFixed(1)})`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 清理已死亡的敌人引用
|
||||
*/
|
||||
private cleanupDeadEnemies(): void {
|
||||
const initialCount = this.enemies.length;
|
||||
this.enemies = this.enemies.filter(enemy => !enemy.isDestroyed);
|
||||
const removedCount = initialCount - this.enemies.length;
|
||||
|
||||
if (removedCount > 0) {
|
||||
console.log(`🧹 清理了 ${removedCount} 个已死亡的敌人引用`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 打印场景信息
|
||||
*/
|
||||
private printSceneInfo(): void {
|
||||
console.log("\n📋 场景信息:");
|
||||
console.log(` 场景名: ${this.name}`);
|
||||
console.log(` 实体数: ${this.entities.count}`);
|
||||
console.log(` 系统数: ${this.entityProcessors.count}`);
|
||||
console.log(` 玩家: ${this.player?.name || '未创建'}`);
|
||||
console.log(` 敌人: ${this.enemies.length} 个`);
|
||||
console.log("\n🎮 控制说明:");
|
||||
console.log(" - WASD 或 方向键: 移动");
|
||||
console.log(" - 空格: 攻击/行动");
|
||||
console.log(" - ESC: 暂停");
|
||||
console.log("\n💡 学习要点:");
|
||||
console.log(" 1. 观察控制台输出,了解ECS运行过程");
|
||||
console.log(" 2. 打开调试面板查看性能数据");
|
||||
console.log(" 3. 尝试修改组件参数观察变化");
|
||||
console.log(" 4. 查看代码学习ECS设计模式\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取玩家实体(供其他系统使用)
|
||||
*/
|
||||
public getPlayer(): Entity | null {
|
||||
return this.player;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有敌人(供其他系统使用)
|
||||
*/
|
||||
public getEnemies(): Entity[] {
|
||||
return this.enemies.filter(enemy => !enemy.isDestroyed);
|
||||
}
|
||||
|
||||
/**
|
||||
* 游戏重置方法
|
||||
*/
|
||||
public resetGame(): void {
|
||||
console.log("🔄 重置游戏...");
|
||||
|
||||
// 销毁所有实体
|
||||
if (this.player) {
|
||||
this.player.destroy();
|
||||
this.player = null;
|
||||
}
|
||||
|
||||
this.enemies.forEach(enemy => enemy.destroy());
|
||||
this.enemies = [];
|
||||
|
||||
// 重新创建实体
|
||||
this.createGameEntities();
|
||||
|
||||
console.log("✅ 游戏重置完成");
|
||||
}
|
||||
|
||||
/**
|
||||
* 场景卸载时调用
|
||||
*/
|
||||
public unload(): void {
|
||||
console.log("🧹 清理示例游戏场景...");
|
||||
|
||||
// 清理引用
|
||||
this.player = null;
|
||||
this.enemies = [];
|
||||
|
||||
super.unload();
|
||||
console.log("✅ 场景清理完成");
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
{
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "da87facc-89a0-47da-a0ef-423255200a51",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
import { Scene } from '@esengine/ecs-framework';
|
||||
|
||||
/**
|
||||
* 游戏场景
|
||||
*
|
||||
* 这是您的主游戏场景。在这里可以:
|
||||
* - 添加游戏系统
|
||||
* - 创建初始实体
|
||||
* - 设置场景参数
|
||||
*/
|
||||
export class GameScene extends Scene {
|
||||
|
||||
/**
|
||||
* 场景初始化
|
||||
* 在场景创建时调用,用于设置基础配置
|
||||
*/
|
||||
public initialize(): void {
|
||||
super.initialize();
|
||||
|
||||
// 设置场景名称
|
||||
this.name = "MainGameScene";
|
||||
|
||||
console.log('🎯 游戏场景已创建');
|
||||
|
||||
// TODO: 在这里添加您的游戏系统
|
||||
// 例如:this.addEntityProcessor(new MovementSystem());
|
||||
|
||||
// TODO: 在这里创建初始实体
|
||||
// 例如:this.createEntity("Player");
|
||||
}
|
||||
|
||||
/**
|
||||
* 场景开始运行
|
||||
* 在场景开始时调用,用于执行启动逻辑
|
||||
*/
|
||||
public onStart(): void {
|
||||
super.onStart();
|
||||
|
||||
console.log('🚀 游戏场景已启动');
|
||||
|
||||
// TODO: 在这里添加场景启动逻辑
|
||||
// 例如:创建UI、播放音乐、初始化游戏状态等
|
||||
}
|
||||
|
||||
/**
|
||||
* 场景卸载
|
||||
* 在场景结束时调用,用于清理资源
|
||||
*/
|
||||
public unload(): void {
|
||||
console.log('🛑 游戏场景已结束');
|
||||
|
||||
// TODO: 在这里添加清理逻辑
|
||||
// 例如:清理缓存、释放资源等
|
||||
|
||||
super.unload();
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
{
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "8fee85be-2224-4200-a898-d3ae2406fb1d",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
{
|
||||
"ver": "1.2.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "a5e3a8c9-3d0b-4a36-9d20-6f70f1380131",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
@@ -1,228 +0,0 @@
|
||||
import { EntitySystem, Entity, Matcher, Time } from '@esengine/ecs-framework';
|
||||
import { HealthComponent } from '../components/HealthComponent';
|
||||
|
||||
/**
|
||||
* 生命值系统 - 处理生命值相关的逻辑
|
||||
*
|
||||
* 展示生命值管理:
|
||||
* 1. 自动回血
|
||||
* 2. 无敌状态管理
|
||||
* 3. 死亡处理
|
||||
* 4. 事件触发
|
||||
*/
|
||||
export class HealthSystem extends EntitySystem {
|
||||
/** 回血延迟时间(受伤后多久开始回血,毫秒) */
|
||||
private regenDelay: number = 3000;
|
||||
|
||||
constructor() {
|
||||
// 只处理拥有HealthComponent的实体
|
||||
super(Matcher.empty().all(HealthComponent));
|
||||
}
|
||||
|
||||
public initialize(): void {
|
||||
super.initialize();
|
||||
console.log("HealthSystem 已初始化 - 开始处理生命值逻辑");
|
||||
}
|
||||
|
||||
/**
|
||||
* 每帧处理:更新生命值相关逻辑
|
||||
*/
|
||||
protected process(entities: Entity[]): void {
|
||||
for (const entity of entities) {
|
||||
const health = entity.getComponent(HealthComponent);
|
||||
|
||||
// 处理无敌状态
|
||||
this.processInvincibility(health);
|
||||
|
||||
// 处理生命值回复
|
||||
this.processHealthRegeneration(entity, health);
|
||||
|
||||
// 检查死亡状态
|
||||
this.checkDeathStatus(entity, health);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理无敌状态
|
||||
*/
|
||||
private processInvincibility(health: HealthComponent): void {
|
||||
if (health.invincible && health.invincibleDuration > 0) {
|
||||
health.invincibleDuration -= Time.deltaTime;
|
||||
|
||||
// 无敌时间结束
|
||||
if (health.invincibleDuration <= 0) {
|
||||
health.invincible = false;
|
||||
health.invincibleDuration = 0;
|
||||
console.log("无敌状态结束");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理生命值回复
|
||||
*/
|
||||
private processHealthRegeneration(entity: Entity, health: HealthComponent): void {
|
||||
// 如果已经满血或者没有回复速度,则不处理
|
||||
if (health.isFullHealth() || health.regenRate <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 检查是否超过了回血延迟时间
|
||||
const currentTime = Date.now();
|
||||
if (currentTime - health.lastDamageTime < this.regenDelay) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 计算回血量
|
||||
const regenAmount = health.regenRate * Time.deltaTime;
|
||||
const oldHealth = health.currentHealth;
|
||||
|
||||
// 执行回血
|
||||
health.heal(regenAmount);
|
||||
|
||||
// 如果实际回了血,输出日志
|
||||
if (health.currentHealth > oldHealth) {
|
||||
console.log(`${entity.name} 回血: ${oldHealth.toFixed(1)} -> ${health.currentHealth.toFixed(1)} (${health.getHealthPercentage() * 100}%)`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查死亡状态
|
||||
*/
|
||||
private checkDeathStatus(entity: Entity, health: HealthComponent): void {
|
||||
if (health.isDead()) {
|
||||
this.handleEntityDeath(entity, health);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理实体死亡
|
||||
*/
|
||||
private handleEntityDeath(entity: Entity, health: HealthComponent): void {
|
||||
console.log(`💀 ${entity.name} 已死亡!`);
|
||||
|
||||
// 触发死亡事件(如果有事件系统)
|
||||
this.triggerDeathEvent(entity);
|
||||
|
||||
// 可以在这里添加死亡效果、掉落物品等逻辑
|
||||
this.createDeathEffect(entity);
|
||||
|
||||
// 标记实体为死亡状态(而不是立即销毁)
|
||||
// 这样其他系统可以处理死亡相关的逻辑
|
||||
entity.addComponent(new DeadMarkerComponent());
|
||||
|
||||
// 可选:延迟销毁实体
|
||||
setTimeout(() => {
|
||||
if (entity && !entity.isDestroyed) {
|
||||
entity.destroy();
|
||||
console.log(`${entity.name} 已被销毁`);
|
||||
}
|
||||
}, 1000); // 1秒后销毁
|
||||
}
|
||||
|
||||
/**
|
||||
* 触发死亡事件
|
||||
*/
|
||||
private triggerDeathEvent(entity: Entity): void {
|
||||
// 如果项目中有事件系统,可以在这里发送死亡事件
|
||||
console.log(`触发死亡事件: ${entity.name}`);
|
||||
|
||||
// 示例事件数据
|
||||
const deathEventData = {
|
||||
entityId: entity.id,
|
||||
entityName: entity.name,
|
||||
deathTime: Date.now(),
|
||||
position: this.getEntityPosition(entity)
|
||||
};
|
||||
|
||||
// 这里可以调用事件系统发送事件
|
||||
// eventBus.emit('entity:died', deathEventData);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建死亡效果
|
||||
*/
|
||||
private createDeathEffect(entity: Entity): void {
|
||||
console.log(`💥 为 ${entity.name} 创建死亡效果`);
|
||||
|
||||
// 在实际游戏中,这里可能会:
|
||||
// 1. 播放死亡动画
|
||||
// 2. 播放死亡音效
|
||||
// 3. 创建粒子效果
|
||||
// 4. 掉落物品
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取实体位置(辅助方法)
|
||||
*/
|
||||
private getEntityPosition(entity: Entity): { x: number; y: number; z: number } {
|
||||
// 尝试获取位置组件
|
||||
const position = entity.getComponent(PositionComponent);
|
||||
if (position) {
|
||||
return {
|
||||
x: position.position.x,
|
||||
y: position.position.y,
|
||||
z: position.position.z
|
||||
};
|
||||
}
|
||||
|
||||
return { x: 0, y: 0, z: 0 };
|
||||
}
|
||||
|
||||
/**
|
||||
* 公共方法:对实体造成伤害
|
||||
* 这个方法可以被其他系统调用
|
||||
*/
|
||||
public damageEntity(entity: Entity, damage: number, source?: Entity): boolean {
|
||||
const health = entity.getComponent(HealthComponent);
|
||||
if (!health || health.invincible) {
|
||||
return false; // 无生命值组件或处于无敌状态
|
||||
}
|
||||
|
||||
const oldHealth = health.currentHealth;
|
||||
health.takeDamage(damage);
|
||||
|
||||
console.log(`⚔️ ${entity.name} 受到 ${damage} 点伤害: ${oldHealth.toFixed(1)} -> ${health.currentHealth.toFixed(1)}`);
|
||||
|
||||
// 如果有伤害来源,可以记录或处理
|
||||
if (source) {
|
||||
console.log(`伤害来源: ${source.name}`);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 公共方法:治疗实体
|
||||
*/
|
||||
public healEntity(entity: Entity, healAmount: number): boolean {
|
||||
const health = entity.getComponent(HealthComponent);
|
||||
if (!health || health.isFullHealth()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const oldHealth = health.currentHealth;
|
||||
health.heal(healAmount);
|
||||
|
||||
console.log(`💚 ${entity.name} 恢复 ${healAmount} 点生命值: ${oldHealth.toFixed(1)} -> ${health.currentHealth.toFixed(1)}`);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 死亡标记组件 - 标记已死亡的实体
|
||||
* 这是一个简单的标记组件,用于标识死亡状态
|
||||
*/
|
||||
class DeadMarkerComponent extends Component {
|
||||
public deathTime: number;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
this.deathTime = Date.now();
|
||||
}
|
||||
}
|
||||
|
||||
// 导入位置组件(用于获取实体位置)
|
||||
import { PositionComponent } from '../components/PositionComponent';
|
||||
import { Component } from '@esengine/ecs-framework';
|
||||
@@ -1,9 +0,0 @@
|
||||
{
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "455c12d1-52a8-41ac-b1b5-0d2b93c079aa",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
@@ -1,99 +0,0 @@
|
||||
import { EntitySystem, Entity, Matcher, Time } from '@esengine/ecs-framework';
|
||||
import { PositionComponent } from '../components/PositionComponent';
|
||||
import { VelocityComponent } from '../components/VelocityComponent';
|
||||
|
||||
/**
|
||||
* 移动系统 - 处理实体的移动逻辑
|
||||
*
|
||||
* EntitySystem示例:
|
||||
* 1. 使用Matcher指定需要的组件(Position + Velocity)
|
||||
* 2. 每帧更新所有移动实体的位置
|
||||
* 3. 展示组件间的协作
|
||||
*/
|
||||
export class MovementSystem extends EntitySystem {
|
||||
constructor() {
|
||||
// 只处理同时拥有PositionComponent和VelocityComponent的实体
|
||||
super(Matcher.empty().all(PositionComponent, VelocityComponent));
|
||||
}
|
||||
|
||||
/**
|
||||
* 每帧执行:更新所有移动实体的位置
|
||||
*/
|
||||
protected process(entities: Entity[]): void {
|
||||
for (const entity of entities) {
|
||||
const position = entity.getComponent(PositionComponent);
|
||||
const velocity = entity.getComponent(VelocityComponent);
|
||||
|
||||
// 基本移动:位置 = 当前位置 + 速度 * 时间
|
||||
position.move(
|
||||
velocity.velocity.x * Time.deltaTime,
|
||||
velocity.velocity.y * Time.deltaTime,
|
||||
velocity.velocity.z * Time.deltaTime
|
||||
);
|
||||
|
||||
// 应用阻尼(摩擦力)
|
||||
velocity.applyDamping(Time.deltaTime);
|
||||
|
||||
// 可选:添加边界检查
|
||||
this.checkBoundaries(position, velocity);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 边界检查(可选功能)
|
||||
* 这个方法演示了如何在系统中实现额外的游戏逻辑
|
||||
*/
|
||||
private checkBoundaries(position: PositionComponent, velocity: VelocityComponent) {
|
||||
const bounds = {
|
||||
left: -400,
|
||||
right: 400,
|
||||
top: 300,
|
||||
bottom: -300
|
||||
};
|
||||
|
||||
// 检查X轴边界
|
||||
if (position.position.x < bounds.left) {
|
||||
position.position.x = bounds.left;
|
||||
velocity.velocity.x = Math.abs(velocity.velocity.x); // 反弹
|
||||
} else if (position.position.x > bounds.right) {
|
||||
position.position.x = bounds.right;
|
||||
velocity.velocity.x = -Math.abs(velocity.velocity.x); // 反弹
|
||||
}
|
||||
|
||||
// 检查Y轴边界
|
||||
if (position.position.y < bounds.bottom) {
|
||||
position.position.y = bounds.bottom;
|
||||
velocity.velocity.y = Math.abs(velocity.velocity.y); // 反弹
|
||||
} else if (position.position.y > bounds.top) {
|
||||
position.position.y = bounds.top;
|
||||
velocity.velocity.y = -Math.abs(velocity.velocity.y); // 反弹
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 系统初始化时调用
|
||||
* 可以在这里设置系统级别的配置
|
||||
*/
|
||||
public initialize(): void {
|
||||
super.initialize();
|
||||
console.log("MovementSystem 已初始化 - 开始处理实体移动");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取系统统计信息(用于调试)
|
||||
*/
|
||||
public getStats(): { processedEntities: number; totalMovement: number } {
|
||||
let totalMovement = 0;
|
||||
const entities = this.entities;
|
||||
|
||||
for (const entity of entities) {
|
||||
const position = entity.getComponent(PositionComponent);
|
||||
totalMovement += position.getMovementDistance();
|
||||
}
|
||||
|
||||
return {
|
||||
processedEntities: entities.length,
|
||||
totalMovement: totalMovement
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
{
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "a8712467-efe0-46ec-a246-a9fa07d203d9",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
@@ -1,173 +0,0 @@
|
||||
import { EntitySystem, Entity, Matcher } from '@esengine/ecs-framework';
|
||||
import { PlayerInputComponent } from '../components/PlayerInputComponent';
|
||||
import { VelocityComponent } from '../components/VelocityComponent';
|
||||
import { input, Input, EventKeyboard, KeyCode } from 'cc';
|
||||
|
||||
/**
|
||||
* 玩家输入系统 - 处理玩家输入并转换为游戏行为
|
||||
*
|
||||
* 展示系统的职责:
|
||||
* 1. 收集输入事件
|
||||
* 2. 更新输入组件状态
|
||||
* 3. 根据输入修改其他组件(如速度)
|
||||
*/
|
||||
export class PlayerInputSystem extends EntitySystem {
|
||||
private moveSpeed: number = 200; // 移动速度
|
||||
|
||||
constructor() {
|
||||
// 只处理拥有PlayerInputComponent的实体
|
||||
super(Matcher.empty().all(PlayerInputComponent));
|
||||
}
|
||||
|
||||
public initialize(): void {
|
||||
super.initialize();
|
||||
console.log("PlayerInputSystem 已初始化 - 开始监听玩家输入");
|
||||
|
||||
// 注册键盘事件监听器
|
||||
this.setupInputListeners();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置输入事件监听器
|
||||
*/
|
||||
private setupInputListeners(): void {
|
||||
// 键盘按下事件
|
||||
input.on(Input.EventType.KEY_DOWN, this.onKeyDown, this);
|
||||
// 键盘抬起事件
|
||||
input.on(Input.EventType.KEY_UP, this.onKeyUp, this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 键盘按下处理
|
||||
*/
|
||||
private onKeyDown(event: EventKeyboard): void {
|
||||
const keyCode = event.keyCode;
|
||||
const keyName = this.getKeyName(keyCode);
|
||||
|
||||
// 更新所有玩家实体的输入状态
|
||||
for (const entity of this.entities) {
|
||||
const playerInput = entity.getComponent(PlayerInputComponent);
|
||||
if (playerInput && playerInput.inputEnabled) {
|
||||
playerInput.setKey(keyName, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 键盘抬起处理
|
||||
*/
|
||||
private onKeyUp(event: EventKeyboard): void {
|
||||
const keyCode = event.keyCode;
|
||||
const keyName = this.getKeyName(keyCode);
|
||||
|
||||
// 更新所有玩家实体的输入状态
|
||||
for (const entity of this.entities) {
|
||||
const playerInput = entity.getComponent(PlayerInputComponent);
|
||||
if (playerInput && playerInput.inputEnabled) {
|
||||
playerInput.setKey(keyName, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 每帧处理:根据输入状态更新实体行为
|
||||
*/
|
||||
protected process(entities: Entity[]): void {
|
||||
for (const entity of entities) {
|
||||
const playerInput = entity.getComponent(PlayerInputComponent);
|
||||
|
||||
if (!playerInput || !playerInput.inputEnabled) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// 处理移动输入
|
||||
this.processMovementInput(entity, playerInput);
|
||||
|
||||
// 处理其他输入(如攻击、跳跃等)
|
||||
this.processActionInput(entity, playerInput);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理移动输入
|
||||
*/
|
||||
private processMovementInput(entity: Entity, playerInput: PlayerInputComponent): void {
|
||||
const velocity = entity.getComponent(VelocityComponent);
|
||||
if (!velocity) return;
|
||||
|
||||
// 根据按键状态计算移动方向
|
||||
let moveX = 0;
|
||||
let moveY = 0;
|
||||
|
||||
if (playerInput.isKeyPressed('A') || playerInput.isKeyPressed('ArrowLeft')) {
|
||||
moveX -= 1;
|
||||
}
|
||||
if (playerInput.isKeyPressed('D') || playerInput.isKeyPressed('ArrowRight')) {
|
||||
moveX += 1;
|
||||
}
|
||||
if (playerInput.isKeyPressed('W') || playerInput.isKeyPressed('ArrowUp')) {
|
||||
moveY += 1;
|
||||
}
|
||||
if (playerInput.isKeyPressed('S') || playerInput.isKeyPressed('ArrowDown')) {
|
||||
moveY -= 1;
|
||||
}
|
||||
|
||||
// 更新输入组件的移动方向
|
||||
playerInput.setMoveDirection(moveX, moveY);
|
||||
|
||||
// 将输入转换为速度
|
||||
const normalizedDirection = playerInput.getNormalizedMoveDirection();
|
||||
velocity.setVelocity(
|
||||
normalizedDirection.x * this.moveSpeed * playerInput.sensitivity,
|
||||
normalizedDirection.y * this.moveSpeed * playerInput.sensitivity,
|
||||
0
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理动作输入(攻击、技能等)
|
||||
*/
|
||||
private processActionInput(entity: Entity, playerInput: PlayerInputComponent): void {
|
||||
// 空格键 - 跳跃或攻击
|
||||
if (playerInput.isKeyPressed('Space')) {
|
||||
console.log(`玩家 ${entity.name} 执行动作:攻击/跳跃`);
|
||||
// 这里可以触发攻击组件或添加跳跃效果
|
||||
}
|
||||
|
||||
// ESC键 - 暂停游戏
|
||||
if (playerInput.isKeyPressed('Escape')) {
|
||||
console.log("玩家请求暂停游戏");
|
||||
// 可以发送暂停事件给游戏管理系统
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将键码转换为字符串
|
||||
*/
|
||||
private getKeyName(keyCode: KeyCode): string {
|
||||
const keyMap: { [key: number]: string } = {
|
||||
[KeyCode.KEY_A]: 'A',
|
||||
[KeyCode.KEY_D]: 'D',
|
||||
[KeyCode.KEY_S]: 'S',
|
||||
[KeyCode.KEY_W]: 'W',
|
||||
[KeyCode.ARROW_LEFT]: 'ArrowLeft',
|
||||
[KeyCode.ARROW_RIGHT]: 'ArrowRight',
|
||||
[KeyCode.ARROW_UP]: 'ArrowUp',
|
||||
[KeyCode.ARROW_DOWN]: 'ArrowDown',
|
||||
[KeyCode.SPACE]: 'Space',
|
||||
[KeyCode.ESCAPE]: 'Escape'
|
||||
};
|
||||
|
||||
return keyMap[keyCode] || `Key_${keyCode}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 系统清理
|
||||
*/
|
||||
public onDestroy(): void {
|
||||
// 移除事件监听器
|
||||
input.off(Input.EventType.KEY_DOWN, this.onKeyDown, this);
|
||||
input.off(Input.EventType.KEY_UP, this.onKeyUp, this);
|
||||
console.log("PlayerInputSystem 已清理");
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
{
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "7b69a39f-926a-4260-94ba-e15e31b324b5",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
Reference in New Issue
Block a user