-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.java
More file actions
105 lines (88 loc) · 2.15 KB
/
Player.java
File metadata and controls
105 lines (88 loc) · 2.15 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
import java.awt.Graphics2D;
public class Player extends Entity implements Collidable {
private final Assign9 main;
private final PlayerInput input;
private int score = 0;
public Player(Assign9 main) {
super("player", new Position(0, 0));
this.input = new PlayerInput(this);
this.main = main;
this.setHitpoints(100);
}
@Override
public void onCollision(Entity other) {
if (other instanceof DefaultNPC) {
final DefaultNPC npc = (DefaultNPC) other;
npc.setDead(true);
} else if (other instanceof Bullet) {
final Bullet bullet = (Bullet) other;
if (bullet.getOwner() != this) {
setHitpoints(getHitpoints() - 25);
if (getHitpoints() <= 0) {
setDead(true);
}
bullet.setDead(true);
}
}
}
@Override
public void update(Graphics2D g) {
setVelocity(getVelocity() + getGravity());
setPosition(new Position(getPosition().getX(), getPosition().getY() + (int) getVelocity()));
if (getCurrentBullet() != null) {
if (getCurrentBullet().getPosition().getX() >= Constants.APPLET_WIDTH) {
getCurrentBullet().setDead(true);
setFiredBullet(false);
}
for (Entity e : World.getEntities()) {
if (e != null) {
if (getCurrentBullet().collidesWithSprite(e)) {
getCurrentBullet().onCollision(e);
if (e instanceof Collidable) {
((Collidable) e).onCollision(getCurrentBullet());
if (e instanceof DefaultNPC) {
World.enemyCount --;
}
}
}
}
}
}
if (isFiredBullet()) {
getCurrentBullet().draw(g);
getCurrentBullet().update(g);
}
if (getPosition().getY() > Constants.APPLET_HEIGHT) {
setDead(true);
return;
}
}
@Override
public void fire(Bullet bullet) {
if (!isFiredBullet()) {
this.setCurrentBullet(bullet);
}
setFiredBullet(true);
SoundEffects.SHOOT.play();
}
public void jump() {
if (getPosition().getY() < 0) {
return;
}
setVelocity(-7.6);
SoundEffects.JUMP.play();
}
/**
* Gets the input instance.
* @return The input.
*/
public PlayerInput getInput() {
return input;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}