Service-Based Architecture
GridBuilding v6.0 introduces a robust Service-Based Architecture implemented in pure C#. This architecture separates data (State), logic (Services), and engine integration (Godot Layers) to provide a testable, maintainable, and performant foundation.
π― Core Design Pattern
The architecture follows a strict unidirectional data flow pattern:
| |
Key Components
State Layer (Pure Data)
- Contains only data and data-manipulation methods.
- Serializable and completely decoupled from the game engine.
- Examples:
BuildingState,UserState,GridState.
Service Layer (Business Logic)
- Handles all game logic and rules.
- Dispatches events when state changes.
- Accepts commands from the Engine Layer.
- Examples:
BuildingService,InputService,ValidationService.
Engine Layer (Integration)
- Handles user input, rendering, and scene management.
- Calls Services to perform actions.
- Listens to Service events to update the view.
- Examples:
BuildingSystem (Node),GridVisualizer.
π Communication Flow
- Godot β Service: The engine calls methods on services (e.g.,
BuildingService.SelectPlaceable()). - Service β State: The service updates the state (e.g., sets the current placeable).
- Service β Godot: The service fires an event (e.g.,
OnPlaceableSelected). - Godot Updates: The engine listens to the event and updates the UI or visuals.
Crucially, the Engine Layer never modifies the State directly.
π System Structure
The Core logic is organized by Systems, reflecting the domain model of grid building:
| |
Service Interfaces
All interaction happens through interfaces, allowing for easy mocking and testing:
IBuildingService: Manage build mode and placement.IManipulationService: Handle object transformations.IInputService: Process input commands.IValidationService: Check placement rules.
π Benefits for Developers
- Testability: Logic can be tested without running Godot.
- Stability: Strict separation prevents “spaghetti code” state mutations.
- Performance: Pure C# logic runs independently of the engine’s node overhead.