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