<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Open World Test (GTA‑Style Controls)</title>

<link href="https://unpkg.com/maplibre-gl@3.6.2/dist/maplibre-gl.css" rel="stylesheet">

<style>
  html, body {
    margin: 0;
    padding: 0;
    overflow: hidden;
    height: 100%;
  }
  #map {
    position: absolute;
    inset: 0;
  }
  #game {
    position: absolute;
    inset: 0;
    pointer-events: none;
  }
</style>
</head>
<body>

<div id="map"></div>
<canvas id="game"></canvas>

<script src="https://unpkg.com/maplibre-gl@3.6.2/dist/maplibre-gl.js"></script>

<script>
let map, player, canvas, ctx;
let lastTime = performance.now();

/* ---------------- INPUT ---------------- */
const keys = { w:false, s:false, a:false, d:false };

window.addEventListener("keydown", e => {
  if (e.key === "w") keys.w = true;
  if (e.key === "s") keys.s = true;
  if (e.key === "a") keys.a = true;
  if (e.key === "d") keys.d = true;
});

window.addEventListener("keyup", e => {
  if (e.key === "w") keys.w = false;
  if (e.key === "s") keys.s = false;
  if (e.key === "a") keys.a = false;
  if (e.key === "d") keys.d = false;
});

/* ---------------- PLAYER (GTA‑STYLE) ---------------- */
class Player {
  constructor(lng, lat) {
    this.lng = lng;
    this.lat = lat;

    this.heading = 0;       // degrees
    this.speed = 0;         // m/s
    this.maxSpeed = 40;
    this.accel = 20;
    this.brake = 30;
    this.turnRate = 120;    // deg/sec
    this.friction = 8;

    this.img = new Image();
    this.img.src = "player.png";
  }

  move(dt) {
    // Steering
    if (keys.a) this.heading -= this.turnRate * dt;
    if (keys.d) this.heading += this.turnRate * dt;

    // Acceleration
    if (keys.w) this.speed += this.accel * dt;
    if (keys.s) this.speed -= this.brake * dt;

    // Clamp speed
    if (this.speed > this.maxSpeed) this.speed = this.maxSpeed;
    if (this.speed < -this.maxSpeed * 0.4) this.speed = -this.maxSpeed * 0.4;

    // Friction
    if (!keys.w && !keys.s) {
      if (this.speed > 0) this.speed -= this.friction * dt;
      if (this.speed < 0) this.speed += this.friction * dt;
      if (Math.abs(this.speed) < 0.1) this.speed = 0;
    }

    // Movement based on heading
    const rad = this.heading * Math.PI / 180;
    const dx = Math.cos(rad) * this.speed * dt;
    const dy = Math.sin(rad) * this.speed * dt;

    const p = map.project([this.lng, this.lat]);
    const newPoint = { x: p.x + dx, y: p.y + dy };
    const ll = map.unproject(newPoint);

    this.lng = ll.lng;
    this.lat = ll.lat;
  }

  draw(ctx) {
    const pos = map.project([this.lng, this.lat]);
    const size = 40;

    ctx.save();
    ctx.translate(pos.x, pos.y);
    ctx.rotate(this.heading * Math.PI / 180);
    ctx.drawImage(this.img, -size/2, -size/2, size, size);
    ctx.restore();
  }
}

/* ---------------- INIT ---------------- */
window.onload = () => {
  map = new maplibregl.Map({
    container: 'map',
    style: 'https://api.maptiler.com/maps/019d3452-4395-7ecd-bbf0-c64a3d5060cd/style.json?key=uHmPABwBFcVhiPJcdCzb',
    center: [173.284, -41.270],
    zoom: 10,
    pitch: 45,
    bearing: 85
  });

  map.keyboard.disable();
  map.dragRotate.enable();
  map.touchZoomRotate.disableRotation();

  canvas = document.getElementById("game");
  ctx = canvas.getContext("2d");

  resizeCanvas();
  window.addEventListener("resize", resizeCanvas);

  player = new Player(173.284, -41.270);

  map.on("render", gameLoop);
};

/* ---------------- HELPERS ---------------- */
function resizeCanvas() {
  canvas.width = window.innerWidth;
  canvas.height = window.innerHeight;
}

/* ---------------- MAIN LOOP ---------------- */
function gameLoop() {
  const now = performance.now();
  const dt = (now - lastTime) / 1000;
  lastTime = now;

  player.move(dt);

  // Camera follows player AND rotates with heading
  map.setCenter([player.lng, player.lat]);
  map.setBearing(player.heading);

  ctx.clearRect(0, 0, canvas.width, canvas.height);
  player.draw(ctx);
}
</script>

</body>
</html>
