From f10d3f70af0c26670b80aba686667896b39e3f48 Mon Sep 17 00:00:00 2001 From: Zukero Date: Mon, 23 Jan 2017 17:55:00 +0100 Subject: [PATCH] Fixed large monsters movement (not used yet, but it bothered me). They couldn't move because there always was a monster in the way: themselves. And redrawing area wasn't computed correctly. --- .../controller/MonsterMovementController.java | 18 ++++++++--------- .../controller/MonsterSpawningController.java | 2 +- .../AndorsTrail/model/map/PredefinedMap.java | 6 ++++-- .../gpl/rpg/AndorsTrail/util/CoordRect.java | 20 +++++++++++++++++++ .../gpl/rpg/AndorsTrail/view/MainView.java | 4 ++-- 5 files changed, 36 insertions(+), 14 deletions(-) diff --git a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/controller/MonsterMovementController.java b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/controller/MonsterMovementController.java index 61d3aa8e5..f31fd7c62 100644 --- a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/controller/MonsterMovementController.java +++ b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/controller/MonsterMovementController.java @@ -52,17 +52,17 @@ public final class MonsterMovementController implements EvaluateWalkable { } } - public static boolean monsterCanMoveTo(final PredefinedMap map, final LayeredTileMap tilemap, final CoordRect p) { + public static boolean monsterCanMoveTo(final Monster movingMonster, final PredefinedMap map, final LayeredTileMap tilemap, final CoordRect p) { if (tilemap != null) { if (!tilemap.isWalkable(p)) return false; } - if (map.getMonsterAt(p) != null) return false; + if (map.getMonsterAt(p, movingMonster) != null) return false; - for (MapObject m : map.eventObjects) { - if (m == null) continue; - if (!m.isActive) continue; - if (!m.position.intersects(p)) continue; - switch (m.type) { + for (MapObject mObj : map.eventObjects) { + if (mObj == null) continue; + if (!mObj.isActive) continue; + if (!mObj.position.intersects(p)) continue; + switch (mObj.type) { case newmap: case keyarea: case rest: @@ -90,7 +90,7 @@ public final class MonsterMovementController implements EvaluateWalkable { } else { determineMonsterNextPosition(m, area, world.model.player.position); - if (!monsterCanMoveTo(map, tileMap, m.nextPosition)) { + if (!monsterCanMoveTo(m, map, tileMap, m.nextPosition)) { cancelCurrentMonsterMovement(m); return; } @@ -155,7 +155,7 @@ public final class MonsterMovementController implements EvaluateWalkable { @Override public boolean isWalkable(CoordRect r) { - return monsterCanMoveTo(world.model.currentMap, world.model.currentTileMap, r); + return monsterCanMoveTo(null, world.model.currentMap, world.model.currentTileMap, r); } public void moveMonsterToNextPosition(final Monster m, final PredefinedMap map) { diff --git a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/controller/MonsterSpawningController.java b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/controller/MonsterSpawningController.java index fcd2c1ce7..830c0437e 100644 --- a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/controller/MonsterSpawningController.java +++ b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/controller/MonsterSpawningController.java @@ -64,7 +64,7 @@ public final class MonsterSpawningController { p.topLeft.set( area.topLeft.x + Constants.rnd.nextInt(area.size.width) ,area.topLeft.y + Constants.rnd.nextInt(area.size.height)); - if (!MonsterMovementController.monsterCanMoveTo(map, tileMap, p)) continue; + if (!MonsterMovementController.monsterCanMoveTo(null, map, tileMap, p)) continue; if (playerPosition != null && p.contains(playerPosition)) continue; return p.topLeft; } diff --git a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/model/map/PredefinedMap.java b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/model/map/PredefinedMap.java index 083c68f78..7965c1de7 100644 --- a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/model/map/PredefinedMap.java +++ b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/model/map/PredefinedMap.java @@ -108,11 +108,13 @@ public final class PredefinedMap { } return false; } - public Monster getMonsterAt(final CoordRect p) { + return getMonsterAt(p, null); + } + public Monster getMonsterAt(final CoordRect p, Monster exceptMe) { for (MonsterSpawnArea a : spawnAreas) { Monster m = a.getMonsterAt(p); - if (m != null) return m; + if (m != null && (exceptMe == null || exceptMe != m)) return m; } return null; } diff --git a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/util/CoordRect.java b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/util/CoordRect.java index 7472936f2..d83843dfc 100644 --- a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/util/CoordRect.java +++ b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/util/CoordRect.java @@ -92,4 +92,24 @@ public final class CoordRect { return new CoordRect(new Coord(x, y), new Size(w, h)); } + + public static CoordRect getBoundingRect(Coord c1, Coord c2, Size s) { + int x, y, w, h; + if (c2.x < c1.x) { + x = c2.x; + w = 1 + c1.x - c2.x; + } else { + x = c1.x; + w = 1 + c2.x - c1.x; + } + if (c2.y < c1.y) { + y = c2.y; + h = 1 + c1.y - c2.y; + } else { + y = c1.y; + h = 1 + c2.y - c1.y; + } + + return new CoordRect(new Coord(x, y), new Size(w + s.width - 1, h + s.height - 1)); + } } diff --git a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/view/MainView.java b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/view/MainView.java index 28b9a2a03..c8f9959f6 100644 --- a/AndorsTrail/src/com/gpl/rpg/AndorsTrail/view/MainView.java +++ b/AndorsTrail/src/com/gpl/rpg/AndorsTrail/view/MainView.java @@ -844,12 +844,12 @@ public final class MainView extends SurfaceView @Override public void onNewSpriteMoveFrame(SpriteMoveAnimation animation) { - redrawMoveArea_(CoordRect.getBoundingRect(animation.origin, animation.destination), animation); + redrawMoveArea_(CoordRect.getBoundingRect(animation.origin, animation.destination, animation.actor.tileSize), animation); } @Override public void onSpriteMoveCompleted(SpriteMoveAnimation animation) { - redrawArea(CoordRect.getBoundingRect(animation.origin, animation.destination), RedrawAreaDebugReason.EffectCompleted); + redrawArea(CoordRect.getBoundingRect(animation.origin, animation.destination, animation.actor.tileSize), RedrawAreaDebugReason.EffectCompleted); } @Override