-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.java
More file actions
259 lines (211 loc) · 8.61 KB
/
Copy pathPlayer.java
File metadata and controls
259 lines (211 loc) · 8.61 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
import greenfoot.*;
import java.util.List;
public class Player extends Actor {
private int speed = 3;
private int health = 200;
private int maxHealth = 200;
private int attackDamage = 40;
private int attackRadius = 50;
private boolean canMove = true;
private boolean facingLeft = false, facingRight = true;
private boolean isAttacking = false, isHurt = false, isDead = false;
private double scale = 2.0;
private int currentFrame = 0, animationCounter = 0, animationSpeed = 3;
private int deathAnimationFrame = 0;
private String currentAnimation = "idle";
private GreenfootImage[] movingRightFrames, movingLeftFrames, idleRightFrames, idleLeftFrames;
private GreenfootImage[] attackFrames, attackLeftFrames, hurtRightFrames, hurtLeftFrames, deathFrames;
private GreenfootSound attack1, attack2, attack3;
public Player() {
loadAnimations();
attack1 = new GreenfootSound("PISTOL.wav");
attack2 = new GreenfootSound("PISTOL.wav");
attack3 = new GreenfootSound("PISTOL.wav");
setImage(idleRightFrames[0]);
setRotation(0);
}
public void act() {
if (isDead) {
playDeathAnimation();
return;
}
boolean moved = false;
if (canMove) {
moved = moveAround();
} else {
moved = false;
}
handleAttack();
animate(moved);
}
public void setCanMove(boolean canMove) {
this.canMove = canMove;
}
private boolean moveAround() {
if (isAttacking || isHurt) return false;
int dx = 0, dy = 0;
if (Greenfoot.isKeyDown("up") || Greenfoot.isKeyDown("w")) dy -= speed;
if (Greenfoot.isKeyDown("down") || Greenfoot.isKeyDown("s")) dy += speed;
if (Greenfoot.isKeyDown("left") || Greenfoot.isKeyDown("a")) {
dx -= speed;
facingLeft = true;
facingRight = false;
}
if (Greenfoot.isKeyDown("right") || Greenfoot.isKeyDown("d")) {
dx += speed;
facingRight = true;
facingLeft = false;
}
boolean isMoving = (dx != 0 || dy != 0);
if (!isMoving) return false;
if (dx != 0) {
setLocation(getX() + dx, getY());
while (isTouchingAnyWall()) {
setLocation(getX() - (int)Math.signum(dx), getY());
}
}
if (dy != 0) {
setLocation(getX(), getY() + dy);
while (isTouchingAnyWall()) {
setLocation(getX(), getY() - (int)Math.signum(dy));
}
}
return true;
}
/**
* Deteksi tabrakan dengan semua jenis tembok (Wall01–Wall09)
*/
private boolean isTouchingAnyWall() {
int offset = 25;
for (Class<?> wallClass : new Class[]{Wall.class, Wall01.class, Wall02.class, Wall03.class, Wall04.class,
Wall05.class, Wall06.class, Wall07.class, Wall08.class, Wall09.class}) {
Actor wall = (Actor) getOneIntersectingObject(wallClass);
if (wall != null) {
int dx = getX() - wall.getX();
int dy = getY() - wall.getY();
int halfW = (getImage().getWidth() / 2) - offset;
int halfH = (getImage().getHeight() / 2) - offset;
int wallW = wall.getImage().getWidth() / 2;
int wallH = wall.getImage().getHeight() / 2;
if (Math.abs(dx) < halfW + wallW && Math.abs(dy) < halfH + wallH) {
return true;
}
}
}
return false;
}
private void handleAttack() {
MouseInfo mouse = Greenfoot.getMouseInfo();
if (mouse == null) return;
facingRight = mouse.getX() >= getX();
facingLeft = !facingRight;
if (Greenfoot.mousePressed(null) && mouse.getButton() == 1 && !isAttacking && !isHurt && canMove) {
isAttacking = true;
currentFrame = 0;
GreenfootSound gunSound = new GreenfootSound("PISTOL.wav");
gunSound.setVolume(80);
gunSound.play();
}
}
private void shootBullet(int mouseX, int mouseY) {
final int BULLET_OFFSET_Y = 20;
Bullet bullet = new Bullet(mouseX, mouseY, this);
getWorld().addObject(bullet, getX(), getY() + BULLET_OFFSET_Y);
}
public void takeDamage(int dmg) {
if (isDead || isHurt) return;
health -= dmg;
if (health <= 0) {
isDead = true;
deathAnimationFrame = 0;
currentAnimation = "dead";
} else {
isHurt = true;
currentFrame = 0;
currentAnimation = "hurt";
}
}
public int getHealth() { return health; }
public int getMaxHealth() { return maxHealth; }
public boolean isDead() { return isDead; }
private void playDeathAnimation() {
if (deathAnimationFrame < deathFrames.length) {
animationCounter++;
if (animationCounter % animationSpeed == 0) {
setImage(deathFrames[deathAnimationFrame]);
deathAnimationFrame++;
}
}
}
private void animate(boolean moved) {
animationCounter++;
if (animationCounter % animationSpeed != 0) return;
GreenfootImage[] frames;
String nextAnimation;
if (isAttacking) {
frames = facingRight ? attackFrames : attackLeftFrames;
nextAnimation = "attack";
} else if (isHurt) {
frames = facingRight ? hurtRightFrames : hurtLeftFrames;
nextAnimation = "hurt";
} else if (moved) {
frames = facingRight ? movingRightFrames : movingLeftFrames;
nextAnimation = "walk";
} else {
frames = facingRight ? idleRightFrames : idleLeftFrames;
nextAnimation = "idle";
}
if (!nextAnimation.equals(currentAnimation)) {
currentFrame = 0;
currentAnimation = nextAnimation;
}
currentFrame++;
int frameLength = frames.length;
if (nextAnimation.equals("attack") || nextAnimation.equals("hurt")) {
if (currentFrame >= frameLength) {
if (isAttacking) {
MouseInfo mouse = Greenfoot.getMouseInfo();
if (mouse != null) shootBullet(mouse.getX(), mouse.getY());
}
isAttacking = false;
isHurt = false;
currentFrame = 0;
currentAnimation = "idle";
setImage(facingRight ? idleRightFrames[0] : idleLeftFrames[0]);
return;
}
}
setImage(frames[currentFrame % frameLength]);
}
private void loadAnimations() {
AnimasiPlayerRight();
AnimasiPlayerLeft();
AnimasiPlayerIdleRight();
AnimasiPlayerIdleLeft();
AnimasiPlayerAttack();
AnimasiPlayerAttackLeft();
AnimasiPlayerHurtRight();
AnimasiPlayerHurtLeft();
AnimasiPlayerDeath();
}
private GreenfootImage[] sliceSheet(GreenfootImage sheet, int frameCount) {
int frameWidth = sheet.getWidth() / frameCount;
int frameHeight = sheet.getHeight();
GreenfootImage[] frames = new GreenfootImage[frameCount];
for (int i = 0; i < frameCount; i++) {
frames[i] = new GreenfootImage(frameWidth, frameHeight);
frames[i].drawImage(sheet, -i * frameWidth, 0);
frames[i].scale((int)(frames[i].getWidth() * scale), (int)(frames[i].getHeight() * scale));
}
return frames;
}
public void AnimasiPlayerRight() { movingRightFrames = sliceSheet(new GreenfootImage("A2 Walk.png"), 7); }
public void AnimasiPlayerLeft() { movingLeftFrames = sliceSheet(new GreenfootImage("A2 Walk Left.png"), 7); }
public void AnimasiPlayerIdleRight() { idleRightFrames = sliceSheet(new GreenfootImage("A2 Idle.png"), 7); }
public void AnimasiPlayerIdleLeft() { idleLeftFrames = sliceSheet(new GreenfootImage("A2 Idle Left.png"), 7); }
public void AnimasiPlayerAttack() { attackFrames = sliceSheet(new GreenfootImage("A2 Shot.png"), 4); }
public void AnimasiPlayerAttackLeft(){ attackLeftFrames = sliceSheet(new GreenfootImage("A2 Shot Left.png"), 4); }
public void AnimasiPlayerHurtRight() { hurtRightFrames = sliceSheet(new GreenfootImage("A2 Hurt.png"), 3); }
public void AnimasiPlayerHurtLeft() { hurtLeftFrames = sliceSheet(new GreenfootImage("A2 Hurt Left.png"), 3); }
public void AnimasiPlayerDeath() { deathFrames = sliceSheet(new GreenfootImage("A2 Dead.png"), 4); }
}