Created
January 30, 2014 21:03
-
-
Save PicklesIIDX/8718620 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System; | |
using System.Reflection; | |
/** | |
* Used in the game code to know how to treat an object | |
* This isn't needed here, but I'm using it to determine if we can reuse an object | |
* instead of instantiating it. | |
**/ | |
public enum ActiveState{ | |
INACTIVE, | |
CANCELLED, | |
ACTIVE, | |
CONFIRMED, | |
DEAD | |
} | |
/** | |
* Interface for outside interaction witht the Factory | |
**/ | |
public interface IFactory { | |
GameObject GetObject(int type, string componentName); | |
} | |
/** | |
* Class that instantiates unity game objects and stores them in a pool | |
**/ | |
public class Factory : MonoBehaviour, IFactory { | |
[SerializeField] | |
private GameObject defaultObject; | |
[SerializeField] | |
private GameObject[] FactoryPrefabs; | |
private Dictionary<int, List<GameObject>> factoryPool = new Dictionary<int, List<GameObject>>(); | |
public GameObject GetObject(int type, string componentName) | |
{ | |
GameObject factoryObject = null; | |
factoryObject = FindObject(type, componentName); | |
if(factoryObject == null) { | |
factoryObject = CreateObject(type, componentName); | |
} | |
GameObject objectModel = factoryObject; | |
return objectModel; | |
} | |
GameObject FindObject(int type, string componentName) | |
{ | |
List<GameObject> objectBin; | |
factoryPool.TryGetValue(type, out objectBin); | |
if(objectBin != null) | |
{ | |
for(int i = 0; i < objectBin.Count; i ++) | |
{ | |
ActiveState state = (ActiveState)GetPropValue(objectBin[i].GetComponent(componentName), "Active"); | |
if(state == ActiveState.INACTIVE) | |
{ | |
state = ActiveState.ACTIVE; | |
SetPropValue<ActiveState>(objectBin[i], "Active", state); | |
return objectBin[i]; | |
} | |
} | |
} | |
return null; | |
} | |
GameObject CreateObject(int type, string componentName) | |
{ | |
GameObject spawnedObject = null; | |
foreach(GameObject factoryObject in FactoryPrefabs) | |
{ | |
if((int)GetPropValue(factoryObject.GetComponent(componentName), "Type") == type) | |
{ | |
spawnedObject = Instantiate(factoryObject) as GameObject; | |
break; | |
} | |
} | |
if(spawnedObject == null) | |
{ | |
Debug.LogError(string.Format("[Factory.cs]: I don't have a prefab for a factory object of type {0}." + | |
" Returning a default object instead!", type)); | |
spawnedObject = Instantiate(defaultObject) as GameObject; | |
} | |
List<GameObject> objectBin = null; | |
factoryPool.TryGetValue(type, out objectBin); | |
// New type of object requested, so we have to create a new bin to store objects of this type | |
if(objectBin == null) | |
{ | |
factoryPool.Add(type, new List<GameObject>(){spawnedObject}); | |
} | |
else // Add the new Bird to the current Bird bin | |
{ | |
objectBin.Add(spawnedObject); | |
} | |
return spawnedObject; | |
} | |
public static object GetPropValue(object src, string propName) | |
{ | |
return src.GetType().GetProperty(propName).GetValue(src, null); | |
} | |
public static object SetPropValue<T>(object src, string propName, object value) | |
{ | |
PropertyInfo prop = src.GetType().GetProperty(propName, BindingFlags.Public | BindingFlags.Instance); | |
if(null != prop && prop.CanWrite) | |
{ | |
var val = Convert.ChangeType(value, typeof(T)); | |
prop.SetValue(src, val, null); | |
} | |
return src; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment