Simple Pixel Game with Web Workers
A pixel-style terrain exploration game built with SvelteKit, TypeScript, and Web Workers. This project demonstrates modern web game development techniques with a focus on performance and maintainability.
Features
- š® Real-time terrain exploration
- š· Web Worker-based game logic
- š Performance monitoring
- šØ Dynamic animations (clouds, water, rivers)
- šÆ Responsive controls
- š± Mobile-friendly design
- š Theme support
- š Debug tools
Technical Stack
- Framework: SvelteKit
- Language: TypeScript
- Styling: Tailwind CSS
- Architecture: Web Workers for game logic
- Performance: RAF-based animation system
- State Management: Custom game state handler
Game Elements
Terrain Types
- š Water (W) - Impassable boundary
- š Beach (B) - Transitional zone
- š² Forest (F) - Standard terrain with cover
- ā° Mountains (M) - Impassable peaks
- š± Grassland (G) - Open terrain
- š River (R) - Crossable water
- š„¾ Swamp (S) - Difficult terrain
- š Hills (H) - Elevated terrain
Animations
- Cloud system with dynamic generation and movement
- Water wave effects
- River flow animations
- Character movement with direction-based sprites
- FPS monitoring
- Memory usage tracking
- Idle state management
- Frame throttling
- Efficient canvas rendering
Debug Features
- Real-time performance metrics
- Game state monitoring
- Terrain information
- Worker status tracking
- Animation controls
Code Structure
src/
āāā lib/
ā āāā components/
ā ā āāā DebugPanel.svelte # Performance monitoring component
ā ā āāā Controls.svelte # Game controls component
ā āāā game/
ā ā āāā Game.ts # Main game logic
ā ā āāā draw.ts # Canvas rendering functions
ā ā āāā state.ts # Game state management
ā āāā workers/
ā ā āāā worker.ts # Web Worker game logic
ā āāā types.ts # TypeScript definitions
ā āāā theme.ts # Theming system
āāā routes/
āāā +page.svelte # Main game component
How Web Workers Work
Web Workers allow you to run JavaScript in background threads. They can perform tasks without interfering with the user interface. This is especially useful for tasks that are computationally expensive or need to run asynchronously.
Web Worker Architecture
graph TD;
A[Main Thread] -->|On Message| B[Web Worker]
B -->|Post Message| A
A -->|Render| C[Canvas]
B -->|Game Logic| D[Game State]
How It Works
- Main Thread: Handles rendering and user input.
- Web Worker: Processes game logic and state management.
- Communication: Main thread and worker communicate via
postMessage
and onmessage
.
- Rendering: Main thread updates the canvas based on the game state.
Example Code
Main Thread
const worker = new Worker('worker.js');
worker.onmessage = (e) => {
const { gameState } = e.data;
// Update the game state and render
};
worker.postMessage({ task: 'START' });
Web Worker
self.onmessage = (e) => {
const { task } = e.data;
if (task === 'START') {
// Initialize game state
postMessage({ gameState });
}
};
Documentation
Game Features
Terrain System
- Different terrain types with unique properties
- Collision detection
- Movement cost calculations
Animation System
- Cloud generation and movement
- Water wave effects
- River flow animations
- Character animations
Performance Optimization
- Web Worker offloading
- Frame rate management
- Idle state handling
- Memory monitoring
Debug Tools
- FPS counter
- Memory usage
- Game state viewer
- Animation controls
Controls
- Arrow keys or WASD for movement
- Toggle panels:
- Debug information
- Terrain legend
- Animation settings
Development
Install dependencies:
bun install
Start development server:
bun run dev
Build for production:
bun run build
Architecture
The game uses a multi-threaded architecture:
- Main Thread: Handles rendering and user input
- Web Worker: Processes game logic and state management
- RAF Loop: Manages animations and frame timing
- Event System: Coordinates state updates
- Web Worker offloads computation
- Frame throttling based on idle state
- Efficient canvas rendering
- Memory usage optimization
- Animation frame management
License
MIT License - Feel free to use and modify
Credits
Created as a demonstration of modern web game development techniques using SvelteKit and TypeScript.