From f6fbad0e6da336cf5e4108df58634329cfa8e77c Mon Sep 17 00:00:00 2001 From: Matt Low Date: Sat, 28 Dec 2019 12:48:20 +0400 Subject: [PATCH] Shorter turning code using vector math --- core/src/com/me/pacman/entity/Entity.java | 4 ++ .../com/me/pacman/entity/MovableEntity.java | 58 +++++-------------- 2 files changed, 19 insertions(+), 43 deletions(-) diff --git a/core/src/com/me/pacman/entity/Entity.java b/core/src/com/me/pacman/entity/Entity.java index dc5e774..b5b5525 100644 --- a/core/src/com/me/pacman/entity/Entity.java +++ b/core/src/com/me/pacman/entity/Entity.java @@ -30,6 +30,10 @@ public abstract class Entity { return (int) pos.x == (int) other.pos.x && (int) pos.y == (int) other.pos.y; } + public Vector2 getTileVector(float offset) { + return new Vector2((int) pos.x + offset, (int) pos.y + offset); + } + public Vector2 getTileVector() { return new Vector2((int) pos.x, (int) pos.y); } diff --git a/core/src/com/me/pacman/entity/MovableEntity.java b/core/src/com/me/pacman/entity/MovableEntity.java index b7e5c56..0b05472 100644 --- a/core/src/com/me/pacman/entity/MovableEntity.java +++ b/core/src/com/me/pacman/entity/MovableEntity.java @@ -43,51 +43,23 @@ public abstract class MovableEntity extends Entity { if (nextDirection != null) { boolean turned = false; - switch (nextDirection) { - case UP: - nextTile = state.level.getTile(pos.x, pos.y + 1f); - if (nextTile == null) { - if (currDirection == Direction.DOWN) { - turned = true; - } - } else if (nextTile.isPassable() && Math.abs(pos.x - ((int) pos.x + 0.5f)) <= tolerance) { - pos.x = ((int) pos.x) + 0.5f; + nextTile = state.level.getTile(nextDirection.getVector().add(pos)); + if (nextTile == null) { + // turned around immediately after existing tunnel (or trying to turn 90 degrees after entering tunnel) + if (nextDirection.isOpposite(currDirection)) { + // only allow if turning 180 degrees + turned = true; + } + } else { + if (nextTile.isPassable()) { + // it's possible to turn - are we close enough to our tile's center to make the turn? + Vector2 tileCenter = getTileVector(0.5f); + if (pos.dst2(tileCenter) <= tolerance * tolerance) { + // yes, move us to the center of our tile + pos = tileCenter; turned = true; } - break; - case RIGHT: - nextTile = state.level.getTile(pos.x + 1f, pos.y); - if (nextTile == null) { - if (currDirection == Direction.LEFT) { - turned = true; - } - } else if (nextTile.isPassable() && Math.abs(pos.y - ((int) pos.y + 0.5f)) <= tolerance) { - pos.y = ((int) pos.y) + 0.5f; - turned = true; - } - break; - case DOWN: - nextTile = state.level.getTile(pos.x, pos.y - 1f); - if (nextTile == null) { - if (currDirection == Direction.UP) { - turned = true; - } - } else if (nextTile.isPassable() && Math.abs(pos.x - ((int) pos.x + 0.5f)) <= tolerance) { - pos.x = ((int) pos.x) + 0.5f; - turned = true; - } - break; - case LEFT: - nextTile = state.level.getTile(pos.x - 1f, pos.y); - if (nextTile == null) { - if (currDirection == Direction.RIGHT) { - turned = true; - } - } else if (nextTile.isPassable() && Math.abs(pos.y - ((int) pos.y + 0.5f)) <= tolerance) { - pos.y = ((int) pos.y) + 0.5f; - turned = true; - } - break; + } } if (turned) { currDirection = nextDirection;