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

File diff suppressed because one or more lines are too long