之前的敌人系统存在以下问题:
EnemyInstance.ts 中的属性初始化逻辑不正确EnemyInstance.ts// 修改前:硬编码默认值
public health: number = 30;
public maxHealth: number = 30;
public speed: number = 50;
public attackPower: number = 10;
public attackInterval: number = 2;
// 修改后:初始化为0,从配置文件读取
public health: number = 0;
public maxHealth: number = 0;
public speed: number = 0;
public attackPower: number = 0;
public attackInterval: number = 0; // 从配置文件读取
// 修改前:直接从根节点读取属性
this.health = this.enemyConfig.health || 30;
this.speed = this.enemyConfig.speed || 50;
this.attackPower = this.enemyConfig.attack || 10;
// 修改后:从正确的嵌套节点读取属性
// 从stats节点读取基础属性
const stats = this.enemyConfig.stats || {};
this.health = stats.health || 30;
this.maxHealth = stats.maxHealth || this.health;
// 从movement节点读取移动速度
const movement = this.enemyConfig.movement || {};
this.speed = movement.speed || 50;
// 从combat节点读取攻击力
const combat = this.enemyConfig.combat || {};
this.attackPower = combat.attackDamage || 10;
this.attackInterval = combat.attackCooldown || 2.0;
// 修改前:总是覆盖攻击间隔
this.attackInterval = 2.0; // 默认攻击间隔
// 修改后:只有在未设置时才使用默认值
if (this.attackInterval <= 0) {
this.attackInterval = 2.0; // 默认攻击间隔
}
EnemyController.ts// 修改前:不匹配配置文件结构
private defaultAttackPower: number = 10;
private defaultHealth: number = 30;
// 修改后:匹配配置文件中的实际数值
private defaultAttackPower: number = 20; // 对应combat.attackDamage
private defaultHealth: number = 10; // 对应stats.health
敌人配置文件 assets/resources/data/enemies.json 采用嵌套结构:
{
"enemies": [
{
"id": "normal_zombie",
"name": "普通僵尸",
"type": "basic",
"stats": {
"health": 10,
"maxHealth": 10,
"attack": 1,
"defense": 0,
"speed": 50.0
},
"movement": {
"speed": 50.0,
"pattern": "direct",
"moveType": "straight"
},
"combat": {
"attackDamage": 20,
"attackRange": 100,
"attackCooldown": 1.0,
"attackType": "melee"
}
}
]
}
| EnemyInstance属性 | JSON配置路径 | 说明 |
|---|---|---|
health |
stats.health |
当前血量 |
maxHealth |
stats.maxHealth |
最大血量 |
speed |
movement.speed |
移动速度 |
attackPower |
combat.attackDamage |
攻击伤害 |
attackInterval |
combat.attackCooldown |
攻击冷却时间 |
通过测试脚本 test_enemy_config_loading.js 验证:
✅ 配置文件状态
✅ 属性数值范围
enemies.json 中添加新的敌人配置stats、movement、combat 节点enemies.json 中对应敌人的属性值运行测试脚本验证配置:
node test_enemy_config_loading.js