public void OnDriversChanged() { DriversChangedEventArgs driverArgs = new DriversChangedEventArgs(); driverArgs.track = track; //EventHandler handler = DriversChanged; Console.WriteLine("ondriverschanged"); DriversChanged.Invoke(this, driverArgs); }
private void FinishRace() { _timer.Stop(); _timer.Elapsed -= OnTimedEvent; RaceFinished?.Invoke(this, new RaceFinishedEventArgs(GetFinishedPlacements())); if (DriversChanged != null) { foreach (Delegate d in DriversChanged.GetInvocationList()) { DriversChanged -= (EventHandler <DriversChangedEventArgs>)d; } } }
private void Overtake(SectionData data, bool test) { var overTaker = data.RightParticipant; var overTakerDistance = data.DistanceRight; data.RightParticipant = data.LeftParticipant; data.DistanceRight = data.DistanceLeft; data.LeftParticipant = overTaker; data.DistanceLeft = overTakerDistance; LogOvertake(overTaker, data.RightParticipant, overTaker.currentSection); DriversChanged.Invoke(this, new DriversChangedEventArgs(Track)); }
public void OnTimedEvent(object source, ElapsedEventArgs e) { //Allows pauses/debugging without crashing the console! if (!_isMoving) { _isMoving = true; MoveParticipants(); RepairOrBreakEquipment(); StartTime = DateTime.Now; DriversChanged?.Invoke(this, new DriversChangedEventArgs() { Track = this.Track }); CheckRaceFinished(this.Track); _isMoving = false; } }
// Runs constantly whenever timer is started //public void OnTimedEvent(object source, ElapsedEventArgs e) { // int sectionSize = 100; // for (int i = _positions.Count - 1; i >= 0; i--) { // var sData = _positions.ElementAt(i).Value; // if (sData.Left != null) { // var distanceTraveled = sData.Left.Equipment.Performance * sData.Left.Equipment.Speed; // sData.DistanceLeft += distanceTraveled; // if (sData.DistanceLeft >= sectionSize) { // var nextSection = _positions.Count <= i + 1 ? _positions.ElementAt(0).Key : _positions.ElementAt(i + 1).Key; // IsFinished(sData); // ToNextSection(sData, 0, nextSection); // } // } // if (sData.Right != null) { // var distanceTraveled = sData.Right.Equipment.Performance * sData.Right.Equipment.Speed; // sData.DistanceRight += distanceTraveled; // if (sData.DistanceRight >= sectionSize) { // var nextSection = _positions.Count <= i + 1 ? _positions.ElementAt(0).Key : _positions.ElementAt(i + 1).Key; // IsFinished(sData); // ToNextSection(sData, 1, nextSection); // } // } // } // var eventArgs = new DriversChangedEventArgs { Track = Data.CurrentRace.Track }; // DriversChanged?.Invoke(source, eventArgs); // //Console.WriteLine($"Timer Elapsed: {e.SignalTime.Ticks}"); //} public void OnTimedEvent(object source, ElapsedEventArgs e) { int sectionSize = 100; int iteration = 1; foreach (KeyValuePair <Section, SectionData> s in _positions) { if (s.Value.Left != null) { int DistanceTraveled = s.Value.Left.Equipment.Performance * s.Value.Left.Equipment.Speed; s.Value.DistanceLeft += DistanceTraveled; if (s.Value.DistanceLeft >= sectionSize) { if (iteration > _positions.Count) { iteration = 0; } Section nextSection = _positions.ElementAt(iteration - 1).Key; ToNextSection(s.Value, 0, nextSection); } } if (s.Value.Right != null) { int DistanceTraveled = s.Value.Right.Equipment.Performance * s.Value.Right.Equipment.Speed; s.Value.DistanceRight += DistanceTraveled; if (s.Value.DistanceRight >= sectionSize) { if (iteration > _positions.Count) { iteration = 0; } Section nextSection = _positions.ElementAt(iteration - 1).Key; ToNextSection(s.Value, 1, nextSection); } } iteration++; } //iteration = 1; var eventArgs = new DriversChangedEventArgs { Track = Data.CurrentRace.Track }; DriversChanged?.Invoke(source, eventArgs); }
public void CleanupEvents() { Delegate[] delegates = DriversChanged?.GetInvocationList(); if (delegates != null) { foreach (var d in delegates) { DriversChanged -= (EventHandler)d; } } delegates = NextRace?.GetInvocationList(); if (delegates != null) { foreach (var d in delegates) { NextRace -= (EventHandler)d; } } }
//Loop through the track on a timed event and check if a driver can be moved to next section private void OnTimedEvent(Object source, ElapsedEventArgs e) { LinkedListNode <Section> section = Track.Sections.Last; SectionData sectionValue = GetSectionData(section.Value); LinkedListNode <Section> previousSection = section; SectionData previousSectionValue = GetSectionData(previousSection.Value); //Loop through sections from the last to the first for (int i = 0; i < Track.Sections.Count; i++) { MoveDriverIfPossible(section, sectionValue, e); //Set the next section to the previous because we loop from the back to the front if (previousSection.Previous != null) { previousSection = previousSection.Previous; previousSectionValue = GetSectionData(previousSection.Value); section = previousSection; sectionValue = GetSectionData(previousSection.Value); } //Set the last section to the first because we loop from the back to the front else { section = previousSection; sectionValue = GetSectionData(previousSection.Value); previousSection = Track.Sections.First; previousSectionValue = GetSectionData(previousSection.Value); } } //Call the methods from the event driverschanged in the gui DriversChanged?.Invoke(this, new DriversChangedEventArgs(Track, Data.Competition)); //Stop the race if all drivers are removed if (DriversRemoved == Participants.Count) { Stop(); } }
//Timer event public void OnTimedEvent(object sender, ElapsedEventArgs args) { Section vorigeSection = _positions.First().Key; foreach (KeyValuePair <Section, SectionData> valuePair in _positions.Reverse()) { SectionData sectionData = valuePair.Value; if (sectionData.Left != null) { IParticipant currentDeelnemer = valuePair.Value.Left; if (!sectionData.Left.Equipment.isBroken) { sectionData.DistanceLeft += sectionData.Left.Equipment.Performance * sectionData.Left.Equipment.Speed; if (sectionData.DistanceLeft >= 100) { if (valuePair.Key.SectionType == SectionTypes.Finish) { if (_rounds[sectionData.Left] > 0) { ParticipantFinished(sectionData, 0); } else { _rounds[sectionData.Left]++; SetParticipantNextSectionLeft(vorigeSection, sectionData); } } else { SetParticipantNextSectionLeft(vorigeSection, sectionData); } SaveTimeForSectionAndParticipant(valuePair.Key, currentDeelnemer, args); } } } if (sectionData.Right != null) { IParticipant currentDeelnemer = valuePair.Value.Right; if (!sectionData.Right.Equipment.isBroken) { sectionData.DistanceRight += sectionData.Right.Equipment.Performance * sectionData.Right.Equipment.Speed; if (sectionData.DistanceRight >= 100) { if (valuePair.Key.SectionType == SectionTypes.Finish) { if (_rounds[sectionData.Right] > 0) { ParticipantFinished(sectionData, 1); } else { _rounds[sectionData.Right]++; SetParticipantNextSectionRight(vorigeSection, sectionData); } } else { SetParticipantNextSectionRight(vorigeSection, sectionData); } SaveTimeForSectionAndParticipant(valuePair.Key, currentDeelnemer, args); } } } vorigeSection = valuePair.Key; } CheckRaceFinish(); SetRandomBroken(args); SetUnBroken(); DriversChanged?.Invoke(this, new DriversChangedEventArgs() { Track = this.Track }); }
public void MoveDrivers(DateTime time) { var finishedDriversThisUpdate = new List <IParticipant>(); foreach (IParticipant p in Participants) { p.Moved = false; } foreach (Section section in Track.Sections.Reverse()) { var data = GetSectionData(section); Section previousSection = GetPreviousSection(section); var previousSectionData = GetSectionData(previousSection); if (data.LeftParticipant == null || data.RightParticipant == null) { if (previousSectionData.LeftParticipant != null && previousSectionData.LeftParticipant.Moved == false && previousSectionData.LeftParticipant.Equipment.IsBroken == false) { var participant = previousSectionData.LeftParticipant; int amountToMove = participant.Equipment.Speed * participant.Equipment.Performance; if (amountToMove + previousSectionData.DistanceLeft > Track.sectionLength) { PlaceParticipantCorrectly(section, data, participant, (previousSectionData.DistanceLeft + amountToMove) % Track.sectionLength); if (previousSectionData.RightParticipant != null) { previousSectionData.LeftParticipant = previousSectionData.RightParticipant; previousSectionData.DistanceLeft = previousSectionData.DistanceRight; previousSectionData.RightParticipant = null; previousSectionData.DistanceRight = 0; } else { previousSectionData.LeftParticipant = null; previousSectionData.DistanceLeft = 0; } if (IsFinishSection(section)) { participant.LapsInCurrentRace++; if (participant.LapsInCurrentRace > lapsToFinish) { finishedDriversThisUpdate.Add(participant); } } LogSectionTime(participant, time, previousSection); DriversChanged.Invoke(this, new DriversChangedEventArgs(Track)); } if (amountToMove + previousSectionData.DistanceLeft <= Track.sectionLength) { previousSectionData.DistanceLeft += amountToMove; } participant.Moved = true; } if (previousSectionData.RightParticipant != null && previousSectionData.RightParticipant.Moved == false && previousSectionData.RightParticipant.Equipment.IsBroken == false) { var participant = previousSectionData.RightParticipant; int amountToMove = participant.Equipment.Speed * participant.Equipment.Performance; if (amountToMove + previousSectionData.DistanceRight > Track.sectionLength) { PlaceParticipantCorrectly(section, data, participant, (previousSectionData.DistanceRight + amountToMove) % Track.sectionLength); previousSectionData.RightParticipant = null; previousSectionData.DistanceRight = 0; if (IsFinishSection(section)) { participant.LapsInCurrentRace++; if (participant.LapsInCurrentRace > lapsToFinish) { finishedDriversThisUpdate.Add(participant); } } LogSectionTime(participant, time, previousSection); DriversChanged.Invoke(this, new DriversChangedEventArgs(Track)); } else if (amountToMove + previousSectionData.DistanceRight <= Track.sectionLength) { previousSectionData.DistanceRight += amountToMove; if (previousSectionData.DistanceRight > previousSectionData.DistanceLeft) { Overtake(previousSectionData, false); } } participant.Moved = true; } } else if (data.LeftParticipant != null && data.RightParticipant != null) { if (previousSectionData.LeftParticipant != null && previousSectionData.LeftParticipant.Moved == false && previousSectionData.LeftParticipant.Equipment.IsBroken == false) { var participant = previousSectionData.LeftParticipant; int amountToMove = participant.Equipment.Speed * participant.Equipment.Performance; if (amountToMove + previousSectionData.DistanceLeft <= Track.sectionLength) { previousSectionData.DistanceLeft += amountToMove; } else { previousSectionData.DistanceRight = Track.sectionLength; } participant.Moved = true; } if (previousSectionData.RightParticipant != null && previousSectionData.RightParticipant.Moved == false && previousSectionData.RightParticipant.Equipment.IsBroken == false) { var participant = previousSectionData.RightParticipant; int amountToMove = participant.Equipment.Speed * participant.Equipment.Performance; if (amountToMove + previousSectionData.DistanceRight < Track.sectionLength) { previousSectionData.DistanceRight += amountToMove; if (previousSectionData.DistanceRight > previousSectionData.DistanceLeft) { Overtake(previousSectionData, true); } } else { previousSectionData.DistanceRight = Track.sectionLength - 1; } participant.Moved = true; } } RemoveFromRace(finishedDriversThisUpdate); } }