You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

411 lines
9.6 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace SUISSEngine
  5. {
  6. public class VehicleManager : MonoBehaviour
  7. {
  8. protected virtual void Awake()
  9. {
  10. }
  11. protected virtual void Start()
  12. {
  13. }
  14. protected virtual void LateUpdate()
  15. {
  16. this.RefreshVehiclesIfNeeded();
  17. }
  18. public virtual void WillBeginBuildingRoads(int type)
  19. {
  20. }
  21. public virtual void DidBuildRoadAt(GridIndex index, Road road)
  22. {
  23. VehicleManager.RoadStats statsForRoadType = this.GetStatsForRoadType(road.type);
  24. statsForRoadType.Area++;
  25. }
  26. public virtual void WillDestroyRoadAt(GridIndex index, Road road)
  27. {
  28. VehicleManager.RoadStats statsForRoadType = this.GetStatsForRoadType(road.type);
  29. statsForRoadType.Area--;
  30. }
  31. public virtual void DidFinishBuildingRoads(int type)
  32. {
  33. this.SetNeedsRefresh();
  34. }
  35. public virtual void DidBuildTileAt(GridIndex index, GridTile tile)
  36. {
  37. }
  38. public virtual void WillDestroyTileAt(GridIndex index, GridTile tile)
  39. {
  40. }
  41. public virtual void VehicleStuck(Vehicle vehicle)
  42. {
  43. if (vehicle == null || !vehicle.IsStuck)
  44. {
  45. return;
  46. }
  47. VehicleManager.RoadStats statsForRoadType = this.GetStatsForRoadType(vehicle.RoadType);
  48. foreach (VehicleManager.VehicleRegistration vehicleRegistration in statsForRoadType.Registrations)
  49. {
  50. int num = vehicleRegistration.Instances.IndexOf(vehicle);
  51. if (num >= 0)
  52. {
  53. vehicleRegistration.Instances.RemoveAt(num);
  54. break;
  55. }
  56. }
  57. UnityEngine.Object.Destroy(vehicle.gameObject);
  58. this.SetNeedsRefresh();
  59. }
  60. public VehicleManager.RoadStats GetStatsForRoadType(int type)
  61. {
  62. VehicleManager.RoadStats roadStats;
  63. if (!this.statsDictionary.TryGetValue(type, out roadStats))
  64. {
  65. roadStats = this.CreateStatsForRoadType(type);
  66. this.statsDictionary.Add(type, roadStats);
  67. }
  68. return roadStats;
  69. }
  70. protected virtual VehicleManager.RoadStats CreateStatsForRoadType(int type)
  71. {
  72. return new VehicleManager.RoadStats(this, type);
  73. }
  74. protected void SetNeedsRefresh()
  75. {
  76. this.needsRefresh = true;
  77. }
  78. protected void RefreshVehiclesIfNeeded()
  79. {
  80. if (this.needsRefresh)
  81. {
  82. this.needsRefresh = false;
  83. this.RefreshVehicles();
  84. }
  85. }
  86. protected void RefreshVehicles()
  87. {
  88. if (this.grid == null)
  89. {
  90. return;
  91. }
  92. foreach (VehicleManager.RoadStats roadStats in this.statsDictionary.Values)
  93. {
  94. foreach (VehicleManager.VehicleRegistration vehicleRegistration in roadStats.Registrations)
  95. {
  96. while (vehicleRegistration.Instances.Count > vehicleRegistration.PreferredCount)
  97. {
  98. int index = vehicleRegistration.Instances.Count - 1;
  99. Vehicle vehicle = vehicleRegistration.Instances[index];
  100. vehicleRegistration.Instances.RemoveAt(index);
  101. UnityEngine.Object.Destroy(vehicle.gameObject);
  102. }
  103. }
  104. }
  105. foreach (VehicleManager.RoadStats roadStats2 in this.statsDictionary.Values)
  106. {
  107. List<Road> list = null;
  108. bool flag = true;
  109. foreach (VehicleManager.VehicleRegistration vehicleRegistration2 in roadStats2.Registrations)
  110. {
  111. while (vehicleRegistration2.Instances.Count < vehicleRegistration2.PreferredCount)
  112. {
  113. if (list == null)
  114. {
  115. list = this.FindRoadsOfType(roadStats2.Type);
  116. }
  117. Road road = this.FindRandomAvailableRoad(list);
  118. if (road == null)
  119. {
  120. flag = false;
  121. break;
  122. }
  123. GridTile component = road.GetComponent<GridTile>();
  124. if (vehicleRegistration2.Prefabs == null || vehicleRegistration2.Prefabs.Length == 0)
  125. {
  126. UnityEngine.Debug.LogError("Failed to instantiate a vehicle for " + vehicleRegistration2 + " because it has no prefabs.");
  127. break;
  128. }
  129. GameObject gameObject = vehicleRegistration2.Prefabs[UnityEngine.Random.Range(0, vehicleRegistration2.Prefabs.Length)];
  130. GameObject gameObject2 = UnityEngine.Object.Instantiate<GameObject>(gameObject);
  131. Vehicle component2 = gameObject2.GetComponent<Vehicle>();
  132. if (component2 == null)
  133. {
  134. UnityEngine.Debug.LogError(string.Concat(new object[]
  135. {
  136. "Failed to instantiate a vehicle for ",
  137. vehicleRegistration2,
  138. " because prefab ",
  139. gameObject,
  140. " is not a Vehicle."
  141. }));
  142. UnityEngine.Object.Destroy(gameObject2);
  143. }
  144. else
  145. {
  146. component2.transform.parent = this.grid.transform;
  147. GridIndex index2 = component.element.Index;
  148. index2.u -= UnityEngine.Random.Range(0, component.size.u);
  149. index2.v -= UnityEngine.Random.Range(0, component.size.v);
  150. vehicleRegistration2.Instances.Add(component2);
  151. component2.SpawnAt(index2, roadStats2.Type);
  152. }
  153. }
  154. if (!flag)
  155. {
  156. break;
  157. }
  158. }
  159. }
  160. }
  161. protected List<Road> FindRoadsOfType(int type)
  162. {
  163. List<Road> list = new List<Road>();
  164. if (this.grid == null)
  165. {
  166. return list;
  167. }
  168. GridSize size = this.grid.Size;
  169. for (int i = 0; i < size.v; i++)
  170. {
  171. for (int j = 0; j < size.u; j++)
  172. {
  173. GridTile tile = this.grid[j, i].Tile;
  174. if (tile != null)
  175. {
  176. Road component = tile.GetComponent<Road>();
  177. if (component != null && component.type == type)
  178. {
  179. list.Add(component);
  180. }
  181. }
  182. }
  183. }
  184. return list;
  185. }
  186. protected Road FindRandomAvailableRoad(List<Road> roads)
  187. {
  188. Road road = null;
  189. while (roads.Count > 0 && road == null)
  190. {
  191. int index = UnityEngine.Random.Range(0, roads.Count);
  192. road = roads[index];
  193. GridIndex index2 = road.GetComponent<GridTile>().index;
  194. int num = 0;
  195. index2.v--;
  196. if (Road.RoadExists(this.grid, index2, road.type))
  197. {
  198. num++;
  199. }
  200. index2.v++;
  201. index2.u++;
  202. if (Road.RoadExists(this.grid, index2, road.type))
  203. {
  204. num++;
  205. }
  206. index2.u--;
  207. index2.v++;
  208. if (Road.RoadExists(this.grid, index2, road.type))
  209. {
  210. num++;
  211. }
  212. index2.v--;
  213. index2.u--;
  214. if (Road.RoadExists(this.grid, index2, road.type))
  215. {
  216. num++;
  217. }
  218. index2.u++;
  219. if (num < 2)
  220. {
  221. roads.RemoveAt(index);
  222. road = null;
  223. }
  224. }
  225. return road;
  226. }
  227. [ChildReference]
  228. public IsometricGrid grid;
  229. protected Dictionary<int, VehicleManager.RoadStats> statsDictionary = new Dictionary<int, VehicleManager.RoadStats>();
  230. protected bool needsRefresh;
  231. public class RoadStats
  232. {
  233. public RoadStats(VehicleManager owner, int type)
  234. {
  235. this._owner = owner;
  236. this._type = type;
  237. this._area = 0;
  238. this._registrations = new List<VehicleManager.VehicleRegistration>();
  239. this._readOnlyRegistrations = this._registrations.AsReadOnly();
  240. }
  241. public VehicleManager Owner
  242. {
  243. get
  244. {
  245. return this._owner;
  246. }
  247. }
  248. public int Type
  249. {
  250. get
  251. {
  252. return this._type;
  253. }
  254. }
  255. public int Area
  256. {
  257. get
  258. {
  259. return this._area;
  260. }
  261. set
  262. {
  263. this._area = value;
  264. }
  265. }
  266. public IList<VehicleManager.VehicleRegistration> Registrations
  267. {
  268. get
  269. {
  270. return this._readOnlyRegistrations;
  271. }
  272. }
  273. public VehicleManager.VehicleRegistration RegisterVehicle(Vehicle.VehicleType vehicleType, params GameObject[] prefabs)
  274. {
  275. VehicleManager.VehicleRegistration vehicleRegistration = this.CreateRegistrationForPrefabs(vehicleType, prefabs);
  276. this._registrations.Add(vehicleRegistration);
  277. return vehicleRegistration;
  278. }
  279. public void UnregisterVehicle(VehicleManager.VehicleRegistration registration)
  280. {
  281. this._registrations.Remove(registration);
  282. }
  283. protected virtual VehicleManager.VehicleRegistration CreateRegistrationForPrefabs(Vehicle.VehicleType vehicleType, params GameObject[] prefabs)
  284. {
  285. return new VehicleManager.VehicleRegistration(this, vehicleType, prefabs);
  286. }
  287. protected VehicleManager _owner;
  288. protected int _type;
  289. protected int _area;
  290. protected List<VehicleManager.VehicleRegistration> _registrations;
  291. protected IList<VehicleManager.VehicleRegistration> _readOnlyRegistrations;
  292. }
  293. public class VehicleRegistration
  294. {
  295. public VehicleRegistration(VehicleManager.RoadStats stats, Vehicle.VehicleType vehicleType, GameObject[] prefabs)
  296. {
  297. this._stats = stats;
  298. this._prefabs = new List<GameObject>(prefabs);
  299. this._instances = new List<Vehicle>();
  300. this._preferredCount = 0;
  301. this._vehicleType = vehicleType;
  302. }
  303. public int RoadType
  304. {
  305. get
  306. {
  307. return this._stats.Type;
  308. }
  309. }
  310. public VehicleManager.RoadStats Stats
  311. {
  312. get
  313. {
  314. return this._stats;
  315. }
  316. }
  317. public GameObject[] Prefabs
  318. {
  319. get
  320. {
  321. return this._prefabs.ToArray();
  322. }
  323. }
  324. public List<Vehicle> Instances
  325. {
  326. get
  327. {
  328. return this._instances;
  329. }
  330. }
  331. public int PreferredCount
  332. {
  333. get
  334. {
  335. return this._preferredCount;
  336. }
  337. set
  338. {
  339. if (value != this._preferredCount)
  340. {
  341. this._preferredCount = Math.Max(0, value);
  342. this._stats.Owner.SetNeedsRefresh();
  343. }
  344. }
  345. }
  346. public Vehicle.VehicleType VehicleType
  347. {
  348. get
  349. {
  350. return this._vehicleType;
  351. }
  352. }
  353. public void AddPrefab(GameObject prefab)
  354. {
  355. this._prefabs.Add(prefab);
  356. }
  357. protected VehicleManager.RoadStats _stats;
  358. protected List<GameObject> _prefabs;
  359. protected List<Vehicle> _instances;
  360. protected int _preferredCount;
  361. protected Vehicle.VehicleType _vehicleType;
  362. }
  363. }
  364. }