File size: 1,953 Bytes
b29710c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import * as THREE from "three";
import {
  PATH_POINTS,
  ROAD_HALF_WIDTH,
  GRID_CELL_SIZE,
} from "../config/gameConfig.js";

export class PathBuilder {
  constructor(scene) {
    this.scene = scene;
    // Clone and snap path points to the grid to ensure alignment
    this.pathPoints = PATH_POINTS.map((p) => {
      const snapped = p.clone
        ? p.clone()
        : new THREE.Vector3(p.x, p.y ?? 0, p.z);
      snapped.x = Math.round(snapped.x / GRID_CELL_SIZE) * GRID_CELL_SIZE;
      snapped.z = Math.round(snapped.z / GRID_CELL_SIZE) * GRID_CELL_SIZE;
      return snapped;
    });
    this.roadMeshes = [];

    // Materials
    this.roadMat = new THREE.MeshStandardMaterial({
      color: 0x393c41,
      metalness: 0.1,
      roughness: 0.9,
    });
  }

  buildPath() {
    // Visualize path line
    this.createPathLine();

    // Build straight road segments only (no bevels or rounded corners)
    for (let i = 0; i < this.pathPoints.length - 1; i++) {
      this.addSegment(this.pathPoints[i], this.pathPoints[i + 1]);
    }
  }

  createPathLine() {
    const pathLineMat = new THREE.LineBasicMaterial({ color: 0xffff00 });
    const pathLineGeo = new THREE.BufferGeometry().setFromPoints(
      this.pathPoints
    );
    const pathLine = new THREE.Line(pathLineGeo, pathLineMat);
    pathLine.position.y = 0.01;
    this.scene.add(pathLine);
  }

  addSegment(a, b) {
    const seg = new THREE.Vector3().subVectors(b, a);
    const len = seg.length();
    if (len <= 0.0001) return;

    const mid = new THREE.Vector3().addVectors(a, b).multiplyScalar(0.5);

    const roadGeo = new THREE.BoxGeometry(len, 0.1, ROAD_HALF_WIDTH * 2);
    const road = new THREE.Mesh(roadGeo, this.roadMat);
    road.castShadow = false;
    road.receiveShadow = true;
    road.position.set(mid.x, 0.05, mid.z);
    const angle = Math.atan2(seg.z, seg.x);
    road.rotation.y = -angle;
    this.scene.add(road);
    this.roadMeshes.push(road);
  }
}