From 549691cc0c831c25899147490dd4053789b7bb41 Mon Sep 17 00:00:00 2001 From: Dawid Pietrykowski Date: Thu, 6 Oct 2022 21:52:35 +0200 Subject: [PATCH] Added spot regeneration --- Assets/Scripts/Driver.cs | 8 +++- Assets/Scripts/ParkingManager.cs | 80 ++++++++++++++++++++++++++------ 2 files changed, 73 insertions(+), 15 deletions(-) diff --git a/Assets/Scripts/Driver.cs b/Assets/Scripts/Driver.cs index 509c26a..fd1231a 100644 --- a/Assets/Scripts/Driver.cs +++ b/Assets/Scripts/Driver.cs @@ -63,8 +63,14 @@ namespace Parking } + public bool Vertical => Perpendicular; + 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 { diff --git a/Assets/Scripts/ParkingManager.cs b/Assets/Scripts/ParkingManager.cs index 302cb1e..9e4a40f 100644 --- a/Assets/Scripts/ParkingManager.cs +++ b/Assets/Scripts/ParkingManager.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using TMPro; using UnityEngine; +using UnityEngine.Assertions; using UnityEngine.UI; namespace Parking @@ -413,6 +414,7 @@ namespace Parking lowerSpot.LastReconfiguration = _currentTime; higherSpot.LastReconfiguration = _currentTime; + return true; } @@ -421,7 +423,8 @@ namespace Parking continue; } - return false; + // Try regeneration before returning false + return RegenerateSpots(newSize); } // TODO: Add new spot generation (lower lane) @@ -618,7 +621,68 @@ namespace Parking 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.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 GetConflictingSpotsLower(Vector3 position, int lane, bool perpendicular, Size size, float margin = 0) { @@ -634,21 +698,9 @@ namespace Parking Vector3 targetSpotPosition = spot1.GameObject.transform.position; if (targetSpotPosition.x < minX || targetSpotPosition.x > maxX) 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); } - // 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; }