Added spot merge, fixed horizontal border bug

This commit is contained in:
Dawid Pietrykowski 2022-09-10 22:34:20 +02:00
parent 729a7107eb
commit 4a36800cde
2 changed files with 108 additions and 70 deletions

View File

@ -338,7 +338,8 @@ namespace Parking
private bool TryReconfigureSpotForSize(Size newSize)
{
foreach (List<Spot> currentLaneSpots in _spotMap)
foreach (Spot spot in currentLaneSpots) {
for (int i = 0; i < currentLaneSpots.Count; i++) {
Spot spot = currentLaneSpots[i];
if (!(spot.Free && spot.Size != Size.D)) // && !spot.Reserved))
continue;
if (GetSpotCountOfSize(Size.A) <= 2 && spot.Size == Size.A)
@ -401,23 +402,37 @@ namespace Parking
continue;
}
else {
float nextBorderLeft = GetNextBorderHorizontal(newPosition, spot.Lane, spot.Perpendicular, newSize,
!spot.AlignToTop);
float nextBorderRight = GetNextBorderHorizontal(newPosition, spot.Lane, spot.Perpendicular,
newSize,
!spot.AlignToTop);
if (nextBorderRight - nextBorderLeft < _spotHeights[(int) newSize]) {
float nextBorderLeft = GetNextBorderHorizontal(position, spot.Lane, spot.Perpendicular,
spot.Size,
false);
float nextBorderRight = GetNextBorderHorizontal(position, spot.Lane, spot.Perpendicular,
spot.Size,
true);
if (nextBorderRight - nextBorderLeft < _spotHeights[(int) newSize] + 1f) {
// Try perpendicular
float bottomBorder = newPosition.y - 2.25f / 2.0f;
float topBorder = newPosition.y + 2.25f / 2.0f;
bool foundByRemoval = false;
if (spot.AlignToTop) {
float newBottomBorder = topBorder - _spotHeights[size];
float newTopBorder = topBorder;
float nextBorder = GetNextBorderVectical(newPosition, spot.Lane, spot.Perpendicular,
newSize,
!spot.AlignToTop);
if (Math.Abs(nextBorder - newBottomBorder) < 5.5f)
continue;
if (Math.Abs(nextBorder - newBottomBorder) < 5.5f) {
if (i < currentLaneSpots.Count - 1) {
Spot nextSpot = currentLaneSpots[i + 1];
if (nextSpot.Free && !nextSpot.Reserved) {
Destroy(nextSpot.GameObject);
currentLaneSpots.RemoveAt(i + 1);
i--;
newPosition.x = nextBorderRight - _spotHeights[(int) newSize] - 0.5f;
newPosition.y = position.y;
foundByRemoval = true;
}else
continue;
}
}
}
else {
float newBottomBorder = bottomBorder;
@ -425,34 +440,72 @@ namespace Parking
float nextBorder = GetNextBorderVectical(newPosition, spot.Lane, spot.Perpendicular,
newSize,
!spot.AlignToTop);
if (Math.Abs(newTopBorder - nextBorder) < 5.5f)
if (Math.Abs(newTopBorder - nextBorder) < 5.5f) {
if (i < currentLaneSpots.Count - 1) {
Spot nextSpot = currentLaneSpots[i + 1];
if (nextSpot.Free && !nextSpot.Reserved) {
Destroy(nextSpot.GameObject);
currentLaneSpots.RemoveAt(i + 1);
i--;
newPosition.x = nextBorderRight - _spotHeights[(int) newSize] - 0.5f;
newPosition.y = position.y;
foundByRemoval = true;
}else
continue;
}
}
}
if (!foundByRemoval) {
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;
}
else {
newPosition.x = newX;
spot.Flipped = currentLaneSpots.FindIndex(spotPredicate => spotPredicate == spot) %
2 == 0;
rotation *= Quaternion.Euler(0, 0, spot.Flipped ? 90 : -90);
spot.Perpendicular = true;
}
}
}
else {
newPosition.x = nextBorderRight - _spotHeights[(int) newSize] / 2.0f - 0.5f;
if (i == 0)
newPosition.x += 0.5f;
newPosition.y = position.y;
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)
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;
// if (i < currentLaneSpots.Count - 1) {
// Spot nextSpot = currentLaneSpots[i + 1];
// if (nextSpot.Free && !nextSpot.Reserved) {
// Destroy(nextSpot.GameObject);
// currentLaneSpots.RemoveAt(i + 1);
// i--;
// newPosition.x = nextBorderRight - _spotHeights[(int) newSize] - 0.5f;
// newPosition.y = position.y;
// continue;
// }
// }
// newPosition.x = nextBorderRight - _spotHeights[(int) newSize] - 0.5f;
}
}
}
@ -519,13 +572,24 @@ namespace Parking
}
allBorders.Sort();
foreach (float adjacentBorder in allBorders)
if (adjacentBorder > position.x)
return adjacentBorder;
if (right) {
foreach (float adjacentBorder in allBorders)
if (adjacentBorder > position.x)
return adjacentBorder;
}
else {
allBorders.Reverse();
foreach (float adjacentBorder in allBorders)
if (adjacentBorder < position.x)
return adjacentBorder;
}
if (perpendicular)
return position.x + (right ? 1 : -1) * _spotHeights[(int) size] / 2.0f;
return (right ? 1 : -1) * Width / 2.0f;
if (right)
return Width / 2.0f;
else
return -Width / 2.0f + 5.5f;
}
@ -662,12 +726,12 @@ namespace Parking
currentX += (i != 0 ? 1 : -1) *
(spotMap[i][j].Perpendicular
? 2.25f / 2.0f
: spotSizes[(int) spotMap[i][j].Size] / 2.0f + (j != 0 ? 0.25f : 0));
: spotSizes[(int) spotMap[i][j].Size] / 2.0f + 0.25f);
else
currentX += (i != 0 ? 1 : -1) *
(spotMap[i][j].Perpendicular
? 2.5f / 2.0f
: spotSizes[(int) spotMap[i][j].Size] / 2.0f + (j != 0 ? 0.25f : 0));
: spotSizes[(int) spotMap[i][j].Size] / 2.0f + 0.25f);
flipped = !flipped;
}

File diff suppressed because one or more lines are too long