Repository navigation
Expand file tree
/
Copy pathGameManagerX.cs
More file actions
108 lines (90 loc) · 3.13 KB
/
Copy pathGameManagerX.cs
File metadata and controls
108 lines (90 loc) · 3.13 KB
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class GameManagerX : MonoBehaviour
{
public TextMeshProUGUI scoreText;
public TextMeshProUGUI gameOverText;
public GameObject titleScreen;
public Button restartButton;
public TextMeshProUGUI timerText;
public List<GameObject> targetPrefabs;
private int score;
private float spawnRate = 1.5f;
public bool isGameActive;
private float timeScore;
private float spaceBetweenSquares = 2.5f;
private float minValueX = -3.75f; // x value of the center of the left-most square
private float minValueY = -3.75f; // y value of the center of the bottom-most square
// Start the game, remove title screen, reset score, and adjust spawnRate based on difficulty button clicked
public void StartGame(int difficulty)
{
timeScore = 60;
spawnRate /= difficulty;
isGameActive = true;
StartCoroutine(SpawnTarget());
score = 0;
UpdateScore(0);
titleScreen.SetActive(false);
}
// While game is active spawn a random target
IEnumerator SpawnTarget()
{
while (isGameActive)
{
yield return new WaitForSeconds(spawnRate);
int index = Random.Range(0, targetPrefabs.Count);
if (isGameActive)
{
Instantiate(targetPrefabs[index], RandomSpawnPosition(), targetPrefabs[index].transform.rotation);
}
}
}
void Update()
{
if (isGameActive)
{
timeScore -= Time.deltaTime;
timerText.text = $"Time: {timeScore:F0}";
//timerText.text = "Time:" + (int)timeScore;
if (timeScore < 0)
{
GameOver();
}
}
}
// Generate a random spawn position based on a random index from 0 to 3
Vector3 RandomSpawnPosition()
{
float spawnPosX = minValueX + (RandomSquareIndex() * spaceBetweenSquares);
float spawnPosY = minValueY + (RandomSquareIndex() * spaceBetweenSquares);
Vector3 spawnPosition = new Vector3(spawnPosX, spawnPosY, 0);
return spawnPosition;
}
// Generates random square index from 0 to 3, which determines which square the target will appear in
int RandomSquareIndex()
{
return Random.Range(0, 4);
}
// Update score with value from target clicked
public void UpdateScore(int scoreToAdd)
{
score += scoreToAdd;
scoreText.text = "Score:" + score;
}
// Stop game, bring up game over text and restart button
public void GameOver()
{
gameOverText.gameObject.SetActive(true);
restartButton.gameObject.SetActive(true);
isGameActive = false;
}
// Restart game by reloading the scene
public void RestartGame()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}