<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Nelson Free Drive</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;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
  }
  #game {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    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>
/* ---------------- INPUT ---------------- */
const keys = { up:false, down:false, left:false, right:false };

window.addEventListener("keydown", e => {
  if (e.key === "w" || e.key === "ArrowUp") keys.up = true;
  if (e.key === "s" || e.key === "ArrowDown") keys.down = true;
  if (e.key === "a" || e.key === "ArrowLeft") keys.left = true;
  if (e.key === "d" || e.key === "ArrowRight") keys.right = true;
});

window.addEventListener("keyup", e => {
  if (e.key === "w" || e.key === "ArrowUp") keys.up = false;
  if (e.key === "s" || e.key === "ArrowDown") keys.down = false;
  if (e.key === "a" || e.key === "ArrowLeft") keys.left = false;
  if (e.key === "d" || e.key === "ArrowRight") keys.right = false;
});

/* ---------------- CAR ---------------- */
class Car {
  constructor(lng, lat) {
    this.lng = lng;
    this.lat = lat;

    this.speed = 0;
    this.direction = 0; // degrees
    this.maxSpeed = 12; // metres per second
    this.accel = 3;     // m/s²
    this.friction = 0.92;

    this.img = new Image();
    this.img.src = "https://png.pngtree.com/png-vector/20250512/ourmid/pngtree-top-view-of-a-sleek-blue-car-png-image_16231333.png";
  }

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

  isOnRoad(lng, lat) {
    const p = map.project([lng, lat]);
    const features = map.queryRenderedFeatures([p.x, p.y], {
      layers: ["transportation"]
    });
    return features.length > 0;
  }

  update(dt) {
    if (keys.up) this.speed += this.accel * dt;
    if (keys.down) this.speed -= this.accel * 0.7 * dt;

    this.speed *= this.friction;

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

    if (keys.left) this.direction -= 120 * dt;
    if (keys.right) this.direction += 120 * dt;

    const rad = this.direction * Math.PI / 180;
    const metres = this.speed * dt;

    const dx = Math.sin(rad) * metres;
    const dy = -Math.cos(rad) * metres;

    const [newLng, newLat] = this.moveMetres(dx, dy);

    if (this.isOnRoad(newLng, newLat)) {
      this.lng = newLng;
      this.lat = newLat;
    } else {
      this.speed = 0;
    }
  }

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

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

/* ---------------- MAP + GAME ---------------- */
let map;
let car;
let canvas, ctx;
let lastTime = performance.now();

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

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

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

  car = new Car(173.284, -41.270);

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

function resizeCanvas() {
  canvas.width = window.innerWidth;
  canvas.height = window.innerHeight;
}

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

  car.update(dt);

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

</body>
</html>