import { Object3D, Vector3 } from "three";
import { ThreeC } from "./ThreeC";
import { PlayerC, PLAYER_HEIGHT } from "./PlayerC";
import { CameraFollowC } from "./CameraFollowC";

// Above and behind the player, along the road's forward (+Z) direction —
// player now spawns at the other end of the road, walking toward +Z, so
// "behind" flipped from +Z to -Z to keep looking the same way they walk.
const CAMERA_OFFSET = new Vector3(0, 9, -9);

// Obstacle checks look from roughly head height rather than the player's
// feet (position.y is anchored at the feet), so short obstacles don't
// falsely count as hiding the player. 0.9 of the full height keeps the ray
// just under the very top edge, avoiding edge-grazing false negatives.
const CAMERA_EYE_HEIGHT = PLAYER_HEIGHT * 0.9;

export class TestSceneC {
  static init() {
    const colliders = this.createMap();

    PlayerC.init(colliders);
    CameraFollowC.init(PlayerC.object, CAMERA_OFFSET, colliders, CAMERA_EYE_HEIGHT);
  }

  private static createMap() {
    const map = ThreeC.getObject("map");
    const colliders: Object3D[] = [];

    // physics-only collision proxies exported alongside the visual meshes;
    // hide them from render, but keep them as camera-obstacle geometry
    map.traverse((child) => {
      if (/collider/i.test(child.name)) {
        child.visible = false;
        colliders.push(child);
      }
    });

    ThreeC.setShadowsStateForChildren(map, true, true);

    ThreeC.addToScene(map);

    return colliders;
  }
}
