mirror of
https://github.com/cliffe/BreakEscape.git
synced 2026-02-20 13:50:46 +00:00
fix: Make stationary NPCs immovable to prevent player from pushing them
NPCs are now set to immovable when in stationary states and movable when moving: - Idle state: immovable (can't be pushed) - Face player state: immovable (can't be pushed) - Dwelling at waypoints: immovable (can't be pushed) - Attacking (in range): immovable (can't be pushed) - Patrolling: movable (can be pushed, normal collision) - Chasing: movable (can be pushed, normal collision) - Backing away (personal space): movable (can be pushed, normal collision) This prevents the player from pushing stationary NPCs across the room while still allowing normal physics interactions when NPCs are moving.
This commit is contained in:
@@ -515,6 +515,9 @@ class NPCBehavior {
|
||||
|
||||
switch (state) {
|
||||
case 'idle':
|
||||
// Make NPC immovable so they can't be pushed by the player
|
||||
this.sprite.body.setImmovable(true);
|
||||
|
||||
// Actively dampen any residual velocity from collisions
|
||||
const currentVelX = this.sprite.body.velocity.x;
|
||||
const currentVelY = this.sprite.body.velocity.y;
|
||||
@@ -529,6 +532,9 @@ class NPCBehavior {
|
||||
break;
|
||||
|
||||
case 'face_player':
|
||||
// Make NPC immovable so they can't be pushed by the player
|
||||
this.sprite.body.setImmovable(true);
|
||||
|
||||
this.facePlayer(playerPos);
|
||||
// Actively dampen any residual velocity from collisions
|
||||
const faceVelX = this.sprite.body.velocity.x;
|
||||
@@ -623,6 +629,9 @@ class NPCBehavior {
|
||||
// Check if dwell time expired
|
||||
const dwellElapsed = time - this.patrolReachedTime;
|
||||
if (dwellElapsed < this.patrolTarget.dwellTime) {
|
||||
// Make NPC immovable while dwelling so they can't be pushed
|
||||
this.sprite.body.setImmovable(true);
|
||||
|
||||
// Still dwelling - actively dampen any residual velocity to prevent sliding
|
||||
const dwellVelX = this.sprite.body.velocity.x;
|
||||
const dwellVelY = this.sprite.body.velocity.y;
|
||||
@@ -678,6 +687,9 @@ class NPCBehavior {
|
||||
return; // Let next frame handle the new waypoint
|
||||
}
|
||||
|
||||
// Make NPC movable while patrolling
|
||||
this.sprite.body.setImmovable(false);
|
||||
|
||||
// Move toward current waypoint
|
||||
const velocityX = (dx / distance) * this.config.patrol.speed;
|
||||
const velocityY = (dy / distance) * this.config.patrol.speed;
|
||||
@@ -971,12 +983,17 @@ class NPCBehavior {
|
||||
|
||||
if (distance === 0) return false; // Avoid division by zero
|
||||
|
||||
// Make NPC movable while backing away from player
|
||||
if (this.sprite.body) {
|
||||
this.sprite.body.setImmovable(false);
|
||||
}
|
||||
|
||||
// Back away using velocity (physics-safe movement)
|
||||
// Normalize direction and apply velocity push
|
||||
const backAwaySpeed = this.config.personalSpace.backAwaySpeed || 30;
|
||||
const velocityX = (dx / distance) * backAwaySpeed;
|
||||
const velocityY = (dy / distance) * backAwaySpeed;
|
||||
|
||||
|
||||
if (this.sprite.body) {
|
||||
this.sprite.body.setVelocity(velocityX, velocityY);
|
||||
}
|
||||
@@ -1005,6 +1022,9 @@ class NPCBehavior {
|
||||
|
||||
// If in attack range, try to attack
|
||||
if (distance <= attackRange) {
|
||||
// Make NPC immovable while attacking (stationary)
|
||||
this.sprite.body.setImmovable(true);
|
||||
|
||||
// Stop moving
|
||||
this.sprite.body.setVelocity(0, 0);
|
||||
this.isMoving = false;
|
||||
@@ -1021,6 +1041,9 @@ class NPCBehavior {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Make NPC movable while chasing
|
||||
this.sprite.body.setImmovable(false);
|
||||
|
||||
// Chase player - move towards them
|
||||
const chaseSpeed = this.config.hostile.chaseSpeed || 120;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user