public void StartBelt(int iNumAsteroids, Asteroid.AsteroidSize iMinSize) { var minSize = Asteroid.AsteroidSize.Large - iMinSize + 1; var asteroids = new List <Asteroid>(); for (int i = 0; i < iNumAsteroids; i++) { var size = Asteroid.AsteroidSize.Large - RandomizeHelper.Random.Next(minSize); asteroids.Add(new Asteroid(size)); } lock (_updateAsteroidsLock) _asteroids = asteroids; }
public void SpawnNewAsteroids() { int activeAsteroids = AsteroidManager.Instance.GetActiveAsteroidAmount(); if (activeAsteroids < _minActiveAsteroids) { int spawnAmount = _minActiveAsteroids - activeAsteroids; for (int i = 0; i < spawnAmount; i++) { Asteroid.AsteroidSize size = (Asteroid.AsteroidSize)Random.Range(0, 3); //TODO TYPE AsteroidManager.Instance.SpawnAsteroid(Asteroid.AsteroidType.Rock, size); } } }
/// <summary> /// Gets an asteroid of type and size from the pool. If there's no free asteroid, a new is added to the pool. /// </summary> /// <param name="type">Type of the asteroid (Rock, Ice, Metal)</param> /// <param name="size">Size of the asteroid (Big, Middle, Small)</param> /// <returns></returns> public Asteroid GetAsteroid(Asteroid.AsteroidType type, Asteroid.AsteroidSize size) { Vector2Int key = new Vector2Int((int)type, (int)size); if (_asteroidObjectPool.ContainsKey(key)) { for (int i = 0; i < _asteroidObjectPool[key].Count; i++) { if (!_asteroidObjectPool[key][i].IsActive()) { return(_asteroidObjectPool[key][i]); } } } return(AddAsteroidToObjectPool(type, size)); }
/// <summary> /// Spwans a new asteroid into the game. /// </summary> /// <param name="type">Type of the asteroid (Rock, Ice, Metal)</param> /// <param name="size">Size of the asteroid (Big, Middle, Small)</param> /// <param name="position">Position of the asteroid</param> /// <param name="velocity">Velocity of the asteroid</param> public void SpawnAsteroid(Asteroid.AsteroidType type, Asteroid.AsteroidSize size, Vector2 position = default(Vector2), Vector2 velocity = default(Vector2)) { if (position == default(Vector2) && velocity == default(Vector2)) { Vector3 playerPosition = GameManager.Instance.Player.transform.position; Vector3 spawnPosition = GameManager.Instance.SpawnOutsideViewport(); playerPosition.x -= spawnPosition.x; playerPosition.y -= spawnPosition.y; position = spawnPosition; velocity = playerPosition.normalized; } else if (velocity == default(Vector2)) { velocity = new Vector2(Random.Range(-1, 1f), Random.Range(-1f, 1f)); } Asteroid asteroid = Instance.GetAsteroid(type, size); asteroid.Activate(position, velocity); }
/// <summary> /// Adds an asteroid of type and size to the asteroid's pool /// </summary> /// <param name="type">Type of the asteroid (Rock, Ice, Metal)</param> /// <param name="size">Size of the asteroid (Big, Middle, Small)</param> /// <returns>The asteroid object added to the pool</returns> private Asteroid AddAsteroidToObjectPool(Asteroid.AsteroidType type, Asteroid.AsteroidSize size) { GameObject asteroidGameObject = null; switch (type) { case Asteroid.AsteroidType.Rock: asteroidGameObject = Instantiate(_rockAsteroidPrefabs[(int)size].gameObject) as GameObject; asteroidGameObject.name = _rockAsteroidPrefabs[(int)size].name; break; case Asteroid.AsteroidType.Ice: asteroidGameObject = Instantiate(_iceAsteroidPrefabs[(int)size].gameObject) as GameObject; asteroidGameObject.name = _iceAsteroidPrefabs[(int)size].name; break; case Asteroid.AsteroidType.Metal: asteroidGameObject = Instantiate(_metalAsteroidPrefabs[(int)size].gameObject) as GameObject; asteroidGameObject.name = _metalAsteroidPrefabs[(int)size].name; break; } asteroidGameObject.transform.SetParent(_asteroidContainer, false); Asteroid asteroid = asteroidGameObject.GetComponent <Asteroid>(); Vector2Int key = new Vector2Int((int)type, (int)size); if (!_asteroidObjectPool.ContainsKey(key)) { _asteroidObjectPool.Add(key, new List <Asteroid>()); } _asteroidObjectPool[key].Add(asteroid); return(asteroid); }
public AsteroidBelt(int iNumAsteroids, Asteroid.AsteroidSize iMinSize) { _updateAsteroidsLock = new object(); StartBelt(iNumAsteroids, iMinSize); }