Parking/Assets/Scripts/ParkingManager.cs
2022-08-31 21:19:20 +02:00

146 lines
5.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using TMPro;
using UnityEngine;
namespace Parking
{
public class ParkingManager : MonoBehaviour
{
public const float Width = 68;
public const float Height = 29;
public static ParkingManager Instance;
public readonly List<List<Spot>> SpotMap = new() {new(), new(), new(), new()};
[SerializeField] private int stepTime = 15;
[SerializeField] private TextMeshProUGUI timeText;
[SerializeField] public TextMeshProUGUI countsText;
[SerializeField] public GameObject spotPrefabA;
[SerializeField] public GameObject spotPrefabB;
[SerializeField] public GameObject spotPrefabC;
[SerializeField] public GameObject spotPrefabD;
private readonly float[] _spotHeights = {3.5f, 4f, 5f, 7.5f};
private TimeSpan _currentTime = TimeSpan.FromHours(8);
private void Awake()
{
Instance = this;
}
private void Start()
{
timeText.text = _currentTime.ToString();
DataImporter.ReadFile("Assets/Data/v1.csv");
Debug.Log(DataImporter.Drivers);
InitialConfigurationGenerator generator = new();
ArrangeSpots(generator.FindSolution());
}
public void AdvanceTime()
{
_currentTime += TimeSpan.FromMinutes(15);
timeText.text = _currentTime.ToString();
}
public void UpdateText(string text)
{
countsText.text = text;
}
private void ArrangeSpots(int[,] spotsCreated)
{
var spotMap = GenerateSpotMap(spotsCreated);
var maxP3 = _spotHeights[(int) spotMap[2].Max().Size];
var maxP2 = _spotHeights[(int) spotMap[1].Max().Size];
for (var i = 0; i < 4; i++)
{
float currentY;
switch (i)
{
case 0:
currentY = -Height / 2.0f + 2f;
break;
case 1:
currentY = Height / 2 - 5.5f - 5f - maxP3 / 2.0f - maxP2;
break;
case 2:
currentY = Height / 2 - 5.5f - 5f - maxP3 / 2.0f;
break;
case 3:
currentY = Height / 2.0f - 2f;
break;
default:
currentY = -10;
break;
}
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;
var flipped = false;
for (var j = 0; j < spotMap[i].Count; j++)
{
spotMap[i][j].Flipped = flipped;
var alignTop = i % 2 != 0;
switch (spotMap[i][j].Size)
{
case Size.A:
spotMap[i][j].GameObject = Instantiate(Instance.spotPrefabA);
spotMap[i][j].GameObject.transform.position = new Vector3(currentX, currentY, 0);
spotMap[i][j].GameObject.transform.rotation =
Quaternion.Euler(new Vector3(0, 0, spotMap[i][j].Flipped ? 180 : 0));
break;
case Size.B:
spotMap[i][j].GameObject = Instantiate(Instance.spotPrefabB);
spotMap[i][j].GameObject.transform.position =
new Vector3(currentX, currentY + (alignTop ? -1 : 1) * 0.25f, 0);
spotMap[i][j].GameObject.transform.rotation =
Quaternion.Euler(new Vector3(0, 0, spotMap[i][j].Flipped ? 180 : 0));
break;
case Size.C:
spotMap[i][j].GameObject = Instantiate(Instance.spotPrefabC);
spotMap[i][j].GameObject.transform.position =
new Vector3(currentX, currentY + (alignTop ? -1 : 1) * 0.5f, 0);
spotMap[i][j].GameObject.transform.rotation =
Quaternion.Euler(new Vector3(0, 0, spotMap[i][j].Flipped ? 180 : 0));
break;
case Size.D:
spotMap[i][j].GameObject = Instantiate(Instance.spotPrefabD);
spotMap[i][j].GameObject.transform.position =
new Vector3(currentX, currentY -2.0f, 0);
spotMap[i][j].GameObject.transform.rotation =
Quaternion.Euler(new Vector3(0, 0, spotMap[i][j].Flipped ? 180 : 0));
break;
}
currentX += 2.25f;
flipped = !flipped;
}
}
}
private List<List<Spot>> GenerateSpotMap(int[,] spotsCreated)
{
for (var i = 0; i < 4; i++)
for (var j = 0; j < spotsCreated.GetLength(1); j++)
for (var k = 0; k < spotsCreated[i, j]; k++)
SpotMap[i].Add(new Spot((Size) j, false));
SpotMap[0].Sort((a, b) => a.Size.CompareTo(b.Size)); // ascending sort
SpotMap[1].Sort((a, b) => b.Size.CompareTo(a.Size)); // descending sort
SpotMap[2].Sort((a, b) => b.Size.CompareTo(a.Size)); // descending sort
SpotMap[3].Sort((a, b) => a.Size.CompareTo(b.Size)); // ascending sort
SpotMap[2].Add(new Spot(Size.D, false));
return SpotMap;
}
}
}