-
Notifications
You must be signed in to change notification settings - Fork 0
/
Path.cs
199 lines (189 loc) · 5.95 KB
/
Path.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public enum BlockType {
Blocked = 0,
Priority = 1,
Open = 2
};
public enum HidePath {
Shown = 0,
Internal = 1,
Hiden = 2
};
/// QPathFinder modified
/// <summary>
/// Path is a connection between 2 Nodes.
/// </summary>
///
[System.Serializable]
public class Path {
/// <summary>
/// Stores first node of path
/// </summary>
[SerializeField] Node a;
/// <summary>
/// Stores second node of path
/// </summary>
[SerializeField] Node b;
/// <summary>
/// Stores cost of traveling this path
/// </summary>
[SerializeField] float cost;
/// <summary>
/// Keeps a list of the times the vehicle entered the track
/// </summary>
[SerializeField] List<float> queueTimes;
/// <summary>
/// Counts vehicles that leave this path
/// </summary>
public int leftQueue = 0;
/// <summary>
/// Counts vehicles that enter this path
/// </summary>
public int entireQueue = 0;
/// <summary>
/// Stores maximum number of vehicles that can stay on path in same time
/// </summary>
public int maxInQueue;
/// <summary>
/// Store ID
/// </summary>
public int autoGeneratedID;
/// <summary>
/// Marks type of path priority
/// </summary>
public BlockType priority;
/// <summary>
/// Marks path as open, open for prioritized, blocked
/// </summary>
public BlockType block = BlockType.Open;
/// <summary>
/// Marks type of hiden path
/// </summary>
[SerializeField] public HidePath hide;
/// <summary>
/// Store references to the street, if it belongs to any
/// </summary>
[SerializeField] public Street street;
/// <summary>
/// Stores references to a Transform component, if it has one
/// </summary>
[HideInInspector] public Transform transform;
/// <summary>
/// Returns the identifier of the first node
/// </summary>
public int IDOfA {
get { return a == null ? -1 : a.ID; }
}
/// <summary>
/// Zwraca identyfikator drugiego wierzchołka
/// </summary>
public int IDOfB {
get { return b == null ? -1 : b.ID; }
}
/// <summary>
/// Returns the identifier of the second node
/// </summary>
public Vector3 PosOfA {
get { return a == null ? Vector3.zero : a.position; }
}
/// <summary>
/// Returns the position of the second node
/// </summary>
public Vector3 PosOfB {
get { return b == null ? Vector3.zero : b.position; }
}
/// <summary>
/// Returns current amount of vehicles i queue
/// </summary>
public int CurrentQueue {
get { return entireQueue - leftQueue; }
}
/// <summary>
/// Returns current cost of travel this ptah
/// </summary>
public float Cost {
get { return CurrentQueue + cost; }
set { cost = value; }
}
/// <summary>
/// Returns sum of times that vehicles wait in queue
/// </summary>
public float SumaryWaitingTime {
get {
float sum = 0;
foreach (float f in queueTimes) {
sum += Time.time - f;
}
return sum;
}
}
/// <summary>
/// Construktor
/// </summary>
/// <param name="A">First node</param>
/// <param name="B">Second node</param>
/// <param name="Parent">Paretn of object</param>
/// <param name="Hide">Hide mark</param>
/// <param name="Prioritet">Priority mark</param>
public Path(Node A, Node B, Transform Parent, HidePath Hide = HidePath.Shown, BlockType Prioritet = BlockType.Open) {
queueTimes = new List<float>();
hide = Hide;
street = Parent.GetComponent<Street>();
priority = Prioritet;
a = A; b = B;
if (this.hide < HidePath.Hiden) {
var go = GameObject.CreatePrimitive(PrimitiveType.Cube);
go.GetComponent<BoxCollider>().size = new Vector3(1.4f, 1f, 1f);
transform = go.transform;
transform.parent = Parent;
go.GetComponent<Renderer>().material = Resources.Load("street.mat", typeof(Material)) as Material;
// go.GetComponent<Renderer>().material = (Material)AssetDatabase.LoadAssetAtPath("Assets/QPathSimulation/Materials/street.mat", typeof(Material));
}
Visualize();
}
/// <summary>
/// The method is responsible for the visual representation of the object
/// </summary>
public void Visualize() {
cost = hide == HidePath.Shown ? Vector3.Distance(PosOfA, PosOfB) : 0;
maxInQueue = (int)Mathf.Floor(Vector3.Distance(PosOfA, PosOfB) - 0.5f);
maxInQueue = maxInQueue < 1 ? 1 : maxInQueue;
maxInQueue = hide != HidePath.Shown && maxInQueue > 2 ? 2 : maxInQueue;
maxInQueue = 100;
if (transform != null) {
transform.position = (PosOfA + PosOfB) / 2f;
transform.LookAt(PosOfB);
transform.localScale = new Vector3(0.6f, 0.1f, Vector3.Distance(transform.position, PosOfB) * 2);
}
}
/// <summary>
/// Answers that vehicle can or can not enter to this path
/// </summary>
/// <param name="Priority">Priority of path of asking vehicle</param>
/// <returns>Returns true if vehicle can enter</returns>
public bool CanEnter(BlockType Priority) {
if (block >= Priority && maxInQueue > entireQueue - leftQueue) {
return true;
}
return false;
}
/// <summary>
/// The method is responsible for adding vehicle to queue
/// </summary>
/// <returns>Returns number of vehicles in queue</returns>
public int EnterQueue() {
entireQueue++;
queueTimes.Add(Time.time);
return entireQueue - 1;
}
/// <summary>
/// The method is responsible for delete vehicle to queue
/// </summary>
public void LeaveQueue() {
queueTimes.RemoveAt(0);
++leftQueue;
}
}