-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.java
268 lines (207 loc) · 5.01 KB
/
Player.java
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
*/
import java.awt.Rectangle;
import java.awt.Color;
class Player extends MapObject {
private double speed;
private Point gunDir; // memorizes the last direction the player moved in to allow player to shoot while staying still
private int health;
private int maxHealth;
private int lives;
private Point lastPos; // for collisions
private int damage; // bullet damage
private int maxAmmo;
private int ammo; // current ammo
private long timeReloadStart;
private long reloadTime; // in milliseconds
private boolean isReloading;
private long timeRespawnStart;
private long respawnTime;
private boolean isRespawning;
private long lastShotTime; //for firing rate
// for pickups
private long speedTime;
private long damageTime;
Player(Point pos, Point gunDir, Color color) {
super(pos, new Point(0, 0), 40, color);
this.gunDir = gunDir;
speed = 3;
maxHealth = 50;
health = maxHealth;
lives = 3;
damage = 10;
maxAmmo = 8;
ammo = maxAmmo;
reloadTime = 1500;
isRespawning = false;
lastPos = new Point(pos.x, pos.y);
speedTime = 0;
damageTime = 0;
lastShotTime = 0;
hitBox = new Rectangle((int) pos.x, (int) pos.y, size, size);
}
//METHODS
/*
move
updates player position, hitbox and gun direction based on direction, speed, and elapsedTime
*/
public void move(double elapsedTime) {
if (dir.x != 0 || dir.y != 0){
gunDir.x = -dir.x;
gunDir.y = -dir.y;
}
// elapsedTime is for faster frame rates
pos = new Point(pos.x + dir.x * speed * elapsedTime * 100,
pos.y + dir.y * speed * elapsedTime * 100);
//move hit box
hitBox = new Rectangle((int) pos.x, (int) pos.y, size, size);
//System.out.println(elapsedTime);
}
//Firing rate methods
public long getLastShotTime() {
return lastShotTime;
}
public void setLastShotTime() {
lastShotTime = System.currentTimeMillis();
}
//Player Position, Direction Methods
public void setDirX(double x) {
dir.x = x;
}
public void setDirY(double y) {
dir.y = y;
}
public Point getGunDir() {
return gunDir;
}
public void setGunDir(Point gunDir) {
this.gunDir = gunDir;
}
//Player Ammo and Reload Methods
public int getAmmo() {
return ammo;
}
public void reduceAmmo() {
--ammo;
}
public void refillAmmo () {
ammo = maxAmmo;
}
public boolean getIsReloading() {
return isReloading;
}
public void setIsReloading(boolean isReloading) {
this.isReloading = isReloading;
}
public long getReloadTime() {
return reloadTime;
}
public long getTimeReloadStart() {
return timeReloadStart;
}
public void setTimeReloadStart(long timeReloadStart) {
this.timeReloadStart = timeReloadStart;
}
//Player Respawning Methods
public boolean getIsRespawning() {
return isRespawning;
}
public void setIsRespawning(boolean isRespawning) {
this.isRespawning = isRespawning;
}
public long getRespawnTime() {
return respawnTime;
}
public long getTimeRespawnStart() {
return timeRespawnStart;
}
public void setTimeRespawnStart(long timeRespawnStart) {
this.timeRespawnStart = timeRespawnStart;
}
//Player Health and Lives Methods
public int getHealth() {
return health;
}
public void reduceHealth(int health) {
this.health -= health;
}
public void setHealth(int health) {
this.health = health;
}
public int getMaxHealth() {
return maxHealth;
}
public void setMaxHealth(int maxHealth) {
this.maxHealth = maxHealth;
}
public int getLives() {
return lives;
}
public void loseLife() {
lives--;
}
//Player Collision - lastPos Methods
public Point getLastPos() {
return lastPos;
}
public void setLastPos(Point lastPos) {
this.lastPos.x = lastPos.x;
this.lastPos.y = lastPos.y;
}
//Player Pickup Methods
public double getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public int getDamage() {
return damage;
}
public void setDamage(int damage) {
this.damage = damage;
}
public long getSpeedTime() {
return speedTime;
}
public void startSpeedPickup() {
if (speedTime == 0) {
speed = (speed*1.5);
}
speedTime = System.currentTimeMillis();
}
public void endSpeedPickup() {
speedTime = 0;
speed = (speed/1.5);
}
public long getDamageTime() {
return damageTime;
}
public void startDamagePickup() {
if (damageTime == 0) {
damage = damage*2;
}
damageTime = System.currentTimeMillis();
}
public void endDamagePickup() {
damageTime = 0;
damage = damage/2;
}
public void useHealthPickup() {
if (health + 20 > maxHealth) {
health = maxHealth;
} else {
health = health + 20;
}
}
public void clearBuffs() {
if (speedTime != 0) {
speedTime = 0;
speed = (speed/1.5);
}
if (damageTime != 0) {
damageTime = 0;
damage = damage/2;
}
}
}