-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPlayer.java
More file actions
286 lines (240 loc) · 7.83 KB
/
Copy pathPlayer.java
File metadata and controls
286 lines (240 loc) · 7.83 KB
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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
/**
* NOTES:
* - benjamin animation looks weirdly cropped??
* - what if mouse is clicked several times before attack finishes
*
* @Daniel Wang
* @version (a version number or a date)
*/
public class Player extends HurtableEntity {
private double xSpeed, ySpeed, percentXSpeed = 1, percentYSpeed = 1;
private boolean isNew;
private String player;
private int coins;
// the number of enemies killed
private int killed;
private int weaponDmg;
public Player() {
super(SettingsWorld.getPlayerSkinImage(), 192);
player = SettingsWorld.getPlayerSkinImage();
curAction = ActionState.NOTHING;
lastAction = ActionState.NOTHING;
// curAnimation = walkAnimation;
// direction = 3;
// image = walkAnimation.getOneImage(Direction.fromInteger(direction), 0);
// setImage(image);
// frame = 0;
xSpeed = 0;
ySpeed = 0;
realX = 0;
realY = 0;
isNew = true;
countdown = 6;
coins = 0;
attackSound = new GreenfootSound("player_attack.wav");
attackSound.setVolume(70);
damage = 10; // test
health = 100;
}
/**
* Act - do whatever the Goblin wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act() {
if (getWorld() instanceof ShopWorld)
return;
if (health > 0) {
checkActionState();
} else if (curAnimation == deathAnimation && frame == highestIndex) {
// finished animating death
getWorld().addObject(new Fader("in", new DeathWorld()), getWorld().getWidth() / 2,
getWorld().getHeight() / 2);
}
// if action state changed, update what current animation needs to be
if (curAction != lastAction) {
countdown = 0;
if (curAction == ActionState.ATTACKING) {
attackSound.play();
frame = 0;
if (player.equals("melissa"))
highestIndex = 7;
else
highestIndex = 5;
curAnimation = attackAnimation;
} else if (curAction == ActionState.WALKING) {
frame = 1;
highestIndex = 8;
curAnimation = walkAnimation;
} else if (curAction == ActionState.NOTHING) {
curAnimation = walkAnimation;
frame = 0;
setImage(walkAnimation.getOneImage(Direction.fromInteger(direction), frame));
}
}
// if not unmoving, animate
if (curAction != ActionState.NOTHING && !dead) {
super.animate();
if (curAction == ActionState.WALKING) {
// move player
// realX += xSpeed;
// realY += ySpeed;
tryMove(xSpeed, ySpeed);
}
}
// }
centreOn(this);
updateLocation();
}
public void pickUpCoin() {
coins++;
}
private void checkActionState() {
lastAction = curAction;
xSpeed = 0;
ySpeed = 0;
// is this if statement not redundant
if (getWorld() instanceof ShopWorld) {
} else {
if (Greenfoot.isKeyDown("a")) {
direction = 1;
xSpeed = -10;
}
if (Greenfoot.isKeyDown("d")) {
direction = 0;
xSpeed = 10;
}
if (Greenfoot.isKeyDown("w")) {
direction = 2;
ySpeed = -10;
}
if (Greenfoot.isKeyDown("s")) {
direction = 3;
ySpeed = 10;
}
}
// account for water tile?!?
xSpeed = xSpeed * percentXSpeed;
ySpeed = ySpeed * percentYSpeed;
if (Greenfoot.mousePressed(null)) {
curAction = ActionState.ATTACKING;
attack();
}
// only nothing or walking if not attacking
if (curAction != ActionState.ATTACKING) {
if (xSpeed == 0 && ySpeed == 0) {
curAction = ActionState.NOTHING;
} else {
curAction = ActionState.WALKING;
}
}
}
public void attack() {
Actor p = getOneIntersectingObject(Enemy.class);
if (p != null) {
takeDamage(weaponDmg);
// add a collision box based on the player's current direction
int xOffset, yOffset, x, y;
if (direction < 2) {
x = 32;
y = 50;
yOffset = 0;
xOffset = direction == 0 ? 32 : -32;
} else {
x = 50;
y = 32;
xOffset = 0;
yOffset = direction == 3 ? 32 : -32;
}
CollisionBox attackCollider = new CollisionBox(x, y, 0, null, true);
getWorld().addObject(attackCollider, getX() + xOffset, getY() + yOffset);
// get intersecting enemies with attack collider instead of player img
// might need to put a cap on this?????
List<Enemy> enemies = attackCollider.getIntersectingEnemies();
for (Enemy e : enemies) {
e.takeDamage(damage);
killed++;
// but enemy doesn't necessarily die after one attack???
}
List<BarrelTile> barrels = attackCollider.getIntersectingBarrels();
if (barrels.size() != 0) {
barrels.get(0).takeDamage(damage);
}
getWorld().removeObject(attackCollider);
}
// killed++;
}
// public void takeDamage(int dmg) {
// health -= damage;
// }
public int getCoin() {
return coins;
}
public void earnCoin() {
Actor p = getOneIntersectingObject(Coin.class);
if (p != null) {
coins++;
}
}
public int getKilled() {
return this.killed;
}
public int getWeaponDmg() {
return this.weaponDmg;
}
public int getHealth() {
return this.health;
}
public void setHealth(int h) {
this.health += h;
}
public void setWeaponDmg(int dmg) {
this.weaponDmg += dmg;
}
public void addKill() {
killed++;
}
// public void setImageSize(int length, int width)
// {
// image.scale(length, width);
// }
public void tryMove(double dx, double dy) {
int oldX = getX();
int oldY = getY();
double oldRealX = realX;
double oldRealY = realY;
// Try X movement
realX += dx;
collider.setLocation(oldX + dx, oldY + 16);
// List<Tile> touchingX = getIntersectingObjects(Tile.class);
List<Tile> touchingX = collider.getIntersectingTiles();
for (Tile tile : touchingX) {
if (!tile.getIsPassable()) {
realX = oldRealX;
collider.setLocation(oldX, oldY + 16);
break;
}
}
// Try Y movement
oldX = getX();
oldY = getY();
oldRealX = realX;
oldRealY = realY;
realY += dy;
collider.setLocation(oldX, oldY + dy + 16);
// List<Tile> touchingY = getIntersectingObjects(Tile.class);
List<Tile> touchingY = collider.getIntersectingTiles();
for (Tile tile : touchingY) {
if (!tile.getIsPassable()) {
realY = oldRealY;
collider.setLocation(oldX, oldY + 16);
break;
}
}
}
public void setSpeedPercents(double newXPercent, double newYPercent) {
percentXSpeed = newXPercent;
percentYSpeed = newYPercent;
}
}