Added spot regeneration

This commit is contained in:
Dawid Pietrykowski 2022-10-06 21:52:35 +02:00
parent 1800ff6f64
commit 549691cc0c
2 changed files with 73 additions and 15 deletions

View File

@ -63,8 +63,14 @@ namespace Parking
} }
public bool Vertical => Perpendicular;
public float LowerBorder => GameObject.transform.position.y - public float LowerBorder => GameObject.transform.position.y -
(Perpendicular ? 2.25f / 2.0f : ParkingManager.SpotHeights[(int) Size] / 2.0f); (!Vertical ? 2.25f / 2.0f : ParkingManager.SpotHeights[(int) Size] / 2.0f);
public float RightBorder => GameObject.transform.position.x +
(Vertical ? 2.25f / 2.0f : ParkingManager.SpotHeights[(int) Size] / 2.0f);
public float LeftBorder => GameObject.transform.position.x -
(Vertical ? 2.25f / 2.0f : ParkingManager.SpotHeights[(int) Size] / 2.0f);
public Vector3 Position public Vector3 Position
{ {

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using TMPro; using TMPro;
using UnityEngine; using UnityEngine;
using UnityEngine.Assertions;
using UnityEngine.UI; using UnityEngine.UI;
namespace Parking namespace Parking
@ -413,6 +414,7 @@ namespace Parking
lowerSpot.LastReconfiguration = _currentTime; lowerSpot.LastReconfiguration = _currentTime;
higherSpot.LastReconfiguration = _currentTime; higherSpot.LastReconfiguration = _currentTime;
return true; return true;
} }
@ -421,7 +423,8 @@ namespace Parking
continue; continue;
} }
return false; // Try regeneration before returning false
return RegenerateSpots(newSize);
} }
// TODO: Add new spot generation (lower lane) // TODO: Add new spot generation (lower lane)
@ -619,6 +622,67 @@ namespace Parking
return false; return false;
} }
private bool RegenerateSpots(Size size)
{
Vector3 newPosition = Vector3.zero;
float lastRightBorder = -Width / 2.0f + 5.5f;
float spaceNeeded = SpotHeights[(int)size];
// var expectedList = _spotMap[0].OrderByDescending(x => x.Position.x);
for (int i = _spotMap[0].Count - 1; i >= 0; i--) {
Spot currentSpot = _spotMap[0][i];
if (currentSpot.LeftBorder - lastRightBorder >= spaceNeeded) {
Spot newSpot = new Spot(size, false);
newPosition.x = (lastRightBorder + SpotHeights[(int)size] / 2.0f);
newPosition.y = (-Height / 2.0f) + 2.25f / 2.0f;
newSpot.Perpendicular = false;
newSpot.GameObject = Instantiate(_spotPrefabs[(int)size],
newPosition, Quaternion.Euler(0, 0, 90), mainPlanContainer);
_spotMap[0].Add(newSpot);
_spotMap[0].Sort((a, b) =>
{
return Comparer<float>.Default.Compare(b.Position.x, a.Position.x);
});
return true;
}
lastRightBorder = currentSpot.RightBorder;
}
return false;
// newPosition.y = bottomBorder + SpotHeights[(int) newSize] / 2.0f;
// float newX = nextBorderRight;
// if (spot.Lane == 0) {
// float xRelativeToRight = -(newX - Width / 2.0f);
// xRelativeToRight -= (xRelativeToRight + 2.25f / 2.0f) % 2.25f;
// xRelativeToRight += 2.25f;
// xRelativeToRight = Math.Max(xRelativeToRight, 2.25f / 2.0f);
// newX = -xRelativeToRight + Width / 2.0f;
// }
// else {
// float xRelativeToLeft = newX + Width / 2.0f;
// xRelativeToLeft -= (xRelativeToLeft + 2.25f / 2.0f) % 2.25f;
// newX = xRelativeToLeft - Width / 2.0f;
// }
//
//
// if (spot.Lane == 0 && Width / 2.0f - newX < 5.5f + 0.5f + 2.25f) {
// // Try to remove one spot next
// continue;
// }
//
// newPosition.x = newX;
// spot.Flipped =
// currentLaneSpots.FindIndex(spotPredicate => spotPredicate == spot) %
// 2 == 0;
// rotation *= Quaternion.Euler(0, 0, spot.Flipped ? 90 : -90);
// spot.Perpendicular = true;
}
private List<int> GetConflictingSpotsLower(Vector3 position, int lane, bool perpendicular, Size size, float margin = 0) private List<int> GetConflictingSpotsLower(Vector3 position, int lane, bool perpendicular, Size size, float margin = 0)
{ {
@ -634,21 +698,9 @@ namespace Parking
Vector3 targetSpotPosition = spot1.GameObject.transform.position; Vector3 targetSpotPosition = spot1.GameObject.transform.position;
if (targetSpotPosition.x < minX || targetSpotPosition.x > maxX) if (targetSpotPosition.x < minX || targetSpotPosition.x > maxX)
continue; continue;
float sizeOffset;
if (spot1.Perpendicular)
sizeOffset = SpotHeights[(int) spot1.Size] / 2.0f;
else
sizeOffset = 2.25f / 2.0f;
// allBorders.Add(targetSpotPosition.y + sizeOffset);
conflictingSpotsIds.Add(i); conflictingSpotsIds.Add(i);
} }
// if (12.85 - 2.5 / 2.0f <= maxX && lane == 2 && up)
// allBorders.Add(Height / 2.0f - 5);
// if (allBorders.Count == 0)
// return -Height / 2.0f;
// float nextBorder = up ? allBorders.Min() : allBorders.Max();
return conflictingSpotsIds; return conflictingSpotsIds;
} }