import 'package:wolf_3d_dart/wolf_3d_engine.dart';

/// Shared projection helpers used by render backends and the raycaster.
mixin ProjectionMath {
  int get projectionWidth;
  int get projectionOffsetX;
  int get projectionViewHeight;
  int get viewHeight;

  /// Returns the texture Y coordinate for a wall sample row.
  int wallTexY(int y, int columnHeight) {
    final int projectionCenterY = projectionViewHeight ~/ 2;
    final double relativeY =
        (y - (-columnHeight ~/ 2 + projectionCenterY)) / columnHeight;
    return (relativeY * 64).toInt().clamp(0, 63);
  }

  /// Returns the texture Y coordinate for a sprite sample row.
  int spriteTexY(int y, int drawStartY, int spriteHeight) {
    final double relativeY = (y - drawStartY) / spriteHeight;
    return (relativeY * 64).toInt().clamp(0, 63);
  }

  /// Returns the screen-space bounds for the player's weapon overlay.
  ({int weaponWidth, int weaponHeight, int startX, int startY})
  weaponScreenBounds(WolfEngine engine) {
    final int ww = (projectionWidth * 0.5).toInt();
    final int wh = (viewHeight * 0.8).toInt();
    final int sx = projectionOffsetX + (projectionWidth ~/ 2) - (ww ~/ 2);
    final int sy = viewHeight - wh + (engine.player.weaponAnimOffset ~/ 4);
    return (weaponWidth: ww, weaponHeight: wh, startX: sx, startY: sy);
  }
}
