190 lines
6.8 KiB
JavaScript
190 lines
6.8 KiB
JavaScript
|
|
export class UI {
|
||
|
|
constructor(ctx, game) {
|
||
|
|
this.ctx = ctx;
|
||
|
|
this.game = game;
|
||
|
|
this.messageLog = document.getElementById('message-log');
|
||
|
|
this.inventoryList = document.getElementById('inventory-list');
|
||
|
|
this.popup = null;
|
||
|
|
this.popupTimeout = null;
|
||
|
|
}
|
||
|
|
|
||
|
|
log(message) {
|
||
|
|
if (!this.messageLog) return;
|
||
|
|
const div = document.createElement('div');
|
||
|
|
div.textContent = message;
|
||
|
|
this.messageLog.appendChild(div);
|
||
|
|
this.messageLog.scrollTop = this.messageLog.scrollHeight;
|
||
|
|
}
|
||
|
|
|
||
|
|
renderMap(map, player, monsters, items, tileSize) {
|
||
|
|
// Fill background
|
||
|
|
this.ctx.fillStyle = '#000000';
|
||
|
|
this.ctx.fillRect(0, 0, this.ctx.canvas.width, this.ctx.canvas.height);
|
||
|
|
|
||
|
|
this.ctx.font = `${tileSize}px monospace`;
|
||
|
|
this.ctx.textBaseline = 'top';
|
||
|
|
|
||
|
|
// Calculate viewport (center on player)
|
||
|
|
const viewWidth = Math.ceil(this.ctx.canvas.width / tileSize);
|
||
|
|
const viewHeight = Math.ceil(this.ctx.canvas.height / tileSize);
|
||
|
|
|
||
|
|
const startX = player.x - Math.floor(viewWidth / 2);
|
||
|
|
const startY = player.y - Math.floor(viewHeight / 2);
|
||
|
|
|
||
|
|
for (let y = 0; y < viewHeight; y++) {
|
||
|
|
for (let x = 0; x < viewWidth; x++) {
|
||
|
|
const mapX = startX + x;
|
||
|
|
const mapY = startY + y;
|
||
|
|
|
||
|
|
if (mapX >= 0 && mapX < map.width && mapY >= 0 && mapY < map.height) {
|
||
|
|
const tile = map.tiles[mapY][mapX];
|
||
|
|
this.drawTile(x * tileSize, y * tileSize, tile, tileSize);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Draw Items
|
||
|
|
if (items) {
|
||
|
|
for (const item of items) {
|
||
|
|
const screenX = (item.x - startX) * tileSize;
|
||
|
|
const screenY = (item.y - startY) * tileSize;
|
||
|
|
if (screenX >= -tileSize && screenX < this.ctx.canvas.width &&
|
||
|
|
screenY >= -tileSize && screenY < this.ctx.canvas.height) {
|
||
|
|
this.drawEntity(screenX, screenY, item, tileSize);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Draw Monsters
|
||
|
|
for (const monster of monsters) {
|
||
|
|
const screenX = (monster.x - startX) * tileSize;
|
||
|
|
const screenY = (monster.y - startY) * tileSize;
|
||
|
|
// Only draw if visible
|
||
|
|
if (screenX >= -tileSize && screenX < this.ctx.canvas.width &&
|
||
|
|
screenY >= -tileSize && screenY < this.ctx.canvas.height) {
|
||
|
|
this.drawEntity(screenX, screenY, monster, tileSize);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Draw Player
|
||
|
|
const playerScreenX = (player.x - startX) * tileSize;
|
||
|
|
const playerScreenY = (player.y - startY) * tileSize;
|
||
|
|
|
||
|
|
this.drawEntity(playerScreenX, playerScreenY, player, tileSize);
|
||
|
|
|
||
|
|
this.drawPopup();
|
||
|
|
}
|
||
|
|
|
||
|
|
updateStats(player, depth) {
|
||
|
|
document.getElementById('stat-hp').textContent = player.hp;
|
||
|
|
document.getElementById('stat-max-hp').textContent = player.maxHp;
|
||
|
|
document.getElementById('stat-str').textContent = player.stats.str;
|
||
|
|
document.getElementById('stat-level').textContent = player.level;
|
||
|
|
document.getElementById('stat-xp').textContent = player.xp;
|
||
|
|
document.getElementById('stat-max-xp').textContent = player.maxXp;
|
||
|
|
document.getElementById('stat-ac').textContent = player.getDefense();
|
||
|
|
document.getElementById('stat-floor').textContent = depth;
|
||
|
|
document.getElementById('stat-gold').textContent = player.gold;
|
||
|
|
}
|
||
|
|
|
||
|
|
updateInventory(player) {
|
||
|
|
this.inventoryList.innerHTML = '';
|
||
|
|
player.inventory.forEach((item, index) => {
|
||
|
|
const li = document.createElement('li');
|
||
|
|
li.textContent = item.name;
|
||
|
|
if (player.equipment.weapon === item || player.equipment.armor === item || player.equipment.shield === item) {
|
||
|
|
li.textContent += " (E)";
|
||
|
|
}
|
||
|
|
li.style.cursor = 'pointer';
|
||
|
|
li.onclick = () => {
|
||
|
|
this.game.useItem(index);
|
||
|
|
};
|
||
|
|
this.inventoryList.appendChild(li);
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
drawTile(screenX, screenY, tile, size) {
|
||
|
|
if (tile === '#') {
|
||
|
|
this.ctx.fillStyle = '#808080'; // Gray wall
|
||
|
|
this.ctx.fillRect(screenX, screenY, size, size);
|
||
|
|
// Add a bevel effect for walls
|
||
|
|
this.ctx.strokeStyle = '#ffffff';
|
||
|
|
this.ctx.lineWidth = 2;
|
||
|
|
this.ctx.beginPath();
|
||
|
|
this.ctx.moveTo(screenX, screenY + size);
|
||
|
|
this.ctx.lineTo(screenX, screenY);
|
||
|
|
this.ctx.lineTo(screenX + size, screenY);
|
||
|
|
this.ctx.stroke();
|
||
|
|
|
||
|
|
this.ctx.strokeStyle = '#404040';
|
||
|
|
this.ctx.beginPath();
|
||
|
|
this.ctx.moveTo(screenX + size, screenY);
|
||
|
|
this.ctx.lineTo(screenX + size, screenY + size);
|
||
|
|
this.ctx.lineTo(screenX, screenY + size);
|
||
|
|
this.ctx.stroke();
|
||
|
|
|
||
|
|
} else if (tile === '>') {
|
||
|
|
// Stairs
|
||
|
|
this.ctx.fillStyle = '#202020';
|
||
|
|
this.ctx.fillRect(screenX, screenY, size, size);
|
||
|
|
this.ctx.fillStyle = '#ffffff';
|
||
|
|
this.ctx.fillText('>', screenX + size / 4, screenY);
|
||
|
|
} else {
|
||
|
|
// Floor
|
||
|
|
this.ctx.fillStyle = '#202020';
|
||
|
|
this.ctx.fillRect(screenX, screenY, size, size);
|
||
|
|
this.ctx.fillStyle = '#404040';
|
||
|
|
this.ctx.fillText('.', screenX + size / 4, screenY);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
showPopup(text, duration) {
|
||
|
|
this.popup = text;
|
||
|
|
if (this.popupTimeout) clearTimeout(this.popupTimeout);
|
||
|
|
this.game.render();
|
||
|
|
|
||
|
|
this.popupTimeout = setTimeout(() => {
|
||
|
|
this.popup = null;
|
||
|
|
this.game.render();
|
||
|
|
}, duration);
|
||
|
|
}
|
||
|
|
|
||
|
|
drawPopup() {
|
||
|
|
if (!this.popup) return;
|
||
|
|
|
||
|
|
const ctx = this.ctx;
|
||
|
|
ctx.font = '16px "Courier New", monospace';
|
||
|
|
const textMetrics = ctx.measureText(this.popup);
|
||
|
|
const textWidth = textMetrics.width;
|
||
|
|
|
||
|
|
const width = textWidth + 40; // Add padding
|
||
|
|
const height = 50;
|
||
|
|
const x = (this.ctx.canvas.width - width) / 2;
|
||
|
|
const y = (this.ctx.canvas.height - height) / 2 - 50; // Slightly above center
|
||
|
|
|
||
|
|
// Background
|
||
|
|
ctx.fillStyle = '#ffffff';
|
||
|
|
ctx.fillRect(x, y, width, height);
|
||
|
|
|
||
|
|
// Border
|
||
|
|
ctx.strokeStyle = '#000000';
|
||
|
|
ctx.lineWidth = 2;
|
||
|
|
ctx.strokeRect(x, y, width, height);
|
||
|
|
|
||
|
|
// Text
|
||
|
|
ctx.fillStyle = '#000000';
|
||
|
|
ctx.textAlign = 'center';
|
||
|
|
ctx.textBaseline = 'middle';
|
||
|
|
ctx.fillText(this.popup, x + width / 2, y + height / 2);
|
||
|
|
|
||
|
|
// Reset text align
|
||
|
|
ctx.textAlign = 'start';
|
||
|
|
ctx.textBaseline = 'alphabetic';
|
||
|
|
}
|
||
|
|
|
||
|
|
drawEntity(screenX, screenY, entity, size) {
|
||
|
|
this.ctx.fillStyle = entity.color;
|
||
|
|
this.ctx.fillText(entity.symbol, screenX + size / 4, screenY);
|
||
|
|
}
|
||
|
|
}
|