Skip to content

Commit

Permalink
add map border (closes #58)
Browse files Browse the repository at this point in the history
  • Loading branch information
myin142 committed Jun 3, 2023
1 parent 6af74eb commit 98afdfc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
20 changes: 15 additions & 5 deletions src/ui/components/tilemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export class TileMap {
private tiles: number[][] = [];
private canvas: HTMLCanvasElement;
private cursor: HTMLImageElement;
private mapSize: Vector = { x: 79, y: 21 }; // Fixed map size? Might change in other version?

constructor(root: HTMLElement, private tileSet: TileSet) {
this.canvas = document.createElement('canvas');
Expand Down Expand Up @@ -91,6 +92,12 @@ export class TileMap {
private clearCanvas() {
this.cursor.style.display = 'none';
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);

const map = mult(this.mapSize, this.tileSize);
const localPos = this.localToCanvas({ x: 1, y: 0 });

this.context.fillStyle = '#111';
this.context.fillRect(localPos.x, localPos.y, map.x, map.y);
}

private rerender() {
Expand Down Expand Up @@ -121,11 +128,7 @@ export class TileMap {

const source = this.tileSet.getCoordinateForTile(tile);
const size = this.tileSet.tileSize;

const globalPos = this.toGlobal({ x, y });
const globalCenter = this.toGlobal(this.center);
const relPosFromCenter = sub(globalPos, globalCenter);
const localPos = add(this.canvasCenter, relPosFromCenter);
const localPos = this.localToCanvas({ x, y });

this.context.drawImage(
// Source
Expand All @@ -142,6 +145,13 @@ export class TileMap {
);
}

private localToCanvas(pos: Vector) {
const globalPos = this.toGlobal(pos);
const globalCenter = this.toGlobal(this.center);
const relPosFromCenter = sub(globalPos, globalCenter);
return add(this.canvasCenter, relPosFromCenter);
}

private toGlobal(vec: Vector): Vector {
return mult(vec, this.tileSize);
}
Expand Down
1 change: 0 additions & 1 deletion src/ui/screens/game-screen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ export class GameScreen extends Screen {
}

public openQuestion(question: string, choices: string[], defaultChoice: string) {
// this.console.appendLine(`${question} ${choices.map(c => c === defaultChoice ? `<strong style="color: red">${c}</strong>` : c)}`);
const dialog = new Question(question, choices, defaultChoice);
this.elem.appendChild(dialog.elem);
}
Expand Down

0 comments on commit 98afdfc

Please sign in to comment.