using System.Collections; using System.Collections.Generic; using UnityEngine; using Mirror; public class MinigameManager : NetworkBehaviour { public static MinigameManager instance; public float mapRadius; public int maxMoons, maxStars = 100; public Transform pickupItemsParent; private List ActiveMoons = new List(); private List ActiveStars = new List(); List MoonPool = new List(); List StarsPool = new List(); public GameObject moon; public GameObject star; private void Awake() { SceneData.GameManager = this; instance = this; } void Update() { if (!isServer) { return; } HandlePickupSpawn(); KillOutOfBoundsPlayers(); } void KillOutOfBoundsPlayers() { SpaceshipController[] players = FindObjectsOfType(); foreach (SpaceshipController player in players) { if (Vector3.Distance(player.transform.position, Vector3.zero) > mapRadius) { //Out of bounds. Kill him player.Die("Playzone"); } } } void HandlePickupSpawn() { int moonsNeed = maxMoons - ActiveMoons.Count; int starsNeed = maxStars - ActiveStars.Count; if (moonsNeed > 0) { // <-- We need more moons! SpawnMoons(moonsNeed); } if (starsNeed > 0) { // <-- We need more moons! SpawnStars(starsNeed); } } public void SpawnLeftoverPickups(Vector3 position, int amount) { SpawnStars(amount, focusedPosition: position); } void SpawnStars(int amount, Vector3? focusedPosition = null) { for (int i = 0; i < amount; i++) { Vector3 newPosition = (focusedPosition == null) ? getRandomPositionOnMap() : getRandomPointInCirlce((Vector3)focusedPosition, 10); if (StarsPool.Count > 0) { // <-- Got some in the pool, no need to spawn new PickupItem pickedStar = StarsPool[0]; pickedStar.Reposition(newPosition); ActiveStars.Add(pickedStar); StarsPool.RemoveAt(0); } else { GameObject newStar = Instantiate(star, pickupItemsParent); NetworkServer.Spawn(newStar); newStar.GetComponent().Reposition(newPosition); ActiveStars.Add(newStar.GetComponent()); } } } void SpawnMoons(int amount) { for (int i = 0; i < amount; i++) { if (MoonPool.Count > 0) { // <-- Got some in the pool, no need to spawn new PickupItem pickedMoon = MoonPool[0]; pickedMoon.Reposition(getRandomPositionOnMap()); ActiveMoons.Add(pickedMoon); MoonPool.RemoveAt(0); } else { GameObject newMoon = Instantiate(moon, pickupItemsParent); NetworkServer.Spawn(newMoon); newMoon.GetComponent().Reposition(getRandomPositionOnMap()); ActiveMoons.Add(newMoon.GetComponent()); } } } public void DeactivatePickupItem(PickupItem item) { if (item.type == PickupItem.PickupType.Moon) { ActiveMoons.Remove(item); MoonPool.Add(item); } else if (item.type == PickupItem.PickupType.Star) { ActiveStars.Remove(item); StarsPool.Add(item); } } public void SetRespawn(GameObject player) { StartCoroutine(setRespawn(player)); } IEnumerator setRespawn(GameObject player) { if (isServer) { player.SetActive(false); yield return new WaitForSeconds(3); Vector3 RespawnPoint = NetworkManager.startPositions[Random.Range(0, NetworkManager.startPositions.Count - 1)].position; player.GetComponent().Respawn(RespawnPoint); } else { yield return new WaitForSeconds(1); } } Vector3 getRandomPositionOnMap() { return getRandomPointInCirlce(Vector3.zero, mapRadius); } Vector3 getRandomPointInCirlce(Vector3 center, float radius) { float r = radius * Mathf.Sqrt(Random.Range(0f, 1f)); float theta = Random.Range(0f, 1f) * 2 * Mathf.PI; float x = center.x + r * Mathf.Cos(theta); float y = center.y + r * Mathf.Sin(theta); return new Vector3(x, y); } void OnDrawGizmos() { Gizmos.DrawWireSphere(transform.position, mapRadius); } }