using System; using System.Collections.Generic; using SUISS.Core; using UnityEngine; namespace SUISSEngine { public sealed class GameObjectManager : SingletonMonobehaviour { protected override void Awake() { base.Awake(); if (!this._isValidNewInstance) { return; } List list = new List(); List list2 = new List(); List list3 = new List(); List list4 = new List(); List list5 = new List(); List list6 = new List(); int count = this._additionalPrefabs.Count; for (int i = 0; i < count; i++) { CIGBuilding component = this._additionalPrefabs[i].GetComponent(); if (!(component == null)) { this.AddToList(list2, component); this.AddToList(list3, component); this.AddToList(list4, component); this.AddToList(list5, component); this.AddToList(list6, component); list.Add(component); } } this.SortList(list2); this.SortList(list3); this.SortList(list4); this.SortList(list5); this.AllBuildings = list.ToArray(); this.Residential = list2.ToArray(); this.Commercial = list3.ToArray(); this.Community = list4.ToArray(); this.Decoration = list5.ToArray(); this.Unlock = list6.ToArray(); this.CollectPrefabs(); } public void Register(GameObject prefab) { if (this.stores.ContainsKey(prefab.name.ToLower())) { UnityEngine.Debug.LogError(string.Format("Prefab with name {0} registered more than once.", prefab.name)); return; } GameObjectStore value = new GameObjectStore(prefab); this.stores.Add(prefab.name.ToLower(), value); } public void ReturnToStore(GameObject obj) { this.CollectPrefabs(); if (!this.stores.ContainsKey(obj.name.ToLower())) { UnityEngine.Debug.LogError(string.Format("Object {0} disabled which is not registered, object will be destroyed instead.", obj.name)); UnityEngine.Object.Destroy(obj); return; } obj.SetActive(false); this.stores[obj.name.ToLower()].objects.Add(obj); } public GameObject GetOrCreate(string name) { this.CollectPrefabs(); name = name.ToLower(); if (this.stores.ContainsKey(name)) { UnityEngine.Debug.LogError(string.Format("Prefab with name {0} NOT registered.", name)); return null; } GameObjectStore gameObjectStore = this.stores[name]; if (gameObjectStore.objects.Count > 0) { GameObject gameObject = gameObjectStore.objects[gameObjectStore.objects.Count - 1]; gameObjectStore.objects.RemoveAt(gameObjectStore.objects.Count - 1); gameObject.SetActive(true); return gameObject; } GameObject gameObject2 = UnityEngine.Object.Instantiate(gameObjectStore.prefab); gameObject2.name = gameObjectStore.prefab.name; return gameObject2; } public bool HavePrefabWithName(string name) { this.CollectPrefabs(); return this.stores.ContainsKey(name.ToLower()); } public GameObject GetPrefabWithName(string name) { this.CollectPrefabs(); name = name.ToLower(); if (this.stores.ContainsKey(name)) { return this.stores[name].prefab; } return null; } public bool AddAdditionalPrefab(GameObject newPrefab) { if (!this._additionalPrefabs.Contains(newPrefab)) { this._additionalPrefabs.Add(newPrefab); return true; } return false; } public List AdditionalPrefabs { get { return this._additionalPrefabs; } } public CIGResidentialBuilding[] Residential { get; private set; } public CIGCommercialBuilding[] Commercial { get; private set; } public CIGCommunityBuilding[] Community { get; private set; } public CIGDecoration[] Decoration { get; private set; } public CIGUnlockBuilding[] Unlock { get; private set; } public CIGBuilding[] AllBuildings { get; private set; } public CIGBuilding GetBuilding(string prefabName) { if (string.IsNullOrEmpty(prefabName)) { return null; } int num = this.AllBuildings.Length; for (int i = 0; i < num; i++) { CIGBuilding cigbuilding = this.AllBuildings[i]; if (cigbuilding.CachedName == prefabName) { return cigbuilding; } } return null; } private void CollectPrefabs() { if (this.prefabsCollected) { return; } if (this._additionalPrefabs != null) { int count = this._additionalPrefabs.Count; for (int i = 0; i < count; i++) { this.Register(this._additionalPrefabs[i]); } } this.prefabsCollected = true; } private void AddToList(List list, object element) where T : class { T t = element as T; if (t != null) { list.Add(t); } } private void SortList(List list) where T : CIGBuilding { list.Sort(delegate(T a, T b) { if (a.unlockLevels.Length != 0 || b.unlockLevels.Length != 0) { int num = Mathf.Min(a.unlockLevels.Length, b.unlockLevels.Length); for (int i = 0; i < num; i++) { if (a.unlockLevels[i] != b.unlockLevels[i]) { return a.unlockLevels[i] - b.unlockLevels[i]; } } return a.unlockLevels.Length - b.unlockLevels.Length; } if (a.baseHappinessValue != b.baseHappinessValue) { return b.baseHappinessValue - a.baseHappinessValue; } decimal value = a.initialPurchasePrice.GetValue("Cash"); decimal value2 = b.initialPurchasePrice.GetValue("Cash"); if (value < value2) { return 1; } if (value > value2) { return -1; } return a.LocalName.Translate().CompareTo(b.LocalName.Translate()); }); } [SerializeField] private List _additionalPrefabs; private Dictionary stores = new Dictionary(); private bool prefabsCollected; } }