From a2c9647681acb7c185af72370e74aea3e3247fda Mon Sep 17 00:00:00 2001 From: Dawid Pietrykowski Date: Mon, 5 Sep 2022 22:00:15 +0200 Subject: [PATCH] Basic reconfiguration added --- .gitignore | 3 +++ Assets/Data/.~lock.Tablica1.csv# | 1 - Assets/Scenes/SampleScene.unity | 4 +-- Assets/Scripts/Driver.cs | 3 +++ Assets/Scripts/ParkingManager.cs | 44 ++++++++++++++++++++++++++++++-- 5 files changed, 50 insertions(+), 5 deletions(-) delete mode 100644 Assets/Data/.~lock.Tablica1.csv# diff --git a/.gitignore b/.gitignore index ab46840..7dce433 100644 --- a/.gitignore +++ b/.gitignore @@ -74,5 +74,8 @@ crashlytics-build.properties /[Aa]ssets/[Ss]treamingAssets/aa.meta /[Aa]ssets/[Ss]treamingAssets/aa/* +# Lock files +**/.~lock* + # Notes /Notes/ diff --git a/Assets/Data/.~lock.Tablica1.csv# b/Assets/Data/.~lock.Tablica1.csv# deleted file mode 100644 index e5d1df5..0000000 --- a/Assets/Data/.~lock.Tablica1.csv# +++ /dev/null @@ -1 +0,0 @@ -,davp,arch-desktop,04.09.2022 23:00,file:///home/davp/.config/libreoffice/4; \ No newline at end of file diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 4fc2374..84535ad 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -532,7 +532,7 @@ Transform: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 557435655} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: -1.6363324, y: 1.5066413, z: -0.28673837} + m_LocalPosition: {x: 0, y: 0, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} m_ConstrainProportionsScale: 0 m_Children: [] @@ -647,7 +647,7 @@ Transform: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 632826359} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: -1.6363324, y: 1.5066413, z: -0.28673837} + m_LocalPosition: {x: 0, y: 0, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} m_ConstrainProportionsScale: 0 m_Children: [] diff --git a/Assets/Scripts/Driver.cs b/Assets/Scripts/Driver.cs index e8bb1fd..9a79dc5 100644 --- a/Assets/Scripts/Driver.cs +++ b/Assets/Scripts/Driver.cs @@ -47,6 +47,9 @@ namespace Parking public bool Reserved = false; public int ReservedPriority = 0; public ParkingPreference ParkingDirection = ParkingPreference.Any; + public int Lane = 0; + public bool Perpendicular = true; + public bool AlignToTop = true; public Spot(Size size, bool flipped) { diff --git a/Assets/Scripts/ParkingManager.cs b/Assets/Scripts/ParkingManager.cs index ae04928..1454913 100644 --- a/Assets/Scripts/ParkingManager.cs +++ b/Assets/Scripts/ParkingManager.cs @@ -29,7 +29,7 @@ namespace Parking [SerializeField] public Transform mainPlanContainer; [SerializeField] public Transform emergencyPlanContainer; - private readonly float[] _spotHeights = {3.5f, 4f, 5f, 7.5f}; + private readonly float[] _spotHeights = {4f, 4.5f, 5f, 7.5f}; private readonly List> _spotMap = new() {new List(), new List(), new List(), new List()}; @@ -231,6 +231,7 @@ namespace Parking 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); driver.GameObject.transform.rotation = Quaternion.Euler(new Vector3(0, 0, spot.Flipped ? 180 : 0)); } @@ -242,6 +243,8 @@ namespace Parking private void ReconfigureSpots() { + List prefabs = new List() + {spotPrefabA, spotPrefabB, spotPrefabC}; int[] freeSpots = GetFreeSpotCount(); var nextCars = GetNextCars(10); int[] plannedSpots = {0, 0, 0, 0}; @@ -255,8 +258,41 @@ namespace Parking if (neededSpots.Sum() > 5) { Debug.Log($"Needed spots = {neededSpots[0]} {neededSpots[1]} {neededSpots[2]} {neededSpots[3]}"); Debug.Log("Attempting reconfiguration..."); - } + for (int size = 0; size < 3; size++) { + bool foundReplacement = true; + while (foundReplacement && neededSpots[size] != 0) { + foundReplacement = false; + foreach (var t in _spotMap) { + foreach (Spot spot in t) { + if ((int) spot.Size > size && spot.Free && spot.Lane != 3 && spot.Size != Size.D) { + foundReplacement = true; + + float diff = (_spotHeights[(int)spot.Size] - _spotHeights[size]) / 2.0f; + if (!spot.AlignToTop) + diff *= -1; + + spot.Size = (Size) size; + Vector3 position = Vector3.zero; + Quaternion rotation = Quaternion.identity; + if (spot.GameObject != null) { + position = spot.GameObject.transform.position; + rotation = spot.GameObject.transform.rotation; + Destroy(spot.GameObject); + } + + + spot.GameObject = Instantiate(prefabs[size], + position + new Vector3(0, diff, 0), rotation, mainPlanContainer); + + goto whileEnd; + } + } + } + whileEnd:; + } + } + } } private int[] GetFreeSpotCount() @@ -353,6 +389,7 @@ namespace Parking new Vector3(currentX, currentY + (alignTop ? -1 : 1) * spotSizes[(int) spotMap[i][j].Size] / 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)); @@ -360,6 +397,8 @@ namespace Parking bool frontalParking = !parkingFromTop ^ flipped; spotMap[i][j].ParkingDirection = frontalParking ? ParkingPreference.Front : ParkingPreference.Back; + spotMap[i][j].Lane = i; + currentX += 2.25f; flipped = !flipped; @@ -406,6 +445,7 @@ namespace Parking Vector3 position = new Vector3(currentX, currentY + 2.25f/2.0f, 0); spawnedSpot = Instantiate(Instance.spotPrefabC, position, Quaternion.Euler(new Vector3(0, 0, 90)), emergencyPlanContainer); + currentX -= 5; }else { bool alignTop = i % 2 != 0;