Compare commits

...

5 Commits

Author SHA1 Message Date
Dawid Pietrykowski
d4a4bcbebe Added perpendicular spot generation 2022-10-29 21:36:32 +02:00
Dawid Pietrykowski
d7218e6352 WIP Perpendicular spot regeneration 2022-10-28 00:19:11 +02:00
Dawid Pietrykowski
8f8635efb8 Fixed reconfiguration of reserved spots 2022-10-27 23:45:00 +02:00
Dawid Pietrykowski
a9f6948b5c Added priority sorting to time-based reservations 2022-10-13 20:16:26 +02:00
Dawid Pietrykowski
75841d7a41 Time-based reservation improvements, bugfixes, added spacing between regenerated spots 2022-10-11 23:35:25 +02:00
6 changed files with 104 additions and 66 deletions

View File

@ -62,6 +62,7 @@ namespace Parking
public bool Reserved = false;
public int ReservedPriority = 0;
public Size Size;
public Driver OccupyingDriver;
public Spot(Size size, bool flipped)
{

View File

@ -141,7 +141,7 @@ namespace Parking
driver.Reserved = false;
}
float rightTime = 10.0f;//(float)_currentTime.TotalHours;
float rightTime = 12.0f;//(float)_currentTime.TotalHours;
float leftTime = 16.0f;//(float)(_currentTime + TimeSpan.FromHours(hours)).TotalHours;
TimeSpan maxArrivalTime = _currentTime + TimeSpan.FromHours(hours);
@ -152,24 +152,31 @@ namespace Parking
}
}
// nextDrivers.Sort(((a, b) => Comparer<TimeSpan>.Default.Compare(a.PlannedDeparture, b.PlannedDeparture)));
nextDrivers.Sort(((a, b) => Comparer<TimeSpan>.Default.Compare(a.PlannedArrival, b.PlannedArrival)));
nextDrivers.Sort(((a, b) => Comparer<int>.Default.Compare(a.Priority, b.Priority)));
//nextDrivers.Sort(((a, b) => Comparer<TimeSpan>.Default.Compare(a.RealDeparture, b.RealDeparture)));
//nextDrivers.Sort(((a, b) => Comparer<TimeSpan>.Default.Compare(a.PlannedArrival, b.PlannedArrival)));
List<Spot> availableSpots = new List<Spot>();
foreach (List<Spot> list in _spotMap)
foreach (Spot spot in list)
if (!spot.Reserved && spot.Free)
// if (!spot.Reserved && spot.Free)
availableSpots.Add(spot);
// availableSpots.Reverse();
// leftTime = (float)nextDrivers.Max(d => d.PlannedDeparture).TotalHours;
Driver FindOccupyingDriver(Spot spot)
{
return spot.OccupyingDriver;
}
for (int i = 0; i < nextDrivers.Count; i++) {
Driver driver = nextDrivers[i];
// float driverTime = (float)driver.PlannedDeparture.TotalHours;
float driverTime = (float)driver.RealDeparture.TotalHours;
float driverTime = (float)driver.PlannedDeparture.TotalHours;
//float driverTime = (float)driver.RealDeparture.TotalHours;
float timeProgress = (driverTime - rightTime) / (leftTime - rightTime);
float xPosition = Width / 2.0f - (Width * timeProgress);
availableSpots.Sort(((a, b) => Comparer<float>.Default.Compare(Mathf.Abs(xPosition - a.Position.x), Mathf.Abs(xPosition - b.Position.x))));
@ -177,7 +184,30 @@ namespace Parking
bool useBottomLane = true;
for (int j = 0; j < availableSpots.Count; j++) {
Spot spot = availableSpots[j];
if (driver.Size == spot.Size && !spot.Reserved && (spot.Lane != 0 || useBottomLane)) {
if (driver.Size == spot.Size && (spot.Lane != 0)) {
TimeSpan leaveTime = spot.Free ? TimeSpan.Zero : FindOccupyingDriver(spot).PlannedDeparture;
if (((!spot.Reserved && spot.Free)
|| (!spot.Free &&
leaveTime <
driver.PlannedArrival))) {
spot.Reserved = true;
driver.Reserved = true;
driver.ReservedSpot = spot;
break;
}
}
}
// if (Mathf.Abs(spot.Position.x - xPosition) > 20.0f) {
// if (useBottomLane)
// break;
// // useBottomLane = true;
// // j = -1;
// }
if(!driver.Reserved)
for (int j = 0; j < availableSpots.Count; j++) {
Spot spot = availableSpots[j];
if (driver.Size == spot.Size && !spot.Reserved) {
spot.Reserved = true;
driver.Reserved = true;
driver.ReservedSpot = spot;
@ -337,20 +367,20 @@ namespace Parking
private bool FindReservedSpot(Driver driver, out Spot spot)
{
if (driver.Reserved) {
if(!driver.ReservedSpot.Free)
Debug.Log("Spot should be free");
if(driver.ReservedSpot.GameObject == null || !driver.ReservedSpot.Free)
Debug.Log("Spot not viable anymore");
else {
spot = driver.ReservedSpot;
return true;
}
}
foreach (List<Spot> list in _spotMap)
foreach (Spot spot1 in list)
if (spot1.Reserved && spot1.ReservedPriority == driver.Priority && driver.Size == spot1.Size) {
spot = spot1;
return true;
}
// foreach (List<Spot> list in _spotMap)
// foreach (Spot spot1 in list)
// if (spot1.Reserved && spot1.ReservedPriority == driver.Priority && driver.Size == spot1.Size) {
// spot = spot1;
// return true;
// }
spot = new Spot();
@ -399,6 +429,7 @@ namespace Parking
}
spot.Free = false;
spot.Reserved = false;
spot.OccupyingDriver = driver;
driver.Spot = spot;
driver.Parked = true;
driver.GameObject = Instantiate(carPrefab);
@ -553,13 +584,14 @@ namespace Parking
}
// Try regeneration before returning false
return RegenerateSpots(newSize);
if (RegenerateSpots(newSize))
return true;
}
foreach (List<Spot> currentLaneSpots in _spotMap)
for (int i = 0; i < currentLaneSpots.Count; i++) {
Spot spot = currentLaneSpots[i];
if (!(spot.Free && spot.Size != Size.D)) // && !spot.Reserved))
if (!spot.Free || spot.Size == Size.D || spot.Reserved)
continue;
if (GetSpotCountOfSize(Size.A) <= 2 && spot.Size == Size.A)
continue;
@ -703,7 +735,8 @@ namespace Parking
}
if (spot.Lane == 0 && Width / 2.0f - newX < 5.5f + 0.5f + 2.25f) {
float distToRightWall = Width / 2.0f - newX + SpotHeights[(int)newSize] / 2.0f;
if (spot.Lane == 0 && distToRightWall < 5.5f && (distToRightWall != 0)) {
// Try to remove one spot next
continue;
}
@ -756,7 +789,7 @@ namespace Parking
Vector3 newPosition = Vector3.zero;
float lastRightBorder = -Width / 2.0f + 5.5f;
float spaceNeeded = SpotHeights[(int)size];
float spaceNeeded = SpotHeights[(int)size] + 1.0f;
@ -792,7 +825,8 @@ namespace Parking
if (leftBorder - lastRightBorder >= spaceNeeded) {
Spot newSpot = new Spot(size, false);
newPosition.x = (lastRightBorder + SpotHeights[(int)size] / 2.0f);
//newPosition.x = (lastRightBorder + SpotHeights[(int)size] / 2.0f);
newPosition.x = (lastRightBorder + 0.5f + SpotHeights[(int)size] / 2.0f);
newPosition.y = (-Height / 2.0f) + 2.25f / 2.0f;
newSpot.Perpendicular = false;
newSpot.GameObject = Instantiate(_spotPrefabs[(int)size],
@ -805,6 +839,35 @@ namespace Parking
lastRightBorder = _spotMap[0][i].RightBorder;
}
// return false;
float lastLeftBorder = Width / 2.0f;
spaceNeeded = 2.25f;
// Try for perpendicular
for (int i = 0; i < _spotMap[0].Count; i++) {
float rightBorder = _spotMap[0][i].RightBorder;
if (lastLeftBorder - rightBorder >= spaceNeeded) {
newPosition.x = (lastLeftBorder - (2.25f / 2.0f));
if(newPosition.x < Width / 2.0f - 2.25f && newPosition.x > Width / 2.0f - 5.5f - 2.25f)
continue;
newPosition.y = (-Height / 2.0f) + SpotHeights[(int)(size)] / 2.0f;
float nextBorder = GetNextBorderVectical(newPosition, 0, true, size, true);
float spotBorder = -Height / 2.0f + SpotHeights[(int) size];
if(nextBorder - spotBorder < 5.5f)
continue;
Spot newSpot = new Spot(size, false);
//newPosition.x = (lastRightBorder + SpotHeights[(int)size] / 2.0f);
newSpot.Perpendicular = true;
newSpot.GameObject = Instantiate(_spotPrefabs[(int)size],
// TODO: Account for order
newPosition, Quaternion.Euler(0, 0, 0), mainPlanContainer);
_spotMap[0].Add(newSpot);
_spotMap[0].Sort((a, b) => Comparer<float>.Default.Compare(b.Position.x, a.Position.x));
return true;
}
lastLeftBorder = _spotMap[0][i].LeftBorder;
}
return false;
// newPosition.y = bottomBorder + SpotHeights[(int) newSize] / 2.0f;
@ -861,8 +924,8 @@ namespace Parking
private float GetNextBorderVectical(Vector3 position, int lane, bool perpendicular, Size size, bool up)
{
float minX = position.x - (perpendicular ? 2.25f : SpotHeights[(int) size]);
float maxX = position.x + (perpendicular ? 2.25f : SpotHeights[(int) size]);
float minX = position.x - (perpendicular ? 2.25f : SpotHeights[(int) size]) - 2.25f/2.0f;
float maxX = position.x + (perpendicular ? 2.25f : SpotHeights[(int) size]) + 2.25f/2.0f;
List<float> allBorders = new();
List<Spot> targetLane = up ? _spotMap[lane + 1] : _spotMap[lane - 1];

File diff suppressed because one or more lines are too long

View File

@ -2,7 +2,7 @@
"dependencies": {
"com.unity.collab-proxy": "1.17.2",
"com.unity.feature.2d": "1.0.0",
"com.unity.ide.rider": "3.0.15",
"com.unity.ide.rider": "3.0.16",
"com.unity.ide.visualstudio": "2.0.16",
"com.unity.ide.vscode": "1.2.5",
"com.unity.test-framework": "1.1.31",

View File

@ -1,11 +1,11 @@
{
"dependencies": {
"com.unity.2d.animation": {
"version": "7.0.6",
"version": "7.0.8",
"depth": 1,
"source": "registry",
"dependencies": {
"com.unity.2d.common": "6.0.3",
"com.unity.2d.common": "6.0.5",
"com.unity.2d.sprite": "1.0.0",
"com.unity.modules.animation": "1.0.0",
"com.unity.modules.uielements": "1.0.0"
@ -13,7 +13,7 @@
"url": "https://packages.unity.com"
},
"com.unity.2d.common": {
"version": "6.0.3",
"version": "6.0.5",
"depth": 2,
"source": "registry",
"dependencies": {
@ -39,12 +39,12 @@
"url": "https://packages.unity.com"
},
"com.unity.2d.psdimporter": {
"version": "6.0.4",
"version": "6.0.6",
"depth": 1,
"source": "registry",
"dependencies": {
"com.unity.2d.animation": "7.0.6",
"com.unity.2d.common": "6.0.3",
"com.unity.2d.animation": "7.0.8",
"com.unity.2d.common": "6.0.5",
"com.unity.2d.sprite": "1.0.0"
},
"url": "https://packages.unity.com"
@ -56,12 +56,12 @@
"dependencies": {}
},
"com.unity.2d.spriteshape": {
"version": "7.0.5",
"version": "7.0.6",
"depth": 1,
"source": "registry",
"dependencies": {
"com.unity.mathematics": "1.1.0",
"com.unity.2d.common": "6.0.3",
"com.unity.2d.common": "6.0.4",
"com.unity.2d.path": "5.0.2",
"com.unity.modules.physics2d": "1.0.0"
},
@ -115,17 +115,17 @@
"depth": 0,
"source": "builtin",
"dependencies": {
"com.unity.2d.animation": "7.0.6",
"com.unity.2d.animation": "7.0.8",
"com.unity.2d.pixel-perfect": "5.0.1",
"com.unity.2d.psdimporter": "6.0.4",
"com.unity.2d.psdimporter": "6.0.6",
"com.unity.2d.sprite": "1.0.0",
"com.unity.2d.spriteshape": "7.0.5",
"com.unity.2d.spriteshape": "7.0.6",
"com.unity.2d.tilemap": "1.0.0",
"com.unity.2d.tilemap.extras": "2.2.3"
}
},
"com.unity.ide.rider": {
"version": "3.0.15",
"version": "3.0.16",
"depth": 0,
"source": "registry",
"dependencies": {
@ -164,7 +164,7 @@
"url": "https://packages.unity.com"
},
"com.unity.services.core": {
"version": "1.4.2",
"version": "1.4.3",
"depth": 1,
"source": "registry",
"dependencies": {

View File

@ -1,2 +1,2 @@
m_EditorVersion: 2021.3.9f1
m_EditorVersionWithRevision: 2021.3.9f1 (ad3870b89536)
m_EditorVersion: 2021.3.12f1
m_EditorVersionWithRevision: 2021.3.12f1 (8af3c3e441b1)