1- using System . Collections . Generic ;
1+ using System . Collections . Generic ;
2+ using System . IO ;
3+ using System . Linq ;
24using UnityEngine ;
5+ using UnityEngine . AI ;
36
4- public static class DataHandler
7+ public class DataHandler : MonoBehaviour
58{
9+ public void Start ( )
10+ {
11+ DeserializeGameData ( ) ;
12+ }
13+
614 public static void LoadGameData ( )
715 {
816 // load building data
@@ -12,14 +20,20 @@ public static void LoadGameData()
1220 Globals . CHARACTER_DATA [ d . code ] = d ;
1321
1422 // load game parameters
23+ string gameUid = CoreDataHandler . instance . GameUID ;
24+
1525 GameParameters [ ] gameParametersList = Resources . LoadAll < GameParameters > ( "ScriptableObjects/Parameters" ) ;
1626 foreach ( GameParameters parameters in gameParametersList )
1727 {
1828 if ( parameters is GamePlayersParameters pp )
19- pp . LoadFromFile ( $ "Games/{ CoreDataHandler . instance . GameUID } /PlayerParameters") ;
29+ pp . LoadFromFile ( $ "Games/{ gameUid } /PlayerParameters") ;
2030 else
2131 parameters . LoadFromFile ( ) ;
2232 }
33+
34+ // load game scene data
35+ GameData . gameUid = gameUid ;
36+ GameData . Load ( ) ;
2337 }
2438
2539 public static void SaveGameData ( )
@@ -34,6 +48,65 @@ public static void SaveGameData()
3448 GameData . Save ( SerializeGameData ( ) ) ;
3549 }
3650
51+ public static void DeserializeGameData ( )
52+ {
53+ GameData data = GameData . Instance ;
54+ if ( data == null ) return ;
55+
56+ GamePlayersParameters playerParameters = GameManager . instance . gamePlayersParameters ;
57+ for ( int p = 0 ; p < playerParameters . players . Length ; p ++ )
58+ {
59+ GamePlayerData d = data . players [ p ] ;
60+
61+ // restore resources
62+ for ( int i = 0 ; i < d . resources . Length ; i ++ )
63+ {
64+ InGameResource r = Globals . GAME_RESOURCE_KEYS [ i ] ;
65+ Globals . GAME_RESOURCES [ p ] [ r ] . Amount = d . resources [ i ] ;
66+ }
67+
68+ // restore units
69+ foreach ( GamePlayerUnitData unit in d . units )
70+ {
71+ Unit u ;
72+ string type = unit . unitType ;
73+ List < ResourceValue > production = new List < ResourceValue > ( ) ;
74+ for ( int i = 0 ; i < unit . production . Length ; i ++ )
75+ {
76+ InGameResource r = Globals . GAME_RESOURCE_KEYS [ i ] ;
77+ production . Add ( new ResourceValue ( r , unit . production [ i ] ) ) ;
78+ }
79+ if ( type == "building" )
80+ {
81+ BuildingData bd = Globals . BUILDING_DATA
82+ . Where ( ( BuildingData x ) => x . code == unit . code )
83+ . First ( ) ;
84+ u = new Building ( bd , p , production ) ;
85+ u . SetPosition ( unit . position ) ;
86+ u . Place ( true ) ;
87+ ( ( Building ) u ) . SetConstructionHP ( unit . constructionHP , true ) ;
88+ }
89+ else
90+ {
91+ CharacterData cd = Globals . CHARACTER_DATA [ unit . code ] ;
92+ u = new Character ( cd , p ) ;
93+ u . ComputeProduction ( ) ;
94+ u . Transform . GetComponent < NavMeshAgent > ( ) . Warp ( unit . position ) ;
95+ }
96+
97+ u . Uid = unit . uid ;
98+ u . HP = unit . currentHealth ;
99+ u . Level = unit . level - 1 ;
100+ u . LevelUp ( true ) ;
101+ u . AttackDamage = unit . attackDamage ;
102+ u . AttackRange = unit . attackRange ;
103+ }
104+ }
105+
106+ Camera . main . transform . position = data . camPosition ;
107+ EventManager . TriggerEvent ( "UpdateResourceTexts" ) ;
108+ }
109+
37110 public static GameData SerializeGameData ( )
38111 {
39112 List < GamePlayerData > players = new List < GamePlayerData > ( ) ;
@@ -55,6 +128,8 @@ public static GameData SerializeGameData()
55128 List < GamePlayerUnitData > units = new List < GamePlayerUnitData > ( ) ;
56129 foreach ( Unit unit in Unit . UNITS_BY_OWNER [ p ] )
57130 {
131+ if ( ! unit . Transform ) continue ;
132+
58133 int [ ] production = new int [ Globals . GAME_RESOURCE_KEYS . Length ] ;
59134 for ( int i = 0 ; i < Globals . GAME_RESOURCE_KEYS . Length ; i ++ )
60135 {
@@ -89,4 +164,33 @@ public static GameData SerializeGameData()
89164 data . camPosition = Camera . main . transform . position ;
90165 return data ;
91166 }
167+
168+ public static List < ( string , System . DateTime ) > GetGamesList ( )
169+ {
170+ string rootPath = Path . Combine (
171+ Application . persistentDataPath ,
172+ BinarySerializable . DATA_DIRECTORY ,
173+ "Games" ) ;
174+ string [ ] gameDirs = Directory . GetDirectories ( rootPath ) ;
175+
176+ // filter to keep only game folders with a game save
177+ IEnumerable < string > validGameDirs = gameDirs
178+ . Where ( ( string d )
179+ => File . Exists ( Path . Combine ( d , GameData . DATA_FILE_NAME ) ) ) ;
180+
181+ // extract the last modification time
182+ List < ( string , System . DateTime ) > games = new List < ( string , System . DateTime ) > ( ) ;
183+ foreach ( string dir in validGameDirs )
184+ {
185+ games . Add ( (
186+ dir ,
187+ File . GetLastWriteTime ( Path . Combine ( dir , GameData . DATA_FILE_NAME ) )
188+ ) ) ;
189+ }
190+
191+ // sort by reverse chronological order (most recent first)
192+ return games
193+ . OrderByDescending ( ( ( string , System . DateTime ) x ) => x . Item2 )
194+ . ToList ( ) ;
195+ }
92196}
0 commit comments