init
This commit is contained in:
commit
e0a842f222
796 changed files with 361371 additions and 0 deletions
63
Assets/Scripts/Level/LevelGeneration.cs
Normal file
63
Assets/Scripts/Level/LevelGeneration.cs
Normal file
|
@ -0,0 +1,63 @@
|
|||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using MyTypes;
|
||||
|
||||
public class LevelGeneration : MonoBehaviour{
|
||||
|
||||
[SerializeField] GameObject levelPrefab;
|
||||
[SerializeField] bool spawnGrid = true;
|
||||
|
||||
[Space]
|
||||
[SerializeField] Material black;
|
||||
[SerializeField] Material white;
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start(){
|
||||
if (GameManager.Instance.autoStart)
|
||||
StartGeneration();
|
||||
}
|
||||
|
||||
public void StartGeneration(){
|
||||
if(spawnGrid)
|
||||
StartCoroutine(SpawnGrid());
|
||||
}
|
||||
|
||||
IEnumerator SpawnGrid(){
|
||||
/*for (int i = 0; i < MyVars.gridWidth; i++){
|
||||
for (int j = 0; j < MyVars.gridHeight; j++){
|
||||
MeshRenderer mesh = Instantiate(levelPrefab, new Vector3(i, -1.5f, j), Quaternion.identity).GetComponentInChildren<MeshRenderer>();
|
||||
mesh.material = (i + j) % 2 == 0 ? black : white;
|
||||
yield return new WaitForSecondsRealtime(0.015625f);
|
||||
}
|
||||
}*/
|
||||
|
||||
List<Vector3> positions = new List<Vector3>();
|
||||
for (int i = 0; i < MyVars.gridWidth; i++){
|
||||
for (int j = 0; j < MyVars.gridHeight; j++){
|
||||
positions.Add(new Vector3(i, -1.5f, j));
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < MyVars.gridWidth * MyVars.gridHeight; i += 3){
|
||||
if (i < MyVars.gridWidth * MyVars.gridHeight){
|
||||
Vector3 pos = positions[Random.Range(0, positions.Count)];
|
||||
MeshRenderer mesh = Instantiate(levelPrefab, pos, Quaternion.identity).GetComponentInChildren<MeshRenderer>();
|
||||
mesh.material = (pos.x + pos.z) % 2 == 0 ? black : white;
|
||||
positions.Remove(pos);
|
||||
}
|
||||
if (i + 1 < MyVars.gridWidth * MyVars.gridHeight){
|
||||
Vector3 pos = positions[Random.Range(0, positions.Count)];
|
||||
MeshRenderer mesh = Instantiate(levelPrefab, pos, Quaternion.identity).GetComponentInChildren<MeshRenderer>();
|
||||
mesh.material = (pos.x + pos.z) % 2 == 0 ? black : white;
|
||||
positions.Remove(pos);
|
||||
}
|
||||
if (i + 2 < MyVars.gridWidth * MyVars.gridHeight){
|
||||
Vector3 pos = positions[Random.Range(0, positions.Count)];
|
||||
MeshRenderer mesh = Instantiate(levelPrefab, pos, Quaternion.identity).GetComponentInChildren<MeshRenderer>();
|
||||
mesh.material = (pos.x + pos.z) % 2 == 0 ? black : white;
|
||||
positions.Remove(pos);
|
||||
}
|
||||
yield return new WaitForSecondsRealtime(0.01f);
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/Scripts/Level/LevelGeneration.cs.meta
Normal file
11
Assets/Scripts/Level/LevelGeneration.cs.meta
Normal file
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 27e33b608f219f5478ca25ba58aa4d2e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
158
Assets/Scripts/Level/Spawner.cs
Normal file
158
Assets/Scripts/Level/Spawner.cs
Normal 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;
|
||||
}
|
||||
|
||||
}
|
11
Assets/Scripts/Level/Spawner.cs.meta
Normal file
11
Assets/Scripts/Level/Spawner.cs.meta
Normal file
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 295df3d001cb49145a48e4d5c7cb3b90
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Loading…
Add table
Add a link
Reference in a new issue