/// <summary> /// This method adds to the score (the value is read from the collectable), /// removes the collectable from the collectables list, /// destroys the collectable and /// updates the score text. /// </summary> /// <param name="collectable"></param> The collectable that calls this method. public void CollectableCollected(Collectable collectable) { _score += collectable.Score; RemoveCollectableFromList(collectable); Destroy(collectable.gameObject); UpdateScore(); }
void Update() { if (_collectables.Count < _maximumCollectableItems) { _collectableSpawnCountdown -= Time.deltaTime; if (_collectableSpawnCountdown < 0f) { _collectableSpawnCountdown = _collectableSpawnRate; Collectable collectable = Instantiate(_collectablePrefab); RandomizeCollectablePosition(collectable); _collectables.Add(collectable); } } }
private IEnumerator Spawn() { while (!GameManager.IsClosing) { yield return(new WaitForSeconds(_spawnInterval)); // Actually checks if the max number is already active. if (_store.Count > 0) { Collectable c = _store.Pop(); c.transform.position = GetRandomPosition(); c.gameObject.SetActive(true); } } }
// Use this for initialization void Awake() { _store = new Stack <Collectable>(); _instance = this; // Pregenerate the objects as inactive. for (int i = 1; i <= maxAtOnce; i++) { Collectable c = Instantiate(_prefab); c.gameObject.SetActive(false); _store.Push(c); } StartCoroutine(Spawn()); }
/// <summary> /// Spawns a collectable at a random position within the spawn area. /// </summary> private void SpawnItem() { Collectable item = collItemPool.GetPooledObject(true); if (item != null) { // Gets random x- and z-coordinates float randX = Random.Range(ItemSpawnAreaCorner1.x, ItemSpawnAreaCorner2.x); float randZ = Random.Range(ItemSpawnAreaCorner1.z, ItemSpawnAreaCorner2.z); item.transform.position = new Vector3(randX, 0, randZ); item.InitDefaults(); item.SetHandler(this); collectables.Add(item); } }
internal void Recycle(Collectable collectable) { collectable.gameObject.SetActive(false); _store.Push(collectable); }
/// <summary> /// Returns a collectable to the pool. /// </summary> /// <param name="item">A collectable</param> public void ReturnItemToPool(Collectable item) { collItemPool.ReturnObject(item); }
/// <summary> /// This method randomizes the location of the parameter collectable to within /// a rectangle formed between two points in the scene. /// </summary> /// <param name="collectable"></param> The collectable that is to be moved. private void RandomizeCollectablePosition(Collectable collectable) { collectable.transform.position = new Vector3(Random.Range(_upperLeftCorner.position.x, _lowerRightCorner.position.x), _collectablePositionY, Random.Range(_upperLeftCorner.position.z, _lowerRightCorner.position.z)); }
/// <summary> /// When a collectable is collected, it calls this method so it can be removed from the collectables list. /// </summary> /// <param name="collectable"></param> The collectable that is to be removed. public void RemoveCollectableFromList(Collectable collectable) { _collectables.Remove(collectable); }