internal void Return(string id, AbilityInstance instance) { if (instanceManager == null) { return; } instanceManager.ReturnInstance(id, instance); }
public void ReturnInstance(string id, AbilityInstance instance) { m_OccupiedInstances.Remove(instance); // Handle Holdings if (m_Holdings.ContainsKey(id)) { m_Holdings[id]--; } instance.transform.position = m_PoolPosition; instance.gameObject.SetActive(false); }
private void ExpandCache(int size) { List <AbilityInstance> instances = new List <AbilityInstance>(m_AbilityInstances); for (int i = 0; i < size; i++) { GameObject instance = ability.CreateRuntimeInstance(); instance.transform.SetParent(transform); instance.transform.position = poolPostion; AbilityInstance abilityInstace = instance.GetComponent <AbilityInstance>(); abilityInstace.ability = m_Ability; instances.Add(abilityInstace); instance.SetActive(false); } m_AbilityInstances = instances.ToArray(); }
public GameObject CreateRuntimeInstance() { GameObject gameObject = new GameObject(title + " Runtime Instance"); AbilityInstance instance = gameObject.AddComponent <AbilityInstance>(); m_Unlocked = m_DefaultLock; // Clone all phases to the ability instance for runtime use instance.phaseLists = new PhaseList[phaseLists.Length]; for (int p = 0; p < phaseLists.Length; p++) { instance.phaseLists[p] = phaseLists[p].Clone(); } // Copy all sub instance links to the ability instance instance.subInstanceLinks = new SubInstanceLink[subInstanceLinks.Length]; for (int s = 0; s < subInstanceLinks.Length; s++) { SubInstanceLink newLink = Instantiate(subInstanceLinks[s]); // Create subinstance obj GameObject linkObj; if (subInstanceLinks[s].obj == null) { // Create an empty game object linkObj = new GameObject(title + " (Sub Instance: " + subInstanceLinks[s].title + ")"); } else { // Instantiate the prefab given linkObj = Instantiate(subInstanceLinks[s].obj); linkObj.name = title + " (Sub Instance: " + subInstanceLinks[s].title + ")"; } // Set the parent to the ability instance linkObj.transform.SetParent(gameObject.transform); linkObj.transform.localPosition = Vector3.zero; // Give the generated object to the sub instance link newLink.obj = linkObj; newLink.CacheComponents(); // Push the sub instance link into the list of the instance instance.subInstanceLinks[s] = newLink; } // Clone all shared variables for the ability instance instance.sharedVariables = new SharedVariable[sharedVariables.Length]; for (int s = 0; s < sharedVariables.Length; s++) { instance.sharedVariables[s] = Instantiate(sharedVariables[s]); } List <SubInstanceLink> subInstanceLinksList = new List <SubInstanceLink>(subInstanceLinks); List <SharedVariable> sharedVariablesList = new List <SharedVariable>(sharedVariables); for (int l = 0; l < phaseLists.Length; l++) { for (int p = 0; p < phaseLists[l].phases.Length; p++) { // Redirecting sub instance links SubInstanceLink[] addingSubInstanceLinks = new SubInstanceLink[phaseLists[l].phases[p].runForSubInstances.Length]; for (int s = 0; s < addingSubInstanceLinks.Length; s++) { SubInstanceLink link = phaseLists[l].phases[p].runForSubInstances[s]; addingSubInstanceLinks[s] = instance.subInstanceLinks[subInstanceLinksList.IndexOf(link)]; } instance.phaseLists[l].phases[p].runForSubInstances = addingSubInstanceLinks; // Redirecting shared variables System.Type phaseType = phaseLists[l].phases[p].GetType(); FieldInfo[] allFields = phaseType.GetFields(); for (int f = 0; f < allFields.Length; f++) { if (allFields[f].FieldType.IsSubclassOf(typeof(SharedVariable))) { object value = allFields[f].GetValue(phaseLists[l].phases[p]); SharedVariable sharedVariable = value as SharedVariable; if (sharedVariable != null) { int index = sharedVariablesList.IndexOf(sharedVariable); allFields[f].SetValue(instance.phaseLists[l].phases[p], instance.sharedVariables[index]); } } if (allFields[f].FieldType.IsSubclassOf(typeof(SubVariable <>))) { object value = allFields[f].GetValue(phaseLists[l].phases[p]); MethodInfo cloneMethod = value.GetType().GetMethod("Clone", BindingFlags.Instance | BindingFlags.NonPublic); object clone = cloneMethod.Invoke(value, new object[] { }); allFields[f].SetValue(instance.phaseLists[l].phases[p], clone); } } } } instance.Initiate(); return(gameObject); }