UNPKG

@erikyuzwa/rogue-punk

Version:

a JavaScript library to help you build your roguelike adventures

131 lines (130 loc) 3.85 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Map = void 0; const ROT = require("rot-js"); const Tile_1 = require("./Tile"); const Vector3d_1 = require("./Vector3d"); class Map { constructor(tiles, player) { this.tiles = tiles; this.width = tiles[0].length; this.height = tiles[0][0].length; this.depth = tiles.length; this.entities = {}; this.items = {}; this.scheduler = new ROT.Scheduler.Simple(); this.engine = new ROT.Engine(this.scheduler); this.player = player; } getWidth() { return this.width; } getHeight() { return this.height; } getDepth() { return this.depth; } getTile(pos) { if (pos.x < 0 || pos.x >= this.width || pos.y < 0 || pos.y >= this.height || pos.z < 0 || pos.z >= this.depth) { return Tile_1.nullTile; } else { return this.tiles[pos.z][pos.x][pos.y] || Tile_1.nullTile; } } dig(pos) { if (this.getTile(pos).isDiggable()) { this.tiles[z][x][y] = Tile_1.floorTile; } } getRandomFloorPosition(z) { const pos = new Vector3d_1.Vector3d(0, 0, 0); do { pos.x = Math.floor(Math.random() * this.width); pos.y = Math.floor(Math.random() * this.height); } while (this.getTile(pos) !== Tile_1.floorTile || this.getEntityAt(pos.x, pos.y, z)); return pos; } getEngine() { return this.engine; } getEntities() { return this.entities; } getEntityAt(x, y, z) { return this.entities[x + ',' + y + ',' + z]; } addEntity(entity) { entity.setMap(this); this.updateEntityPosition(entity); this.scheduler.add(entity, true); } addEntityAtRandomPosition(entity, z) { const position = this.getRandomFloorPosition(z); entity.x = position.x; entity.y = position.y; entity.z = position.z; this.addEntity(entity); } removeEntity(entity) { const key = entity.x + ',' + entity.y + ',' + entity.z; if (this.entities[key] === entity) { delete this.entities[key]; } } ; updateEntityPosition(entity, oldPosition) { if (oldPosition) { const oldKey = oldPosition.x + ',' + oldPosition.y + ',' + oldPosition.z; if (this.entities[oldKey] === entity) { delete this.entities[oldKey]; } } if (entity.x < 0 || entity.x >= this.width || entity.y < 0 || entity.y >= this.height) { throw new Error("Entity's position is out of bounds."); } const key = entity.x + ',' + entity.y + ',' + entity.z; if (this.entities[key]) { throw new Error('Tried to add an entity at an occupied position.'); } this.entities[key] = entity; } getItemsAt(x, y, z) { return this.items[x + ',' + y + ',' + z]; } setItemsAt(x, y, z, items) { const key = x + ',' + y + ',' + z; if (items.length === 0) { if (this.items[key]) { delete this.items[key]; } } else { this.items[key] = items; } } addItem(x, y, z, item) { const key = x + ',' + y + ',' + z; if (this.items[key]) { this.items[key].push(item); } else { this.items[key] = [item]; } } addItemAtRandomPosition(item, z) { const position = this.getRandomFloorPosition(z); this.addItem(position.x, position.y, position.z, item); } getPlayer() { return this.player; } } exports.Map = Map;