-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameController.cs
137 lines (116 loc) · 4.14 KB
/
GameController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using TMPro;
public class GameController : MonoBehaviour
{
private int score = 0;
public TextMeshProUGUI scoreText;
public GameObject[] enemySpawners;
public GameObject dustPrefab;
public int initialMinDust;
public int initialMaxDust;
public GameObject gameOverObj;
public TextMeshProUGUI gameOverScore;
public float gameOverTime;
private int minDust;
private int maxDust;
public float scoreToSpeedFactor;
// Start is called before the first frame update
void Start()
{
score = 0;
resetDustSpawnRate();
spawnEdges();
SpawnEnemy();
}
// Update is called once per frame
void Update()
{
// if (Input.GetKeyDown(KeyCode.Space)) {
// SpawnEnemy();
// }
// TODO eventually build pause screen but Escape key will return us to Main Menu for now
if (Input.GetKeyDown(KeyCode.Escape)) {
SceneManager.LoadScene("MainMenu");
}
}
// Adds a score to the total in the UI
public void AddScore(int amount)
{
score += amount;
scoreText.text = "Score: " + score;
gameOverScore.text = "Final Score: " + score;
}
// Spawn an enemy in one of the enemySpawners
public void SpawnEnemy()
{
float speedIncrease = score * scoreToSpeedFactor;
enemySpawners[Random.Range(0, enemySpawners.Length)].GetComponent<EnemySpawner>().SpawnJellyEnemy(speedIncrease);
}
// Spawns dust pickups at position provided (usually after enemy dies)
public void SpawnDust(Vector2 pos)
{
int dustAmount = Random.Range(minDust, maxDust + 1);
float rotationShift = 360.0f / dustAmount;
float zRotation = Random.Range(0, 360);
for (int i = 0; i < dustAmount; i++) {
Instantiate(dustPrefab, pos, Quaternion.Euler(0, 0, zRotation));
zRotation += rotationShift;
}
}
// Modifies the min/max dust spawning temporarily (has to be reset with resetDustSpawnRate())
public void modifyDustSpawnRate(int minInclusive, int maxInclusive)
{
minDust = minInclusive;
maxDust = maxInclusive;
}
public void resetDustSpawnRate()
{
minDust = initialMinDust;
maxDust = initialMaxDust;
}
public void spawnEdges()
{
float halfHeight = Camera.main.orthographicSize;
float halfWidth = halfHeight * Camera.main.aspect;
GameObject topEdge = new GameObject("TopEdge");
topEdge.transform.position = new Vector2(0, halfHeight);
EdgeCollider2D topEdgeCollider = topEdge.AddComponent<EdgeCollider2D>();
topEdgeCollider.points = (new Vector2[] {
new Vector2(-halfWidth, 0),
new Vector2(halfWidth, 0)
});
GameObject rightEdge = new GameObject("RightEdge");
rightEdge.transform.position = new Vector2(halfWidth, 0);
EdgeCollider2D rightEdgeCollider = rightEdge.AddComponent<EdgeCollider2D>();
rightEdgeCollider.points = (new Vector2[] {
new Vector2(0, halfHeight),
new Vector2(0, -halfHeight)
});
GameObject bottomEdge = new GameObject("BottomEdge");
bottomEdge.transform.position = new Vector2(0, -halfHeight);
EdgeCollider2D bottomEdgeCollider = bottomEdge.AddComponent<EdgeCollider2D>();
bottomEdgeCollider.points = (new Vector2[] {
new Vector2(-halfWidth, 0),
new Vector2(halfWidth, 0)
});
GameObject leftEdge = new GameObject("LeftEdge");
leftEdge.transform.position = new Vector2(-halfWidth, 0);
EdgeCollider2D leftEdgeCollider = leftEdge.AddComponent<EdgeCollider2D>();
leftEdgeCollider.points = (new Vector2[] {
new Vector2(0, halfHeight),
new Vector2(0, -halfHeight)
});
}
// Reloads the Scene after waiting a bit, using WaitToRestart()
public void GameOver()
{
gameOverObj.SetActive(true);
}
public void GoToMainMenu()
{
SceneManager.LoadScene("MainMenu");
}
}