/// <summary> /// Spawns an individual fish. /// </summary> private void SpawnFish(int spawnTypeIndex, Vector3 spawnLocation) { GameObject fish = pool.GetPooledObject(spawnTypeIndex); if (fish == null) { return; } fish.transform.position = spawnLocation; fish.transform.rotation = Quaternion.identity; LightSource lightSource = fish.GetComponent <LightSource>(); AbstractFish abstractFish = fish.GetComponent <AbstractFish>(); if (lightSource != null) { float variance = Random.Range(0, lightSource.LightEnergy.CurrentEnergy * energyVariance); lightSource.LightEnergy.Deplete(variance); } if (abstractFish != null) { // Override the fish's default swim direction abstractFish.DefaultWanderAngle = initialSwimAngle; abstractFish.OnActiveChange(true); } fishes.Add(fish); NpcID npcID = fish.GetComponent <NpcID>(); if (npcID != null) { string identity = lightSource.LightSourceID; npcID.ID = identity; } }
/// <summary> /// Creates the pool /// </summary> public void Start() { pool = new List <GameObject> [pooledObjects.Length]; if (!isServer) { return; } for (int i = 0; i < pooledObjects.Length; i++) { pool[i] = new List <GameObject>(); for (int j = 0; j < pooledAmount[i]; j++) { GameObject gameobject = (GameObject)Instantiate(pooledObjects[i]); AbstractFish fish = gameobject.GetComponent <AbstractFish>(); if (fish != null) { fish.OnActiveChange(false); } else { gameobject.SetActive(false); } pool[i].Add(gameobject); LightSource lightSource = gameobject.GetComponent <LightSource>(); NpcID npcID = gameobject.GetComponent <NpcID>(); if (npcID != null) { string identity = lightSource.LightSourceID; npcID.ID = identity; } } } }
/// <summary> /// Check and update existing fish status. /// Check if fish should be active or deactivated. /// </summary> private void UpdateFishStatus() { if (fishes.Count > 0) { for (int i = 0; i < fishes.Count; i++) { if (fishes[i] != null) { AbstractFish fish = fishes[i].GetComponent <AbstractFish>(); if (fish != null) { fish.OnActiveChange(true); CheckDistanceToPlayer(fish); } } } } }
/// <summary> /// Checks the distance from the fish to the player. /// Activates the fish if sufficiently close to the player, /// and deactivates it otherwise. /// </summary> private void CheckDistanceToPlayer(AbstractFish fish) { if (fish == null) { return; } for (int i = 0; i < players.Count; i++) { float distanceSquared = (fish.transform.position - players[i].position).sqrMagnitude; if (distanceSquared > maxDistanceSquared) { fish.gameObject.SetActive(false); } else if (fish.gameObject.activeSelf == false && !fish.Dead) { fish.gameObject.SetActive(true); fish.OnActiveChange(true); } } }
/// <summary> /// Resets the pool, as if it had never been used /// </summary> public void ResetPool() { for (int i = 0; i < pooledObjects.Length; i++) { for (int j = 0; j < pool[i].Count; j++) { GameObject current = pool[i][j]; if (current.activeSelf == true) { AbstractFish fish = current.GetComponent <AbstractFish>(); if (fish != null) { fish.OnActiveChange(false); } else { current.SetActive(false); } } ReactivateObjectLight(current); } } }