-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.java
308 lines (230 loc) · 7.66 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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// Final Project
// CS201
// DiveKick Applet - Player class
// Neil Steiner
// Jana Parsons
import java.awt.*;
import java.awt.geom.AffineTransform;
public class Player {
//instance variables
// integer modifier for all speed related things
int gameSpeed;
int scoreCount;
//player parts
Rectangle head;
Rectangle torso;
Rectangle straightLeg;
Rectangle straightFoot;
Rectangle kickLeg;
Rectangle kickFoot;
//player part dimension constants
int HEAD_HEIGHT = 20;
int HEAD_WIDTH = 20;
int TORSO_HEIGHT = 50;
int TORSO_WIDTH = 40;
int LEG_HEIGHT = 50;
int LEG_WIDTH = 10;
int FOOT_HEIGHT = 10;
int FOOT_WIDTH = 20;
// set to -1 if facing left, 1 if facing right---------------------------------
int left;
// keep track to prevent repeated kick cheat
boolean isKicking;
int height;
int width;
int xPosition;
int yPosition;
int xVelocity;
int yVelocity;
int killStreak;
int headStreak;
FightCanvas parent;
//constructor
public Player(int x, int direction, FightCanvas fc) {
parent = fc;
int fcHeight = parent.getSize().height;
int fcWidth = parent.getSize().width;
int appWidth = parent.parent.getSize().width;
xPosition = ((appWidth - width) * x)/5;
yPosition = 300;
width = TORSO_WIDTH - LEG_WIDTH + FOOT_WIDTH;
height = FOOT_HEIGHT + LEG_HEIGHT + TORSO_HEIGHT + HEAD_HEIGHT;
//BUILD CHARACTER
head = new Rectangle(xPosition + (TORSO_WIDTH-HEAD_WIDTH)/2,
yPosition - FOOT_HEIGHT - LEG_HEIGHT - TORSO_HEIGHT - HEAD_HEIGHT,
HEAD_WIDTH, HEAD_HEIGHT);
torso = new Rectangle(xPosition,
yPosition - FOOT_HEIGHT - LEG_HEIGHT - TORSO_HEIGHT,
TORSO_WIDTH, TORSO_HEIGHT);
straightLeg = new Rectangle(xPosition,
yPosition - FOOT_HEIGHT - LEG_HEIGHT,
LEG_WIDTH , LEG_HEIGHT);
straightFoot = new Rectangle(xPosition,
yPosition - FOOT_HEIGHT,
FOOT_WIDTH , FOOT_HEIGHT);
kickLeg = new Rectangle(xPosition + TORSO_WIDTH - LEG_WIDTH,
yPosition - FOOT_HEIGHT - LEG_HEIGHT,
LEG_WIDTH, LEG_HEIGHT);
kickFoot = new Rectangle(xPosition + TORSO_WIDTH - LEG_WIDTH,
yPosition - FOOT_HEIGHT,
FOOT_WIDTH, FOOT_HEIGHT);
// player characteristics
xVelocity = 0;
yVelocity = 0;
gameSpeed = 5;
left = direction;
isKicking = false;
killStreak = 0;
scoreCount = 0;
headStreak = 0;
}
//resets player positions for a new round
public void newRound(int x, int direction){
// place character at either 1/4 or 3/4 of canvas x dimension
// and standing on ground
xPosition = ((parent.parent.getSize().width-width) * x)/5;
yPosition = 300;
xVelocity = 0;
yVelocity = 0;
gameSpeed = 5;
left = direction;
isKicking = false;
}
//handles jumps
public void jumpUp() {
this.yVelocity -= gameSpeed * 7; //update velocity
}
//handles jump backs
public void jumpBack() {
//need to be based on left variable
//because xVelocity will be different for each player.
this.xVelocity -= left * gameSpeed * 1;
this.yVelocity -= gameSpeed * 5;
}
// kick downward
public void kick() {
this.xVelocity -= left * gameSpeed * -3;
this.yVelocity += gameSpeed * 5;
isKicking = true;
}
// make sure characters are facing the right way
public void rotate() {
left = -left;
kickFoot.translate(left*FOOT_WIDTH/2, 0);
straightFoot.translate(left*FOOT_WIDTH/2, 0);
}
// apply acceleration to velocity to player position
public void movePlayer() {
// apply gravity (positive is down)
yVelocity += 1;
// apply velocity to position
xPosition += xVelocity;
yPosition += yVelocity;
head.setLocation(xPosition + (TORSO_WIDTH-HEAD_WIDTH)/2,
yPosition - FOOT_HEIGHT - LEG_HEIGHT - TORSO_HEIGHT - HEAD_HEIGHT);
torso.setLocation(xPosition,
yPosition - FOOT_HEIGHT - LEG_HEIGHT - TORSO_HEIGHT);
straightLeg.setLocation(xPosition,
yPosition - FOOT_HEIGHT - LEG_HEIGHT);
kickLeg.setLocation(xPosition + TORSO_WIDTH - LEG_WIDTH,
yPosition - FOOT_HEIGHT - LEG_HEIGHT);
if (left == 1) {
kickFoot.setLocation(xPosition + TORSO_WIDTH - LEG_WIDTH,
yPosition - FOOT_HEIGHT);
straightFoot.setLocation(xPosition,
yPosition - FOOT_HEIGHT);
}
else {
kickFoot.setLocation(xPosition + TORSO_WIDTH - FOOT_WIDTH,
yPosition - FOOT_HEIGHT);
straightFoot.setLocation(xPosition + LEG_WIDTH - FOOT_WIDTH,
yPosition - FOOT_HEIGHT);
}
}
public void draw() {
//BUILD CHARACTER
//draw head
parent.g2.fillRect(xPosition + (TORSO_WIDTH-HEAD_WIDTH)/2,
yPosition - FOOT_HEIGHT - LEG_HEIGHT - TORSO_HEIGHT - HEAD_HEIGHT,
HEAD_WIDTH, HEAD_HEIGHT);
//draw torso
parent.g2.fillRect(xPosition,
yPosition - FOOT_HEIGHT - LEG_HEIGHT - TORSO_HEIGHT,
TORSO_WIDTH, TORSO_HEIGHT);
//draw sraightleg
parent.g2.fillRect(xPosition,
yPosition - FOOT_HEIGHT - LEG_HEIGHT,
LEG_WIDTH , LEG_HEIGHT);
//draw kickleg
parent.g2.fillRect(xPosition + TORSO_WIDTH - LEG_WIDTH,
yPosition - FOOT_HEIGHT - LEG_HEIGHT,
LEG_WIDTH, LEG_HEIGHT);
if (left == 1) {
//draw kickfoot
parent.g2.fillRect(xPosition + TORSO_WIDTH - LEG_WIDTH,
yPosition - FOOT_HEIGHT,
FOOT_WIDTH, FOOT_HEIGHT);
//draw straightfoot
parent.g2.fillRect(xPosition,
yPosition - FOOT_HEIGHT,
FOOT_WIDTH , FOOT_HEIGHT);
}
else {
//draw kickfoot
parent.g2.fillRect(xPosition + TORSO_WIDTH - FOOT_WIDTH,
yPosition - FOOT_HEIGHT,
FOOT_WIDTH, FOOT_HEIGHT);
//draw straightfoot
parent.g2.fillRect(xPosition + LEG_WIDTH - FOOT_WIDTH,
yPosition - FOOT_HEIGHT,
FOOT_WIDTH , FOOT_HEIGHT);
}
}
//make floor
public void floor() {
isKicking = false;
yVelocity=0;
yPosition = parent.getSize().height;
xVelocity = 0;
}
//make left wall
public void leftWall() {
xPosition = 0;
xVelocity = 0;
}
//make right wall
public void rightWall() {
xPosition = parent.getSize().width - width;
xVelocity = 0;
}
//right collision detection
public boolean collision(Player opponent){
//if bottom corner of player 1 is greater than player 2 left x position
//less than player 2 right x position
//greater than player 2 y position
//less than player 2 bottom y position
//if player kickfoot or straight foot intersects wqith any part of opponents body, its a collision
if (isKicking && (kickFoot.intersects(opponent.torso) ||
kickFoot.intersects(opponent.straightLeg) ||
kickFoot.intersects(opponent.straightFoot) ||//unless its the opponents foot and the opponent is already kicking.
kickFoot.intersects(opponent.kickLeg) ||
kickFoot.intersects(opponent.kickFoot) ||
straightFoot.intersects(opponent.torso) ||
straightFoot.intersects(opponent.straightLeg) ||
straightFoot.intersects(opponent.straightFoot)||
straightFoot.intersects(opponent.kickLeg) ||
straightFoot.intersects(opponent.kickFoot))) {
killStreak += 1;
headStreak = 0;
scoreCount += 1;
return true;
} else if (kickFoot.intersects(opponent.head) | straightFoot.intersects(opponent.head)){ //if player hits the opponents head also increase headstreak
killStreak += 1;
headStreak += 1;
scoreCount += 1;
return true;
} else {
return false;
}
}
}