-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreature.java
More file actions
114 lines (87 loc) · 2.73 KB
/
Creature.java
File metadata and controls
114 lines (87 loc) · 2.73 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
package mainPackage;
public abstract class Creature extends Entity {
public static final int DEFAULT_HEALTH = 10;
public static final float DEFAULT_SPEED = 3.0f;
public static final int DEFAULT_CREATURE_WIDTH = 64,
DEFAULT_CREATURE_HEIGHT = 64;
protected int health;
protected float speed;
protected float xMove, yMove;
public Creature(Handler handler, float x, float y, int width, int height) {
super(handler, x, y, width, height);
health = DEFAULT_HEALTH;
speed = DEFAULT_SPEED;
xMove = 0;
yMove = 0;
}
public void move() {
moveX();
moveY();
}
public void moveX() {
if(xMove > 0) { //moving right
int tx = (int) (x + xMove + bounds.x + bounds.width) / Tile.TILEWIDTH;
if(!collisionWithTile(tx, (int) (y + bounds.y) / Tile.TILEHEIGHT) &&
!collisionWithTile(tx, (int) (y + bounds.y + bounds.height) / Tile.TILEHEIGHT)) {
x += xMove;
} else {
x = tx * Tile.TILEWIDTH - bounds.x - bounds.width - 1;
}
} else if(xMove < 0) { //moving left
int tx = (int) (x + xMove + bounds.x) / Tile.TILEWIDTH;
if(!collisionWithTile(tx, (int) (y + bounds.y) / Tile.TILEHEIGHT) &&
!collisionWithTile(tx, (int) (y + bounds.y + bounds.height) / Tile.TILEHEIGHT)) {
x += xMove;
} else {
x = tx * Tile.TILEWIDTH + Tile.TILEWIDTH - bounds.x;
}
}
}
public void moveY() {
if(yMove < 0) { //moving up
int ty = (int) (y + yMove + bounds.y) / Tile.TILEHEIGHT;
if(!collisionWithTile((int) (x + bounds.x) / Tile.TILEWIDTH, ty) &&
!collisionWithTile((int) (x + bounds.x + bounds.width) / Tile.TILEWIDTH, ty)) {
y += yMove;
} else {
y = ty * Tile.TILEHEIGHT + Tile.TILEHEIGHT - bounds.y;
}
} else if(yMove > 0) { //moving down
int ty = (int) (y + yMove + bounds.y + bounds.height) / Tile.TILEHEIGHT;
if(!collisionWithTile((int) (x + bounds.x) / Tile.TILEWIDTH, ty) &&
!collisionWithTile((int) (x + bounds.x + bounds.width) / Tile.TILEWIDTH, ty)) {
y += yMove;
} else {
y = ty * Tile.TILEHEIGHT - bounds.y - bounds.height - 1;
}
}
}
protected boolean collisionWithTile(int x, int y) {
return handler.getWorld().getTile(x, y).isSolid();
}
//getters and setters
public float getxMove() {
return xMove;
}
public void setxMove(float xMove) {
this.xMove = xMove;
}
public float getyMove() {
return yMove;
}
public void setyMove(float yMove) {
this.yMove = yMove;
}
public int getHealth() {
return health;
}
public void setHealth(int health) {
this.health = health;
}
public float getSpeed() {
return speed;
}
public void setSpeed(float speed) {
this.speed = speed;
}
}