-
Notifications
You must be signed in to change notification settings - Fork 0
/
RespawnScript
194 lines (162 loc) · 4.61 KB
/
RespawnScript
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class respawn : MonoBehaviour
{
//Dette script sidder på playeren og gør flere ting
//Scriptet styrer hans hp, checkpoints, og død
float health;
public float maxHealth = 100;
float healthFraction;
public int coinDamage = 40;
public int bombDamage = 80;
public int projectileDamage = 20;
public int fishDamage = 30;
PlayerMovement pM;
public movementController mC;
Rigidbody2D rb2d;
public Animator playerAnimator;
public Transform greenBar;
public Transform healthBar;
Vector3 barlocalScale;
int levelBottom = -30;
public int checkPointRange = 1;
public bool alive = true;
public bool hitByCoin;
public bool hitByBomb;
public float reviveTime = 2;
public float dieTime;
public Transform[] checkPoints;
Transform currentCheckPoint;
// Start is called before the first frame update
void Start()
{
currentCheckPoint = checkPoints[0];
spawnIfDead();
pM = GetComponent<PlayerMovement>();
mC = GetComponent<movementController>();
rb2d = GetComponent<Rigidbody2D>();
barlocalScale = greenBar.localScale;
}
void Update()
{
if (hitByCoin)
{
if (mC.canTakeDamage)
{
health -= coinDamage;
}
hitByCoin = false;
}
dieAndSpawn();
newCheckPointIfReached();
healthBarShow();
}
public void spawnIfDead()
{
playerAnimator.SetBool("dead", false);
playerAnimator.SetBool("inWater", false);
transform.position = currentCheckPoint.transform.position;
alive = true;
mC.showItems = true;
//pM.crouch = false;
health = maxHealth;
}
public void dieAndSpawn()
{
if(health < 1)
{
die();
health = 0;
Debug.Log("no health left");
}
if(transform.position.y < levelBottom)
{
Debug.Log("Fell too far");
die();
}
if (!alive)
{
rb2d.velocity = new Vector2(0,rb2d.velocity.y);
}
if (!alive && Time.time > dieTime + reviveTime)
{
spawnIfDead();
}
}
void die()
{
if (alive)
{
mC.showItems = false;
alive = false;
dieTime = Time.time;
playerAnimator.SetBool("dead", true);
playerAnimator.SetTrigger("death");
pM.crouch = false;
//refToSR.color = new Color(1, 1, 1, 0);
}
}
void OnCollisionEnter2D(Collision2D collision)
{
if (mC.canTakeDamage)
{
if (collision.gameObject.layer == 8)
{
health -= projectileDamage;
}
if(collision.gameObject.layer == 16)
{
health -= fishDamage;
}
}
if (collision.gameObject.layer == 11)
{
die();
}
}
void checkIfReachedCheckPoint(int which)
{
if(transform.position.x > checkPoints[which].position.x - checkPointRange && transform.position.x < checkPoints[which].position.x + checkPointRange)
{
if(transform.position.y > checkPoints[which].position.y - checkPointRange && transform.position.y < checkPoints[which].position.y + checkPointRange)
{
currentCheckPoint = checkPoints[which];
}
}
}
void newCheckPointIfReached()
{
for(int i = 0; i < checkPoints.Length; i++)
{
checkIfReachedCheckPoint(i);
}
}
void healthBarShow()
{
if(gameObject.transform.localScale.x < 0)
{
if(healthBar.localScale.x > 0)
{
Vector3 Xcale = healthBar.localScale;
Xcale.x *= -1;
healthBar.localScale = Xcale;
}
}
if (gameObject.transform.localScale.x > 0)
{
if (healthBar.localScale.x < 0)
{
Vector3 Xcale = healthBar.localScale;
Xcale.x *= -1;
healthBar.localScale = Xcale;
}
}
healthFraction = health / maxHealth;
Vector3 greenScale = new Vector3(2, 0.3f, 1);
greenScale.x = healthFraction * barlocalScale.x;
greenBar.localScale = greenScale;
Vector3 greenBarPos = new Vector3(-1+healthFraction,0,0);
greenBar.localPosition = greenBarPos;
}
}