107 lines
4.3 KiB
JavaScript
107 lines
4.3 KiB
JavaScript
import { Vec3 } from "./common.js";
|
|
import { Sprite } from "./graphics.js";
|
|
export var TileEdge;
|
|
(function (TileEdge) {
|
|
TileEdge[TileEdge["None"] = 0] = "None";
|
|
TileEdge[TileEdge["Left"] = 1] = "Left";
|
|
TileEdge[TileEdge["Right"] = 2] = "Right";
|
|
TileEdge[TileEdge["Both"] = 3] = "Both";
|
|
})(TileEdge || (TileEdge = {}));
|
|
export class Tile {
|
|
fill = {
|
|
top: [1, 0, 1, 1],
|
|
left: [0, 0, 0, 1],
|
|
right: [0, 0, 0, 1],
|
|
};
|
|
edge = TileEdge.None;
|
|
constructor(fill, edge) {
|
|
if (fill !== undefined)
|
|
this.fill = fill;
|
|
if (edge !== undefined)
|
|
this.edge = edge;
|
|
}
|
|
}
|
|
export function ColorToTile(c) {
|
|
return {
|
|
left: c,
|
|
top: c,
|
|
right: c,
|
|
};
|
|
}
|
|
export class Grid {
|
|
position;
|
|
tiles3d;
|
|
tileSize;
|
|
width;
|
|
breadth;
|
|
height;
|
|
topHeight;
|
|
constructor(position, tileSize, width, breadth, height) {
|
|
this.tiles3d = new Array(height);
|
|
this.position = position;
|
|
this.tileSize = tileSize;
|
|
this.width = width;
|
|
this.breadth = breadth;
|
|
this.height = height;
|
|
this.topHeight = 0;
|
|
let layer = new Array(width * breadth);
|
|
for (let i = 0; i < this.height; ++i)
|
|
this.tiles3d[i] = { ...layer };
|
|
}
|
|
fillLayer(tile, z) {
|
|
if (z + 1 > this.topHeight)
|
|
this.topHeight = z + 1;
|
|
for (let i = 0; i < this.width; ++i) {
|
|
for (let j = 0; j < this.breadth; ++j) {
|
|
this.tiles3d[z][i + j * this.width] = { ...tile };
|
|
}
|
|
}
|
|
for (let i = 0; i < this.width; ++i) {
|
|
this.tiles3d[z][this.breadth * i - 1] = { ...tile, edge: TileEdge.Right };
|
|
}
|
|
for (let i = 0; i < this.breadth - 1; ++i) {
|
|
this.tiles3d[z][this.width * (this.breadth - 1) + i] = { ...tile, edge: TileEdge.Left };
|
|
}
|
|
this.tiles3d[z][this.width * this.breadth - 1] = { ...tile, edge: TileEdge.Both };
|
|
}
|
|
setTile(tile, coord) {
|
|
let index = coord.x + coord.y * this.width;
|
|
this.tiles3d[coord.z][index] = { ...tile };
|
|
if (coord.z + 1 > this.topHeight)
|
|
this.topHeight = coord.z + 1;
|
|
}
|
|
getTile(coord) {
|
|
let index = coord.x + coord.y * this.width;
|
|
let tile = this.tiles3d[coord.z][index];
|
|
if (tile === undefined)
|
|
return null;
|
|
return tile;
|
|
}
|
|
}
|
|
export function tree(grid, position) {
|
|
let log = Sprite.tile(2);
|
|
let leaves = Sprite.tile(1);
|
|
grid.setTile(new Tile(log, TileEdge.Both), new Vec3(position.x, position.y, position.z + 1));
|
|
grid.setTile(new Tile(log, TileEdge.Both), new Vec3(position.x, position.y, position.z + 2));
|
|
grid.setTile(new Tile(log, TileEdge.Both), new Vec3(position.x, position.y, position.z + 3));
|
|
grid.setTile(new Tile(log, TileEdge.Both), new Vec3(position.x, position.y, position.z + 4));
|
|
grid.setTile(new Tile(log, TileEdge.None), new Vec3(position.x, position.y, position.z + 4));
|
|
grid.setTile(new Tile(leaves, TileEdge.Both), new Vec3(position.x, position.y + 1, position.z + 4));
|
|
grid.setTile(new Tile(leaves, TileEdge.Both), new Vec3(position.x, position.y + 1, position.z + 5));
|
|
grid.setTile(new Tile(leaves, TileEdge.Both), new Vec3(position.x, position.y, position.z + 5));
|
|
grid.setTile(new Tile(leaves, TileEdge.Both), new Vec3(position.x + 1, position.y, position.z + 4));
|
|
grid.setTile(new Tile(leaves, TileEdge.Both), new Vec3(position.x + 1, position.y, position.z + 5));
|
|
grid.setTile(new Tile(leaves, TileEdge.Both), new Vec3(position.x + 1, position.y + 1, position.z + 4));
|
|
grid.setTile(new Tile(leaves, TileEdge.Both), new Vec3(position.x + 1, position.y + 1, position.z + 5));
|
|
}
|
|
export function bush(grid, position) {
|
|
let leaves = Sprite.tile(1);
|
|
grid.setTile(new Tile(leaves, TileEdge.Both), position.extend(1));
|
|
grid.setTile(new Tile(leaves, TileEdge.Both), new Vec3(position.x + 1, position.y, 1));
|
|
grid.setTile(new Tile(leaves, TileEdge.Both), new Vec3(position.x, position.y + 1, 1));
|
|
grid.setTile(new Tile(leaves, TileEdge.Both), new Vec3(position.x + 1, position.y + 1, 1));
|
|
grid.setTile(new Tile(leaves, TileEdge.Both), position.extend(2));
|
|
grid.setTile(new Tile(leaves, TileEdge.Both), new Vec3(position.x + 1, position.y, 2));
|
|
grid.setTile(new Tile(leaves, TileEdge.Both), new Vec3(position.x, position.y + 1, 2));
|
|
}
|