Skip to content

Commit

Permalink
Start stopwatches when vehicles start driving. Added Finish to lap ti…
Browse files Browse the repository at this point in the history
…mes label. Removed old code.
  • Loading branch information
henkmollema committed Oct 29, 2014
1 parent 4cc1a1d commit 7a18c46
Showing 1 changed file with 40 additions and 44 deletions.
84 changes: 40 additions & 44 deletions src/Driftr/Driftr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,35 +27,33 @@ public partial class Driftr : Form

private readonly Vehicle[] _vehicles = { new Vehicle(), new Vehicle() };
private readonly Dictionary<Vehicle, int> _vehicleLaps = new Dictionary<Vehicle, int>();

private readonly Dictionary<Vehicle, List<TimeSpan>> _vehicleLapTimes = new Dictionary<Vehicle, List<TimeSpan>>();
private readonly Dictionary<Vehicle, Stopwatch> _vehicleStopwatch = new Dictionary<Vehicle, Stopwatch>();
private readonly Dictionary<Vehicle, int> _vehiclePitstops = new Dictionary<Vehicle, int>();
private readonly Dictionary<Vehicle, bool> _vehiclePitstopsCheckpoint = new Dictionary<Vehicle, bool>();
private readonly Dictionary<Vehicle, bool[]> _vehicleCheckpoints = new Dictionary<Vehicle, bool[]>();

private readonly List<Color> _checkpoints = new List<Color>
{
Color.FromArgb(255, 241, 0),
Color.FromArgb(0, 162, 232),
Color.FromArgb(185, 122, 87),
Color.FromArgb(254, 126, 39),
Color.FromArgb(236, 27, 36),
Color.FromArgb(162, 73, 164),
Color.FromArgb(255, 174, 200)
};
{
Color.FromArgb(255, 241, 0),
Color.FromArgb(0, 162, 232),
Color.FromArgb(185, 122, 87),
Color.FromArgb(254, 126, 39),
Color.FromArgb(236, 27, 36),
Color.FromArgb(162, 73, 164),
Color.FromArgb(255, 174, 200)
};

private static readonly Color _pitstop = Color.FromArgb(92, 92, 92);
private static readonly Color _obstacle = Color.FromArgb(16, 245, 0);
private static readonly Color _grass = Color.FromArgb(21, 115, 0);
private static readonly Color _pitstopCheckpoint = Color.FromArgb(0, 246, 255);

public Driftr()
{
InitializeComponent();
Application.Idle += Application_Idle;
screen.Paint += screen_Paint;
screen.MouseUp += screen_MouseUp;
KeyUp += Driftr_KeyUp;
KeyDown += Driftr_KeyDown;

Expand All @@ -70,7 +68,7 @@ public Driftr()
_vehicleLapTimes.Add(v, new List<TimeSpan>());
}

StartStopwatches();
//StartStopwatches();
Init(screen.Size);
pictureBox1.Parent = screen;
pictureBox2.Parent = screen;
Expand All @@ -85,12 +83,6 @@ public Driftr()
pictureBox2.Image = Resources.dashboard_5_yellow;
}

void screen_MouseUp(object sender, MouseEventArgs e)
{
var p = ((Bitmap)screen.BackgroundImage).GetPixel(e.X, e.Y);
Debug.WriteLine(p);
}

private void Init(Size size)
{
screen.BackgroundImage = Resources.MapBackground;
Expand All @@ -100,7 +92,7 @@ private void Init(Size size)
_bufferSize = size;
_backbuffer = new Bitmap(_bufferSize.Width, _bufferSize.Height);
_graphics = Graphics.FromImage(_backbuffer);

_timer.GetETime();

_vehicles[0].Setup(new Vector(3, 8) / 2.0f, 5, Resources.CarRed);
Expand Down Expand Up @@ -156,6 +148,16 @@ private void DrawScreen()
{
lapTimeYellowLabel2.Text += x.ToString("mm':'ss':'ff") + Environment.NewLine;
}

if (_vehicleLaps[_vehicles[0]] == 5)
{
lapTimeRedLabel2.Text += "FINISH!";
}

if (_vehicleLaps[_vehicles[1]] == 5)
{
lapTimeYellowLabel2.Text += "FINISH!";
}
}

private void ProcessCheckpoints()
Expand All @@ -165,23 +167,8 @@ private void ProcessCheckpoints()
{
var vehicle = _vehicles[i];
var pos = VehicleRelativePosition(i);

var color = background.GetPixel((int)pos.X, (int)pos.Y);

//if (color == _pitstopCheckpoint)
//{
// if (_vehiclePitstopsCheckpoint[vehicle])
// {
// _vehiclePitstops[vehicle]++;
// }
// else
// {
// _vehiclePitstopsCheckpoint[vehicle] = true;
// }
//
// return;
//}

if (_checkpoints.Any(x => x == color))
{
int index = _checkpoints.IndexOf(color);
Expand All @@ -194,7 +181,7 @@ private void ProcessCheckpoints()
{
_vehicleCheckpoints[vehicle][n] = false;
}

_vehicleLapTimes[vehicle].Add(_vehicleStopwatch[vehicle].Elapsed);

_vehicleStopwatch[vehicle].Reset();
Expand Down Expand Up @@ -342,6 +329,23 @@ private void ProcessInput()

private void Driftr_KeyDown(object sender, KeyEventArgs e)
{
// Start vehicle stopwatches.
switch (e.KeyCode)
{
case Keys.Left:
case Keys.Right:
case Keys.Up:
case Keys.Down:
_vehicleStopwatch[_vehicles[0]].Start();
break;
case Keys.W:
case Keys.A:
case Keys.S:
case Keys.D:
_vehicleStopwatch[_vehicles[1]].Start();
break;
}

switch (e.KeyCode)
{
case Keys.Left:
Expand Down Expand Up @@ -534,14 +538,6 @@ private void timerLaps_Tick(object sender, EventArgs e)
lapTimeYellowLabel.Text = ealpsedYellow;
}

private void StartStopwatches()
{
foreach(var s in _vehicleStopwatch)
{
s.Value.Start();
}
}

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Up || keyData == Keys.Down || keyData == Keys.Left || keyData == Keys.Right)
Expand Down

0 comments on commit 7a18c46

Please sign in to comment.