-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.java
323 lines (298 loc) · 7.8 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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/**
* @(#)Player.java
*
*
* @author
* @version 1.00 2018/6/6
*/
import java.awt.*;
import java.util.ArrayList;
import java.util.Random;
import java.awt.event.*;
import javax.swing.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
public class Player {
private int maxDamage, minDamage;
private double x, xPos, yPos, vx, vy, sx, sy;
private String direction, type = "archer";
private boolean jump, ground;
private int screenX = 1900, screenY = 1000, lives;
double health;
private int green, black, red, grey;
private BufferedImage mask = null;
public Player(BufferedImage image){
x = 0; //position of background image on screen
xPos = 100; //player X
yPos = 400; //player Y
vx = 0;
vy = 0;
direction = "right";
jump = false;
mask = image;
green = getPixelCol(mask, 25, 25); //platform colour
red = getPixelCol(mask, 75, 25); //lava
black = getPixelCol(mask, 225, 25); //cannons
grey = getPixelCol(mask, 475, 25);
ground = false;
lives = 1;
health = 100;
}
public void loadImage(BufferedImage image){
mask = image;
}
///////////////////////////////PIXEL COLOUR////////////////////////////////////
public int getPixelCol(BufferedImage image, int xx, int yy){
return image.getRGB(xx + (int)x, yy);
}
public boolean getColTop(BufferedImage image, int xx, int yy, int col){
boolean b = false;
int c = image.getRGB(xx + (int) x, yy);
for (int d = 0; d < 68; d++){
c = image.getRGB(d + xx + (int) x, yy);
if (c == col){
b = true;
}
}
return b;
}
public boolean getColBottom(BufferedImage image, int xx, int yy, int col){
boolean b = false;
int c = image.getRGB(xx + (int )x, yy + 84);
for (int d = 0; d < 68; d++){
c = image.getRGB(d + xx + (int) x, yy + 84);
if (c == col){
b = true;
}
}
return b;
}
public boolean getColRight(BufferedImage image, int xx, int yy, int col){
boolean b = false;
int c = image.getRGB(xx + (int) x + 68, yy + 84);
for (int d = 0; d < 84; d++){
c = image.getRGB(xx + (int) x + 68, yy + d);
if (c == col){
b = true;
}
}
return b;
}
public boolean getColLeft(BufferedImage image, int xx, int yy, int col){
boolean b = false;
int c = image.getRGB(xx + (int) x - 1, yy + 84);
for (int d = 0; d < 84; d++){
c = image.getRGB(xx + (int) x - 1, yy + d);
if (c == col){
b = true;
}
}
return b;
}
///////////////////////////////VERTICAL MOVEMENT///////////////////////////////
public void jump(){//jump method
if (vy < 0){//if already jumping(in motion)
for (int i = 0; i < (int) vy * -1; i++){
if (!getColTop(mask, (int) xPos, (int) yPos, green) && !getColTop(mask, (int) xPos, (int) yPos, black)){//if no block or cannon on top move up
yPos -= 1;
ground = false;
}
else{
vy = 0;
ground = false;
}
}
}
else{
for (int i = 0; i < (int) vy; i++){
if (!getColBottom(mask, (int) xPos, (int) yPos, green) && !getColBottom(mask, (int) xPos, (int) yPos, black)){//if no block or cannon below
yPos += 1;
}
else{
vy = 0;
jump = false;
ground = true;
}
}
}
vy += 1;
if (getColBottom(mask, (int) xPos, (int) yPos, grey)){//if hit lava set health to 0
health = 0;
}
}
public void fall(){//falling method
for (int i = 0; i < (int) vy; i++){
if (!getColBottom(mask, (int) xPos, (int) yPos, green) && !getColBottom(mask, (int) xPos, (int) yPos, black)){//if not on block or cannon move down
yPos += 1;
ground = false;
}
else{//if on block or cannon set jump false, ground true, stop falling
vy = 0;
jump = false;
ground = true;
}
}
vy += 1;
if (getColBottom(mask, (int) xPos, (int) yPos, grey)){//if hit lava set health to 0
health = 0;
}
}
//////////////////////////////HORIZONTAL MOVEMENT//////////////////////////////
public void move(String d){//move method
if (vx < 5){//if not at max speed add 0.4 to velocity
vx += 0.4;
}
if (d.equals("right")){
direction = "right";
for (int i = 0; i < (int) vx; i++){
if (!getColRight(mask, (int) xPos, (int) yPos, green) && !getColRight(mask, (int) xPos, (int) yPos, black)){//can move if no block to right
if (xPos <= 890){
xPos += 1;//(int) vx;
}
else if (x >= 23090){//if at edge of map
if (xPos < 1830){ //max distance
xPos += 1;//(int) vx;
}
}
else{
x += 1;//(int) vx;
}
}
}
}
else{
direction = "left";
for (int i = 0; i < (int) vx; i++){
if (!getColLeft(mask, (int) xPos, (int) yPos, green) && !getColLeft(mask, (int) xPos, (int) yPos, black)){//if no block to left can move left
if (xPos >= 910){
xPos -= 1;//(int) vx;
}
else if (x <= 10){
if (xPos > 10){ //min distance
xPos -= 1;//(int) vx;
}
}
else{
x -= 1;//(int) vx;
}
}
}
}
if (getColBottom(mask, (int) xPos, (int) yPos, grey)){//if hit lava set health to 0
health = 0;
}
}
public void slowDown(String d){//method for slowing down
if (vx > 0){//if not stationary subtract 0.3 from velocity
vx -= 0.3;
}
if (d.equals("right")){
direction = "right";
if (!getColRight(mask, (int) xPos + (int) vx, (int) yPos, green) && !getColRight(mask, (int) xPos, (int) yPos, black)){//if no block to the right
if (xPos <= 890){
xPos += (int) vx;
}
else if (x >= 23090){//if at rightmost edge of map
if (xPos < 1830){ //max distance
xPos += (int) vx;
}
}
else{
x += (int) vx;
}
}
}
else{
direction = "left";
if (!getColLeft(mask, (int) xPos - (int) vx, (int) yPos, green) && !getColLeft(mask, (int) xPos, (int) yPos, black)){//if no block to the left
if (xPos >= 910){
xPos -= (int) vx;
}
else if (x <= 10){//if at leftmost edge of map
if (xPos > 10){ //min distance
xPos -= (int) vx;
}
}
else{
x -= (int) vx;
}
}
}
if (getColBottom(mask, (int) xPos, (int) yPos, grey)){//if hit lava health set to 0
health = 0;
}
}
////////////////////////////GETTERS AND SETTERS////////////////////////////////
public int getX(){
return (int) x;
}
public int getXPos(){
return (int) xPos;
}
public void setX(double d){
x = d;
}
public void setXPos(double d){
xPos = d;
}
public int getYPos(){
return (int) yPos;
}
public void setYPos(double d){
yPos = d;
}
public int getVx(){
return (int) vx;
}
public void setVx(int vx){
this.vx = vx;
}
public int getVy(){
return (int) vy;
}
public void setVy(int vy){
this.vy = vy;
}
public void addVy(double v){
vy += v;
}
public boolean getJump(){
return jump;
}
public void setJump(boolean b){
jump = b;
}
public void setGround(boolean b){
ground = b;
}
public boolean getGround(){
return ground;
}
public int getHealth(){
return (int)health;
}
///////////////////////////////////////////////////////////////////////////////
public void takeDamage(double d){//method for taking damage
health -= d;
if (health < 0){//if health would be negative set health to 0
health = 0;
}
}
public void addHealth(double i){//method for adding health
health += i;
if (health > 100){//if health would be greater than 100 set health to 100
health = 100;
}
}
public void addLife(){
lives++;
}
public void subtractLife(){
lives--;
}
public int getLives(){
return lives;
}
}