This repository has been archived by the owner on Mar 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.pde
209 lines (177 loc) · 7.18 KB
/
Player.pde
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
import java.util.*;
class Player extends Entity
{
private float lastShotTimestamp; // Represents the time (in milliseconds) when the last projectile was fired by the player
private float lastHealth, maxHealth;
private float greenBoostTintEffect; // Used in animating boosting of the health bar
private boolean playTintEffect; // Used in animating boosting of the health bar
Explosion deathExplosion;
Player(float x, float y, float speed, float scale, float health)
{
super(x, y, speed, scale, health);
this.lastShotTimestamp = 0;
this.lastHealth = this.health;
this.maxHealth = this.health;
this.greenBoostTintEffect = 0;
this.playTintEffect = false;
this.entityTexture = loadImage("Assets/tank.png");
this.missileTexture = loadImage("Assets/tank_missile.png");
}
// Method that resets the player to maximum health specified
void ResetHealth(float health)
{
this.health = health;
this.lastHealth = health;
this.maxHealth = health;
this.playTintEffect = true;
}
// Method for making sure the player doesn't leave the scene's bounds
private void CheckPlayerBoundsCollision()
{
if (this.position.x + (this.scale / 2) > 1500)
this.position.x = 1500 - (this.scale / 2);
else if (this.position.x - (this.scale / 2) < 0)
this.position.x = (this.scale / 2);
}
// Method for handling movement of the player
private void Move(final boolean keysPressed[])
{
if (keysPressed[LEFT] || keysPressed['a'] || keysPressed['A'])
this.velocity.x = -this.speed;
else if (keysPressed[RIGHT] || keysPressed['d'] || keysPressed['D'])
this.velocity.x = this.speed;
else
this.velocity.x = 0.0f;
}
// Method which checks if the player's collided with an enemy, if so then the enemy is destroyed and the player's health is decreased
void CheckEnemyCollision(final ArrayList<Enemy> enemies, ArrayList<Explosion> explosions, float difficulty)
{
Iterator it = enemies.listIterator();
while (it.hasNext())
{
Enemy enemy = (Enemy)it.next();
if (this.position.x + (this.scale / 2) >= enemy.position.x - (enemy.scale / 2) &&
this.position.x - (this.scale / 2) <= enemy.position.x + (enemy.scale / 2) &&
this.position.y + (this.scale / 2) >= enemy.position.y - (enemy.scale / 2) &&
this.position.y - (this.scale / 2) <= enemy.position.y + (enemy.scale / 2))
{
this.health -= 40 + (difficulty * 50);
explosions.add(new Explosion(enemy.GetPosition().x, enemy.GetPosition().y, 75));
it.remove();
}
}
}
// Method that shoots a missile projectile in the direction of oncoming enemies
void ShootMissileProjectile(final boolean keysPressed[])
{
final float projectileCooldown = 400.0f;
if (keysPressed[' '] && (millis() - this.lastShotTimestamp >= projectileCooldown))
{
this.projectiles.add(new Missile(this.position.x, this.position.y + (this.scale / 2), 5, 100));
this.lastShotTimestamp = millis();
}
}
// Method that handles the animation of the health bar when decreasing
void PlayHealthBarAnimation()
{
if (this.lastHealth > this.health)
{
if (this.lastHealth - (this.maxHealth / 100.0f) < this.health)
this.lastHealth = this.health;
else
this.lastHealth -= (this.maxHealth / 100.0f);
}
else if (this.lastHealth < this.health)
{
if (this.lastHealth + (this.maxHealth / 100.0f) > this.health)
this.lastHealth = this.health;
else
this.lastHealth += (this.maxHealth / 100.0f);
}
}
// Method that tracks the largest amount of health the player had
void UpdateMaximumHealth()
{
if (this.maxHealth < this.health)
this.maxHealth = this.health;
}
// Method that animates the bar to indicate that it has been reset and boosted
void PlayHeathBarResetAnimation()
{
if (this.playTintEffect && this.greenBoostTintEffect < 105)
this.greenBoostTintEffect += 2.5f;
else if (this.playTintEffect && this.greenBoostTintEffect == 105)
this.playTintEffect = false;
else if (!this.playTintEffect && this.greenBoostTintEffect > 0)
this.greenBoostTintEffect -= 2.5f;
}
// Method for updating the player state e.g. position, health etc.
void Update(final boolean keysPressed[], ArrayList<Enemy> enemies, ArrayList<Explosion> explosions, float difficulty)
{
// Update health bar state
this.UpdateMaximumHealth();
this.PlayHealthBarAnimation();
this.PlayHeathBarResetAnimation();
// Update the player state
this.Move(keysPressed);
this.ShootMissileProjectile(keysPressed);
this.position.AddVector(this.velocity);
for (Enemy enemy : enemies)
this.CheckProjectileCollision(enemy.GetProjectiles(), explosions);
// Do collision detections
this.CheckPlayerBoundsCollision();
this.CheckEnemyCollision(enemies, explosions, difficulty);
// Update the projectiles shot by the player
for (Missile missile : this.projectiles)
missile.Update();
// Destroy projectiles that have left scene bounds
Iterator missileIt = this.projectiles.listIterator();
while (missileIt.hasNext())
{
Missile missile = (Missile)missileIt.next();
if(missile.GetPosition().y - (missile.GetSize().y / 2) >= 900)
missileIt.remove();
}
// Update death explosion (if the player has been destroyed i.e the health is fully depleted)
if (this.deathExplosion != null)
this.deathExplosion.Update();
// Exit game application if the player's health has fully depleted
if (this.health <= 0 && this.deathExplosion == null)
this.deathExplosion = new Explosion(this.position.x, this.position.y, 150);
// Exit once death explosion animation has played
if (this.deathExplosion != null && this.deathExplosion.GetOpacity() <= 0)
exit();
}
// Override method for rendering the player
@Override
void Render()
{
imageMode(CENTER);
// Render the projectiles shot by the player
for (Missile missile : this.projectiles)
missile.Render(this.missileTexture);
// Render the player
if (this.health > 0)
image(this.entityTexture, this.position.x, this.position.y, this.scale, this.scale);
// Render the health bar
if ((this.health / this.maxHealth) * 100 >= 65) // Health bar is rendered as green
fill(0, 150 + this.greenBoostTintEffect, 0);
else if ((this.health / this.maxHealth) * 100 >= 40) // Health bar is rendered as yellow
fill(255, 255, 0);
else // Health bar is rendered as red
fill(255, 0, 0);
noStroke();
rectMode(CORNER);
rect(1525, 875, 50, -max((((this.lastHealth / this.maxHealth) * 100.0f) * 8.5f), 0.0f));
// Draws the outer shell of the health bar
noFill();
stroke(0, 0, 225);
strokeWeight(5);
rect(1525, 25, 50, 850);
// Render death explosion (if the player has been destroyed i.e the health is fully depleted)
if (this.deathExplosion != null)
this.deathExplosion.Render();
}
// Getter function that returned the maximum health of the player
final float GetMaximumHealth() { return this.maxHealth; }
}