package actor;
import render.Matrix;
public class SpiralBehavior extends AbstractBehavior
{
public final int SPIRAL_IN = 0, SPIRAL_OUT = 1;
private Point3D center;
private double maxRadius, minRadius;
private Matrix rotationMatrix = new Matrix();
private MotionChange returnValue = new MotionChange();
private int spiralType = SPIRAL_IN;
public SpiralBehavior()
{
this(new Point3D(0, 0, 0), 2.5, 1.0);
}
public SpiralBehavior(Point3D center, double maxRadius, double minRadius)
{
this.center = center;
this.maxRadius = maxRadius;
this.minRadius = minRadius;
}
public MotionChange doBehavior(WorldState world, InvariantActor actor)
{
double currentRadius = Point3D.subtract(actor.getPosition(), center).norm();
double newActorYRotation =
actor.getDirection().getYRotation()
+ actor.getSize().y * (actor.getAnimationTravel() / currentRadius);
double newRadius = currentRadius;
if (spiralType == SPIRAL_IN)
{
if (currentRadius > minRadius)
{
newRadius =
currentRadius
- (actor.getSize().y * 0.075 * actor.getAnimationTravel());
}
else
{
spiralType = SPIRAL_OUT;
}
}
else if (spiralType == SPIRAL_OUT)
{
if (currentRadius < maxRadius)
{
newRadius =
currentRadius
+ (actor.getSize().y * 0.075 * actor.getAnimationTravel());
}
else
{
spiralType = SPIRAL_IN;
}
}
rotationMatrix.identity();
rotationMatrix.rotateY(newActorYRotation);
Point3D actorRotation = Point3D.createDirectionVector(rotationMatrix);
Point3D actorPosition =
Point3D.add(
center,
Point3D.matrixTransform(rotationMatrix, -newRadius, 0, 0));
returnValue.setOldValues(actor);
returnValue.setNewDirection(actorRotation);
returnValue.setNewPosition(actorPosition);
return returnValue;
}
}