From 7571ca1d5d2f4779a2118356ffb60d2d2407ff13 Mon Sep 17 00:00:00 2001 From: Dawid Pietrykowski Date: Mon, 5 Sep 2022 23:31:33 +0200 Subject: [PATCH] Added parallel spots --- .../Scripts/InitialConfigurationGenerator.cs | 26 ++++-- Assets/Scripts/ParkingManager.cs | 87 +++++++++++-------- 2 files changed, 73 insertions(+), 40 deletions(-) diff --git a/Assets/Scripts/InitialConfigurationGenerator.cs b/Assets/Scripts/InitialConfigurationGenerator.cs index 1896dc1..4644f38 100644 --- a/Assets/Scripts/InitialConfigurationGenerator.cs +++ b/Assets/Scripts/InitialConfigurationGenerator.cs @@ -15,7 +15,7 @@ namespace Parking { PreProcessCombinations(out int[] spotCountsPerpendicular, out int[,] spotCountsParallel); - int[,] spotsCreated = new int[4, 4]; + int[,] spotsCreated = new int[4, 8]; int maxCount = TestCombinations(spotCountsPerpendicular, spotCountsParallel, spotsCreated); @@ -24,7 +24,7 @@ namespace Parking int[] counts = {0, 0, 0, 0}; for (int i = 0; i < spotsCreated.GetLength(0); i++) for (int j = 0; j < spotsCreated.GetLength(1); j++) - counts[j] += spotsCreated[i, j]; + counts[j % 4] += spotsCreated[i, j]; string countsString = $"Małe: {counts[0]} Średnie: {counts[1]} Duże: {counts[2]} " + $"Suma: {counts.Sum() + 1}"; @@ -70,7 +70,7 @@ namespace Parking private int TryFillCombination(int[] sizeIds, int[] spotCountsPerpendicularRef, int[,] spotCountsParallelRef, int[] counts, int[,] spotsCreated, bool copyToArray = false) { - int[,] spotsCreatedTemp = new int[4, 4]; + int[,] spotsCreatedTemp = new int[4, 8]; for (int i = 0; i < spotsCreatedTemp.GetLength(0); i++) for (int j = 0; j < spotsCreatedTemp.GetLength(1); j++) @@ -84,6 +84,12 @@ namespace Parking for (int j = 0; j < spotCountsParallelRef.GetLength(1); j++) spotCountsParallel[i, j] = spotCountsParallelRef[i, j]; + float[] parallelLengthAvailable = new[] + { + ParkingManager.Width - 5.5f, ParkingManager.Width - 5.5f, + ParkingManager.Width - 5.5f, 0 + }; + float[] spotSizes = {4, 4.5f, 5}; foreach (Driver driver in DataImporter.Drivers) { var laneIds = new List {0, 1, 2, 3}; @@ -92,13 +98,21 @@ namespace Parking for (int i = 0; i < 4; i++) { int laneId = laneIds[i]; - bool freeSpotsAvailable = spotCountsPerpendicular[laneId] != 0; - bool carFits = AvailableSizesCombinations[sizeIds[laneId]] >= spotSizes[(int) driver.Size]; - if (carFits && freeSpotsAvailable) { + bool freePerpendicularSpotsAvailable = spotCountsPerpendicular[laneId] != 0; + bool freeParallelSpotsAvailable = parallelLengthAvailable[laneId] >= 4; + bool carFitsPerpendicular = AvailableSizesCombinations[sizeIds[laneId]] >= spotSizes[(int) driver.Size]; + bool carFitsParallel = (sizeIds[laneId] == 1) && parallelLengthAvailable[laneId] >= spotSizes[(int) driver.Size]; + if (carFitsPerpendicular && freePerpendicularSpotsAvailable) { spotCountsPerpendicular[laneId]--; spotsCreatedTemp[laneId, (int) driver.Size]++; break; } + else if(carFitsParallel && freeParallelSpotsAvailable){ + parallelLengthAvailable[laneId] -= spotSizes[(int) driver.Size]; + spotCountsPerpendicular[laneId] = 0; + spotsCreatedTemp[laneId, (int) driver.Size + 4]++; + break; + } } } diff --git a/Assets/Scripts/ParkingManager.cs b/Assets/Scripts/ParkingManager.cs index a1b706b..3fd14bf 100644 --- a/Assets/Scripts/ParkingManager.cs +++ b/Assets/Scripts/ParkingManager.cs @@ -227,14 +227,15 @@ namespace Parking spot.Reserved = false; driver.Spot = spot; driver.Parked = true; - driver.GameObject = Instantiate(carPrefab, spot.GameObject.transform, true); + driver.GameObject = Instantiate(carPrefab); driver.GameObject.GetComponentInChildren().text = driver.Number.ToString(); driver.GameObject.GetComponentInChildren().transform.rotation = - Quaternion.Euler(new Vector3(0, 0, spot.Flipped ? 180 : 0)); - driver.GameObject.transform.position = spot.GameObject.transform.position; - driver.GameObject.transform.localPosition += new Vector3(0, 0, -1); + Quaternion.Euler(new Vector3(0, 0, (spot.Flipped ? 180 : 0) + (!spot.Perpendicular ? 90 : 0))); driver.GameObject.transform.rotation = - Quaternion.Euler(new Vector3(0, 0, spot.Flipped ? 180 : 0)); + Quaternion.Euler(new Vector3(0, 0, (spot.Flipped ? 180 : 0) + (!spot.Perpendicular ? 90 : 0))); + driver.GameObject.transform.position = spot.GameObject.transform.position; + driver.GameObject.transform.SetParent(spot.GameObject.transform, true); + driver.GameObject.transform.localPosition += new Vector3(0, 0, -1); } public void UpdateText(string text) @@ -281,9 +282,18 @@ namespace Parking Destroy(spot.GameObject); } + spot.Reserved = false; + + Vector3 offset; + if (spot.Perpendicular) + offset = new Vector3(0, diff, 0); + else + offset = new Vector3(-diff, 0, 0); + + // TODO: check how much space to right to push spot.GameObject = Instantiate(prefabs[size], - position + new Vector3(0, diff, 0), rotation, mainPlanContainer); + position + offset, rotation, mainPlanContainer); goto whileEnd; } @@ -329,13 +339,14 @@ namespace Parking float maxP2 = 0; foreach (Spot spot in spotMap[2]) if (spot.Size != Size.D) - maxP3 = Math.Max(maxP3, _spotHeights[(int) spot.Size]); + maxP3 = Math.Max(maxP3, spot.Perpendicular ? _spotHeights[(int) spot.Size] : 2.25f); foreach (Spot spot in spotMap[1]) if (spot.Size != Size.D) - maxP2 = Math.Max(maxP2, _spotHeights[(int) spot.Size]); + maxP2 = Math.Max(maxP2, spot.Perpendicular ? _spotHeights[(int) spot.Size] : 2.25f); // float maxP3 = spotMap[2].Count == 0 ? 0 : _spotHeights[(int) spotMap[2].Max().Size]; // float maxP2 = spotMap[1].Count == 0 ? 0 : _spotHeights[(int) spotMap[1].Max().Size]; + var prefabs = new List {spotPrefabA, spotPrefabB, spotPrefabC, spotPrefabD}; for (int i = 0; i < 4; i++) { float currentY; @@ -360,45 +371,53 @@ namespace Parking float currentX; if (i != 0) currentX = -Width / 2f - 2.25f / 2f + 2.25f; - else currentX = -Width / 2f + 5.5f - 2.25f / 2f + 2.25f + 1.75f; + else currentX = Width / 2f; // + 5.5f - 2.25f / 2f + 2.25f + 1.75f; bool flipped = false; for (int j = 0; j < spotMap[i].Count; j++) { + if (!spotMap[i][j].Perpendicular && i == 0 && j == 0) + currentX -= spotSizes[(int) spotMap[i][j].Size] / 2.0f; + else if (spotMap[i][j].Perpendicular && i == 0 && j == 0) + currentX -= 2.25f / 2.0f; + spotMap[i][j].Flipped = flipped; bool alignTop = i % 2 != 0; - switch (spotMap[i][j].Size) { - case Size.A: - spotMap[i][j].GameObject = Instantiate(Instance.spotPrefabA, mainPlanContainer); - break; - case Size.B: - spotMap[i][j].GameObject = Instantiate(Instance.spotPrefabB, mainPlanContainer); - break; - case Size.C: - spotMap[i][j].GameObject = Instantiate(Instance.spotPrefabC, mainPlanContainer); - break; - case Size.D: - spotMap[i][j].GameObject = Instantiate(Instance.spotPrefabD, mainPlanContainer); - spotMap[i][j].GameObject.transform.position = - new Vector3(currentX, currentY, 0); - break; - } + spotMap[i][j].GameObject = Instantiate(prefabs[(int) spotMap[i][j].Size], mainPlanContainer); - spotMap[i][j].GameObject.transform.position = - new Vector3(currentX, - currentY + (alignTop ? -1 : 1) * spotSizes[(int) spotMap[i][j].Size] / 2.0f, 0); + if (spotMap[i][j].Perpendicular) { + spotMap[i][j].GameObject.transform.position = + new Vector3(currentX, + currentY + (alignTop ? -1 : 1) * spotSizes[(int) spotMap[i][j].Size] / 2.0f, 0); + } + else { + spotMap[i][j].GameObject.transform.position = + new Vector3(currentX, + currentY + (alignTop ? -1 : 1) * 2.25f / 2.0f, 0); + } spotMap[i][j].AlignToTop = alignTop; - spotMap[i][j].GameObject.transform.rotation = - Quaternion.Euler(new Vector3(0, 0, spotMap[i][j].Flipped ? 180 : 0)); + + if (!spotMap[i][j].Perpendicular) + spotMap[i][j].Flipped = alignTop; + + if (spotMap[i][j].Perpendicular) { + spotMap[i][j].GameObject.transform.rotation = + Quaternion.Euler(new Vector3(0, 0, spotMap[i][j].Flipped ? 180 : 0)); + } + else { + spotMap[i][j].GameObject.transform.rotation = + Quaternion.Euler(new Vector3(0, 0, (spotMap[i][j].Flipped ? 180 : 0) + 90)); + } bool frontalParking = !parkingFromTop ^ flipped; spotMap[i][j].ParkingDirection = frontalParking ? ParkingPreference.Front : ParkingPreference.Back; spotMap[i][j].Lane = i; - currentX += 2.25f; + currentX += (i != 0 ? 1 : -1) * + (spotMap[i][j].Perpendicular ? 2.25f : spotSizes[(int) spotMap[i][j].Size]); flipped = !flipped; } @@ -432,8 +451,6 @@ namespace Parking if (i != 0) currentX = -Width / 2f - 2.25f / 2f + 2.25f; else currentX = Width / 2f - 5.0f / 2f; - bool flipped = false; - GameObject spawnedSpot; int[] emergencyMap = {12, 27, 27, 20}; @@ -467,7 +484,9 @@ namespace Parking for (int i = 0; i < 4; i++) for (int j = 0; j < spotsCreated.GetLength(1); j++) for (int k = 0; k < spotsCreated[i, j]; k++) { - _spotMap[i].Add(new Spot((Size) j, false)); + Spot spot = new((Size) (j % 4), false); + spot.Perpendicular = j < 4; + _spotMap[i].Add(spot); _initialConfigurationSpotCount++; }