-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathGameSession.cs
More file actions
66 lines (57 loc) · 1.41 KB
/
GameSession.cs
File metadata and controls
66 lines (57 loc) · 1.41 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
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class GameSession : MonoBehaviour {
[SerializeField] int playerLives = 3;
[SerializeField] int score = 0;
[SerializeField] Text livesText;
[SerializeField] Text scoreText;
private void Awake()
{
int numGameSessions = FindObjectsOfType<GameSession>().Length;
if (numGameSessions > 1)
{
Destroy(gameObject);
}
else
{
DontDestroyOnLoad(gameObject);
}
}
// Use this for initialization
void Start () {
livesText.text = playerLives.ToString();
scoreText.text = score.ToString();
}
public void AddToScore (int pointsToAdd)
{
score += pointsToAdd;
scoreText.text = score.ToString();
}
public void ProcessPlayerDeath()
{
if (playerLives > 1)
{
TakeLife();
}
else
{
ResetGameSession();
}
}
private void TakeLife()
{
playerLives--;
var currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
SceneManager.LoadScene(currentSceneIndex);
livesText.text = playerLives.ToString();
}
private void ResetGameSession()
{
SceneManager.LoadScene(0);
Destroy(gameObject);
}
}