UNPKG

shellquest

Version:

Terminal-based procedurally generated dungeon crawler

90 lines (87 loc) 2.98 kB
/** * Lighting System - Main export file * * Complete dynamic lighting with flickering torches for dungeon environments. * * Features: * - Point light sources with smooth radius falloff * - Flickering effect using Perlin noise for realistic candle simulation * - Player torch that follows the player * - Wall torch placement during dungeon generation * - Darkness overlay for unlit areas (configurable ambient level) * - Performance optimized for visible viewport only * * Usage in ZombieAttackGame: * * 1. Import: * import {DungeonLighting} from './lighting/DungeonLighting.ts'; * import {applyLighting} from './lighting/LightingRenderer.ts'; * * 2. Add property to class: * private dungeonLighting: DungeonLighting | null = null; * * 3. Initialize after level generation (in initializeSync): * if (this.level instanceof DungeonLevel) { * this.dungeonLighting = new DungeonLighting({ * ambientLight: 0.08, // Darkness level (0 = pitch black, 1 = fully lit) * }); * * // Place wall torches - uses the internal grid from DungeonLevel * // You'll need to expose the grid or call this from DungeonLevel * const grid = this.level.getGrid(); // Add this method to DungeonLevel * this.dungeonLighting.placeTorches(grid, MAP_WIDTH, MAP_HEIGHT, seedHash); * * // Initialize player torch * this.dungeonLighting.initializePlayerTorch(player.x, player.y); * } * * 4. Update each tick (in update method): * if (this.dungeonLighting) { * this.dungeonLighting.update(deltaTime); * this.dungeonLighting.updatePlayerTorch( * this.level.player.x + TILE_SIZE / 2, * this.level.player.y + TILE_SIZE / 2 * ); * } * * 5. Apply lighting in render (after compositing layers, before buffer.render): * // Composite all layers to mainCanvas * for (const layer of this.layers) { * this.mainCanvas.drawCanvas(layer); * } * * // Apply lighting * if (this.dungeonLighting) { * const lightMap = this.dungeonLighting.computeLightMap( * this.mainCanvas.width, * this.mainCanvas.height, * this.camera.x, * this.camera.y * ); * applyLighting(this.mainCanvas, lightMap, true); * } * * // Render to buffer * this.mainCanvas.render(buffer, x, y); */ // Main exports export {LightingSystem, type LightSource, type LightingConfig} from './LightingSystem.ts'; export { DungeonLighting, type TorchPosition, type TorchPlacementConfig, } from './DungeonLighting.ts'; export { applyLighting, renderWithLighting, applySimpleDarkness, type LightingRenderConfig, } from './LightingRenderer.ts'; // Re-export factory functions export { createTorchLight, createPlayerTorchLight, TORCH_LIGHT_CONFIG, PLAYER_TORCH_CONFIG, DEFAULT_LIGHTING_CONFIG, } from './LightingSystem.ts';