更新rts示例代码(矿工自动采矿)
This commit is contained in:
@@ -88,11 +88,16 @@ export class BehaviorTreeComponent extends ECSComponent {
|
||||
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);
|
||||
// 注意:只设置行为树中实际定义的变量
|
||||
// 这些变量需要在对应的.btree文件的blackboard数组中预先定义
|
||||
|
||||
// 设置基础信息 - 注释掉未在行为树中定义的变量
|
||||
// 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);
|
||||
|
||||
console.log('BehaviorTreeComponent黑板设置完成,未设置任何变量以避免警告');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -107,13 +112,14 @@ export class BehaviorTreeComponent extends ECSComponent {
|
||||
|
||||
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);
|
||||
}
|
||||
// 只更新行为树中实际定义的变量
|
||||
// this.blackboard.setValue('deltaTime', deltaTime);
|
||||
// this.blackboard.setValue('currentTime', Date.now() / 1000);
|
||||
// if (this.cocosNode) {
|
||||
// this.blackboard.setValue('worldPosition', this.cocosNode.worldPosition);
|
||||
// }
|
||||
}
|
||||
|
||||
// 执行行为树
|
||||
|
||||
@@ -1,17 +1,20 @@
|
||||
import { _decorator, Component, resources, JsonAsset } from 'cc';
|
||||
import { BehaviorTree, BehaviorTreeBuilder, Blackboard, BehaviorTreeJSONConfig } from '@esengine/ai';
|
||||
import { _decorator, Component, resources, JsonAsset, Vec3 } from 'cc';
|
||||
import { BehaviorTree, BehaviorTreeBuilder, Blackboard, BehaviorTreeJSONConfig, ExecutionContext, EventRegistry } from '@esengine/ai';
|
||||
import { UnitController } from './UnitController';
|
||||
import { RTSBehaviorHandler } from './RTSBehaviorHandler';
|
||||
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
/**
|
||||
* 执行上下文接口
|
||||
* 游戏执行上下文接口
|
||||
* 继承框架的ExecutionContext,添加游戏特定的属性
|
||||
*/
|
||||
interface GameExecutionContext {
|
||||
blackboard?: Blackboard;
|
||||
interface GameExecutionContext extends ExecutionContext {
|
||||
unitController: UnitController;
|
||||
gameObject: any;
|
||||
[key: string]: any;
|
||||
eventRegistry?: EventRegistry;
|
||||
// 确保继承索引签名
|
||||
[key: string]: unknown;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -24,16 +27,18 @@ export class BehaviorTreeManager extends Component {
|
||||
debugMode: boolean = true;
|
||||
|
||||
@property
|
||||
tickInterval: number = 0.1; // 行为树更新间隔(秒)
|
||||
tickInterval: number = 0.1; // 行为树更新间隔(秒)- 10fps更新频率,平衡性能和响应性
|
||||
|
||||
private behaviorTree: BehaviorTree<GameExecutionContext> | null = null;
|
||||
private blackboard: Blackboard | null = null;
|
||||
private context: GameExecutionContext | null = null;
|
||||
private eventRegistry: EventRegistry | null = null;
|
||||
private isLoaded: boolean = false;
|
||||
private isRunning: boolean = false;
|
||||
private lastTickTime: number = 0;
|
||||
private unitController: UnitController | null = null;
|
||||
private currentBehaviorTreeName: string = '';
|
||||
private behaviorHandler: RTSBehaviorHandler | null = null;
|
||||
|
||||
/**
|
||||
* 初始化行为树
|
||||
@@ -42,12 +47,22 @@ export class BehaviorTreeManager extends Component {
|
||||
this.currentBehaviorTreeName = behaviorTreeName;
|
||||
this.unitController = unitController;
|
||||
|
||||
// 获取RTSBehaviorHandler组件
|
||||
this.behaviorHandler = this.getComponent(RTSBehaviorHandler);
|
||||
if (!this.behaviorHandler) {
|
||||
console.error(`BehaviorTreeManager: 未找到RTSBehaviorHandler组件 - ${this.node.name}`);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await this.loadBehaviorTree(behaviorTreeName);
|
||||
this.setupBlackboard();
|
||||
this.isLoaded = true;
|
||||
this.isRunning = true;
|
||||
console.log(`行为树初始化成功: ${behaviorTreeName}`);
|
||||
console.log(`✅ 行为树初始化成功: ${behaviorTreeName} for ${this.unitController.node.name}`);
|
||||
console.log(` - 行为树: ${this.behaviorTree ? '已创建' : '未创建'}`);
|
||||
console.log(` - 黑板变量: ${this.blackboard ? '已创建' : '未创建'}`);
|
||||
console.log(` - 运行状态: ${this.isRunning ? '运行中' : '已停止'}`);
|
||||
} catch (error) {
|
||||
console.error(`行为树初始化失败: ${behaviorTreeName}`, error);
|
||||
}
|
||||
@@ -59,6 +74,7 @@ export class BehaviorTreeManager extends Component {
|
||||
private async loadBehaviorTree(behaviorTreeName: string): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const jsonPath = `${behaviorTreeName}.bt`;
|
||||
console.log(`🔍 尝试加载行为树文件: ${jsonPath}`);
|
||||
|
||||
resources.load(jsonPath, JsonAsset, (err, asset) => {
|
||||
if (err) {
|
||||
@@ -72,15 +88,17 @@ export class BehaviorTreeManager extends Component {
|
||||
|
||||
// 创建执行上下文
|
||||
this.blackboard = new Blackboard();
|
||||
this.eventRegistry = this.createEventRegistry();
|
||||
this.context = {
|
||||
blackboard: this.blackboard,
|
||||
unitController: this.unitController!,
|
||||
gameObject: this.node
|
||||
};
|
||||
gameObject: this.node,
|
||||
eventRegistry: this.eventRegistry
|
||||
} as GameExecutionContext;
|
||||
|
||||
// 从JSON数据创建行为树
|
||||
const buildResult = BehaviorTreeBuilder.fromBehaviorTreeConfig(behaviorTreeData, this.context);
|
||||
this.behaviorTree = buildResult.tree as BehaviorTree<GameExecutionContext>;
|
||||
const buildResult = BehaviorTreeBuilder.fromBehaviorTreeConfig<GameExecutionContext>(behaviorTreeData, this.context);
|
||||
this.behaviorTree = buildResult.tree;
|
||||
this.blackboard = buildResult.blackboard;
|
||||
|
||||
resolve();
|
||||
@@ -92,35 +110,70 @@ export class BehaviorTreeManager extends Component {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建事件注册表
|
||||
*/
|
||||
private createEventRegistry(): EventRegistry {
|
||||
const registry = new EventRegistry();
|
||||
|
||||
// 注册简化的矿工行为事件处理器
|
||||
const eventHandlers = {
|
||||
// 矿工核心行为
|
||||
'find-and-mine-ore': (context: any, params?: any) => this.callBehaviorHandler('onFindAndMineOre', params),
|
||||
'store-ore': (context: any, params?: any) => this.callBehaviorHandler('onStoreOre', params),
|
||||
'idle-behavior': (context: any, params?: any) => this.callBehaviorHandler('onIdleBehavior', params)
|
||||
};
|
||||
|
||||
// 将事件处理器注册到EventRegistry
|
||||
Object.entries(eventHandlers).forEach(([eventName, handler]) => {
|
||||
registry.registerAction(eventName, handler);
|
||||
});
|
||||
|
||||
return registry;
|
||||
}
|
||||
|
||||
/**
|
||||
* 调用行为处理器的方法
|
||||
*/
|
||||
private callBehaviorHandler(methodName: string, params: any = {}): string {
|
||||
if (!this.behaviorHandler) {
|
||||
console.error(`BehaviorTreeManager: RTSBehaviorHandler未初始化 - ${this.node.name}`);
|
||||
return 'failure';
|
||||
}
|
||||
|
||||
try {
|
||||
// 直接调用RTSBehaviorHandler的方法
|
||||
const method = (this.behaviorHandler as any)[methodName];
|
||||
if (typeof method === 'function') {
|
||||
console.log(`🎯 调用行为处理器: ${methodName} (${this.node.name})`);
|
||||
const result = method.call(this.behaviorHandler, params);
|
||||
console.log(`📤 行为处理器返回: ${methodName} -> "${result}" (${this.node.name})`);
|
||||
return result || 'success'; // 确保有返回值
|
||||
} else {
|
||||
console.error(`BehaviorTreeManager: 方法不存在: ${methodName}`);
|
||||
return 'failure';
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`BehaviorTreeManager: 调用方法失败: ${methodName}`, error);
|
||||
return 'failure';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置黑板基础信息
|
||||
*/
|
||||
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('maxHealth', this.unitController.maxHealth);
|
||||
this.blackboard.setValue('currentCommand', 'mine');
|
||||
this.blackboard.setValue('hasOre', false);
|
||||
this.blackboard.setValue('hasTarget', false);
|
||||
this.blackboard.setValue('isSelected', false);
|
||||
|
||||
// 设置单位控制器引用,供行为树节点使用
|
||||
this.blackboard.setValue('unitController', this.unitController);
|
||||
this.blackboard.setValue('gameObject', this.node);
|
||||
this.blackboard.setValue('targetPosition', null);
|
||||
this.blackboard.setValue('isMoving', false);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -165,45 +218,20 @@ export class BehaviorTreeManager extends Component {
|
||||
|
||||
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('hasTarget', this.unitController.targetPosition && !this.unitController.targetPosition.equals(Vec3.ZERO));
|
||||
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');
|
||||
this.blackboard.setValue('isMoving', this.unitController.targetPosition && !this.unitController.targetPosition.equals(Vec3.ZERO));
|
||||
}
|
||||
|
||||
// 执行行为树
|
||||
try {
|
||||
this.behaviorTree.tick(deltaTime);
|
||||
if (this.debugMode && Math.random() < 0.01) { // 1%的概率打印调试信息
|
||||
console.log(`行为树执行完成, 单位: ${this.node.name}`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`行为树执行错误:`, error);
|
||||
console.error(`行为树执行错误: ${this.node.name}`, error);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,142 @@
|
||||
import { _decorator, Component, Vec3, Node } from 'cc';
|
||||
import { UnitController } from './UnitController';
|
||||
|
||||
const { ccclass } = _decorator;
|
||||
|
||||
/**
|
||||
* 矿工行为处理器 - 专门处理矿工的三个核心行为
|
||||
* 展示如何使用黑板变量参数和事件系统
|
||||
*/
|
||||
@ccclass('RTSBehaviorHandler')
|
||||
export class RTSBehaviorHandler extends Component {
|
||||
|
||||
private unitController: UnitController | null = null;
|
||||
private minerDemo: any = null; // MinerDemo组件引用
|
||||
|
||||
start() {
|
||||
this.unitController = this.getComponent(UnitController);
|
||||
// 获取场景中的MinerDemo组件
|
||||
this.minerDemo = this.node.parent?.getComponent('MinerDemo');
|
||||
|
||||
if (!this.unitController) {
|
||||
console.error('RTSBehaviorHandler: 未找到UnitController组件');
|
||||
}
|
||||
if (!this.minerDemo) {
|
||||
console.error('RTSBehaviorHandler: 未找到MinerDemo组件');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 寻找并挖掘矿石
|
||||
* @param params 事件参数,包含黑板变量值
|
||||
*/
|
||||
onFindAndMineOre(params: any = {}): string {
|
||||
if (!this.unitController || !this.minerDemo) return 'failure';
|
||||
|
||||
// 从参数中获取黑板变量值
|
||||
const unitType = params.unitType || 'unknown';
|
||||
const currentHealth = params.currentHealth || 100;
|
||||
|
||||
console.log(`⛏️ ${unitType}矿工开始寻找矿石 (生命值: ${currentHealth})`);
|
||||
|
||||
// 获取所有可用矿石
|
||||
const ores = this.minerDemo.getAllOres();
|
||||
if (ores.length === 0) {
|
||||
console.log(`👷 ${this.node.name}: 没有可挖掘的矿石了`);
|
||||
return 'failure';
|
||||
}
|
||||
|
||||
// 寻找最近的矿石
|
||||
const currentPos = this.node.worldPosition;
|
||||
let nearestOre: Node | null = null;
|
||||
let minDistance = Infinity;
|
||||
|
||||
for (const ore of ores) {
|
||||
const distance = Vec3.distance(currentPos, ore.worldPosition);
|
||||
if (distance < minDistance) {
|
||||
minDistance = distance;
|
||||
nearestOre = ore;
|
||||
}
|
||||
}
|
||||
|
||||
if (!nearestOre) return 'failure';
|
||||
|
||||
// 检查是否已经到达矿石位置
|
||||
if (minDistance < 1.5) {
|
||||
// 开始挖掘
|
||||
console.log(`⛏️ ${this.node.name}: 开始挖掘矿石`);
|
||||
|
||||
// 设置携带矿石状态(更新黑板)
|
||||
this.unitController.setBlackboardValue('hasOre', true);
|
||||
|
||||
// 移除矿石
|
||||
this.minerDemo.removeOre(nearestOre);
|
||||
|
||||
// 清除移动目标
|
||||
this.unitController.clearTarget();
|
||||
|
||||
return 'success';
|
||||
} else {
|
||||
// 移动到矿石位置
|
||||
this.unitController.setTarget(nearestOre.worldPosition);
|
||||
console.log(`🚶 ${this.node.name}: 前往矿石位置 距离${minDistance.toFixed(1)}米`);
|
||||
return 'running';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 前往仓库存储矿石
|
||||
* @param params 事件参数,包含黑板变量值
|
||||
*/
|
||||
onStoreOre(params: any = {}): string {
|
||||
if (!this.unitController || !this.minerDemo) return 'failure';
|
||||
|
||||
// 从参数中获取黑板变量值
|
||||
const unitType = params.unitType || 'unknown';
|
||||
const targetPosition = params.targetPosition || null;
|
||||
|
||||
console.log(`🏭 ${unitType}矿工前往仓库存储 (目标位置: ${JSON.stringify(targetPosition)})`);
|
||||
|
||||
const warehouse = this.minerDemo.getWarehouse();
|
||||
if (!warehouse) {
|
||||
console.log(`👷 ${this.node.name}: 找不到仓库`);
|
||||
return 'failure';
|
||||
}
|
||||
|
||||
// 计算到仓库的距离
|
||||
const currentPos = this.node.worldPosition;
|
||||
const warehousePos = warehouse.worldPosition;
|
||||
const distance = Vec3.distance(currentPos, warehousePos);
|
||||
|
||||
// 检查是否已经到达仓库
|
||||
if (distance < 2.5) {
|
||||
// 存储矿石
|
||||
console.log(`🏭 ${this.node.name}: 在仓库存储矿石`);
|
||||
|
||||
// 清除携带矿石状态(更新黑板)
|
||||
this.unitController.setBlackboardValue('hasOre', false);
|
||||
|
||||
// 清除移动目标
|
||||
this.unitController.clearTarget();
|
||||
|
||||
return 'success';
|
||||
} else {
|
||||
// 移动到仓库
|
||||
this.unitController.setTarget(warehousePos);
|
||||
console.log(`🚚 ${this.node.name}: 运输矿石到仓库 距离${distance.toFixed(1)}米`);
|
||||
return 'running';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 待机行为
|
||||
* @param params 事件参数,包含黑板变量值
|
||||
*/
|
||||
onIdleBehavior(params: any = {}): string {
|
||||
// 从参数中获取黑板变量值
|
||||
const unitType = params.unitType || 'unknown';
|
||||
|
||||
console.log(`😴 ${unitType}矿工待机中`);
|
||||
return 'success';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "739ff9ee-42d5-4542-bb5b-3e7611c729e2",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
import { _decorator, Component, Node, Vec3, MeshRenderer, BoxCollider, RigidBody, Mesh, Material, Color, primitives, utils } from 'cc';
|
||||
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
/**
|
||||
* 简单预制体工厂 - 创建基本的游戏对象
|
||||
* 用于在没有预制体资源时创建基本的单位、建筑和资源
|
||||
*/
|
||||
@ccclass('SimplePrefabFactory')
|
||||
export class SimplePrefabFactory extends Component {
|
||||
|
||||
/**
|
||||
* 创建单位节点
|
||||
*/
|
||||
static createUnit(name: string, color: Color = Color.WHITE): Node {
|
||||
const unit = new Node(name);
|
||||
|
||||
// 添加网格渲染器
|
||||
const meshRenderer = unit.addComponent(MeshRenderer);
|
||||
|
||||
// 创建立方体网格
|
||||
const mesh = utils.createMesh(primitives.box({ width: 1, height: 1, length: 1 }));
|
||||
meshRenderer.mesh = mesh;
|
||||
|
||||
// 创建材质
|
||||
const material = new Material();
|
||||
material.initialize({ effectName: 'builtin-unlit' });
|
||||
material.setProperty('mainColor', color);
|
||||
meshRenderer.material = material;
|
||||
|
||||
// 添加碰撞器
|
||||
const collider = unit.addComponent(BoxCollider);
|
||||
collider.size = new Vec3(1, 1, 1);
|
||||
|
||||
// 添加刚体
|
||||
const rigidBody = unit.addComponent(RigidBody);
|
||||
rigidBody.type = RigidBody.Type.KINEMATIC;
|
||||
|
||||
console.log(`创建单位: ${name}`);
|
||||
return unit;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建建筑节点
|
||||
*/
|
||||
static createBuilding(name: string, size: Vec3 = new Vec3(2, 2, 2), color: Color = Color.GRAY): Node {
|
||||
const building = new Node(name);
|
||||
|
||||
// 添加网格渲染器
|
||||
const meshRenderer = building.addComponent(MeshRenderer);
|
||||
|
||||
// 创建立方体网格
|
||||
const mesh = utils.createMesh(primitives.box({
|
||||
width: size.x,
|
||||
height: size.y,
|
||||
length: size.z
|
||||
}));
|
||||
meshRenderer.mesh = mesh;
|
||||
|
||||
// 创建材质
|
||||
const material = new Material();
|
||||
material.initialize({ effectName: 'builtin-unlit' });
|
||||
material.setProperty('mainColor', color);
|
||||
meshRenderer.material = material;
|
||||
|
||||
// 添加碰撞器
|
||||
const collider = building.addComponent(BoxCollider);
|
||||
collider.size = size;
|
||||
|
||||
console.log(`创建建筑: ${name}`);
|
||||
return building;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建资源节点
|
||||
*/
|
||||
static createResource(name: string, color: Color = Color.YELLOW): Node {
|
||||
const resource = new Node(name);
|
||||
|
||||
// 添加网格渲染器
|
||||
const meshRenderer = resource.addComponent(MeshRenderer);
|
||||
|
||||
// 创建球体网格
|
||||
const mesh = utils.createMesh(primitives.sphere(0.5));
|
||||
meshRenderer.mesh = mesh;
|
||||
|
||||
// 创建材质
|
||||
const material = new Material();
|
||||
material.initialize({ effectName: 'builtin-unlit' });
|
||||
material.setProperty('mainColor', color);
|
||||
meshRenderer.material = material;
|
||||
|
||||
// 添加碰撞器
|
||||
const collider = resource.addComponent(BoxCollider);
|
||||
collider.size = new Vec3(1, 1, 1);
|
||||
|
||||
console.log(`创建资源: ${name}`);
|
||||
return resource;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建地面节点
|
||||
*/
|
||||
static createGround(size: Vec3 = new Vec3(50, 0.1, 50), color: Color = new Color(100, 150, 100, 255)): Node {
|
||||
const ground = new Node('Ground');
|
||||
|
||||
// 添加网格渲染器
|
||||
const meshRenderer = ground.addComponent(MeshRenderer);
|
||||
|
||||
// 创建平面网格
|
||||
const mesh = utils.createMesh(primitives.box({
|
||||
width: size.x,
|
||||
height: size.y,
|
||||
length: size.z
|
||||
}));
|
||||
meshRenderer.mesh = mesh;
|
||||
|
||||
// 创建材质
|
||||
const material = new Material();
|
||||
material.initialize({ effectName: 'builtin-unlit' });
|
||||
material.setProperty('mainColor', color);
|
||||
meshRenderer.material = material;
|
||||
|
||||
// 添加碰撞器
|
||||
const collider = ground.addComponent(BoxCollider);
|
||||
collider.size = size;
|
||||
|
||||
console.log(`创建地面: ${size}`);
|
||||
return ground;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "ac45cfc7-cf47-4315-bdf0-ba002b45b4b6",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import { _decorator, Component, Node, Vec3, Material, MeshRenderer, Color, tween } from 'cc';
|
||||
import { BehaviorTreeManager } from './BehaviorTreeManager';
|
||||
import { RTSBehaviorHandler } from './RTSBehaviorHandler';
|
||||
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@@ -29,7 +30,7 @@ export class UnitController extends Component {
|
||||
public unitType: string = '';
|
||||
public maxHealth: number = 100;
|
||||
public currentHealth: number = 100;
|
||||
public moveSpeed: number = 3;
|
||||
public moveSpeed: number = 1.5;
|
||||
public attackRange: number = 2;
|
||||
public attackDamage: number = 25;
|
||||
public isSelected: boolean = false;
|
||||
@@ -40,7 +41,13 @@ export class UnitController extends Component {
|
||||
public attackCooldown: number = 1.5;
|
||||
public color: string = 'white';
|
||||
|
||||
// 移动状态管理
|
||||
private isMoving: boolean = false;
|
||||
private moveStartTime: number = 0;
|
||||
private lastTargetUpdateTime: number = 0;
|
||||
|
||||
private behaviorTreeManager: BehaviorTreeManager | null = null;
|
||||
private behaviorHandler: Component | null = null;
|
||||
private meshRenderer: MeshRenderer | null = null;
|
||||
|
||||
onLoad() {
|
||||
@@ -48,6 +55,14 @@ export class UnitController extends Component {
|
||||
|
||||
// 创建行为树管理器
|
||||
this.behaviorTreeManager = this.addComponent(BehaviorTreeManager);
|
||||
|
||||
// 添加RTS行为处理器
|
||||
try {
|
||||
// 添加RTSBehaviorHandler组件
|
||||
this.behaviorHandler = this.addComponent(RTSBehaviorHandler);
|
||||
} catch (error) {
|
||||
console.warn('RTSBehaviorHandler组件添加失败,将使用默认行为处理', error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -65,12 +80,15 @@ export class UnitController extends Component {
|
||||
// 设置材质颜色
|
||||
this.setUnitColor(config.color);
|
||||
|
||||
// 设置节点名称显示单位类型
|
||||
this.node.name = `${config.unitType.toUpperCase()}_${this.node.name}`;
|
||||
|
||||
// 初始化行为树
|
||||
if (this.behaviorTreeManager) {
|
||||
this.behaviorTreeManager.initializeBehaviorTree(config.behaviorTreeName, this);
|
||||
}
|
||||
|
||||
console.log(`单位 ${this.node.name} 设置完成 - 类型: ${config.unitType}, 行为树: ${config.behaviorTreeName}`);
|
||||
console.log(`🎮 单位设置完成: ${this.node.name} | 类型: ${config.unitType.toUpperCase()} | 行为树: ${config.behaviorTreeName}`);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -93,6 +111,8 @@ export class UnitController extends Component {
|
||||
this.meshRenderer.material.setProperty('mainColor', color);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 设置选择状态
|
||||
*/
|
||||
@@ -165,6 +185,29 @@ export class UnitController extends Component {
|
||||
console.log(`单位 ${this.node.name} 接收命令: ${command}`, target);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置黑板变量值
|
||||
*/
|
||||
setBlackboardValue(key: string, value: any) {
|
||||
if (this.behaviorTreeManager) {
|
||||
this.behaviorTreeManager.updateBlackboardValue(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置移动目标
|
||||
*/
|
||||
setTarget(position: Vec3) {
|
||||
this.targetPosition = position.clone();
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除移动目标
|
||||
*/
|
||||
clearTarget() {
|
||||
this.targetPosition = Vec3.ZERO.clone();
|
||||
}
|
||||
|
||||
/**
|
||||
* 受到伤害
|
||||
*/
|
||||
@@ -219,23 +262,45 @@ export class UnitController extends Component {
|
||||
}
|
||||
|
||||
/**
|
||||
* 移动到目标位置
|
||||
* 移动到目标位置(只在水平面移动,不改变Y轴)
|
||||
*/
|
||||
moveToTarget(targetPos: Vec3, speed?: number): boolean {
|
||||
moveToTarget(targetPos: Vec3, speed?: number, deltaTime?: number): boolean {
|
||||
const currentPos = this.node.worldPosition;
|
||||
const distance = currentPos.subtract(targetPos).length();
|
||||
|
||||
if (distance < 0.5) {
|
||||
// 只计算水平面距离(忽略Y轴)
|
||||
const currentPos2D = new Vec3(currentPos.x, 0, currentPos.z);
|
||||
const targetPos2D = new Vec3(targetPos.x, 0, targetPos.z);
|
||||
const distance = currentPos2D.subtract(targetPos2D).length();
|
||||
|
||||
if (distance < 0.8) { // 增加到达阈值,减少抖动
|
||||
this.isMoving = false;
|
||||
return true; // 已到达目标
|
||||
}
|
||||
|
||||
// 简单的移动逻辑
|
||||
const direction = targetPos.subtract(currentPos).normalize();
|
||||
// 平滑移动逻辑(只在水平面)
|
||||
const direction2D = targetPos2D.subtract(currentPos2D).normalize();
|
||||
const moveSpeed = speed || this.moveSpeed;
|
||||
const deltaTime = 1/60; // 假设60fps
|
||||
const dt = deltaTime || 0.016; // 使用传入的deltaTime或默认值
|
||||
|
||||
// 计算移动距离,确保不会超过目标位置
|
||||
const moveDistance = Math.min(moveSpeed * dt, distance);
|
||||
const movement2D = direction2D.multiplyScalar(moveDistance);
|
||||
|
||||
// 新位置保持原有的Y轴位置
|
||||
const newPosition = new Vec3(
|
||||
currentPos.x + movement2D.x,
|
||||
currentPos.y, // 保持Y轴不变
|
||||
currentPos.z + movement2D.z
|
||||
);
|
||||
|
||||
const newPosition = currentPos.add(direction.multiplyScalar(moveSpeed * deltaTime));
|
||||
this.node.setWorldPosition(newPosition);
|
||||
this.isMoving = true;
|
||||
|
||||
// 减少日志输出频率
|
||||
if (Date.now() - this.moveStartTime > 1000) { // 每秒输出一次
|
||||
console.log(`${this.node.name}: 移动中 距离目标${distance.toFixed(2)}米`);
|
||||
this.moveStartTime = Date.now();
|
||||
}
|
||||
|
||||
return false; // 还在移动中
|
||||
}
|
||||
@@ -263,26 +328,56 @@ export class UnitController extends Component {
|
||||
}
|
||||
|
||||
update(deltaTime: number) {
|
||||
// 更新行为树黑板中的时间相关变量
|
||||
// 自动移动逻辑 - 如果有目标位置就自动移动
|
||||
if (this.targetPosition && !this.targetPosition.equals(Vec3.ZERO)) {
|
||||
const arrived = this.moveToTarget(this.targetPosition, undefined, deltaTime);
|
||||
if (arrived) {
|
||||
// 不要清除目标位置,让行为树决定下一步动作
|
||||
this.isMoving = false;
|
||||
|
||||
// 更新黑板状态
|
||||
if (this.behaviorTreeManager) {
|
||||
this.behaviorTreeManager.updateBlackboardValue('isMoving', false);
|
||||
// 不要设置hasTarget为false,让行为树自己管理
|
||||
}
|
||||
} else {
|
||||
this.isMoving = true;
|
||||
|
||||
// 更新移动状态到黑板
|
||||
if (this.behaviorTreeManager) {
|
||||
this.behaviorTreeManager.updateBlackboardValue('isMoving', true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 更新行为树黑板中的核心变量
|
||||
if (this.behaviorTreeManager) {
|
||||
this.behaviorTreeManager.updateBlackboardValue('deltaTime', deltaTime);
|
||||
this.behaviorTreeManager.updateBlackboardValue('currentTime', Date.now() / 1000);
|
||||
// 基础属性更新
|
||||
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.behaviorTreeManager.updateBlackboardValue('currentCommand', this.currentCommand);
|
||||
this.behaviorTreeManager.updateBlackboardValue('hasTarget', this.targetPosition && !this.targetPosition.equals(Vec3.ZERO));
|
||||
this.behaviorTreeManager.updateBlackboardValue('targetPosition', this.targetPosition);
|
||||
this.behaviorTreeManager.updateBlackboardValue('isSelected', this.isSelected);
|
||||
this.behaviorTreeManager.updateBlackboardValue('isMoving', this.isMoving);
|
||||
|
||||
// 位置信息更新
|
||||
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);
|
||||
// 根据单位类型设置特定的黑板变量
|
||||
if (this.unitType === 'worker') {
|
||||
// 工人特有的变量
|
||||
// 这里可以添加工人特有的状态更新
|
||||
} else if (this.unitType === 'soldier') {
|
||||
// 士兵特有的变量
|
||||
this.behaviorTreeManager.updateBlackboardValue('lastAttackTime', this.lastAttackTime);
|
||||
} else if (this.unitType === 'scout') {
|
||||
// 侦察兵特有的变量
|
||||
// 这里可以添加侦察兵特有的状态更新
|
||||
}
|
||||
|
||||
// 更新状态标志
|
||||
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');
|
||||
}
|
||||
|
||||
// 调试信息显示
|
||||
|
||||
Reference in New Issue
Block a user