您不能選擇超過 %s 個話題 話題必須以字母或數字為開頭,可包含連接號 ('-') 且最長為 35 個字
 
 
 

360 行
7.6 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using SUISS.Storage;
  4. using SUISSEngine;
  5. using UnityEngine;
  6. public class CIGIslandState : IslandState
  7. {
  8. protected override void Awake()
  9. {
  10. base.Awake();
  11. }
  12. protected override void Start()
  13. {
  14. base.Start();
  15. }
  16. protected override void OnNewGameState()
  17. {
  18. base.OnNewGameState();
  19. this.SetValue("Propagate", this.propagate);
  20. this.Happiness = 0;
  21. this.Population = 0;
  22. this.Housing = 0;
  23. this.Employees = 0;
  24. this.Jobs = 0;
  25. this.Transport = this.baseTransport;
  26. if (this.baseHappiness != 0)
  27. {
  28. this.AddHappiness(this.baseHappiness);
  29. }
  30. }
  31. public int Happiness
  32. {
  33. get
  34. {
  35. return this.GetValue<int>("Happiness", 0);
  36. }
  37. private set
  38. {
  39. this.SetValue("Happiness", value);
  40. }
  41. }
  42. public int AvailableHappiness
  43. {
  44. get
  45. {
  46. if (this.infiniteTransport)
  47. {
  48. return this._gameState.reference.GlobalHappiness;
  49. }
  50. int num = this.Happiness;
  51. int transport = this.Transport;
  52. if (transport > 0)
  53. {
  54. int num2 = this._gameState.reference.GlobalHappiness - this._gameState.reference.GlobalHousing - (this.Happiness - this.Housing);
  55. if (num2 > 0)
  56. {
  57. num += Mathf.Min(num2, transport);
  58. }
  59. }
  60. return num;
  61. }
  62. }
  63. public int Population
  64. {
  65. get
  66. {
  67. return this.GetValue<int>("Population", 0);
  68. }
  69. private set
  70. {
  71. this.SetValue("Population", value);
  72. }
  73. }
  74. public int AvailablePopulation
  75. {
  76. get
  77. {
  78. if (this.infiniteTransport)
  79. {
  80. return this._gameState.reference.GlobalPopulation;
  81. }
  82. int num = this.Population;
  83. int transport = this.Transport;
  84. if (transport > 0)
  85. {
  86. int num2 = this._gameState.reference.GlobalPopulation - this._gameState.reference.GlobalJobs - (this.Population - this.Jobs);
  87. if (num2 > 0)
  88. {
  89. num += Mathf.Min(num2, transport);
  90. }
  91. }
  92. return num;
  93. }
  94. }
  95. public int Housing
  96. {
  97. get
  98. {
  99. return this.GetValue<int>("Housing", 0);
  100. }
  101. private set
  102. {
  103. this.SetValue("Housing", value);
  104. }
  105. }
  106. public int Employees
  107. {
  108. get
  109. {
  110. return this.GetValue<int>("Employees", 0);
  111. }
  112. private set
  113. {
  114. this.SetValue("Employees", value);
  115. }
  116. }
  117. public int Jobs
  118. {
  119. get
  120. {
  121. return this.GetValue<int>("Jobs", 0);
  122. }
  123. private set
  124. {
  125. this.SetValue("Jobs", value);
  126. }
  127. }
  128. public int Transport
  129. {
  130. get
  131. {
  132. return this.GetValue<int>("Transport", 0);
  133. }
  134. private set
  135. {
  136. this.SetValue("Transport", value);
  137. }
  138. }
  139. public bool Propagate
  140. {
  141. get
  142. {
  143. return this.GetValue<bool>("Propagate", false);
  144. }
  145. set
  146. {
  147. bool flag = this.Propagate;
  148. this.SetValue("Propagate", value);
  149. if (flag != this.Propagate)
  150. {
  151. this.AddGlobalValues((!value) ? -1 : 1);
  152. }
  153. }
  154. }
  155. protected void AddGlobalValues(int factor)
  156. {
  157. this._gameState.reference.AddGlobalPopulation(factor * this.Population);
  158. this._gameState.reference.AddGlobalHousing(factor * this.Housing);
  159. this._gameState.reference.AddGlobalHappiness(factor * this.Happiness);
  160. this._gameState.reference.AddGlobalEmployees(factor * this.Employees);
  161. this._gameState.reference.AddGlobalJobs(factor * this.Jobs);
  162. }
  163. public void AddPopulation(int pop)
  164. {
  165. this.Population += pop;
  166. if (this.Propagate)
  167. {
  168. this._gameState.reference.AddGlobalPopulation(pop);
  169. }
  170. }
  171. public void AddHousing(int room)
  172. {
  173. this.Housing += room;
  174. if (this.Propagate)
  175. {
  176. this._gameState.reference.AddGlobalHousing(room);
  177. }
  178. }
  179. public void AddHappiness(int smiles)
  180. {
  181. this.Happiness += smiles;
  182. if (this.Propagate)
  183. {
  184. this._gameState.reference.AddGlobalHappiness(smiles);
  185. }
  186. }
  187. public void AddEmployees(int emp)
  188. {
  189. this.Employees += emp;
  190. if (this.Propagate)
  191. {
  192. this._gameState.reference.AddGlobalEmployees(emp);
  193. }
  194. }
  195. public void AddJobs(int jobs)
  196. {
  197. this.Jobs += jobs;
  198. if (this.Propagate)
  199. {
  200. this._gameState.reference.AddGlobalJobs(jobs);
  201. }
  202. }
  203. public void AddTransport(int x)
  204. {
  205. this.Transport += x;
  206. }
  207. public Currencies CalculateProfitPerHour()
  208. {
  209. Currencies currencies = Currencies.Zero;
  210. foreach (GridTile gridTile in IsometricIsland.Current.grid.Tiles)
  211. {
  212. CIGCommercialBuilding component = gridTile.GetComponent<CIGCommercialBuilding>();
  213. if (component != null)
  214. {
  215. currencies += component.ProfitPerHour;
  216. }
  217. }
  218. return currencies;
  219. }
  220. public static Dictionary<string, object> GetDictionary(Island island)
  221. {
  222. Storage storage = Storage.Get(StorageLifecycle.Game);
  223. string key = island.ToString() + "State";
  224. if (storage.Root.ContainsKey(key))
  225. {
  226. return (Dictionary<string, object>)storage.Root[key];
  227. }
  228. return new Dictionary<string, object>();
  229. }
  230. public void LoadValuesFromBuildings()
  231. {
  232. bool flag = false;
  233. int num = 0;
  234. int num2 = 0;
  235. foreach (CIGCommercialBuilding cigcommercialBuilding in UnityEngine.Object.FindObjectsOfType<CIGCommercialBuilding>())
  236. {
  237. num += cigcommercialBuilding.Employees;
  238. num2 += cigcommercialBuilding.MaxEmployees;
  239. }
  240. if (num != this.Employees || num2 != this.Jobs)
  241. {
  242. UnityEngine.Debug.LogWarning(string.Format("Employees gamestate values changed from {2}/{3} to {0}/{1}.", new object[]
  243. {
  244. num,
  245. num2,
  246. this.Employees,
  247. this.Jobs
  248. }));
  249. if (this.Propagate)
  250. {
  251. this._gameState.reference.AddGlobalJobs(num2 - this.Jobs);
  252. }
  253. this.Jobs = num2;
  254. if (this.Propagate)
  255. {
  256. this._gameState.reference.AddGlobalEmployees(num - this.Employees);
  257. }
  258. this.Employees = num;
  259. flag = true;
  260. }
  261. int num3 = 0;
  262. int num4 = 0;
  263. foreach (CIGResidentialBuilding cigresidentialBuilding in UnityEngine.Object.FindObjectsOfType<CIGResidentialBuilding>())
  264. {
  265. num3 += cigresidentialBuilding.People;
  266. num4 += cigresidentialBuilding.MaxPeople;
  267. }
  268. if (num3 != this.Population || num4 != this.Housing)
  269. {
  270. UnityEngine.Debug.LogWarning(string.Format("Employees gamestate values changed from {2}/{3} to {0}/{1}.", new object[]
  271. {
  272. num3,
  273. num4,
  274. this.Population,
  275. this.Housing
  276. }));
  277. if (this.Propagate)
  278. {
  279. this._gameState.reference.AddGlobalHousing(num4 - this.Housing);
  280. }
  281. this.Housing = num4;
  282. if (this.Propagate)
  283. {
  284. this._gameState.reference.AddGlobalPopulation(num3 - this.Population);
  285. }
  286. this.Population = num3;
  287. flag = true;
  288. }
  289. int num5 = this.baseHappiness;
  290. foreach (CIGBuilding cigbuilding in UnityEngine.Object.FindObjectsOfType<CIGBuilding>())
  291. {
  292. num5 += cigbuilding.Happiness;
  293. }
  294. foreach (CIGScenery cigscenery in UnityEngine.Object.FindObjectsOfType<CIGScenery>())
  295. {
  296. num5 += cigscenery.happiness;
  297. }
  298. if (num5 != this.Happiness)
  299. {
  300. UnityEngine.Debug.LogWarning(string.Format("Happiness gamestate value changed from {1} to {0}.", num5, this.Happiness));
  301. if (this.Propagate)
  302. {
  303. this._gameState.reference.AddGlobalHappiness(num5 - this.Happiness);
  304. }
  305. this.Happiness = num5;
  306. flag = true;
  307. }
  308. if (flag)
  309. {
  310. StorageController.SaveAll();
  311. }
  312. }
  313. public const string HappinessKey = "Happiness";
  314. public const string PopulationKey = "Population";
  315. public const string HousingKey = "Housing";
  316. public const string EmployeesKey = "Employees";
  317. public const string JobsKey = "Jobs";
  318. public const string TransportKey = "Transport";
  319. public const string PropagateKey = "Propagate";
  320. public int baseTransport = 1000000;
  321. public int baseHappiness;
  322. public bool infiniteTransport = true;
  323. public bool propagate = true;
  324. protected MagicalReference<CIGGameState> _gameState = new MagicalReference<CIGGameState>();
  325. }