|
| 1 | +# Design Document |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +The flying birds ambient system adds atmospheric life to the game world by periodically spawning formations of 5 birds that fly across the screen in a V-shape pattern. Birds appear at random intervals (3-5 minutes), spawn from random screen edges, fly across the viewport, and despawn when they reach the opposite edge. The system is designed to be lightweight, non-intrusive, and visually appealing. |
| 6 | + |
| 7 | +## Architecture |
| 8 | + |
| 9 | +The system follows a component-based architecture integrated into the existing game loop: |
| 10 | + |
| 11 | +``` |
| 12 | +MyGdxGame (render loop) |
| 13 | + └── BirdFormationManager |
| 14 | + ├── SpawnTimer (manages spawn intervals) |
| 15 | + ├── BirdFormation (active formation) |
| 16 | + │ ├── Bird (x5 entities) |
| 17 | + │ └── FlightPath (trajectory data) |
| 18 | + └── BirdRenderer (draws birds on top layer) |
| 19 | +``` |
| 20 | + |
| 21 | +### Integration Points |
| 22 | + |
| 23 | +- **MyGdxGame.create()**: Initialize BirdFormationManager |
| 24 | +- **MyGdxGame.update()**: Update bird positions and spawn timer |
| 25 | +- **MyGdxGame.render()**: Render birds after all other game objects (top layer) |
| 26 | +- **MyGdxGame.dispose()**: Clean up bird textures and resources |
| 27 | + |
| 28 | +## Components and Interfaces |
| 29 | + |
| 30 | +### BirdFormationManager |
| 31 | + |
| 32 | +Central manager class that coordinates spawning, updating, and rendering of bird formations. |
| 33 | + |
| 34 | +```java |
| 35 | +public class BirdFormationManager { |
| 36 | + private BirdFormation activeFormation; |
| 37 | + private float spawnTimer; |
| 38 | + private float nextSpawnInterval; |
| 39 | + private Random random; |
| 40 | + private OrthographicCamera camera; |
| 41 | + private Viewport viewport; |
| 42 | + private SpawnBoundary lastSpawnBoundary; |
| 43 | + |
| 44 | + public void initialize(); |
| 45 | + public void update(float deltaTime, float playerX, float playerY); |
| 46 | + public void render(SpriteBatch batch); |
| 47 | + public void dispose(); |
| 48 | + |
| 49 | + private void spawnFormation(); |
| 50 | + private float generateRandomInterval(); // Returns 180-300 seconds |
| 51 | + private SpawnPoint selectRandomSpawnPoint(); |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +### BirdFormation |
| 56 | + |
| 57 | +Represents an active formation of 5 birds flying across the screen. |
| 58 | + |
| 59 | +```java |
| 60 | +public class BirdFormation { |
| 61 | + private List<Bird> birds; // Always 5 birds |
| 62 | + private Vector2 velocity; |
| 63 | + private SpawnBoundary spawnBoundary; |
| 64 | + private SpawnBoundary targetBoundary; |
| 65 | + private boolean active; |
| 66 | + |
| 67 | + public BirdFormation(SpawnPoint spawnPoint, Vector2 velocity); |
| 68 | + public void update(float deltaTime); |
| 69 | + public void render(SpriteBatch batch); |
| 70 | + public boolean hasReachedTarget(float viewWidth, float viewHeight); |
| 71 | + public void dispose(); |
| 72 | + |
| 73 | + private void initializeVFormation(SpawnPoint spawnPoint); |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +### Bird |
| 78 | + |
| 79 | +Individual bird entity with position and animation state. |
| 80 | + |
| 81 | +```java |
| 82 | +public class Bird { |
| 83 | + private float x, y; |
| 84 | + private Texture texture; |
| 85 | + private float animationTime; |
| 86 | + |
| 87 | + public Bird(float x, float y); |
| 88 | + public void update(float deltaTime, Vector2 velocity); |
| 89 | + public void render(SpriteBatch batch); |
| 90 | + public void dispose(); |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +### SpawnPoint |
| 95 | + |
| 96 | +Data class representing a spawn location and direction. |
| 97 | + |
| 98 | +```java |
| 99 | +public class SpawnPoint { |
| 100 | + public SpawnBoundary boundary; // TOP, BOTTOM, LEFT, RIGHT |
| 101 | + public float x, y; // Spawn coordinates |
| 102 | + public Vector2 direction; // Normalized flight direction |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +### SpawnBoundary |
| 107 | + |
| 108 | +Enum representing screen edges. |
| 109 | + |
| 110 | +```java |
| 111 | +public enum SpawnBoundary { |
| 112 | + TOP, BOTTOM, LEFT, RIGHT |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +## Data Models |
| 117 | + |
| 118 | +### Bird Formation Layout |
| 119 | + |
| 120 | +V-formation with consistent spacing: |
| 121 | + |
| 122 | +``` |
| 123 | + Bird 1 (lead) |
| 124 | + / \ |
| 125 | + Bird 2 Bird 3 |
| 126 | + / \ |
| 127 | +Bird 4 Bird 5 |
| 128 | +``` |
| 129 | + |
| 130 | +- Lead bird at apex |
| 131 | +- 2 birds on left wing, 2 on right wing |
| 132 | +- Spacing: 40 pixels between birds (diagonal distance) |
| 133 | +- Formation angle: 30 degrees from center line |
| 134 | + |
| 135 | +### Spawn Mechanics |
| 136 | + |
| 137 | +**Spawn Interval**: Random value between 180-300 seconds (3-5 minutes) |
| 138 | + |
| 139 | +**Spawn Position Selection**: |
| 140 | +1. Randomly select boundary (TOP, BOTTOM, LEFT, RIGHT) |
| 141 | +2. For vertical boundaries (LEFT/RIGHT): Random Y within viewport height |
| 142 | +3. For horizontal boundaries (TOP/BOTTOM): Random X within viewport width |
| 143 | +4. Ensure variation from previous spawn |
| 144 | + |
| 145 | +**Flight Path**: |
| 146 | +- Direction: From spawn boundary toward opposite boundary |
| 147 | +- Speed: 100 pixels/second (constant) |
| 148 | +- Trajectory: Straight line across screen |
| 149 | + |
| 150 | +## Correctness Properties |
| 151 | + |
| 152 | +*A property is a characteristic or behavior that should hold true across all valid executions of a system-essentially, a formal statement about what the system should do. Properties serve as the bridge between human-readable specifications and machine-verifiable correctness guarantees.* |
| 153 | + |
| 154 | +### Property 1: Spawn interval bounds |
| 155 | + |
| 156 | +*For any* spawn event, the time since the last spawn should be between 180 and 300 seconds (inclusive) |
| 157 | +**Validates: Requirements 1.1** |
| 158 | + |
| 159 | +### Property 2: Spawn position on boundary |
| 160 | + |
| 161 | +*For any* bird formation spawn, the spawn position should be located on one of the four screen boundaries (within 1 pixel tolerance) |
| 162 | +**Validates: Requirements 1.2** |
| 163 | + |
| 164 | +### Property 3: Formation reaches opposite boundary |
| 165 | + |
| 166 | +*For any* bird formation with a given spawn boundary and velocity, updating the formation for sufficient time should result in the formation reaching the opposite boundary |
| 167 | +**Validates: Requirements 1.3** |
| 168 | + |
| 169 | +### Property 4: Despawn triggers timer reset |
| 170 | + |
| 171 | +*For any* bird formation that reaches the target boundary, the system should despawn the formation and initialize a new spawn timer |
| 172 | +**Validates: Requirements 1.4** |
| 173 | + |
| 174 | +### Property 5: Consecutive spawn variation |
| 175 | + |
| 176 | +*For any* two consecutive spawn events, the spawn boundary or spawn position should differ |
| 177 | +**Validates: Requirements 1.5** |
| 178 | + |
| 179 | +### Property 6: Formation contains exactly 5 birds |
| 180 | + |
| 181 | +*For any* spawned bird formation, the formation should contain exactly 5 bird entities |
| 182 | +**Validates: Requirements 2.1** |
| 183 | + |
| 184 | +### Property 7: V-shape arrangement |
| 185 | + |
| 186 | +*For any* bird formation, the relative positions of the 5 birds should form a V-shape with one lead bird and two trailing birds on each side |
| 187 | +**Validates: Requirements 2.2** |
| 188 | + |
| 189 | +### Property 8: V-shape invariant during flight |
| 190 | + |
| 191 | +*For any* bird formation at any point during its flight, the relative positions of the birds should maintain the V-shape pattern |
| 192 | +**Validates: Requirements 2.3** |
| 193 | + |
| 194 | +### Property 9: Consistent bird spacing |
| 195 | + |
| 196 | +*For any* bird formation, the distance between adjacent birds in the formation should remain constant throughout the flight |
| 197 | +**Validates: Requirements 2.4** |
| 198 | + |
| 199 | +### Property 10: Boundary selection distribution |
| 200 | + |
| 201 | +*For any* sequence of spawn events, all four boundaries (TOP, BOTTOM, LEFT, RIGHT) should be selected over time |
| 202 | +**Validates: Requirements 4.1** |
| 203 | + |
| 204 | +### Property 11: Vertical boundary Y-coordinate variation |
| 205 | + |
| 206 | +*For any* sequence of spawns on vertical boundaries (LEFT or RIGHT), the Y-coordinates should vary across the viewport height |
| 207 | +**Validates: Requirements 4.2** |
| 208 | + |
| 209 | +### Property 12: Horizontal boundary X-coordinate variation |
| 210 | + |
| 211 | +*For any* sequence of spawns on horizontal boundaries (TOP or BOTTOM), the X-coordinates should vary across the viewport width |
| 212 | +**Validates: Requirements 4.3** |
| 213 | + |
| 214 | +### Property 13: Flight path toward opposite boundary |
| 215 | + |
| 216 | +*For any* bird formation with spawn boundary B, the flight direction should point toward the opposite boundary |
| 217 | +**Validates: Requirements 4.4** |
| 218 | + |
| 219 | +### Property 14: No rendering when not visible |
| 220 | + |
| 221 | +*For any* game state where no bird formation is active, no bird rendering operations should be performed |
| 222 | +**Validates: Requirements 5.1** |
| 223 | + |
| 224 | +### Property 15: Resource cleanup on despawn |
| 225 | + |
| 226 | +*For any* bird formation that despawns, all associated textures and resources should be properly disposed |
| 227 | +**Validates: Requirements 5.4** |
| 228 | + |
| 229 | +## Error Handling |
| 230 | + |
| 231 | +### Texture Loading Failures |
| 232 | + |
| 233 | +- **Issue**: Bird texture file missing or corrupted |
| 234 | +- **Handling**: Log error, disable bird system gracefully, continue game without birds |
| 235 | +- **Recovery**: Check for texture on next spawn attempt |
| 236 | + |
| 237 | +### Invalid Spawn Positions |
| 238 | + |
| 239 | +- **Issue**: Calculated spawn position outside viewport bounds |
| 240 | +- **Handling**: Clamp position to valid boundary coordinates |
| 241 | +- **Fallback**: Use center of boundary if clamping fails |
| 242 | + |
| 243 | +### Formation Update Errors |
| 244 | + |
| 245 | +- **Issue**: Exception during bird position update |
| 246 | +- **Handling**: Log error, despawn current formation, reset spawn timer |
| 247 | +- **Recovery**: Next spawn attempt proceeds normally |
| 248 | + |
| 249 | +## Testing Strategy |
| 250 | + |
| 251 | +### Unit Tests |
| 252 | + |
| 253 | +- Test spawn interval generation (180-300 second range) |
| 254 | +- Test spawn position calculation for each boundary |
| 255 | +- Test V-formation initialization with correct bird positions |
| 256 | +- Test flight path calculation toward opposite boundary |
| 257 | +- Test boundary detection for despawning |
| 258 | +- Test resource disposal on formation cleanup |
| 259 | + |
| 260 | +### Property-Based Tests |
| 261 | + |
| 262 | +Property-based tests will use Java's jqwik library for property testing. Each test will run a minimum of 100 iterations with randomly generated inputs. |
| 263 | + |
| 264 | +- **Property 1**: Generate random spawn times, verify all are within 180-300 seconds |
| 265 | +- **Property 2**: Generate random spawn points, verify all are on screen boundaries |
| 266 | +- **Property 3**: Simulate formation flight, verify it reaches opposite boundary |
| 267 | +- **Property 4**: Test despawn triggers timer reset correctly |
| 268 | +- **Property 5**: Generate consecutive spawns, verify variation in position/boundary |
| 269 | +- **Property 6**: Test all formations contain exactly 5 birds |
| 270 | +- **Property 7**: Verify V-shape geometry at spawn time |
| 271 | +- **Property 8**: Verify V-shape maintained during flight (invariant) |
| 272 | +- **Property 9**: Verify consistent spacing throughout flight |
| 273 | +- **Property 10**: Test boundary selection distribution over many spawns |
| 274 | +- **Property 11**: Test Y-coordinate variation on vertical boundaries |
| 275 | +- **Property 12**: Test X-coordinate variation on horizontal boundaries |
| 276 | +- **Property 13**: Verify flight direction points to opposite boundary |
| 277 | +- **Property 14**: Verify no rendering when formation is null |
| 278 | +- **Property 15**: Verify texture disposal on despawn |
| 279 | + |
| 280 | +### Integration Tests |
| 281 | + |
| 282 | +- Test bird system integration with game render loop |
| 283 | +- Test birds render on top of all other game objects |
| 284 | +- Test bird system performance with multiple game objects |
| 285 | +- Test bird spawning during gameplay (player movement, menu open/close) |
| 286 | + |
| 287 | +## Implementation Notes |
| 288 | + |
| 289 | +### Rendering Layer Priority |
| 290 | + |
| 291 | +Birds must render after all other game objects to appear on top. In MyGdxGame.render(): |
| 292 | + |
| 293 | +```java |
| 294 | +// Existing rendering order: |
| 295 | +// 1. Ground/terrain |
| 296 | +// 2. Puddles |
| 297 | +// 3. Planted bamboos/trees |
| 298 | +// 4. Trees, stones, items |
| 299 | +// 5. Player and remote players |
| 300 | +// 6. Apple/banana trees (foliage) |
| 301 | +// 7. Rain effects |
| 302 | +// 8. Player name tags |
| 303 | +// 9. Health bars |
| 304 | +// 10. Compass |
| 305 | +// 11. Inventory UI |
| 306 | +// 12. Game menu |
| 307 | + |
| 308 | +// NEW: Birds render here (after rain, before UI) |
| 309 | +if (birdFormationManager != null) { |
| 310 | + batch.begin(); |
| 311 | + birdFormationManager.render(batch); |
| 312 | + batch.end(); |
| 313 | +} |
| 314 | +``` |
| 315 | + |
| 316 | +### Performance Considerations |
| 317 | + |
| 318 | +- **Texture Sharing**: All birds share a single texture instance |
| 319 | +- **Culling**: Only render birds when formation is active |
| 320 | +- **Update Frequency**: Birds update every frame but spawn infrequently |
| 321 | +- **Memory**: Minimal footprint (5 birds × 2 floats + shared texture) |
| 322 | + |
| 323 | +### Camera Independence |
| 324 | + |
| 325 | +Birds spawn relative to camera/viewport position, not world coordinates. This ensures they appear regardless of player location in the infinite world. |
| 326 | + |
| 327 | +### Multiplayer Considerations |
| 328 | + |
| 329 | +Birds are client-side only (not synchronized). Each client spawns birds independently based on their local timer. This is acceptable for ambient effects that don't affect gameplay. |
0 commit comments