Recompute train direction when needed

This commit is contained in:
Arne Keller 2020-03-04 20:17:03 +01:00
parent 428e1862fc
commit 860d31d8fb
2 changed files with 2 additions and 20 deletions

View File

@ -31,10 +31,6 @@ public final class Train {
* positions when moving the train. * positions when moving the train.
*/ */
private LinkedList<Vector2D> position; private LinkedList<Vector2D> position;
/**
* Direction of the train head.
*/
private Vector2D direction;
/** /**
* Set of rails this train occupies. * Set of rails this train occupies.
*/ */
@ -197,7 +193,6 @@ public final class Train {
}).collect(Collectors.toSet()); }).collect(Collectors.toSet());
this.position = positions; this.position = positions;
this.direction = rawDirection.normalized();
this.occupiedRails = occupiedRailsSet; this.occupiedRails = occupiedRailsSet;
return true; return true;
@ -207,7 +202,7 @@ public final class Train {
* @return whether this train is placed on a rail network * @return whether this train is placed on a rail network
*/ */
public boolean isPlaced() { public boolean isPlaced() {
return position != null && direction != null && occupiedRails != null; return position != null && occupiedRails != null;
} }
/** /**
@ -250,7 +245,7 @@ public final class Train {
*/ */
public Vector2D getDirection() { public Vector2D getDirection() {
// make sure caller can not modify internal state // make sure caller can not modify internal state
return new Vector2D(direction); return position.get(0).subtract(position.get(1));
} }
/** /**
@ -341,22 +336,11 @@ public final class Train {
} // else: only touching new rail } // else: only touching new rail
} }
/**
* Update the direction of this train.
*
* @param newDirection new direction of the train
*/
public void setDirection(final Vector2D newDirection) {
// make sure the caller can not modify internal data afterwards
direction = new Vector2D(newDirection);
}
/** /**
* Remove this train from the rail network. * Remove this train from the rail network.
*/ */
public void removeFromRails() { public void removeFromRails() {
position = null; position = null;
direction = null;
occupiedRails = null; occupiedRails = null;
} }

View File

@ -280,7 +280,6 @@ public final class TrainManager {
collisions.add(new HashSet<>(Arrays.asList(train))); collisions.add(new HashSet<>(Arrays.asList(train)));
} else { } else {
train.moveTo(railNetwork, nextPosition); train.moveTo(railNetwork, nextPosition);
train.setDirection(direction);
} }
}); });
getStaticCollisions(collisions); getStaticCollisions(collisions);
@ -306,7 +305,6 @@ public final class TrainManager {
collisions.add(new HashSet<>(Arrays.asList(train))); collisions.add(new HashSet<>(Arrays.asList(train)));
} else { } else {
train.moveBackTo(railNetwork, nextPosition); train.moveBackTo(railNetwork, nextPosition);
train.setDirection(front.subtract(train.getFrontPosition()));
} }
}); });
getStaticCollisions(collisions); getStaticCollisions(collisions);