This is a 2D grid-based adventure game where players can:
- Move around a grid-based world
- Collect items like coins, health packs, and keys
- Interact with environment objects (doors, chests, switches)
- Manage inventory and use items
- Save and load game progress
The game world is represented as a 10x10 grid where:
0 1 2 3 4 5 6 7 8 9
┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┐
0 │ P │ . │ . │ . │ . │ . │ . │ . │ . │ T │
├───┼───┼───┼───┼───┼───┼───┼───┼───┼───┤
1 │ . │ . │ . │ . │ . │ . │ . │ . │ . │ K │
├───┼───┼───┼───┼───┼───┼───┼───┼───┼───┤
2 │ . │ . │ . │ . │ . │ . │ . │ . │ . │ . │
├───┼───┼───┼───┼───┼───┼───┼───┼───┼───┤
3 │ . │ . │ . │ W │ . │ . │ . │ H │ . │ . │
├───┼───┼───┼───┼───┼───┼───┼───┼───┼───┤
4 │ . │ . │ . │ . │ D │ S │ . │ . │ . │ . │
├───┼───┼───┼───┼───┼───┼───┼───┼───┼───┤
5 │ . │ . │ C │ . │ . │ K │ . │ . │ . │ . │
├───┼───┼───┼───┼───┼───┼───┼───┼───┼───┤
6 │ . │ . │ . │ . │ . │ . │ . │ . │ . │ . │
├───┼───┼───┼───┼───┼───┼───┼───┼───┼───┤
7 │ . │ . │ . │ . │ . │ . │ C │ . │ . │ . │
├───┼───┼───┼───┼───┼───┼───┼───┼───┼───┤
8 │ . │ P │ . │ . │ . │ . │ . │ . │ D │ . │
├───┼───┼───┼───┼───┼───┼───┼───┼───┼───┤
9 │ . │ . │ . │ . │ . │ . │ . │ . │ . │ . │
└───┴───┴───┴───┴───┴───┴───┴───┴───┴───┘
P: Player (player1 at position {x: 0, y: 0})C: Coin (item_01 at {x: 2, y: 5})H: Health Pack (item_02 at {x: 7, y: 3})P: Power Up (item_03 at {x: 1, y: 8}) (Note: Same symbol as Player due to overlap; distinguished by context)K: Key (item_04 at {x: 9, y: 1}, item_05 at {x: 5, y: 5})D: Door (door_01 at {x: 4, y: 4}, door_02 at {x: 8, y: 8})S: Switch (switch_01 at {x: 4, y: 5})C: Chest (chest_01 at {x: 6, y: 7}) (Note: Same symbol as Coin due to overlap; distinguished by context)T: Teleporter (teleporter_01 at {x: 0, y: 9})W: Wall (wall_01 at {x: 3, y: 3}).: Empty space
https://documenter.getpostman.com/view/32714719/2sB2qi7cxHA robust system for managing the state of a simple 2D game, focusing on state representation, transitions, and persistence.
- Player state management (position, health, inventory)
- Item system with pickup and usage mechanics
- Environment interaction (doors, switches, chests)
- State persistence with save/load functionality
- Security middleware (rate limiting, CORS, helmet)
├── src/
│ ├── controllers/ # Request handlers
│ ├── models/ # Data models
│ ├── routes/ # API routes
│ ├── services/ # Business logic
│ └── utils/ # Helper functions
| └── middleware # middleware functions
├── data/ # Game state data
├── config/ # Configuration files
- Install dependencies:
npm install- Start the server:
npm startThe server will run on port 3000 in development mode.
POST /api/player/move- Move player to new positionGET /api/player/status- Get player statusPOST /api/player/pickup/:itemId- Pick up an itemPOST /api/player/use/:itemId- Use an itemPOST /api/player/drop/:itemId- Drop an itemGET /api/player/inventory- Get player inventory
POST /api/environment/interact/:objectId- Interact with environment objects
GET /api/world/state- Get complete game statePOST /api/game/save- Save game statePOST /api/game/load- Load game statePOST /api/game/reset- Reset game state
- Used JSON for state representation due to:
- Human readability
- Easy serialization/deserialization
- Flexible schema
- Native JavaScript support
- Centralized state service for all state modifications
- Validation before state changes
- Immutable state updates
- Clear separation of concerns
- Custom error types for different scenarios
- Global error handler
- Detailed error messages
- Proper HTTP status codes
-
Invalid State Transitions:
- Position validation
- Item existence checks
- Inventory validation
- Environment object validation
-
Data Validation:
- Health bounds checking
- Position bounds checking
- Item ID validation
- Save file corruption handling
-
State Reset:
- Complete state reset functionality
- Preserves initial configuration
The system is designed to be easily extensible:
-
Adding New Items:
- Extend items object in state
- Add item-specific logic in stateService
- No changes needed to core structure
-
Adding New Environment Objects:
- Extend environment object in state
- Add interaction logic in interactWithEnvironment
- Maintains existing validation
-
Adding New Player Attributes:
- Extend player object in state
- Add validation in stateService
- No breaking changes to existing code
-
State Representation:
- Chose JSON for its simplicity and flexibility
- Considered using a database but opted for file-based storage for simplicity
-
Code Organization:
- MVC pattern for clear separation of concerns
- Service layer for business logic
- Utility functions for common operations
-
Performance vs Readability:
- Chose readable code over micro-optimizations
- Used clear variable names over shorter ones
- Prioritized maintainability
-
Memory Usage vs Code Complexity:
- Kept state in memory for fast access
- Could have used streaming for large states
- Balanced between simplicity and scalability
-
Concurrency:
- Current implementation is single-threaded
- Could implement locking mechanism for concurrent access
- Need to consider race conditions in future
-
State Size:
- Current implementation loads entire state
- Could implement partial state loading
- Need to consider memory usage for large games