A 2D underwater survival game built with Rust and Turbo, featuring a clean, modular component-based architecture.
The game has been refactored from a monolithic structure into a clean, component-based architecture that separates concerns and makes the codebase more maintainable and extensible.
- PhysicsSystem: Handles all physics calculations, forces, and collisions
- SpawnSystem: Manages spawning of entities, items, and resources
- WorldSystem: Handles world generation, chunk management, and terrain
- AISystem: Manages AI behavior for fish, monsters, and other entities
- GameEntity: Base trait for all game entities
- EntityManager: Manages entity lifecycle and spatial organization
- EntityFactory: Creates different types of game entities
- Component System: Health, Inventory, Stats components
- RenderSystem: Main rendering system with layer-based rendering
- CameraSystem: Smooth camera movement and positioning
- UIRenderer: Handles all UI rendering separately from game rendering
- InputSystem: Centralized input processing and state management
- InputMapping: Configurable key bindings and input configuration
- GameManager: Coordinates all systems and manages game state
- SceneManager: Handles different game scenes and transitions
- ResourceManager: Manages game resources (textures, sounds, data)
- Separation of Concerns: Each system handles one specific aspect of the game
- Modularity: Systems can be easily added, removed, or modified independently
- Testability: Individual components can be tested in isolation
- Maintainability: Code is organized logically and easier to understand
- Extensibility: New features can be added without affecting existing systems
- Performance: Better organization allows for more efficient updates and rendering
Input → GameManager → Systems → Entities → Renderer → Screen
↑ ↓
└─────────────── UI Events ←───────────────────────────┘
- Input System processes user input and updates input state
- Game Manager coordinates all systems based on current scene
- Systems update game logic (physics, AI, spawning, world)
- Entity Manager updates all entities and manages lifecycle
- Renderer renders the game world and UI
- UI Events can trigger input changes
Entities are composed of multiple components:
- HealthComponent: Manages health, damage, and invulnerability
- InventoryComponent: Handles item storage and management
- StatsComponent: Manages speed, strength, defense, and stamina
- RenderData: Contains visual information for rendering
The renderer uses a layer-based system for proper depth ordering:
- Background (sky, ocean)
- Terrain (blocks, chunks)
- Underwater (water effects)
- Entity (fish, items, particles)
- Player (player character)
- UI (HUD, menus)
- Foreground (effects, overlays)
The game supports multiple scenes:
- MainMenu: Title screen and options
- Playing: Main gameplay
- Inventory: Item management
- Crafting: Crafting interface
- Paused: Pause menu
- Spatial Hashing: Efficient entity queries by location
- Chunked World: Only loads and renders nearby world chunks
- Render Queue: Sorts render commands by layer for efficient rendering
- Entity Pooling: Reuses entity objects to reduce allocation
To add new features to the game:
- New System: Create a new system in
src/components/systems/ - New Entity: Add entity type to
EntityTypeenum and implementGameEntity - New Component: Create component struct and add to entities
- New Renderer: Extend
RenderSystemwith new rendering logic - New Input: Add input handling to
InputSystem
// 1. Add to EntityType enum
pub enum EntityType {
// ... existing types
NewEnemy,
}
// 2. Create enemy entity
pub struct NewEnemyEntity {
// ... implementation
}
// 3. Add to EntityFactory
impl EntityFactory {
pub fn create_new_enemy(&mut self, position: V2) -> Box<dyn GameEntity> {
Box::new(NewEnemyEntity::new(self.next_entity_id(), position))
}
}
// 4. Add AI behavior in AISystem
// 5. Add rendering in RenderSystem# Build the project
cargo build
# Run the game
cargo run- Turbo: Game engine and rendering
- Rust: Programming language
- Audio System: Sound effects and music
- Save System: Game state persistence
- Multiplayer: Network play support
- Modding: Plugin system for custom content
- Mobile: Touch controls and mobile optimization
When contributing to this project:
- Follow the existing component architecture
- Keep systems focused on single responsibilities
- Use the entity component system for new game objects
- Add proper error handling and logging
- Write tests for new systems
- Update documentation for new features
This project is licensed under the MIT License.