From 8a0298b2db69ba171f4e7998d253ddf3e3944d69 Mon Sep 17 00:00:00 2001 From: Matt Low Date: Sat, 28 Dec 2019 12:21:28 +0400 Subject: [PATCH] Shorter movement code making use of vector math --- .../com/me/pacman/entity/MovableEntity.java | 75 ++++++++----------- 1 file changed, 30 insertions(+), 45 deletions(-) diff --git a/core/src/com/me/pacman/entity/MovableEntity.java b/core/src/com/me/pacman/entity/MovableEntity.java index ff5b96f..b7e5c56 100644 --- a/core/src/com/me/pacman/entity/MovableEntity.java +++ b/core/src/com/me/pacman/entity/MovableEntity.java @@ -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); - Vector2 new_pos = new Vector2(pos); - - switch (currDirection) { - case UP: - if (currentTile == null && pos.y > state.level.height + 1) { - 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(); - } - 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 (currentTile == null) { + // handle traveling back onto the screen + switch (currDirection) { + case UP: + if (pos.y >= state.level.height + 1f) { + new_pos.y = -1f; + } + break; + case DOWN: + if (pos.y <= 0) { + new_pos.y = state.level.height + 1f; + } + break; + case LEFT: + 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.