protected override void PreUpdate(CanvasContext2D context)
{
// Draw background
context.DrawImage(_backgroundImage, 0, 0);
switch (Status)
{
case RaceStatus.Running:
// Handle left and right turns
if (Speed > 0)
{
float increment = (60 - Math.Max(Speed, 20)) / 80;
if (Left)
{
Shift += increment * DeltaTime;
CarSystem.CarObject.CurrentAnimation = Down ? "b-Left" : "Left";
}
if (Right)
{
Shift -= increment * DeltaTime;
CarSystem.CarObject.CurrentAnimation = Down ? "b-Right" : "Right";
}
}
if (!(Left ^ Right)) CarSystem.CarObject.CurrentAnimation = Down ? "b-Forward" : "Forward";
// Handle acceleration, braking and inertia
if (Down) Speed -= 0.4f;
else if (Up) Speed += 0.3f;
else Speed -= 0.1f;
if (Up) _rpm += 40;
else _rpm -= 40;
if (_rpm > 4500) _rpm = 4500;
else if (_rpm < 200) _rpm = 200;
// When driving off the road
if (Math.Abs(Shift) > 350)
{
Speed *= 0.95f;
if (Math.Abs(Shift) > 450)
Shift = (Shift / Math.Abs(Shift)) * 450;
}
break;
case RaceStatus.Win:
case RaceStatus.Fail:
Speed -= 1;
_rpm -= 40;
break;
case RaceStatus.Crashing:
Speed -= 0.3f;
Shift -= Shift * DeltaTime / 1000;
break;
}
// Speed capping
if (Speed > MaxSpeed) Speed = MaxSpeed;
if (Speed < 0) Speed = 0;
// Calculating new position
Position += Speed * DeltaTime;
// Drift in turns
Shift += Curve * Speed * 150;
if (Position >= RoadLength && Status == RaceStatus.Running)
{
_music.Pause();
Status = RaceStatus.Win;
CarSystem.CarObject.StartAnimation("Skid");
ShowMessage(_winMessage);
RemoveSystem(_engineSoundSystem);
RemoveSystem(_npcSystem);
int timeLeft = Math.Floor(TimeLeft / 1000);
CurrentGame.Score += 1000 + timeLeft * 500;
//if (!_practice)
//{
// pendingTimers.Add(
// Window.SetTimeout(delegate()
// {
// UpdateMessage("<p>Time Left: " + timeLeft + "</p>");
// }, 1500));
// pendingTimers.Add(
// Window.SetTimeout(delegate()
// {
// UpdateMessage("<p>Score: " + CurrentGame.Score + "</p>");
// }, 2500));
//}
pendingTimers.Add(
Window.SetTimeout(delegate()
{
UpdateMessage("<p>Press a key to continue.</p>");
}, 3000));
}
}