import 'dart:math' as math;

/// Represents the four primary compass directions used for movement and orientation.
///
/// The [radians] values are aligned with standard 2D Cartesian coordinates where
/// East is 0.0 and rotations proceed clockwise.
enum CardinalDirection {
  /// 0 degrees (pointing Right)
  east(0.0),

  /// 90 degrees (pointing Down)
  south(math.pi / 2),

  /// 180 degrees (pointing Left)
  west(math.pi),

  /// 270 degrees (pointing Up)
  north(3 * math.pi / 2)
  ;

  /// The rotation value in radians associated with this direction.
  final double radians;

  const CardinalDirection(this.radians);

  /// Decodes Wolf3D enemy directional orientation from map tile indices.
  ///
  /// In the original Wolfenstein 3D map format, enemies are stored in
  /// blocks where the specific index (modulo 4) determines their starting facing.
  static CardinalDirection fromEnemyIndex(int index) {
    // The engine uses a specific pattern: 0=East, 1=North, 2=West, 3=South
    switch (index % 4) {
      case 0:
        return CardinalDirection.east;
      case 1:
        return CardinalDirection.north;
      case 2:
        return CardinalDirection.west;
      case 3:
        return CardinalDirection.south;
      default:
        // Fallback safety, though mathematically unreachable with % 4
        return CardinalDirection.east;
    }
  }
}
