Skip to content

Commit

Permalink
Merge pull request #18 from AndyTechGuy/pathfindingChange
Browse files Browse the repository at this point in the history
Listen for path calculation in FindPathToNode
  • Loading branch information
AndyTechGuy committed Aug 10, 2019
2 parents 83986dc + 5fe037a commit a543403
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
29 changes: 20 additions & 9 deletions src/main/java/org/terasology/minion/move/FindPathToNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
*/
package org.terasology.minion.move;

import com.google.gson.annotations.Expose;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.common.util.concurrent.SettableFuture;
import org.terasology.logic.behavior.BehaviorAction;
import org.terasology.logic.behavior.core.Actor;
import org.terasology.logic.behavior.core.BaseAction;
Expand All @@ -29,6 +32,7 @@
import org.terasology.registry.In;

import java.util.Arrays;
import java.util.List;

/**
* Requests a path to a target defined using the <b>MinionMoveComponent.target</b>.<br/>
Expand Down Expand Up @@ -58,6 +62,8 @@ public void construct(final Actor actor) {
if(pathfinderSystem==null){setup();}
final MinionMoveComponent moveComponent = actor.getComponent(MinionMoveComponent.class);
Vector3f targetLocation = moveComponent.target;
moveComponent.path = null;
actor.save(moveComponent);
WalkableBlock currentBlock = moveComponent.currentBlock;
if (currentBlock == null || targetLocation == null) {
moveComponent.path = Path.INVALID;
Expand All @@ -68,21 +74,26 @@ public void construct(final Actor actor) {
moveComponent.path = Path.INVALID;
return;
}
pathfinderSystem.requestPath(
SettableFuture<List<Path>> pathFuture = pathfinderSystem.requestPath(
actor.getEntity(), currentBlock.getBlockPosition(),
Arrays.asList(workTarget.getBlockPosition()));
/*, new PathfinderSystem.PathReadyCallback() {
@Override
public void pathReady(int pathId, List<Path> path, WalkableBlock target, List<WalkableBlock> start) {

if (path == null) {
Futures.addCallback(pathFuture, new FutureCallback<List<Path>>() {
@Override
public void onSuccess(List<Path> paths) {
if (paths == null) {
moveComponent.path = Path.INVALID;
} else if (path.size() > 0) {
moveComponent.path = path.get(0);
} else if (paths.size() > 0) {
moveComponent.path = paths.get(0);
}
actor.save(moveComponent);
}
});*/

@Override
public void onFailure(Throwable t) {
moveComponent.path = Path.INVALID;
}
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.terasology.navgraph.WalkableBlock;
import org.terasology.pathfinding.componentSystem.PathRenderSystem;
import org.terasology.pathfinding.model.Path;
import org.terasology.registry.CoreRegistry;
import org.terasology.registry.In;

/**
Expand All @@ -40,11 +41,14 @@
public class MoveAlongPathNode extends BaseAction {
private static final Logger logger = LoggerFactory.getLogger(MoveAlongPathNode.class);

@In
// @In
private transient PathRenderSystem pathRenderSystem;

@Override
public void construct(Actor actor) {
// TODO: Temporary fix for injection malfunction in actions, ideally remove this in the future.
pathRenderSystem = CoreRegistry.get(PathRenderSystem.class);

MinionMoveComponent moveComponent = actor.getComponent(MinionMoveComponent.class);
if (moveComponent != null && moveComponent.path != null && moveComponent.path != Path.INVALID) {
pathRenderSystem.addPath(moveComponent.path);
Expand Down

0 comments on commit a543403

Please sign in to comment.