From 7be75b7b406049d73e43f9232dac10b89dce6dcb Mon Sep 17 00:00:00 2001 From: pacien Date: Fri, 2 Feb 2018 18:02:21 +0100 Subject: Implement robot movement Signed-off-by: pacien --- .../java/fr/umlv/java/wallj/block/RobotBlock.java | 19 +++++++++++++------ .../java/fr/umlv/java/wallj/board/PathFinder.java | 2 +- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/main/java/fr/umlv/java/wallj/block/RobotBlock.java b/src/main/java/fr/umlv/java/wallj/block/RobotBlock.java index e41c57f..7d370e5 100644 --- a/src/main/java/fr/umlv/java/wallj/block/RobotBlock.java +++ b/src/main/java/fr/umlv/java/wallj/block/RobotBlock.java @@ -13,6 +13,8 @@ import org.jbox2d.dynamics.World; import java.awt.*; import java.time.Duration; import java.util.Collections; +import java.util.Deque; +import java.util.LinkedList; import java.util.List; /** @@ -21,10 +23,11 @@ import java.util.List; * @author Pacien TRAN-GIRARD */ public class RobotBlock extends Block { - // TODO: define robot moving speed + private static final float SPEED = 0.2f; // px/ms + private Vec2 pos; - private List path = Collections.emptyList(); private PathFinder pathFinder; + private Deque path = new LinkedList<>(); RobotBlock(Vec2 pos) { super(BlockType.ROBOT); @@ -46,7 +49,7 @@ public class RobotBlock extends Block { Events.findFirst(context.getEvents(), MoveRobotOrder.class) .ifPresent(event -> updatePath(context.getGame().getCurrentStage().getBoard(), event.getTarget())); - if (!path.isEmpty()) move(context.getTimeDelta()); + move(context.getTimeDelta()); paint(context.getGraphicsContext()); return setupBomb(context.getEvents()); } @@ -60,12 +63,16 @@ public class RobotBlock extends Block { private void updatePath(Board board, TileVec2 target) { if (!board.getBlockTypeAt(target).isTraversable()) return; if (pathFinder == null) pathFinder = new PathFinder(board); - path = pathFinder.findPath(TileVec2.of(pos), target); + path = new LinkedList<>(pathFinder.findPath(TileVec2.of(pos), target)); } private void move(Duration timeDelta) { - // TODO: follow the current path - + if (path.isEmpty()) return; + Vec2 dest = path.getFirst().toVec2(); + Vec2 dir = dest.sub(pos); + float dist = dir.normalize(); + Vec2 dp = dir.mul(timeDelta.toMillis() * SPEED); + pos = dp.length() < dist ? pos.add(dp) : path.removeFirst().toVec2(); } private void paint(GraphicsContext graphicsContext) { diff --git a/src/main/java/fr/umlv/java/wallj/board/PathFinder.java b/src/main/java/fr/umlv/java/wallj/board/PathFinder.java index 8dbab2d..2f2f009 100644 --- a/src/main/java/fr/umlv/java/wallj/board/PathFinder.java +++ b/src/main/java/fr/umlv/java/wallj/board/PathFinder.java @@ -121,7 +121,7 @@ public class PathFinder { * * @param origin the origin coordinates * @param target the target coordinates - * @return a path + * @return a path from the origin to the target position * @implNote uses A* with euclidean distance heuristic */ public List findPath(TileVec2 origin, TileVec2 target) { -- cgit v1.2.3