90 lines
2.3 KiB
TypeScript
90 lines
2.3 KiB
TypeScript
import Entity from "./entity";
|
|
import type Item from "./item";
|
|
|
|
export enum TileType {
|
|
NORMAL,
|
|
START,
|
|
DOOR,
|
|
LOCKED_DOOR,
|
|
DOORWAY,
|
|
END,
|
|
}
|
|
|
|
export default class Tile extends Entity {
|
|
public connections: Tile[] = [];
|
|
public items: Item[] = [];
|
|
public isOpen = false;
|
|
|
|
constructor(position: [number, number], size: number, public type: TileType = TileType.NORMAL) {
|
|
super(position, [size, size]);
|
|
}
|
|
|
|
protected draw(ctx: CanvasRenderingContext2D) {
|
|
if (this.hovered) {
|
|
ctx.fillStyle = `rgba(255, 255, 255, 0.2)`;
|
|
ctx.fillRect(0, 0, 1, 1);
|
|
|
|
// ctx.lineWidth = 1 / this.width;
|
|
// ctx.strokeStyle = 'red';
|
|
|
|
// for (const tile of this.connections) {
|
|
// const center = [
|
|
// 0.5 + (tile.centerX - this.centerX) / this.width,
|
|
// 0.5 + (tile.centerY - this.centerY) / this.height,
|
|
// ];
|
|
|
|
// ctx.beginPath();
|
|
// ctx.moveTo(0.5, 0.5);
|
|
// ctx.lineTo(center[0], center[1]);
|
|
// ctx.stroke();
|
|
// }
|
|
}
|
|
if (this.items.length > 0) {
|
|
if (this.isOpen) {
|
|
const item = this.items[0];
|
|
ctx.drawImage(item.type, 0.1, 0.1, 0.8, 0.8);
|
|
|
|
if (this.items.length > 1) {
|
|
ctx.strokeStyle = 'red';
|
|
ctx.lineWidth = 2 / this.width;
|
|
|
|
ctx.strokeRect(0.1, 0.1, 0.8, 0.8);
|
|
}
|
|
} else {
|
|
ctx.fillStyle = 'white';
|
|
ctx.fillRect(0.1, 0.1, 0.8, 0.8);
|
|
|
|
ctx.fillStyle = 'black';
|
|
ctx.font = '0.2px Arial';
|
|
ctx.fillText('❓', 0.5, 0.5);
|
|
}
|
|
}
|
|
}
|
|
|
|
get enemy() {
|
|
return this.items.find(i => i.isEnemy);
|
|
}
|
|
|
|
public open() {
|
|
this.isOpen = true;
|
|
}
|
|
|
|
public removeItem(item: Item | null | undefined): boolean {
|
|
if (!item) {
|
|
return false;
|
|
}
|
|
const itemIndex = this.items.findIndex(i => i === item);
|
|
|
|
if (itemIndex >= 0) {
|
|
this.items.splice(itemIndex, 1);
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public killEnemy() {
|
|
this.removeItem(this.enemy);
|
|
}
|
|
}
|