-
Notifications
You must be signed in to change notification settings - Fork 1
/
Player.cs
222 lines (193 loc) · 6.48 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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
using UnityEngine;
using System.Collections;
using StasisElements;
public class Player : Damagable {
public Element selected;
public float knockbackFactor;
public bool isDead;
public int lives;
public AudioSource noise;
// Use this for initialization
void Start () {
this.hP = 101;
this.lives = GameObject.FindGameObjectWithTag("Game").GetComponent<Game>().lives;
}
// Update is called once per frame
void Update () {
if(this.hP <= 0 && !isDead) {
this.die();
}
else if(this.hP > 101) {
this.hP = 101;
}
Game game = GameObject.FindGameObjectWithTag("Game").GetComponent<Game>();
if (!UI.paused) {
if(Input.GetKeyDown(KeyCode.Mouse0) && !game.levelBeaten) {
atk();
}
if(Input.GetKeyDown(KeyCode.Mouse1) && !game.levelBeaten) {
change();
}
if (Input.GetAxis("Mouse ScrollWheel") > 0) {
selected++;
if ((int)selected==5)
selected = Element.Plant;
}
else if (Input.GetAxis("Mouse ScrollWheel") < 0) {
selected--;
if ((int)selected==-1)
selected = Element.Earth;
}
if (Input.GetKeyDown(KeyCode.Alpha1))
selected = Element.Fire;
if (Input.GetKeyDown(KeyCode.Alpha2))
selected = Element.Water;
if (Input.GetKeyDown(KeyCode.Alpha3))
selected = Element.Shock;
if (Input.GetKeyDown(KeyCode.Alpha4))
selected = Element.Earth;
if (Input.GetKeyDown(KeyCode.Alpha5))
selected = Element.Plant;
}
}
void atk() {
// Fire a different attack method based on the
// currently selected element
// Check if the target is in front of the arm-gun
noise.Play();
Vector3 mousPos = Input.mousePosition;
mousPos.z = transform.position.z - Camera.main.transform.position.z;
mousPos = Camera.main.ScreenToWorldPoint(mousPos);
if(!GetComponentInChildren<SphereCollider>().bounds.Contains(mousPos)) {
switch(selected) {
case Element.Plant:
plantBall();
break;
case Element.Fire:
fireBall();
break;
case Element.Water:
waterBall();
break;
case Element.Shock:
shockBall();
break;
case Element.Earth:
earthBall();
break;
}
}
}
void change(){
// Switch to the next element
switch(selected) {
case Element.Plant:
selected=Element.Fire;
break;
case Element.Fire:
selected=Element.Water;
break;
case Element.Water:
selected=Element.Shock;
break;
case Element.Shock:
selected=Element.Earth;
break;
case Element.Earth:
selected=Element.Plant;
break;
}
}
// The following Five methods all instantiate
// different attacks in the correct place on screen
void fireBall() {
PlayerController controller = GameObject.FindWithTag ("Player").GetComponent<PlayerController>();
Transform spawnPoint = GameObject.FindWithTag("atkPoint").GetComponent<Transform>();
if(controller.facingRight) {
Instantiate(Resources.Load("prefabs/attacks/fireball"),
new Vector3(spawnPoint.position.x,spawnPoint.position.y,0.0f), Quaternion.identity);
}
else {
Instantiate(Resources.Load("prefabs/attacks/fireball"),
new Vector3(spawnPoint.position.x,spawnPoint.position.y,0.0f), Quaternion.identity);
}
}
void waterBall() {
PlayerController controller = GameObject.FindWithTag ("Player").GetComponent<PlayerController>();
Transform spawnPoint = GameObject.FindWithTag("atkPoint").GetComponent<Transform>();
if(controller.facingRight) {
Instantiate(Resources.Load("prefabs/attacks/waterball"),
new Vector3(spawnPoint.position.x,spawnPoint.position.y,0.0f), Quaternion.identity);
}
else {
Instantiate(Resources.Load("prefabs/attacks/waterball"),
new Vector3(spawnPoint.position.x,spawnPoint.position.y,0.0f), Quaternion.identity);
}
}
void shockBall() {
PlayerController controller = GameObject.FindWithTag ("Player").GetComponent<PlayerController>();
Transform spawnPoint = GameObject.FindWithTag("atkPoint").GetComponent<Transform>();
if(controller.facingRight) {
Instantiate(Resources.Load("prefabs/attacks/lightningBall"),
new Vector3(spawnPoint.position.x,spawnPoint.position.y,0.0f), Quaternion.identity);
}
else {
Instantiate(Resources.Load("prefabs/attacks/lightningBall"),
new Vector3(spawnPoint.position.x,spawnPoint.position.y,0.0f), Quaternion.identity);
}
}
void earthBall() {
PlayerController controller = GameObject.FindWithTag ("Player").GetComponent<PlayerController>();
Transform spawnPoint = GameObject.FindWithTag("atkPoint").GetComponent<Transform>();
if(controller.facingRight) {
Instantiate(Resources.Load("prefabs/attacks/earthBall"),
new Vector3(spawnPoint.position.x,spawnPoint.position.y,0.0f), Quaternion.identity);
}
else {
Instantiate(Resources.Load("prefabs/attacks/earthBall"),
new Vector3(spawnPoint.position.x,spawnPoint.position.y,0.0f), Quaternion.identity);
}
}
void plantBall() {
PlayerController controller = GameObject.FindWithTag ("Player").GetComponent<PlayerController>();
Transform spawnPoint = GameObject.FindWithTag("atkPoint").GetComponent<Transform>();
if(controller.facingRight) {
Instantiate(Resources.Load("prefabs/attacks/plantBall"),
new Vector3(spawnPoint.position.x,spawnPoint.position.y,0.0f), Quaternion.identity);
}
else {
Instantiate(Resources.Load("prefabs/attacks/plantBall"),
new Vector3(spawnPoint.position.x,spawnPoint.position.y,0.0f), Quaternion.identity);
}
}
//Adds health to the player. Called by HealthPickup script.
public void addHealth(int amount) {
this.hP += amount;
}
public void addLife(int amount) {
this.lives += amount;
}
public new void dmg(Element type) {
// Add knockback and reduce health upon taking damage
//rigidbody2D.AddForce(new Vector2(0,200));
//rigidbody2D.velocity = knockback*knockbackFactor;
hP -= 15;
}
public void die() {
//Switch to death animation/physics
GameObject.FindGameObjectWithTag("Game").GetComponent<Game>().score -= 1000;
GameObject.FindGameObjectWithTag("Game").GetComponent<Game>().lives--;
GameObject play = GameObject.FindGameObjectWithTag("Player");
play.transform.position = play.GetComponent<PlayerController>().getRespawn().position;
this.lives--;
// rigidbody2D.AddForce(new Vector2(0, 1000));
// CircleCollider2D collider1 = GameObject.FindWithTag ("Player").GetComponent<CircleCollider2D>();
// BoxCollider2D collider2 = GameObject.FindWithTag ("Player").GetComponent<BoxCollider2D>();
// Rigidbody2D rigid = GameObject.FindWithTag ("Player").GetComponent<Rigidbody2D>();
//
// collider1.isTrigger = false;
// collider2.isTrigger = false;
// rigid.isKinematic = true;
isDead = true;
}
}