UNPKG

shellquest

Version:

Terminal-based procedurally generated dungeon crawler

53 lines (42 loc) 1.49 kB
import type {ZombieAttackGame} from './ZombieAttackGame.ts'; import {TILE_SIZE} from './TileMap.ts'; import {MAP_HEIGHT, MAP_WIDTH} from './constants.ts'; export class Camera { constructor(private game: ZombieAttackGame) {} // Camera position in pixels x: number = 0; y: number = 0; // Target positions for smooth interpolation targetX: number = 0; targetY: number = 0; get viewportWidth(): number { return this.game.width; } get viewportHeight(): number { return this.game.height; } update(playerPixelX: number, playerPixelY: number): void { // Calculate ideal camera position to center player const centerX = this.viewportWidth / 2; const centerY = this.viewportHeight / 2; // Calculate ideal camera position in pixels this.targetX = playerPixelX - centerX + TILE_SIZE / 2; this.targetY = playerPixelY - centerY + TILE_SIZE / 2; // Clamp to map bounds in pixels const maxX = MAP_WIDTH * TILE_SIZE - this.viewportWidth; const maxY = MAP_HEIGHT * TILE_SIZE - this.viewportHeight; if (this.targetX < 0) { this.targetX = 0; } else if (this.targetX > maxX) { this.targetX = maxX; } if (this.targetY < 0) { this.targetY = 0; } else if (this.targetY > maxY) { this.targetY = maxY; } // Smooth interpolation (instant for now, can be smoothed later) this.x = this.targetX; this.y = this.targetY; } }