public void notifyTowerBuilt(TowerBase tower) { System.Type type = tower.GetType(); // TODO: instead of using type, use hash code instead { TowerScript script = tower.GetComponent <TowerScript>(); if (script) { type = script.GetType(); } } ActiveTowersInfo info = null; if (!m_activeTowerInfos.TryGetValue(type, out info)) { // Returning null here, this will keep getting called but // will never create a towers info for this tower if (!tower.Ability) { return; } AbilityBase abilityInstance = Instantiate(tower.Ability); if (!abilityInstance) { return; } #if UNITY_EDITOR abilityInstance.gameObject.hideFlags = HideFlags.HideInHierarchy | HideFlags.HideInInspector; #endif info = new ActiveTowersInfo(); info.m_count = 0; info.m_ability = abilityInstance; m_activeTowerInfos.Add(type, info); m_availableAbilities.Add(info.m_ability); } ++info.m_count; }
public void notifyTowerDestroyed(TowerBase tower) { System.Type type = tower.GetType(); // TODO: instead of using type, use hash code instead { TowerScript script = tower.GetComponent <TowerScript>(); if (script) { type = script.GetType(); } } ActiveTowersInfo info = null; if (m_activeTowerInfos.TryGetValue(type, out info)) { --info.m_count; if (info.m_count <= 0) { AbilityBase abilityInstance = info.m_ability; // Be sure to unselect this if not available anymore if (m_selectedAbility == abilityInstance.abilityId) { unselectAbility(); } m_availableAbilities.Remove(abilityInstance); m_activeTowerInfos.Remove(type); // This game object only exists locally Destroy(abilityInstance.gameObject); } } }