public override bool Update(float elapsedSeconds, World.World worldState)
{
bool ballMadeAction = base.Update(elapsedSeconds, worldState);
if(Ball.IsAlive)
{
Vector2 upDir = Vector2.Normalize(Ball.Position - worldState.StaticGeometry.gravityPoint);
Vector2 leftDir = new Vector2(-upDir.Y, upDir.X);
if (KeyPressed[InputMessage.MessageType.ControlsLeft])
{
var speed = WalkingTime < SlowWalkTime ? WalkingSpeedSlow : WalkingSpeedNormal;
Ball.Velocity = new Vector2(Min(-speed, Ball.Velocity.X), Ball.Velocity.Y);
WalkingTime += elapsedSeconds;
}
else if (KeyPressed[InputMessage.MessageType.ControlsRight])
{
var speed = WalkingTime < SlowWalkTime ? WalkingSpeedSlow : WalkingSpeedNormal;
Ball.Velocity = new Vector2(Max(speed, Ball.Velocity.X), Ball.Velocity.Y);
WalkingTime += elapsedSeconds;
}
else
{
WalkingTime = 0;
}
if (Ball.Velocity.X > 0.01 && Ball.ViewRotation < 0.99)
Ball.ViewRotation += ViewRotationSpeed * elapsedSeconds;
else if (Ball.Velocity.X < -0.01 && Ball.ViewRotation > -0.99)
Ball.ViewRotation -= ViewRotationSpeed * elapsedSeconds;
else if (Abs(Ball.Velocity.X) < 0.01 && Abs(Ball.ViewRotation) > 0.01)
Ball.ViewRotation -= Math.Sign(Ball.ViewRotation) * ViewRotationSpeed * elapsedSeconds;
Ball.ViewRotation = Max(-1, Min(Ball.ViewRotation, 1));
// Up/Down keys rotate the aim vector
if (KeyPressed[InputMessage.MessageType.ControlsUp])
{
var v = Ball.AimDirection;
// Rotate at 60°/s. Use sign of v.x to determine the direction, so that the up key always moves the crosshair upwards.
var radians = (v.X > 0 ? 1 : -1) * elapsedSeconds * 2 * (float)Math.PI * 60f / 360f;
Ball.AimDirection = v.Rotate(radians);
}
if (KeyPressed[InputMessage.MessageType.ControlsDown])
{
var v = Ball.AimDirection;
// Rotate at 60°/s. Use sign of v.x to determine the direction, so that the up key always moves the crosshair upwards.
var radians = (v.X > 0 ? -1 : 1) * elapsedSeconds * 2 * (float)Math.PI * 60f / 360f;
Ball.AimDirection = v.Rotate(radians);
}
if(KeyPressed[InputMessage.MessageType.ControlsLeft])
Ball.AimDirection = new Vector2(-Math.Abs(Ball.AimDirection.X), Ball.AimDirection.Y);
else if(KeyPressed[InputMessage.MessageType.ControlsRight])
Ball.AimDirection = new Vector2(Math.Abs(Ball.AimDirection.X), Ball.AimDirection.Y);
// Handle single-shot input events
ProcessInput(controlInput);
}
controlInput = null;
return ballMadeAction;
}