Contains game-jumpnrun and game-labyrinth projects. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
115 lines
3.3 KiB
JavaScript
115 lines
3.3 KiB
JavaScript
// Monster-KI
|
|
|
|
const Monster = {
|
|
col: 0,
|
|
row: 0,
|
|
targetCol: 0,
|
|
targetRow: 0,
|
|
visualX: 0,
|
|
visualY: 0,
|
|
speed: 3, // Aktuelle Geschwindigkeit (Tiles/Sek)
|
|
baseSpeed: 3,
|
|
maxSpeed: 5,
|
|
speedIncrease: 0.15, // Zunahme pro Sekunde
|
|
moving: false,
|
|
moveProgress: 0,
|
|
path: [],
|
|
pathRecalcTimer: 0,
|
|
pathRecalcInterval: 0.3, // Pfad alle 0.3s neu berechnen
|
|
animFrame: 0,
|
|
animTimer: 0,
|
|
|
|
init(col, row, baseSpeed, speedIncrease, maxSpeed) {
|
|
this.col = col;
|
|
this.row = row;
|
|
this.targetCol = col;
|
|
this.targetRow = row;
|
|
this.visualX = col;
|
|
this.visualY = row;
|
|
this.speed = baseSpeed;
|
|
this.baseSpeed = baseSpeed;
|
|
this.maxSpeed = maxSpeed;
|
|
this.speedIncrease = speedIncrease;
|
|
this.moving = false;
|
|
this.moveProgress = 0;
|
|
this.path = [];
|
|
this.pathRecalcTimer = 0;
|
|
this.animFrame = 0;
|
|
},
|
|
|
|
recalcPath(grid, playerCol, playerRow) {
|
|
this.path = Utils.bfs(grid, this.col, this.row, playerCol, playerRow);
|
|
},
|
|
|
|
update(dt, grid, playerCol, playerRow, elapsedTime) {
|
|
// Geschwindigkeit erhöhen
|
|
this.speed = Math.min(
|
|
this.baseSpeed + this.speedIncrease * elapsedTime,
|
|
this.maxSpeed
|
|
);
|
|
|
|
// Animation
|
|
this.animTimer += dt;
|
|
if (this.animTimer > 0.2) {
|
|
this.animFrame = (this.animFrame + 1) % 2;
|
|
this.animTimer = 0;
|
|
}
|
|
|
|
// Pfad regelmäßig neu berechnen
|
|
this.pathRecalcTimer += dt;
|
|
if (this.pathRecalcTimer >= this.pathRecalcInterval) {
|
|
this.pathRecalcTimer = 0;
|
|
this.recalcPath(grid, playerCol, playerRow);
|
|
}
|
|
|
|
// Bewegung
|
|
if (!this.moving && this.path.length > 0) {
|
|
const next = this.path.shift();
|
|
this.targetCol = next.col;
|
|
this.targetRow = next.row;
|
|
this.moving = true;
|
|
this.moveProgress = 0;
|
|
}
|
|
|
|
if (this.moving) {
|
|
this.moveProgress += this.speed * dt;
|
|
|
|
if (this.moveProgress >= 1) {
|
|
this.col = this.targetCol;
|
|
this.row = this.targetRow;
|
|
this.moving = false;
|
|
this.moveProgress = 0;
|
|
|
|
// Direkt nächsten Schritt
|
|
if (this.path.length > 0) {
|
|
const next = this.path.shift();
|
|
this.targetCol = next.col;
|
|
this.targetRow = next.row;
|
|
this.moving = true;
|
|
this.moveProgress = 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Visuelle Position
|
|
if (this.moving) {
|
|
this.visualX = Utils.lerp(this.col, this.targetCol, this.moveProgress);
|
|
this.visualY = Utils.lerp(this.row, this.targetRow, this.moveProgress);
|
|
} else {
|
|
this.visualX = this.col;
|
|
this.visualY = this.row;
|
|
}
|
|
},
|
|
|
|
// Kollision mit Spieler (Distanz-basiert)
|
|
checkCollision(playerX, playerY) {
|
|
const dx = this.visualX - playerX;
|
|
const dy = this.visualY - playerY;
|
|
return Math.sqrt(dx * dx + dy * dy) < 0.6;
|
|
},
|
|
|
|
draw(time) {
|
|
Renderer.drawMonster(this.visualX, this.visualY, this.animFrame, time);
|
|
}
|
|
};
|