Exemple #1
0
    public BaseObject SpawnObjectFromPool(BaseObject.Type type, Vector2 position)
    {
        BaseObject toRet = objectPoolDictionary[type].Pop();

        toRet.transform.parent = activeObjectParentDict[type];
        toRet.Spawn(position);
        return(toRet);
    }
Exemple #2
0
 private static List <CitySlot> TypeToList(BaseObject.Type t)
 {
     if (!boTypeDict.ContainsKey(t))
     {
         boTypeDict.Add(t, new List <CitySlot>());
     }
     return(boTypeDict[t]);
 }
Exemple #3
0
    private void DeclareBaseObjectType(BaseObject.Type type, BaseObject prefab)
    {
        ObjectPool newPool = new ObjectPool(this, type, prefab);

        objectPoolDictionary.Add(type, newPool);

        GameObject activeObjs = new GameObject("Active " + type.ToString() + "s");

        activeObjs.transform.parent = activeObjectParent;
        activeObjectParentDict.Add(type, activeObjs.transform);
    }
Exemple #4
0
 public static CitySlot GetRandomSlotOfType(BaseObject.Type type)
 {
     if (boTypeDict.ContainsKey(type))
     {
         List <CitySlot> slotsOfType = boTypeDict[type];
         if (slotsOfType.Count == 0)
         {
             return(nullSlot);
         }
         int rand = Random.Range(0, slotsOfType.Count);
         return(slotsOfType.ElementAt(rand));
     }
     Debug.Log("Could not get empty slot, returning nullSlot...");
     return(nullSlot);
 }
Exemple #5
0
        public ObjectPool(SceneObjectManager newManager, BaseObject.Type newType, BaseObject newReferenceObject)
        {
            manager         = newManager;
            type            = newType;
            referenceObject = newReferenceObject;

            transformParent        = new GameObject(type.ToString() + "s").transform;
            transformParent.parent = manager.objectPoolParent.transform;

            stack = new Stack <BaseObject>();
            for (int i = 0; i < referenceObject.pooledQuantity; i++)
            {
                BaseObject newBO = referenceObject.CreateCopy();
                newBO.Initialize(manager);
                Push(newBO);
            }

            referenceObject.gameObject.SetActive(false);
        }
Exemple #6
0
        public bool BuildNewBuilding(BaseObject.Type typeToBuild)
        {
            if (CitySlot.HasAvailableSlot)
            {
                CitySlot s = CitySlot.GetRandomEmptySlot();
                if (!s.IsNull)
                {
                    s.SetOccupant(((BO_Static)manager.manager.SpawnObjectFromPool(typeToBuild, s.position)));
                    return(true);
                }
                else
                {
                    Debug.LogError("Get Random Empty Slot error?");
                    return(false);
                }
            }
            else
            {
                BaseObject.Type replaceType;
                CitySlot        s = CitySlot.nullSlot;
                switch (typeToBuild)
                {
                case BaseObject.Type.Sam:
                    replaceType = BaseObject.Type.Building;
                    if ((s = CitySlot.GetRandomSlotOfType(replaceType)).IsNull)
                    {
                        return(false);
                    }
                    break;

                case BaseObject.Type.NukeLauncher:
                    replaceType = BaseObject.Type.Building;

                    if ((s = CitySlot.GetRandomSlotOfType(replaceType)).IsNull)
                    {
                        replaceType = BaseObject.Type.Sam;
                    }
                    if ((s = CitySlot.GetRandomSlotOfType(replaceType)).IsNull)
                    {
                        return(false);
                    }
                    break;

                case BaseObject.Type.ShieldGenerator:
                    replaceType = BaseObject.Type.Building;
                    if ((s = CitySlot.GetRandomSlotOfType(replaceType)).IsNull)
                    {
                        replaceType = BaseObject.Type.Sam;
                    }
                    if ((s = CitySlot.GetRandomSlotOfType(replaceType)).IsNull)
                    {
                        return(false);
                    }
                    break;
                }

                if (s.IsNull)
                {
                    return(false);
                }
                s.occupant.GetReplaced();
                s.SetOccupant(((BO_Static)manager.manager.SpawnObjectFromPool(typeToBuild, s.position)));
                return(true);
            }
            return(false);
        }