Allow pistons to shove robots.

This commit is contained in:
Florian Nücke
2021-01-15 14:43:12 +01:00
parent f3dcf40910
commit b26936d42c
6 changed files with 73 additions and 10 deletions

View File

@@ -97,6 +97,7 @@ public final class RobotEntity extends Entity {
private final RobotVirtualMachineState state;
private final RobotItemStackHandlers items = new RobotItemStackHandlers();
private final RobotBusElement busElement = new RobotBusElement();
private long lastPistonMovement;
///////////////////////////////////////////////////////////////////
@@ -131,6 +132,10 @@ public final class RobotEntity extends Entity {
return items;
}
public long getLastPistonMovement() {
return lastPistonMovement;
}
public void start() {
final World world = getEntityWorld();
if (world == null || world.isRemote()) {
@@ -319,7 +324,8 @@ public final class RobotEntity extends Entity {
@Override
protected Vector3d handlePistonMovement(final Vector3d pos) {
return Vector3d.ZERO;
lastPistonMovement = getEntityWorld().getGameTime();
return super.handlePistonMovement(pos);
}
///////////////////////////////////////////////////////////////////
@@ -530,6 +536,7 @@ public final class RobotEntity extends Entity {
action.initialize(RobotEntity.this);
}
}
RobotActions.performServer(RobotEntity.this, action);
}
}

View File

@@ -25,6 +25,9 @@ public abstract class AbstractRobotActionType {
public void initializeData(final RobotEntity robot) {
}
public void performServer(final RobotEntity robot, final AbstractRobotAction currentAction) {
}
public void performClient(final RobotEntity robot) {
}

View File

@@ -32,6 +32,12 @@ public final class RobotActions {
}
}
public static void performServer(final RobotEntity robot, final AbstractRobotAction currentAction) {
for (final AbstractRobotActionType type : ACTIONS) {
type.performServer(robot, currentAction);
}
}
public static void performClient(final RobotEntity robot) {
for (final AbstractRobotActionType type : ACTIONS) {
type.performClient(robot);

View File

@@ -95,16 +95,9 @@ public final class RobotMovementAction extends AbstractRobotAction {
throw new IllegalStateException();
}
moveTowards(robot, targetPos);
validateTarget(robot);
final boolean didCollide = robot.collidedHorizontally || robot.collidedVertically;
if (didCollide && !robot.getEntityWorld().isRemote()) {
final BlockPos newStart = target;
target = start;
start = newStart;
targetPos = getTargetPositionInBlock(target);
robot.getDataManager().set(RobotEntity.TARGET_POSITION, target);
}
moveAndResolveCollisions(robot);
if (robot.getPositionVec().squareDistanceTo(targetPos) < TARGET_EPSILON) {
if (Objects.equals(target, origin)) {
@@ -151,4 +144,44 @@ public final class RobotMovementAction extends AbstractRobotAction {
targetPos = getTargetPositionInBlock(target);
}
}
private void moveAndResolveCollisions(final RobotEntity robot) {
moveTowards(robot, targetPos);
final boolean didCollide = robot.collidedHorizontally || robot.collidedVertically;
final long gameTime = robot.getEntityWorld().getGameTime();
if (didCollide && !robot.getEntityWorld().isRemote()
&& robot.getLastPistonMovement() < gameTime - 1) {
final BlockPos newStart = target;
target = start;
start = newStart;
targetPos = getTargetPositionInBlock(target);
robot.getDataManager().set(RobotEntity.TARGET_POSITION, target);
}
}
private void validateTarget(final RobotEntity robot) {
final BlockPos currentPosition = robot.getPosition();
if (Objects.equals(currentPosition, start) || Objects.equals(currentPosition, target)) {
return;
}
// Got pushed out of our original path. Adjust start and target by the least offset.
final BlockPos fromStart = currentPosition.subtract(start);
final BlockPos fromTarget = currentPosition.subtract(target);
final int deltaStart = fromStart.getX() + fromStart.getY() + fromStart.getZ();
final int deltaTarget = fromTarget.getX() + fromTarget.getY() + fromTarget.getZ();
if (deltaStart < deltaTarget) {
start = currentPosition;
target = target.add(fromStart);
} else {
start = start.add(fromTarget);
target = currentPosition;
}
targetPos = getTargetPositionInBlock(target);
robot.getDataManager().set(RobotEntity.TARGET_POSITION, target);
}
}

View File

@@ -23,6 +23,13 @@ public final class RobotMovementActionType extends AbstractRobotActionType {
robot.getDataManager().set(RobotEntity.TARGET_POSITION, robot.getPosition());
}
@Override
public void performServer(final RobotEntity robot, final AbstractRobotAction currentAction) {
if (!(currentAction instanceof RobotMovementAction)) {
robot.getDataManager().set(RobotEntity.TARGET_POSITION, robot.getPosition());
}
}
@Override
public void performClient(final RobotEntity robot) {
final Vector3d target = RobotMovementAction.getTargetPositionInBlock(robot.getDataManager().get(RobotEntity.TARGET_POSITION));

View File

@@ -23,6 +23,13 @@ public final class RobotRotationActionType extends AbstractRobotActionType {
robot.getDataManager().set(RobotEntity.TARGET_DIRECTION, robot.getHorizontalFacing());
}
@Override
public void performServer(final RobotEntity robot, final AbstractRobotAction currentAction) {
if (!(currentAction instanceof RobotRotationAction)) {
robot.getDataManager().set(RobotEntity.TARGET_DIRECTION, robot.getHorizontalFacing());
}
}
@Override
public void performClient(final RobotEntity robot) {
final Direction target = robot.getDataManager().get(RobotEntity.TARGET_DIRECTION);