UNPKG

@erikyuzwa/rogue-punk

Version:

a JavaScript library to help you build your roguelike adventures

42 lines (41 loc) 1.28 kB
import * as ROT from 'rot-js'; export class Stage { constructor(options) { this.options = { ...options }; this.width = options.width || 60; this.height = options.height || 30; this.id = options.id || 'stage'; this.display = new ROT.Display(this.options); this.displayContainer = null; this.cameraTarget = null; this.center = null; this.setDimensions(this.width, this.height); this.attachToElement(); } setDimensions(x, y) { this.width = x; this.height = y; this.center.x = Math.round(x / 2); this.center.y = Math.round(y / 2); } attachToElement() { this.displayContainer = document.getElementById(this.id); this.displayContainer.appendChild(this.display.getContainer()); } getDisplay() { return this.display; } clear() { this.display.clear(); } setCameraTarget(cameraTarget) { this.cameraTarget = cameraTarget; } draw(x, y, glyph, fgColor, bgColor) { if (this.cameraTarget) { x += (this.center.x - this.cameraTarget.x); y += (this.center.y - this.cameraTarget.y); } return this.display.draw(x, y, glyph, fgColor, bgColor); } }