-
Notifications
You must be signed in to change notification settings - Fork 0
/
Road.cs
173 lines (145 loc) · 5.8 KB
/
Road.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using static Crossroad.RoadSizes;
using System.Collections.Generic;
using System.Windows.Media;
using System.Diagnostics;
namespace Crossroad
{
public class Road
{
private Road() { }
private static Road _road;
public static Road GetRoad()
{
if (_road == null)
{
_road = new Road();
}
return _road;
}
public Canvas[] LanesSet { set; get; } = new Canvas[ROADS_COUNT];
public Crosswalk[] CrosswalkSet { set; get; } = new Crosswalk[ROADS_COUNT];
public TrafficLight[] LightsSet { set; get; } = new TrafficLight[ROADS_COUNT];
public ObservableCollection<Car> Cars { set; get; } = new();
public Axes Axis { set; get; } = Axes.Horizontal;
public int Lane { set; get; } = 1;
public double LaneWidth { set; get; } = GEWAY_ONE_LANE_WIDTH;
public bool GenerationStarted { set; get; } = false;
public bool Reset { set; get; } = false;
public double CarPeriod { set; get; } = 0;
public double PedestrianPeriod { set; get; } = 0;
public Timer GoTimer { set; get; } = new();
public Timer CarTimer { set; get; } = new();
public Timer PedestrianTimer { set; get; } = new();
public void Go()
{
GoTimer.Interval = UPDATE_TIME;
if(!Reset)
GoTimer.Elapsed += (s, e) =>
{
Application.Current.Dispatcher.BeginInvoke(() =>
{
var workPedestrians = CrosswalkSet
.Where(cs => cs.LocAxis == Axis);
foreach (var p in workPedestrians)
if (p.Pedestrians.Count != 0) p.Go();
var workCars = Cars
.Where(c =>
((int)c.RoadPart % 2 == (int)Axis)
&& (c.QueueIndex == 0))
.ToList();
foreach (var c in workCars)
c.PrepareToMove();
});
};
GoTimer.Start();
}
public void UpdateCars()
{
Car.EndPoint = new uint[ROADS_COUNT, MAX_LANE_COUNT];
foreach (Car car in Cars)
{
LanesSet[(int)car.RoadPart].Children.Remove(car.View);
car.InDelay = false;
car.InLane = 0;
car.QueueIndex = 0;
car.Offset = 0;
car.TransformGroup.Children.Clear();
car.LocateOnRoad();
}
}
public RoadParts GetOppositeRoad(int r)
{
return (RoadParts)((r + 2) % ROADS_COUNT);
}
public RoadParts GetFutureRoad(int d, int r)
{
return (RoadParts)((d + r + 1) % ROADS_COUNT);
}
public void GenerateTraffic()
{
var random = new Random();
var car = new Car((RoadParts)random.Next(ROADS_COUNT),
(Directions)random.Next(DIRECTIONS_COUNT));
Cars.Add(car);
CarTimer.Interval = CarPeriod;
if (CarTimer.Enabled)
{
CarTimer.Stop();
PedestrianTimer.Stop();
}
if (!GenerationStarted)
CarTimer.Elapsed += (s,e) =>
{
Application.Current.Dispatcher.Invoke(() =>
{
var activeCars = Car.InMovement.Where(c =>
c.Direction == Directions.OnLeft);
var availableRoads = new List<RoadParts>()
{ RoadParts.South, RoadParts.West, RoadParts.North, RoadParts.East };
var r = random.Next(availableRoads.Count);
var car = new Car(availableRoads[r],
(Directions)random.Next(DIRECTIONS_COUNT));
Cars.Add(car);
});
};
CarTimer.Start();
PedestrianTimer.Interval = PedestrianPeriod;
if(!GenerationStarted)
PedestrianTimer.Elapsed += (s, e) =>
{
Application.Current.Dispatcher.Invoke(() =>
{
var workPedestrians = CrosswalkSet
.SelectMany(cs => cs.Pedestrians)
.ToList();
var availableRoads = Enumerable.Range(0, ROADS_COUNT).ToList();
foreach (var road in CrosswalkSet)
{
if (workPedestrians
.Where(p => p.RoadPart == road.RoadPart)
.Count() == PEDESTRIAN_DIRECTIONS_COUNT)
availableRoads.Remove((int)road.RoadPart);
}
if (availableRoads.Count != 0)
{
var r = random.Next(availableRoads.Count);
var availableDirections = new List<PedestrianDirections>()
{ PedestrianDirections.Forward, PedestrianDirections.Backward };
var pedestrian = workPedestrians.FirstOrDefault(p => (int)p.RoadPart == availableRoads[r]);
if (pedestrian is not null)
availableDirections.Remove(pedestrian.Direction);
var d = random.Next(availableDirections.Count);
CrosswalkSet[availableRoads[r]].AddPedestrian(availableDirections[d]);
}
});
};
PedestrianTimer.Start();
}
}
}