-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.cs
145 lines (138 loc) · 4.99 KB
/
Player.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
138
139
140
141
142
143
144
145
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
//This class contain the script that handles the player object game state.
public class Player : MonoBehaviour
{
public Slider slider;
public Transform Respawn;
public AudioClip gettinghit;
public AudioClip dead;
public AudioClip coinsound;
public AudioClip heartsound;
public GameObject game;
[SerializeField] Text cointext;
private int coincount = 0;
private int startingHitPoints;
private int currplayerhealth;
Coroutine damageCoroutine;
//This function initialize the player health, health bar and coin amount at the start.
void Start()
{
startingHitPoints = 5;
currplayerhealth = startingHitPoints;
slider.value = currplayerhealth;
cointext.text = coincount.ToString();
}
//This function resets the character when the player dies.
public void ResetCharacter()
{
setvol.paused = false;
Time.timeScale = 1f;
game.SetActive(false);
currplayerhealth = startingHitPoints;
slider.value = currplayerhealth;
gameObject.transform.position = Respawn.position;
gameObject.transform.rotation = Respawn.rotation;
}
//This function handle updating the game state of the player until the player take damages.
IEnumerator DamageCharacter(int damage, float interval)
{
while (true)
{
currplayerhealth = currplayerhealth - damage;
slider.value = currplayerhealth;
if (currplayerhealth <= 0 )
{
AudioSource.PlayClipAtPoint(dead, Camera.main.transform.position);
Time.timeScale = 0f;
setvol.paused = true;
game.SetActive(true);
break;
}
AudioSource.PlayClipAtPoint(gettinghit, Camera.main.transform.position);
StartCoroutine(FlickerChar());
if (interval > 0)
{
yield return new WaitForSeconds(interval);
}
else
{
break;
}
}
}
//This function create a filckering animation when the player take damage.
IEnumerator FlickerChar()
{
GetComponent<SpriteRenderer>().color = Color.red;
yield return new WaitForSeconds(0.1f);
GetComponent<SpriteRenderer>().color = Color.white;
}
// This function is called when the player collides with a game object.
void OnCollisionEnter2D(Collision2D collision)
{
string tagg = collision.gameObject.tag;
if (tagg == "Pickable")
{
AudioSource.PlayClipAtPoint(coinsound, Camera.main.transform.position);
coincount += 1;
cointext.text = coincount.ToString();
collision.gameObject.SetActive(false);
}
else if(tagg == "heart")
{
AudioSource.PlayClipAtPoint(heartsound, Camera.main.transform.position);
if (currplayerhealth < startingHitPoints)
{
currplayerhealth = currplayerhealth + 1;
slider.value = currplayerhealth;
}
collision.gameObject.SetActive(false);
}else if (tagg == "Enermy")
{
int damage = collision.gameObject.GetComponent<Enemy>().damageStrength;
damageCoroutine = StartCoroutine(DamageCharacter(damage, 1.0f));
}
else if (tagg == "enermy2")
{
int damage = collision.gameObject.GetComponent<Enemy>().damageStrength;
damageCoroutine = StartCoroutine(DamageCharacter(damage, 1.0f));
}
else if (tagg == "bullet3")
{
currplayerhealth = currplayerhealth - 1;
slider.value = currplayerhealth;
if (currplayerhealth <= 0)
{
AudioSource.PlayClipAtPoint(dead, Camera.main.transform.position);
Time.timeScale = 0f;
setvol.paused = true;
game.SetActive(true);
return;
}
AudioSource.PlayClipAtPoint(gettinghit, Camera.main.transform.position);
StartCoroutine(FlickerChar());
}
}
//This function is called after the OnCollisionEnter function to stop the coroutine from continously triggering and damaging the player.
void OnCollisionExit2D(Collision2D collision)
{
if (collision.gameObject.tag == "Enermy" )
{
if (damageCoroutine != null)
{
StopCoroutine(damageCoroutine);
damageCoroutine = null;
}
}else if(collision.gameObject.tag == "enermy2")
{
if (damageCoroutine != null)
{
StopCoroutine(damageCoroutine);
damageCoroutine = null;
}
}
}
}