|
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using SUISS.Core;
- using SUISS.Scheduling;
- using SUISSEngine;
- using UnityEngine;
-
- public class CIGVehicleManager : VehicleManager
- {
- protected override void Awake()
- {
- base.Awake();
- foreach (GameObject gameObject in this.vehiclePrefabs)
- {
- Vehicle component = gameObject.GetComponent<Vehicle>();
- if (component != null && component.requiredRoadType > 0)
- {
- this.RegisterVehicle(component, gameObject);
- }
- }
- }
-
- protected override void Start()
- {
- base.Start();
- this.SetPreferredCountsOnNextFrame();
- }
-
- public override void WillBeginBuildingRoads(int type)
- {
- base.WillBeginBuildingRoads(type);
- this._buildingRoads = true;
- }
-
- public override void DidBuildRoadAt(GridIndex index, Road road)
- {
- base.DidBuildRoadAt(index, road);
- if (!this._buildingRoads)
- {
- this.SetPreferredCountsOnNextFrame();
- }
- }
-
- public override void WillDestroyRoadAt(GridIndex index, Road road)
- {
- base.WillDestroyRoadAt(index, road);
- if (!this._buildingRoads)
- {
- this.SetPreferredCountsOnNextFrame();
- }
- }
-
- public override void DidFinishBuildingRoads(int type)
- {
- base.DidFinishBuildingRoads(type);
- this._buildingRoads = false;
- this.SetPreferredCountsOnNextFrame();
- }
-
- public override void DidBuildTileAt(GridIndex index, GridTile tile)
- {
- base.DidBuildTileAt(index, tile);
- if (this.IsTileInteresting(tile))
- {
- this.SetPreferredCountsOnNextFrame();
- }
- }
-
- public override void WillDestroyTileAt(GridIndex index, GridTile tile)
- {
- base.WillDestroyTileAt(index, tile);
- if (this.IsTileInteresting(tile))
- {
- this.SetPreferredCountsOnNextFrame();
- }
- }
-
- public void BuildingStateChanged(GridTile tile)
- {
- if (this.IsTileInteresting(tile))
- {
- this.SetPreferredCountsOnNextFrame();
- }
- }
-
- private VehicleManager.VehicleRegistration GetRegistration(int roadType, Vehicle.VehicleType vehicleType)
- {
- foreach (VehicleManager.VehicleRegistration vehicleRegistration in this._registrations)
- {
- if (vehicleRegistration.VehicleType == vehicleType && vehicleRegistration.RoadType == roadType)
- {
- return vehicleRegistration;
- }
- }
- return null;
- }
-
- private void RegisterVehicle(Vehicle vehicle, GameObject prefab)
- {
- VehicleManager.VehicleRegistration vehicleRegistration = this.GetRegistration(vehicle.requiredRoadType, vehicle.type);
- if (vehicleRegistration != null)
- {
- vehicleRegistration.AddPrefab(prefab);
- }
- else
- {
- VehicleManager.RoadStats statsForRoadType = base.GetStatsForRoadType(vehicle.requiredRoadType);
- vehicleRegistration = statsForRoadType.RegisterVehicle(vehicle.type, new GameObject[]
- {
- prefab
- });
- this._registrations.Add(vehicleRegistration);
- }
- }
-
- private void SetPreferredCountsOnNextFrame()
- {
- if (!this._willUpdateCounts)
- {
- this._willUpdateCounts = true;
- SingletonMonobehaviour<Scheduler>.Instance.ExecuteNextFrame(base.gameObject, new Action(this.SetPreferredCounts));
- }
- }
-
- private void SetPreferredCounts()
- {
- this._willUpdateCounts = false;
- IEnumerator enumerator = Enum.GetValues(typeof(CIGVehicleManager.RoadType)).GetEnumerator();
- try
- {
- while (enumerator.MoveNext())
- {
- object obj = enumerator.Current;
- CIGVehicleManager.RoadType roadType = (CIGVehicleManager.RoadType)obj;
- VehicleManager.RoadStats statsForRoadType = base.GetStatsForRoadType((int)roadType);
- int num = statsForRoadType.Area / 7;
- IEnumerator enumerator2 = Enum.GetValues(typeof(Vehicle.VehicleType)).GetEnumerator();
- try
- {
- while (enumerator2.MoveNext())
- {
- object obj2 = enumerator2.Current;
- Vehicle.VehicleType vehicleType = (Vehicle.VehicleType)obj2;
- if (vehicleType >= (Vehicle.VehicleType)0)
- {
- VehicleManager.VehicleRegistration registration = this.GetRegistration((int)roadType, vehicleType);
- if (registration != null)
- {
- int num2 = Mathf.Min(num, this.CountBuildingsForVehicleType(vehicleType));
- registration.PreferredCount = num2;
- num -= num2;
- }
- }
- }
- }
- finally
- {
- IDisposable disposable;
- if ((disposable = (enumerator2 as IDisposable)) != null)
- {
- disposable.Dispose();
- }
- }
- VehicleManager.VehicleRegistration registration2 = this.GetRegistration((int)roadType, Vehicle.VehicleType.Taxi);
- if (registration2 != null && num > 0)
- {
- int num3 = Mathf.Max(1, num / 8);
- registration2.PreferredCount = num3;
- num -= num3;
- }
- VehicleManager.VehicleRegistration registration3 = this.GetRegistration((int)roadType, Vehicle.VehicleType.Civilian);
- if (registration3 != null && num > 0)
- {
- registration3.PreferredCount = num;
- }
- else if (num > 0)
- {
- UnityEngine.Debug.LogWarning(string.Format("Unable to fill preferred counts for roadType {0}; have {1} vehicles leftover.", roadType, num));
- }
- }
- }
- finally
- {
- IDisposable disposable2;
- if ((disposable2 = (enumerator as IDisposable)) != null)
- {
- disposable2.Dispose();
- }
- }
- }
-
- private bool IsTileInteresting(GridTile tile)
- {
- CIGBuilding component = tile.GetComponent<CIGBuilding>();
- if (component != null && component.IsConstructed && component.spawnsVehicleType > (Vehicle.VehicleType)0)
- {
- foreach (VehicleManager.VehicleRegistration vehicleRegistration in this._registrations)
- {
- if (vehicleRegistration.VehicleType == component.spawnsVehicleType)
- {
- return true;
- }
- }
- return false;
- }
- return false;
- }
-
- private int CountBuildingsForVehicleType(Vehicle.VehicleType vehicleType)
- {
- int num = 0;
- foreach (CIGBuilding cigbuilding in UnityEngine.Object.FindObjectsOfType<CIGBuilding>())
- {
- if (cigbuilding.IsConstructed && cigbuilding.spawnsVehicleType == vehicleType)
- {
- num++;
- }
- }
- return num;
- }
-
- public GameObject[] vehiclePrefabs;
-
- private bool _buildingRoads;
-
- private bool _willUpdateCounts;
-
- private List<VehicleManager.VehicleRegistration> _registrations = new List<VehicleManager.VehicleRegistration>();
-
- public enum RoadType
- {
- Road = 1,
- Walkway,
- River = 4
- }
- }
|