Sprint 4: Pause / Save / New Game


# ๐ŸŽ‰ Sprint 4: Save/Load System - COMPLETED

**Sprint Duration:** Week 4  

**Status:** โœ… COMPLETE  

**Completion Date:** November 29, 2025

---

## ๐Ÿ“‹ Sprint Goal

Implement a complete save/load system that persists all game state between sessions, including visit counts, building activations, and animal discoveries.

---

## โœ… Completed Features

### 1. Save/Load Core System

- โœ… JSON-based serialization using Unity's JsonUtility

- โœ… GameSaveData structure with visit counts, animals, and buildings

- โœ… Auto-save every 2 minutes

- โœ… Manual save via pause menu "Save Game" button

- โœ… Auto-save on application quit

- โœ… Save file path: `Application.persistentDataPath/outback_save.json`

### 2. Visit System Persistence

- โœ… All zone visit counts save/load correctly

- โœ… Visit data restored to VisitManager on game start

- โœ… Buildings activate based on loaded visit thresholds

### 3. Building Activation Persistence

- โœ… Activated building IDs tracked at runtime

- โœ… Buildings restore activation state on load

- โœ… BuildingIDHelper for easy ID assignment in Inspector

- โœ… Integration with ActivatableObject component

### 4. Animal Discovery Persistence

- โœ… Discovered animals save/load correctly

- โœ… Discovery HUD updates after loading save data

- โœ… Animal icons show correct discovered/undiscovered state

- โœ… "X/4 Animals Discovered" counter persists

### 5. Pause Menu with Save/Load UI

- โœ… ESC key opens pause menu

- โœ… "Resume" button returns to game

- โœ… "Save Game" button triggers manual save

- โœ… "New Game" button resets game state

- โœ… "Instructions" panel with game controls

- โœ… Time.timeScale pauses game properly

### 6. New Game Functionality

- โœ… Deletes save file

- โœ… Resets all manager states

- โœ… Reloads scene cleanly without crashes

- โœ… Destroys DontDestroyOnLoad singletons before reload

### 7. Debug Features

- โœ… F5 = Manual save

- โœ… F6 = Manual load

- โœ… F7 = Delete save

- โœ… F8 = New game

- โœ… Comprehensive console logging

---

## ๐Ÿ› Bugs Fixed

### Bug #1: New Game Crash ๐Ÿ’ฅ โ†’ โœ… FIXED

**Problem:** Clicking "New Game" crashed Unity with duplicate singleton warnings

**Root Cause:** DontDestroyOnLoad singletons (VisitManager, AnimalDiscoveryManager, GameSaveManager) persisted across scene reload, creating duplicate instances

**Solution:** Modified `GameSaveManager.StartNewGame()` to destroy all DontDestroyOnLoad singletons BEFORE calling `SceneManager.LoadScene()`

**Files Modified:** `GameSaveManager.cs`

---

### Bug #2: Animal Discovery UI Reset ๐Ÿฆ˜ โ†’ โœ… FIXED

**Problem:** After save/load, HUD showed "0/4 Animals Discovered" instead of correct count

**Root Cause:** Save data loaded correctly into AnimalDiscoveryManager, but HUD never updated because no events were fired during load

**Solution:** Added `Start()` method to HUDController that queries discovery count after save data loads (delayed by one frame to ensure load completes first)

**Files Modified:** `HUDController.cs`

---

## ๐Ÿ“Š Technical Implementation

### Architecture Pattern: Observer Pattern

- GameSaveManager observes ActivatableObject activations

- HUDController observes AnimalDiscoveryManager events

- Clean separation of concerns

### Design Patterns Used

- **Singleton:** GameSaveManager, VisitManager, AnimalDiscoveryManager

- **Observer:** Event-driven updates for UI and state changes

- **Serialization:** GameSaveData as data transfer object

### Code Quality

- โœ… Follows OOP principles

- โœ… Follows SOLID principles

- โœ… Comprehensive error handling

- โœ… Defensive null checks

- โœ… Clear code comments

- โœ… Consistent naming conventions

---

## ๐Ÿงช Testing Completed

### Test Case 1: Basic Save/Load

1. Started game (fresh)

2. Discovered 2 animals โ†’ HUD shows "2/4"

