wangwang/js/ai.js

55 lines
1.7 KiB
JavaScript

export default class AIOpponent {
constructor(difficulty = 'normal') {
this.difficulty = difficulty;
this.state = 'IDLE';
this.stateTimer = 0;
this.barkStrength = 0.7;
const configs = {
easy: { idleMin: 0.8, idleMax: 2.5, barkMin: 0.3, barkMax: 0.6, cooldownMin: 0.8, cooldownMax: 1.5, strengthMin: 0.4, strengthMax: 0.7 },
normal: { idleMin: 1.0, idleMax: 2.5, barkMin: 0.2, barkMax: 0.4, cooldownMin: 0.8, cooldownMax: 1.5, strengthMin: 0.3, strengthMax: 0.5 },
hard: { idleMin: 0.1, idleMax: 0.6, barkMin: 0.15, barkMax: 0.3, cooldownMin: 0.1, cooldownMax: 0.4, strengthMin: 0.8, strengthMax: 1.0 }
};
this.config = configs[difficulty] || configs.normal;
}
reset() {
this.state = 'IDLE';
this.stateTimer = this._rand(this.config.idleMin, this.config.idleMax);
this.barkStrength = 0;
}
update(dt) {
this.stateTimer -= dt;
if (this.stateTimer > 0) return null;
switch (this.state) {
case 'IDLE':
this.state = 'BARK';
this.stateTimer = this._rand(this.config.barkMin, this.config.barkMax);
this.barkStrength = this._rand(this.config.strengthMin, this.config.strengthMax);
return null;
case 'BARK':
{
const power = this.barkStrength * 0.06;
this.state = 'COOLDOWN';
this.stateTimer = this._rand(this.config.cooldownMin, this.config.cooldownMax);
return { type: 'bark', power };
}
case 'COOLDOWN':
this.state = 'IDLE';
this.stateTimer = this._rand(this.config.idleMin, this.config.idleMax);
return null;
default:
return null;
}
}
_rand(min, max) {
return min + Math.random() * (max - min);
}
}