Shorter movement code making use of vector math

This commit is contained in:
Matt Low 2019-12-28 12:21:28 +04:00
parent ac347dc6d8
commit 8a0298b2db

View File

@ -95,52 +95,37 @@ public abstract class MovableEntity extends Entity {
}
}
float dist = speed * dt;
LevelTile currentTile = state.level.getTile(pos.x, pos.y);
Vector2 new_pos = new Vector2(pos);
LevelTile currentTile = state.level.getTile(pos);
Vector2 new_pos = currDirection.getVector(speed * dt).add(pos);
if (currentTile == null) {
// handle traveling back onto the screen
switch (currDirection) {
case UP:
if (currentTile == null && pos.y > state.level.height + 1) {
if (pos.y >= state.level.height + 1f) {
new_pos.y = -1f;
canMove = true;
} else {
new_pos.y += dist;
nextTile = state.level.getTile(new_pos.x, new_pos.y + 0.5f);
canMove = nextTile == null || nextTile.isPassable();
}
break;
case RIGHT:
if (currentTile == null && pos.x > state.level.width + 1) {
new_pos.x = -1f;
canMove = true;
} else {
new_pos.x += dist;
nextTile = state.level.getTile(new_pos.x + 0.5f, new_pos.y);
canMove = nextTile == null || nextTile.isPassable();
}
break;
case DOWN:
if (currentTile == null && pos.y < 0) {
new_pos.y = state.level.height + 1;
canMove = true;
} else {
new_pos.y -= dist;
nextTile = state.level.getTile(new_pos.x, new_pos.y - 0.5f);
canMove = nextTile == null || nextTile.isPassable();
if (pos.y <= 0) {
new_pos.y = state.level.height + 1f;
}
break;
case LEFT:
if (currentTile == null && pos.x < 0) {
new_pos.x = state.level.width + 1;
canMove = true;
} else {
new_pos.x -= dist;
nextTile = state.level.getTile(new_pos.x - 0.5f, new_pos.y);
canMove = nextTile == null || nextTile.isPassable();
if (pos.x <= 0) {
new_pos.x = state.level.width + 1f;
}
break;
case RIGHT:
if (pos.x >= state.level.width + 1f) {
new_pos.x = -1f;
}
break;
}
canMove = true;
} else {
nextTile = state.level.getTile(currDirection.getVector(0.5f).add(new_pos));
canMove = nextTile == null || nextTile.isPassable();
}
// if move isn't going to collide with wall, move normally.