using System.Collections; using System.Collections.Generic; using UnityEngine; using MyTypes; using Cinemachine; public class Spawner : MonoBehaviour{ static Spawner _instance; public static Spawner Instance{ get{ if (_instance == null){ _instance = FindObjectOfType(); if (_instance == null){ GameObject container = new GameObject("GameManager"); _instance = container.AddComponent(); } } return _instance; } } [SerializeField] PlayerManager players; //[SerializeField] CinemachineVirtualCamera virtualCamera; public GameObject enemy; public GameObject spike; public GameObject SpawnPlayer(){ PlayerManager playerManager = Instantiate(players); //playerManager.followCamera = virtualCamera; playerManager.Init(); GameManager.Instance.Grid.grid[MyVars.p1StartPos.x, MyVars.p1StartPos.y].Pawn = playerManager.Player1; GameManager.Instance.Grid.grid[MyVars.p2StartPos.x, MyVars.p2StartPos.y].Pawn = playerManager.Player2; GameManager.Instance.Grid.RemoveAvailableCell(playerManager.Player1.CurrentCell); GameManager.Instance.Grid.RemoveAvailableCell(playerManager.Player2.CurrentCell); return playerManager.gameObject; } public List SpawnEnemy(int amount) { List safetyCells = new List(); if (GameManager.Instance.player != null) { for(int i=-1; i<2; i++) { for(int j=-1; j<2; j++) { if (!(i == 0 && j == 0)) //prevent self cell check (player is there) { MyCell aux = GameManager.Instance.Grid.getCell(GameManager.Instance.player.GetComponent().Player1.CurrentCell.position, new Vector2Int(i, j)); if (aux.Pawn == null) { safetyCells.Add(aux); GameManager.Instance.Grid.RemoveAvailableCell(aux); } } } } //check surrounding player1 cells for (int i = -1; i < 2; i++) { for (int j = -1; j < 2; j++) { if (!(i == 0 && j == 0)) //prevent self cell check (player is there) { MyCell aux = GameManager.Instance.Grid.getCell(GameManager.Instance.player.GetComponent().Player2.CurrentCell.position, new Vector2Int(i, j)); if (aux.Pawn == null) { safetyCells.Add(aux); GameManager.Instance.Grid.RemoveAvailableCell(aux); } } } } //check surrounding player2 cells //Safety cells have been removed from the available cells list. At the end of the methods, these cells will be fed back into the available cell list. } List enemies = new List(); List cellsToAddBack = new List(); for (int i = 0; i < amount; ++i) { GameObject auxEnemy = Instantiate(enemy, Vector3.zero, Quaternion.identity); enemies.Add(auxEnemy); MyCell auxCell = GameManager.Instance.Grid.GetAvailableCell(); auxEnemy.transform.position = new Vector3 (auxCell.position.x*MyVars.cellSize, 0, auxCell.position.y * MyVars.cellSize); cellsToAddBack.Add(auxCell); } //pushing cells where the enemies spawned back into the available cell list since the enemies movement isnt grid-based and said cells should be occupiable by the player later on. for(int i=0; i SpawnEnemy(int amount, float speed) { List enemies = new List(); List cellsToAddBack = new List(); for (int i = 0; i < amount; ++i) { GameObject auxEnemy = Instantiate(enemy, Vector3.zero, Quaternion.identity); auxEnemy.GetComponent().speed = speed; enemies.Add(auxEnemy); MyCell auxCell = GameManager.Instance.Grid.GetAvailableCell(); auxEnemy.transform.position = new Vector3(auxCell.position.x * MyVars.cellSize, 0, auxCell.position.y * MyVars.cellSize); cellsToAddBack.Add(auxCell); } //pushing cells where the enemies spawned back into the available cell list since the enemies movement isnt grid-based and said cells should be occupiable by the player later on. for (int i = 0; i < amount; ++i) { GameManager.Instance.Grid.AddAvailableCell(cellsToAddBack[cellsToAddBack.Count - 1]); cellsToAddBack.RemoveAt(cellsToAddBack.Count - 1); } return enemies; } public List SpawnSpike(int amount) { List ans = new List(); for (int i = 0; i < amount; ++i) { MyPawn auxSpike = Instantiate(spike, Vector3.zero, Quaternion.identity).GetComponent(); GameManager.Instance.Grid.GetAvailableCell().Pawn = auxSpike; ans.Add(auxSpike.gameObject); } return ans; } }