-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathPlayer.java
More file actions
172 lines (144 loc) · 4.65 KB
/
Copy pathPlayer.java
File metadata and controls
172 lines (144 loc) · 4.65 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
package bomberman.entity.player;
import bomberman.GameLoop;
import bomberman.Renderer;
import bomberman.animations.PlayerAnimations;
import bomberman.animations.Sprite;
import bomberman.constants.Direction;
import bomberman.constants.GlobalConstants;
import bomberman.entity.Entity;
import bomberman.entity.KillableEntity;
import bomberman.entity.MovingEntity;
import bomberman.geometry.Boundary;
import bomberman.scenes.Sandbox;
public class Player implements MovingEntity, KillableEntity {
private int health;
private boolean isAlive;
Boundary playerBoundary;
Sprite currentSprite;
PlayerAnimations playerAnimations;
Direction currentDirection;
String name;
public Player() {
init(64, 64);
}
public Player(int posX, int posY) {
init(posX, posY);
health = 100;
isAlive = true;
}
private void init(int x, int y) {
name = "Player";
playerAnimations = new PlayerAnimations(this);
playerBoundary = new Boundary(x, y, GlobalConstants.PLAYER_WIDTH, GlobalConstants.PLAYER_HEIGHT);
currentSprite = playerAnimations.getPlayerIdleSprite();
}
public void move(Direction direction) {
move(1, direction);
}
private void setCurrentSprite(Sprite s) {
if (s != null) {
currentSprite = s;
} else {
System.out.println("Sprite missing!");
}
}
public int getHealth() {
return health;
}
public boolean isAlive() {
return isAlive;
}
public String toString() {
return name;
}
@Override
public boolean isColliding(Entity b) {
return playerBoundary.intersects(b.getBoundary());
}
@Override
public void draw() {
if (currentSprite != null) {
Renderer.playAnimation(currentSprite);
}
}
@Override
public void die() {
setCurrentSprite(playerAnimations.getPlayerDying());
}
private boolean checkCollisions(double x, double y) {
Boundary collision = playerBoundary.setMin(x, y);
for (Entity e : Sandbox.getEntities()) {
if (e != this && !e.isPlayerCollisionFriendly() &&
collision.intersects(e.getBoundary())) {
return true;
}
}
playerBoundary = collision;
return false;
}
@Override
public void move(double steps, Direction direction) {
steps *= GameLoop.getDeltaTime();
if (steps == 0) {
setCurrentSprite(playerAnimations.getPlayerIdleSprite());
return;
} else {
switch (direction) {
case UP:
if(!checkCollisions(playerBoundary.getMinX(), playerBoundary.getMinY() - steps)) {
setCurrentSprite(playerAnimations.getMoveUpSprite());
currentDirection = Direction.UP;
}
break;
case DOWN:
if(!checkCollisions(playerBoundary.getMinX(), playerBoundary.getMinY() + steps)) {
setCurrentSprite(playerAnimations.getMoveDownSprite());
currentDirection = Direction.DOWN;
}
break;
case LEFT:
if(!checkCollisions(playerBoundary.getMinX() - steps, playerBoundary.getMinY())) {
setCurrentSprite(playerAnimations.getMoveLeftSprite());
currentDirection = Direction.LEFT;
}
break;
case RIGHT:
if(!checkCollisions(playerBoundary.getMinX() + steps, playerBoundary.getMinY())) {
setCurrentSprite(playerAnimations.getMoveRightSprite());
currentDirection = Direction.RIGHT;
}
break;
default:
setCurrentSprite(playerAnimations.getPlayerIdleSprite());
}
}
}
@Override
public void reduceHealth(int damage) {
if (health - damage <= 0) {
die();
} else {
health -= damage;
}
}
@Override
public void removeFromScene() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public double getPositionX() {
return playerBoundary.getMinX();
}
@Override
public double getPositionY() {
return playerBoundary.getMinY();
}
@Override
public Boundary getBoundary() {
return playerBoundary;
}
@Override
public boolean isPlayerCollisionFriendly() {
return true;
}
}