Améliore la facon dont se déplace le monstre : class Monster extends Entity {…
- User
Améliore la facon dont se déplace le monstre :
class Monster extends Entity { constructor(x, y, width, height, col, attackPower) { super(x, y, width, height, col); this.attackPower = attackPower; this.detectionRange = 300; // Portée de détection du joueur this.attackRange = 100; // Portée d'attaque this.state = 'idle'; // États possibles: 'idle', 'chase', 'attack' this.patrolPoints = []; // Points de patrouille this.currentPatrolIndex = 0; this.attackCooldown = 0; this.maxAttackCooldown = 60; // Délai entre les attaques (en frames) }
update() { // super.update();
codeconst distanceToPlayer = dist(this.x, this.y, character.x, character.y); // Mise à jour de l'état du monstre if (distanceToPlayer <= this.attackRange) { this.state = 'attack'; } else if (distanceToPlayer <= this.detectionRange) { this.state = 'chase'; } else { this.state = 'idle'; } // Comportement basé sur l'état switch (this.state) { case 'idle': this.patrol(); break; case 'chase': this.chase(character); break; case 'attack': this.attack(character); break; } // Gestion du cooldown d'attaque if (this.attackCooldown > 0) { this.attackCooldown--; }}
patrol() { if (this.patrolPoints.length > 0) { const target = this.patrolPoints[this.currentPatrolIndex]; this.goTo(target);
codeif (dist(this.x, this.y, target.x, target.y) < 10) { this.currentPatrolIndex = (this.currentPatrolIndex + 1) % this.patrolPoints.length; } }}
chase(target) { this.goTo(target); }
attack(target) { if (this.attackCooldown === 0) { console.log("Monster attacks!");
code// Logique d'attaque ici (pas encore faite) // target.takeDamage(this.attackPower); this.attackCooldown = this.maxAttackCooldown; }}
goTo(target) { // Votre code existant pour goTo // ...
code Assistant