<?php
// This is your original game with ONLY steering upgraded
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>GTA-V Style 2.5D Driving</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%; background:#000; }
#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>

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;
});

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

class Player {
  constructor(lng, lat) {
    this.lng = lng;
    this.lat = lat;

    this.heading = 0;
    this.speed = 0;

    this.maxSpeed = 40;
    this.accel = 20;
    this.brake = 30;
    this.turnRate = 120;
    this.friction = 8;

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

  update(dt) {

    // 🔥 SMART STEERING (ONLY CHANGE)
    const speedFactor = Math.min(Math.abs(this.speed) / this.maxSpeed, 1);
    const effectiveTurn = this.turnRate * (0.3 + speedFactor);

    if (keys.a) this.heading -= effectiveTurn * dt;
    if (keys.d) this.heading += effectiveTurn * dt;

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

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

    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;
    }

    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]);
    ctx.drawImage(this.img, pos.x-25, pos.y-25, 50, 50);
  }
}

window.onload = () => {
  map = new maplibregl.Map({
    container: 'map',
    style: './map-style.json',
    center: [173.284, -41.270],
    zoom: 17.5,
    pitch: 55
  });

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

  canvas.width = innerWidth;
  canvas.height = innerHeight;

  player = new Player(173.284, -41.270);

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

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

  player.update(dt);

  map.setCenter([player.lng, player.lat]);

  ctx.clearRect(0,0,canvas.width,canvas.height);
  player.draw(ctx);
}

</script>
</body>
</html>