3. Visited zones to activate 2 buildings

4. Clicked ESC โ†’ "Save Game"

5. Quit game

6. Restarted game

7. โœ… HUD showed "2/4 Animals Discovered"

8. โœ… Buildings remained activated

9. โœ… Visit counts persisted

### Test Case 2: New Game

1. Loaded game with progress

2. Clicked ESC โ†’ "New Game"

3. โœ… Scene reloaded cleanly (no crash)

4. โœ… HUD showed "0/4 Animals Discovered"

5. โœ… All buildings deactivated

6. โœ… No duplicate manager warnings

### Test Case 3: Auto-Save

1. Played for 3 minutes

2. Observed console logs

3. โœ… Auto-save triggered at 2-minute mark

4. โœ… Save file updated successfully

### Test Case 4: Debug Keys

1. Pressed F5 โ†’ โœ… Manual save worked

2. Pressed F6 โ†’ โœ… Manual load worked

3. Pressed F7 โ†’ โœ… Save deleted

4. Pressed F8 โ†’ โœ… New game started

---

## ๐Ÿ“ Files Created/Modified

### New Files Created

- `GameSaveData.cs` - Serializable save data structure

- `GameSaveManager.cs` - Save/load system manager

- `BuildingIDHelper.cs` - Inspector helper for assigning building IDs

### Files Modified

- `ActivatableObject.cs` - Added save system integration

- `VisitManager.cs` - Added LoadVisitData() and GetAllVisitData()

- `AnimalDiscoveryManager.cs` - Added LoadDiscoveryData() and ResetAllDiscoveries()

- `HUDController.cs` - Added pause menu, save/load buttons, discovery HUD update on load

- `HUD.uxml` - Added pause menu UI structure

- `HUD.uss` - Added pause menu styling

---

## ๐Ÿ“ˆ Metrics

**Lines of Code Added:** ~800 lines  

**Files Created:** 3  

**Files Modified:** 6  

**Bugs Fixed:** 2 critical  

**Test Cases Passed:** 4/4  

---

## ๐ŸŽฏ Definition of Done - Sprint 4

- [x] Save system persists visit counts

- [x] Save system persists building states

- [x] Save system persists discovered animals

- [x] Auto-save works every 2 minutes

- [x] Manual save/load works via pause menu

- [x] Save on quit works

- [x] New Game button works without crash

- [x] Animal discovery UI updates after loading

- [x] Debug keys functional (F5/F6/F7/F8)

- [x] Code follows OOP/SOLID principles

- [x] No compiler errors/warnings

- [x] All features tested in Play mode

- [x] Code committed to Git with clear messages

---

## ๐Ÿš€ Ready for Sprint 5

Sprint 4 is complete with all features working as expected. The save/load system is robust, tested, and ready for production.

**Next Sprint Focus:** Add 3 new animals (Koala, Frillneck Lizard, Platypus) to expand discovery content.

---

## ๐Ÿ“ Lessons Learned

### What Went Well โœ…

- Observer pattern made event-driven updates clean and maintainable

- Singleton managers kept global state management simple

- GameSaveData structure with helper methods made serialization straightforward

- Comprehensive logging made debugging efficient

### Challenges Overcome ๐Ÿ”ง

- DontDestroyOnLoad singleton lifecycle during scene reload (fixed with proper destruction)

- Event firing during save data load (fixed with delayed UI update)

- Method naming consistency across codebase (resolved with compatibility aliases)

### Future Improvements ๐Ÿ’ก

- Consider player position save/load for spawn location

- Add save file backup system

- Implement save file version migration for future updates

- Add cloud save integration (future feature)

---

## ๐ŸŽฎ Player Experience

The save/load system is completely transparent to players:

- Game automatically saves progress every 2 minutes

- Progress persists between sessions without player action

- Manual save available via pause menu for peace of mind

- New Game option allows fresh starts

- No loading screens or save confirmations interrupt gameplay

**Sprint 4 delivers a polished, production-ready save system!** โœจ

---

**Sprint Completed By:** Andrew (Developer) + Claude (AI Assistant)  

**Sprint Review Date:** November 29, 2025  

**Status:** APPROVED - Ready for Sprint 5

Get Sunday Drive

Leave a comment

Log in with itch.io to leave a comment.