-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMapGenerator.js
43 lines (38 loc) · 1.01 KB
/
MapGenerator.js
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
// MapGenerator.js
export class Tile {
constructor(x, y, terrainType, object = null) {
this.x = x;
this.y = y;
this.terrainType = terrainType; // 'grass', 'water', 'mountain', etc.
this.object = object; // 'tree', 'building', etc.
}
}
export class Map {
constructor(width, height) {
this.width = width;
this.height = height;
this.tiles = [];
// Initialize tiles
for (let x = 0; x < width; x++) {
this.tiles[x] = [];
for (let y = 0; y < height; y++) {
// Initialize all tiles to 'grass'
const terrainType = (x + y) % 2 === 0 ? 'grass' : 'ground';
this.tiles[x][y] = new Tile(x, y, terrainType);
}
}
}
setTerrain(x, y, terrainType) {
if (this.isValidTile(x, y)) {
this.tiles[x][y].terrainType = terrainType;
}
}
placeObject(x, y, objectType) {
if (this.isValidTile(x, y)) {
this.tiles[x][y].object = objectType;
}
}
isValidTile(x, y) {
return x >= 0 && x < this.width && y >= 0 && y < this.height;
}
}