aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpacien2018-02-02 18:02:21 +0100
committerpacien2018-02-02 18:02:21 +0100
commit7be75b7b406049d73e43f9232dac10b89dce6dcb (patch)
tree29471c620003188792db6abdc908fbf345b7e6b5
parent6d05e0e8b91ccdd69a8f45227104a543febf5982 (diff)
downloadwallj-7be75b7b406049d73e43f9232dac10b89dce6dcb.tar.gz
Implement robot movement
Signed-off-by: pacien <pacien.trangirard@pacien.net>
-rw-r--r--src/main/java/fr/umlv/java/wallj/block/RobotBlock.java19
-rw-r--r--src/main/java/fr/umlv/java/wallj/board/PathFinder.java2
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;
13import java.awt.*; 13import java.awt.*;
14import java.time.Duration; 14import java.time.Duration;
15import java.util.Collections; 15import java.util.Collections;
16import java.util.Deque;
17import java.util.LinkedList;
16import java.util.List; 18import java.util.List;
17 19
18/** 20/**
@@ -21,10 +23,11 @@ import java.util.List;
21 * @author Pacien TRAN-GIRARD 23 * @author Pacien TRAN-GIRARD
22 */ 24 */
23public class RobotBlock extends Block { 25public class RobotBlock extends Block {
24 // TODO: define robot moving speed 26 private static final float SPEED = 0.2f; // px/ms
27
25 private Vec2 pos; 28 private Vec2 pos;
26 private List<TileVec2> path = Collections.emptyList();
27 private PathFinder pathFinder; 29 private PathFinder pathFinder;
30 private Deque<TileVec2> path = new LinkedList<>();
28 31
29 RobotBlock(Vec2 pos) { 32 RobotBlock(Vec2 pos) {
30 super(BlockType.ROBOT); 33 super(BlockType.ROBOT);
@@ -46,7 +49,7 @@ public class RobotBlock extends Block {
46 Events.findFirst(context.getEvents(), MoveRobotOrder.class) 49 Events.findFirst(context.getEvents(), MoveRobotOrder.class)
47 .ifPresent(event -> updatePath(context.getGame().getCurrentStage().getBoard(), event.getTarget())); 50 .ifPresent(event -> updatePath(context.getGame().getCurrentStage().getBoard(), event.getTarget()));
48 51
49 if (!path.isEmpty()) move(context.getTimeDelta()); 52 move(context.getTimeDelta());
50 paint(context.getGraphicsContext()); 53 paint(context.getGraphicsContext());
51 return setupBomb(context.getEvents()); 54 return setupBomb(context.getEvents());
52 } 55 }
@@ -60,12 +63,16 @@ public class RobotBlock extends Block {
60 private void updatePath(Board board, TileVec2 target) { 63 private void updatePath(Board board, TileVec2 target) {
61 if (!board.getBlockTypeAt(target).isTraversable()) return; 64 if (!board.getBlockTypeAt(target).isTraversable()) return;
62 if (pathFinder == null) pathFinder = new PathFinder(board); 65 if (pathFinder == null) pathFinder = new PathFinder(board);
63 path = pathFinder.findPath(TileVec2.of(pos), target); 66 path = new LinkedList<>(pathFinder.findPath(TileVec2.of(pos), target));
64 } 67 }
65 68
66 private void move(Duration timeDelta) { 69 private void move(Duration timeDelta) {
67 // TODO: follow the current path 70 if (path.isEmpty()) return;
68 71 Vec2 dest = path.getFirst().toVec2();
72 Vec2 dir = dest.sub(pos);
73 float dist = dir.normalize();
74 Vec2 dp = dir.mul(timeDelta.toMillis() * SPEED);
75 pos = dp.length() < dist ? pos.add(dp) : path.removeFirst().toVec2();
69 } 76 }
70 77
71 private void paint(GraphicsContext graphicsContext) { 78 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 {
121 * 121 *
122 * @param origin the origin coordinates 122 * @param origin the origin coordinates
123 * @param target the target coordinates 123 * @param target the target coordinates
124 * @return a path 124 * @return a path from the origin to the target position
125 * @implNote uses A* with euclidean distance heuristic 125 * @implNote uses A* with euclidean distance heuristic
126 */ 126 */
127 public List<TileVec2> findPath(TileVec2 origin, TileVec2 target) { 127 public List<TileVec2> findPath(TileVec2 origin, TileVec2 target) {