This commit is contained in:
Gerard Gascón 2025-04-24 17:26:56 +02:00
commit e0a842f222
796 changed files with 361371 additions and 0 deletions

View file

@ -0,0 +1,158 @@
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<Spawner>();
if (_instance == null){
GameObject container = new GameObject("GameManager");
_instance = container.AddComponent<Spawner>();
}
}
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<GameObject> SpawnEnemy(int amount)
{
List<MyCell> safetyCells = new List<MyCell>();
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<PlayerManager>().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<PlayerManager>().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<GameObject> enemies = new List<GameObject>();
List<MyCell> cellsToAddBack = new List<MyCell>();
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<amount; ++i)
{
GameManager.Instance.Grid.AddAvailableCell(cellsToAddBack[cellsToAddBack.Count - 1]);
cellsToAddBack.RemoveAt(cellsToAddBack.Count-1);
}
//feeding back the safety cells into the available cells list
foreach(MyCell c in safetyCells)
{
GameManager.Instance.Grid.AddAvailableCell(c);
}
return enemies;
}
public List<GameObject> SpawnEnemy(int amount, float speed)
{
List<GameObject> enemies = new List<GameObject>();
List<MyCell> cellsToAddBack = new List<MyCell>();
for (int i = 0; i < amount; ++i)
{
GameObject auxEnemy = Instantiate(enemy, Vector3.zero, Quaternion.identity);
auxEnemy.GetComponent<EnemyBehavior>().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<GameObject> SpawnSpike(int amount)
{
List<GameObject> ans = new List<GameObject>();
for (int i = 0; i < amount; ++i)
{
MyPawn auxSpike = Instantiate(spike, Vector3.zero, Quaternion.identity).GetComponent<SpikeBehavior>();
GameManager.Instance.Grid.GetAvailableCell().Pawn = auxSpike;
ans.Add(auxSpike.gameObject);
}
return ans;
}
}