|
- using System;
- using System.Collections.Generic;
- using SUISS.Core;
- using UnityEngine;
-
- namespace SUISSEngine
- {
- public sealed class GameObjectManager : SingletonMonobehaviour<GameObjectManager>
- {
- protected override void Awake()
- {
- base.Awake();
- if (!this._isValidNewInstance)
- {
- return;
- }
- List<CIGBuilding> list = new List<CIGBuilding>();
- List<CIGResidentialBuilding> list2 = new List<CIGResidentialBuilding>();
- List<CIGCommercialBuilding> list3 = new List<CIGCommercialBuilding>();
- List<CIGCommunityBuilding> list4 = new List<CIGCommunityBuilding>();
- List<CIGDecoration> list5 = new List<CIGDecoration>();
- List<CIGUnlockBuilding> list6 = new List<CIGUnlockBuilding>();
- int count = this._additionalPrefabs.Count;
- for (int i = 0; i < count; i++)
- {
- CIGBuilding component = this._additionalPrefabs[i].GetComponent<CIGBuilding>();
- if (!(component == null))
- {
- this.AddToList<CIGResidentialBuilding>(list2, component);
- this.AddToList<CIGCommercialBuilding>(list3, component);
- this.AddToList<CIGCommunityBuilding>(list4, component);
- this.AddToList<CIGDecoration>(list5, component);
- this.AddToList<CIGUnlockBuilding>(list6, component);
- list.Add(component);
- }
- }
- this.SortList<CIGResidentialBuilding>(list2);
- this.SortList<CIGCommercialBuilding>(list3);
- this.SortList<CIGCommunityBuilding>(list4);
- this.SortList<CIGDecoration>(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<GameObject>(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<GameObject> 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<T>(List<T> list, object element) where T : class
- {
- T t = element as T;
- if (t != null)
- {
- list.Add(t);
- }
- }
-
- private void SortList<T>(List<T> 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<GameObject> _additionalPrefabs;
-
- private Dictionary<string, GameObjectStore> stores = new Dictionary<string, GameObjectStore>();
-
- private bool prefabsCollected;
- }
- }
|