Example #1
0
    public Factory(RecycleObject prefab, int defaultPoolSize = 5)
    {
        this.prefab          = prefab;
        this.defaultPoolSize = defaultPoolSize;

        Debug.Assert(this.prefab != null, "Prefba is null");
    }
Example #2
0
    public PoolFactory(RecycleObject prefab, int defaultPoolSize = 5)
    {
        this._prefab          = prefab;
        this._defaultPoolSize = defaultPoolSize;

        Debug.Assert(this._prefab != null, "Prefab is null!");
    }
Example #3
0
 void loadImage(RecycleObject item)
 {
     Debug.Log(Application.dataPath + item.image_name);
     imageToLoad = Resources.Load <Sprite>(item.image_name);
     Debug.Log(imageToLoad);
     itemImage.GetComponent <Image>().sprite = imageToLoad;
 }
Example #4
0
 private void OnTriggerEnter2D(Collider2D collision)
 {
     if (collision.gameObject.layer == LayerMask.NameToLayer("Target"))
     {
         GameManager.current.RaiseScore();
         RecycleObject.Recycle(transform.parent.gameObject);
     }
 }
Example #5
0
 void CreatePool()
 {
     for (int i = 0; i < _defaultPoolSize; ++i)
     {
         RecycleObject obj = GameObject.Instantiate(_prefab) as RecycleObject;
         obj.gameObject.SetActive(false);
         _pool.Add(obj);
     }
 }
Example #6
0
 void CreatPool()
 {
     for (int i = 0; i < DefaultPoolSize; i++)
     {
         RecycleObject obj = prefab as RecycleObject;
         obj.gameObject.SetActive(false);
         pool.Add(obj);
     }
 }
Example #7
0
    void SpawnMissile()
    {
        RecycleObject missile = missileFactory.Get();

        missile.Activate(GetMissileSpawnPosition(), buildingManager.GetRandomBuildingPosition());
        missile.Destroyed   += OnMissileDestroyed;
        missile.OutOfScreen += OnMissileOutOfScreen;
        missiles.Add(missile);
        currentMissileCount++;
    }
Example #8
0
 public void itemFound(RecycleObject item)
 {
     loadImage(item);
     itemIndicator.SetActive(true);
     itemImage.SetActive(true);
     itemRecyclable.SetActive(true);
     itemInstructions.SetActive(true);
     ItemFoundLabel.text    = "Item Found";
     IsRecyclableLabel.text = "Can it be Recycled: " + item.recyclable;
     InstructionsLabel.text = "Extra Instructions: " + item.instructions;
 }
Example #9
0
    void RestoreMissile(RecycleObject missile)
    {
        missile.Destroyed   -= OnMissileDestroyed;
        missile.OutOfScreen -= this.OnMissileOutOfScreen;
        int index = missiles.IndexOf(missile);

        missiles.RemoveAt(index);
        missileFactory.Restore(missile);

        CheckAllMissileResotred();
    }
Example #10
0
    public RecycleObject Get()
    {
        if (pool.Count == 0)
        {
            CreatPool();
        }
        int           lastIndex = pool.Count - 1;
        RecycleObject obj       = pool[lastIndex];

        pool.RemoveAt(lastIndex);
        obj.gameObject.SetActive(true);
        return(obj);
    }
Example #11
0
    void OnBulletDestroyed(RecycleObject usedBullet)
    {
        Vector3 lastBulletPosition = usedBullet.transform.position;

        usedBullet.Destroyed -= OnBulletDestroyed;
        bulletFactory.Restore(usedBullet);

        RecycleObject explosion = explosionFactory.Get();

        explosion.Activate(lastBulletPosition);
        explosion.Destroyed += OnExplosionDestroyed;

        AudioManager.instance.PlaySound(SoundID.BulletExplosion);
    }
