import { PHYSICS } from '@/config/physics.config';
import { BaseEnemy } from './BaseEnemy';

export class Patroller extends BaseEnemy {
  private moveDirection: 1 | -1 = 1;
  private startX: number;
  private patrolDistance: number;

  constructor(scene: Phaser.Scene, x: number, y: number) {
    super(scene, x, y, 'enemy');

    this.startX = x;
    this.patrolDistance = PHYSICS.enemies.patroller.patrolDistance;
  }

  protected updateBehavior(): void {
    const body = this.body as Phaser.Physics.Arcade.Body;

    // Move in current direction
    body.setVelocityX(PHYSICS.enemies.patroller.speed * this.moveDirection);

    // Check patrol bounds
    if (this.x > this.startX + this.patrolDistance) {
      this.moveDirection = -1;
      this.setFlipX(true);
    } else if (this.x < this.startX - this.patrolDistance) {
      this.moveDirection = 1;
      this.setFlipX(false);
    }

    // Check for walls
    if (body.blocked.right) {
      this.moveDirection = -1;
      this.setFlipX(true);
    } else if (body.blocked.left) {
      this.moveDirection = 1;
      this.setFlipX(false);
    }
  }
}
