49 lines
1.7 KiB
C#
49 lines
1.7 KiB
C#
using NUnit.Framework;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class RandomDoorsController : MonoBehaviour
|
|
{
|
|
[SerializeField] private List<GameObject> fakeDoors;
|
|
[SerializeField] private GameObject correctDoor;
|
|
[SerializeField] private List<Transform> doorsSpawnPoints;
|
|
|
|
public void RandomizeDoors()
|
|
{
|
|
if (doorsSpawnPoints.Count < fakeDoors.Count + 1)
|
|
{
|
|
Debug.LogError("Not enough spawn points for all doors!");
|
|
return;
|
|
}
|
|
|
|
// Δημιουργούμε ένα αντίγραφο της λίστας των spawn points και τα ανακατεύουμε
|
|
List<Transform> shuffledSpawnPoints = new List<Transform>(doorsSpawnPoints);
|
|
ShuffleList(shuffledSpawnPoints);
|
|
|
|
int index = 0;
|
|
|
|
// Τοποθετούμε την correctDoor σε τυχαία θέση
|
|
correctDoor.transform.position = shuffledSpawnPoints[index].position;
|
|
correctDoor.transform.rotation = shuffledSpawnPoints[index].rotation;
|
|
index++;
|
|
|
|
// Τοποθετούμε τις fakeDoors στα υπόλοιπα σημεία
|
|
for (int i = 0; i < fakeDoors.Count; i++)
|
|
{
|
|
fakeDoors[i].transform.position = shuffledSpawnPoints[index].position;
|
|
fakeDoors[i].transform.rotation = shuffledSpawnPoints[index].rotation;
|
|
index++;
|
|
}
|
|
}
|
|
|
|
// Μέθοδος που ανακατεύει μια λίστα (Fisher-Yates Shuffle)
|
|
private void ShuffleList<T>(List<T> list)
|
|
{
|
|
for (int i = list.Count - 1; i > 0; i--)
|
|
{
|
|
int randomIndex = Random.Range(0, i + 1);
|
|
(list[i], list[randomIndex]) = (list[randomIndex], list[i]);
|
|
}
|
|
}
|
|
}
|