init
This commit is contained in:
commit
e0a842f222
796 changed files with 361371 additions and 0 deletions
235
Assets/Scripts/GameManager.cs
Normal file
235
Assets/Scripts/GameManager.cs
Normal file
|
@ -0,0 +1,235 @@
|
|||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using MyTypes;
|
||||
using TMPro;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
public class GameManager : MonoBehaviour
|
||||
{
|
||||
static GameManager _instance;
|
||||
public static GameManager Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_instance == null)
|
||||
{
|
||||
_instance = GameObject.FindObjectOfType<GameManager>();
|
||||
|
||||
if (_instance == null)
|
||||
{
|
||||
GameObject container = new GameObject("GameManager");
|
||||
_instance = container.AddComponent<GameManager>();
|
||||
}
|
||||
}
|
||||
|
||||
return _instance;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
//game references
|
||||
private MyTypes.MyGrid grid;
|
||||
public MyTypes.MyGrid Grid
|
||||
{
|
||||
get
|
||||
{
|
||||
return grid;
|
||||
}
|
||||
}
|
||||
|
||||
[System.NonSerialized]
|
||||
public GameObject player;
|
||||
public List<GameObject> enemies;
|
||||
public List<GameObject> spikes;
|
||||
|
||||
public GameObject scoreCanvas;
|
||||
private ScoreSystem scoreSys;
|
||||
public ScoreSystem ScoreSys { get { return scoreSys; } }
|
||||
public Animator fade;
|
||||
public LevelGeneration levelGeneration;
|
||||
|
||||
public bool autoStart = false;
|
||||
//Testing
|
||||
public bool hardStart = false;
|
||||
private int round = 0;
|
||||
|
||||
private bool gameActive = false;
|
||||
|
||||
|
||||
private void Start()
|
||||
{
|
||||
CreateGrid();
|
||||
InitializeScore();
|
||||
if (SceneManager.GetActiveScene().buildIndex == 1)
|
||||
{
|
||||
Debug.Log(SceneManager.GetActiveScene().buildIndex);
|
||||
StartGame();
|
||||
}else if (hardStart){
|
||||
StartGame();
|
||||
}
|
||||
|
||||
AudioManager.instance.Play("MainMusic");
|
||||
}
|
||||
|
||||
public void Lose(){
|
||||
AudioManager.instance.Play("GameOver");
|
||||
PlayerPrefs.SetInt("Score", scoreSys.Score);
|
||||
StartCoroutine(Lose(2f));
|
||||
}
|
||||
IEnumerator Lose(float delay){
|
||||
yield return new WaitForSeconds(delay);
|
||||
fade.SetTrigger("FadeIn");
|
||||
yield return new WaitForSeconds(1f);
|
||||
Loader.Load(4);
|
||||
}
|
||||
|
||||
private float timeSinceStart = 0;
|
||||
private void Update()
|
||||
{
|
||||
if (gameActive) {
|
||||
timeSinceStart += (Time.deltaTime*(round+1));
|
||||
if (timeSinceStart>=1)
|
||||
{
|
||||
timeSinceStart = 0;
|
||||
scoreSys.AddScore(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void InitializeScore()
|
||||
{
|
||||
scoreSys = scoreCanvas.GetComponentInChildren<ScoreSystem>();
|
||||
}
|
||||
private void CreateGrid()
|
||||
{
|
||||
|
||||
//grid initialization
|
||||
grid = new MyTypes.MyGrid(MyVars.gridWidth, MyVars.gridHeight);
|
||||
|
||||
//grid population
|
||||
for (int i = 0; i < MyVars.gridWidth; ++i)
|
||||
{
|
||||
for (int j = 0; j < MyVars.gridHeight; ++j)
|
||||
{
|
||||
grid.grid[i, j] = new MyCell(i, j);
|
||||
grid.AddAvailableCell(grid.grid[i, j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void StopGame()
|
||||
{
|
||||
GameManager.Instance.gameActive = false;
|
||||
}
|
||||
public void StartGame(){
|
||||
|
||||
GameManager.Instance.player = Spawner.Instance.SpawnPlayer();
|
||||
GameManager.Instance.spikes = new List<GameObject>(Spawner.Instance.SpawnSpike((int)MyVars.spikeAmount));
|
||||
GameManager.Instance.enemies = new List<GameObject>(Spawner.Instance.SpawnEnemy((int)MyVars.minEnemyAmount)); //add the enemies last since it is relevant for grid cell purposes
|
||||
|
||||
GameManager.Instance.scoreCanvas.SetActive(true);
|
||||
|
||||
GameManager.Instance.gameActive = true;
|
||||
|
||||
if(levelGeneration!=null)
|
||||
levelGeneration.StartGeneration();
|
||||
}
|
||||
public void movePawn(MyPawn pawn, MyCell targetCell)
|
||||
{
|
||||
grid.movePawn(pawn, targetCell);
|
||||
}
|
||||
public void movePawn(MyPawn pawn, MyDirection dir)
|
||||
{
|
||||
Vector2Int pawnPos = pawn.CurrentCell.position;
|
||||
|
||||
//prevent movement if you are on the edge of the screen
|
||||
if (pawnPos.x == MyVars.gridWidth - 1 && dir == MyDirection._RIGHT)
|
||||
return;
|
||||
else if (pawnPos.x == 0 && dir == MyDirection._LEFT)
|
||||
return;
|
||||
else if (pawnPos.y == MyVars.gridHeight - 1 && dir == MyDirection._UP)
|
||||
return;
|
||||
else if (pawnPos.y == 0 && dir == MyDirection._DOWN)
|
||||
return;
|
||||
|
||||
|
||||
//look up destination cell
|
||||
MyCell targetCell;
|
||||
switch (dir)
|
||||
{
|
||||
default:
|
||||
case MyDirection._UP:
|
||||
targetCell = grid.grid[pawn.CurrentCell.position.x, pawn.CurrentCell.position.y+1];
|
||||
break;
|
||||
case MyDirection._DOWN:
|
||||
targetCell = grid.grid[pawn.CurrentCell.position.x, pawn.CurrentCell.position.y - 1];
|
||||
break;
|
||||
case MyDirection._RIGHT:
|
||||
targetCell = grid.grid[pawn.CurrentCell.position.x+1, pawn.CurrentCell.position.y];
|
||||
break;
|
||||
case MyDirection._LEFT:
|
||||
targetCell = grid.grid[pawn.CurrentCell.position.x-1, pawn.CurrentCell.position.y];
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
//if cell is empty, proceed
|
||||
if (targetCell.Pawn == null)
|
||||
{
|
||||
grid.movePawn(pawn, targetCell);
|
||||
}
|
||||
}
|
||||
|
||||
public void SlowTime(bool slowmo)
|
||||
{
|
||||
if (slowmo)
|
||||
Time.timeScale = MyVars.slowTime;
|
||||
else
|
||||
Time.timeScale = MyVars.defaultTime;
|
||||
}
|
||||
|
||||
public void nextRound()
|
||||
{
|
||||
round++;
|
||||
enemies.AddRange(Spawner.Instance.SpawnEnemy((int)Mathf.Min(MyVars.minEnemyAmount+round*MyVars.enemyAmountGrowth, MyVars.maxEnemyAmount), Mathf.Min(MyVars.enemyMaxSpeed, round* MyVars.enemySpeedGrowth)));
|
||||
}
|
||||
|
||||
private void ClearGame()
|
||||
{
|
||||
foreach (GameObject go in enemies)
|
||||
Destroy(go);
|
||||
enemies.Clear();
|
||||
|
||||
foreach (GameObject go in spikes)
|
||||
Destroy(go);
|
||||
spikes.Clear();
|
||||
|
||||
scoreSys.Score = 0;
|
||||
|
||||
Destroy(player);
|
||||
|
||||
CreateGrid();
|
||||
}
|
||||
|
||||
public void PlayAgain()
|
||||
{
|
||||
ClearGame();
|
||||
StartGame();
|
||||
round = 0;
|
||||
}
|
||||
public void Exit()
|
||||
{
|
||||
#if UNITY_STANDALONE
|
||||
//Quit the application
|
||||
Application.Quit();
|
||||
#endif
|
||||
|
||||
//If we are running in the editor
|
||||
#if UNITY_EDITOR
|
||||
//Stop playing the scene
|
||||
UnityEditor.EditorApplication.isPlaying = false;
|
||||
#endif
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue