import * as Colors from "./colors.js"; import {Vec2, Vec3} from "./common.js"; import {Texture} from "./graphics.js"; export enum TileEdge { None, Left, Right, Both, } export class Tile { fill: Texture | Colors.Color = [1, 0, 1, 1]; edge: TileEdge = TileEdge.None; constructor(fill?: Texture | Colors.Color, edge?: TileEdge) { if (fill !== undefined) this.fill = fill; if (edge !== undefined) this.edge = edge; } } export class Grid { position: Vec3; tiles3d: Tile[][]; tileSize: number; width: number; length: number; height: number; constructor(position: Vec3, tileSize: number, width: number, length: number, height: number) { this.tiles3d = new Array(height); this.position = position; this.tileSize = tileSize; this.width = width; this.length = length; this.height = height; let layer = new Array(width * length); for (let i = 0; i < this.height; ++i) this.tiles3d[i] = {...layer}; } fillLayer(tile: Tile, z: number) { for (let i = 0; i < this.width; ++i) { for (let j = 0; j < this.length; ++j) { this.tiles3d[z][i + j * this.width] = {...tile}; } } for (let i = 0; i < this.width; ++i) { this.tiles3d[z][this.length * i - 1] = {...tile, edge: TileEdge.Right}; } for (let i = 0; i < this.length - 1; ++i) { this.tiles3d[z][this.width * (this.length - 1) + i] = {...tile, edge: TileEdge.Left}; } this.tiles3d[z][this.width * this.length - 1] = {...tile, edge: TileEdge.Both}; } setTile(tile: Tile, coord: Vec3) { let index = coord.x + coord.y * this.width; this.tiles3d[coord.z][index] = {...tile}; } getTile(coord: Vec3): Tile | null { 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: Grid, position: Vec2) { grid.setTile(new Tile(Colors.Brown, TileEdge.Both), new Vec3(position.x, position.y, 1)); grid.setTile(new Tile(Colors.Green, TileEdge.Both), new Vec3(position.x, position.y, 4)); grid.setTile(new Tile(Colors.Green, TileEdge.Both), new Vec3(position.x, position.y + 1, 3)); grid.setTile(new Tile(Colors.Green, TileEdge.Both), new Vec3(position.x, position.y + 1, 4)); grid.setTile(new Tile(Colors.Green, TileEdge.Both), new Vec3(position.x, position.y, 5)); grid.setTile(new Tile(Colors.Green, TileEdge.Both), new Vec3(position.x + 1, position.y, 3)); grid.setTile(new Tile(Colors.Green, TileEdge.Both), new Vec3(position.x + 1, position.y, 4)); grid.setTile(new Tile(Colors.Green, TileEdge.Both), new Vec3(position.x, position.y, 6)); }