Example #12
0
    public void OnFireButtonPressed(Vector3 position)
    {
        if (!canShoot)
        {
            return;
        }

        RecycleObject bullet = bulletFactory.Get();

        bullet.Activate(firePosition.position, position);
        bullet.Destroyed += OnBulletDestroyed;

        AudioManager.instance.PlaySound(SoundID.Shoot);

        canShoot = false;
    }
Example #13
0
    private static ObjectPool GetObjectPool(RecycleObject reference)
    {
        ObjectPool pool = null;

        if (pools.ContainsKey(reference))
        {
            pool = pools [reference];
        }
        else
        {
            var poolContainer = new GameObject(reference.gameObject.name + "ObjectPool");
            pool        = poolContainer.AddComponent <ObjectPool>();
            pool.prefab = reference;
            pools.Add(reference, pool);
        }
        return(pool);
    }
Example #14
0
    private static ObjectPool getObjectPool(RecycleObject recycle)
    {
        ObjectPool pool = null;

        if (pools.ContainsKey(recycle))
        {
            pool = pools[recycle];
        }
        else
        {
            var obj = new GameObject(recycle.gameObject.name);
            pool        = obj.AddComponent <ObjectPool>();
            pool.prefab = recycle;
            pools.Add(recycle, pool);
        }
        return(pool);
    }
Example #15
0
    void OnBuildingDestryed(Building building)
    {
        AudioManager.instance.PlaySound(SoundID.BuildingExplosion);

        RecycleObject effect = effectFactory.Get();

        effect.Activate(building.transform.position);
        effect.Destroyed += OnEffectDestroyed;

        building.Destroyed -= OnBuildingDestryed;
        int index = buildings.IndexOf(building);

        buildings.RemoveAt(index);
        GameObject.Destroy(building.gameObject);

        if (buildings.Count == 0)
        {
            AllBuildingsDestroyed?.Invoke();
        }
    }
Example #16
0
    public RecycleObject nextObject(Vector2 pos)
    {
        RecycleObject obj = null;

        foreach (var item in poolInstance)
        {
            if (!obj.gameObject.activeSelf)
            {
                obj = item;
                obj.transform.position = pos;
                break;
            }
        }
        if (obj == null)
        {
            obj = createItem(pos);
        }
        obj.restart();
        return(obj);
    }
Example #17
0
    public RecycleObject NextObject(Vector3 pos)
    {
        RecycleObject instance = null;

        foreach (var go in poolInstances)
        {
            if (go.gameObject.activeSelf != true)
            {
                instance = go;
                instance.transform.position = pos;
            }
        }

        if (instance == null)
        {
            instance = CreateInstance(pos);
        }

        instance.Restart();

        return(instance);
    }
Example #18
0
 void OnExplosionDestroyed(RecycleObject usedExplosion)
 {
     usedExplosion.Destroyed -= OnExplosionDestroyed;
     explosionFactory.Restore(usedExplosion);
 }
Example #19
0
 public void Restore(RecycleObject obj)
 {
     Debug.Assert(obj != null, "NUll object to be returned!");
     obj.gameObject.SetActive(false);
     _pool.Add(obj);
 }
Example #20
0
 void OnMissileOutOfScreen(RecycleObject missile)
 {
     RestoreMissile(missile);
 }
Example #21
0
 void OnMissileDestroyed(RecycleObject missile)
 {
     RestoreMissile(missile);
     missileDestroyed?.Invoke();
 }
Example #22
0
 public void Restore(RecycleObject obj)
 {
     obj.gameObject.SetActive(false);
     pool.Add(obj);
 }
Example #23
0
 void OnEffectDestroyed(RecycleObject effect)
 {
     effect.Destroyed -= OnEffectDestroyed;
     effectFactory.Restore(effect);
 }
Example #24
0
 public Factory(RecycleObject prefab, int DefaultPoolSize)
 {
     this.prefab          = prefab;
     this.DefaultPoolSize = DefaultPoolSize;
 }