5Y5T3M/Assets/Scripts/GameManager.cs
2025-09-16 00:08:50 +02:00

119 lines
3.2 KiB
C#

using System;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEditor;
using UnityEngine;
using UnityEngine.Rendering;
[ExecuteAlways]
public class GameManager : MonoBehaviour
{
[HideInInspector]
public static GameManager instance;
[SerializeField]
private Transform NodeParent;
[SerializeField]
private GameObject NodePrefab;
[Header("Parameters")]
[Range(0, 100)]
public float maxConnectionLength;
private List<Node> nodes = new List<Node>();
[SerializeField]
public List<Connection> connections = new List<Connection>();
[Serializable]
public struct Connection
{
public Node nodeA, nodeB;
public bool allowed;
}
[ContextMenu("Generate Along Sphere")]
void GenerateAlongSphere()
{
connections.Clear();
nodes.Clear();
int pointCount = 100;
float radius = 20f;
float goldenRatio = (1f + Mathf.Sqrt(5f)) / 2f;
float angleIncrement = 2f * Mathf.PI * goldenRatio;
for (int i = 0; i < pointCount; i++)
{
float t = (float)i / pointCount; // von 0 bis 1
float inclination = Mathf.Acos(1f - 2f * t);
float azimuth = angleIncrement * i;
float x = Mathf.Sin(inclination) * Mathf.Cos(azimuth);
float y = Mathf.Sin(inclination) * Mathf.Sin(azimuth);
float z = Mathf.Cos(inclination);
Vector3 pos = new Vector3(x, y, z) * radius;
var auto = Instantiate(NodePrefab, NodeParent);
auto.transform.localPosition = pos;
nodes.Add(auto.GetComponent<Node>());
}
}
[ContextMenu("Generate Connections")]
void GenerateConnections()
{
connections.Clear();
foreach (Node nodeA in nodes)
{
if (nodeA == null) continue;
foreach (Node nodeB in nodes)
{
if (nodeB == null) continue;
bool conExists = false;
if (nodeA == nodeB || Math.Abs(Vector3.Distance(nodeA.transform.position, nodeB.transform.position)) > maxConnectionLength)
continue;
foreach (Connection con in connections)
{
if ((con.nodeA == nodeA && con.nodeB == nodeB) || (con.nodeA == nodeB && con.nodeB == nodeA))
{
conExists = true;
break;
}
}
if (!conExists)
connections.Add(new Connection { nodeA = nodeA, nodeB = nodeB });
}
}
}
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
}
#if UNITY_EDITOR
void OnDrawGizmos()
{
if (connections == null) return;
// Linien respektieren den Z-Buffer
Handles.zTest = UnityEngine.Rendering.CompareFunction.LessEqual;
foreach (var con in connections)
{
Handles.color = con.allowed ? Color.green : Color.red;
Handles.DrawLine(con.nodeA.transform.position, con.nodeB.transform.position);
}
}
#endif
}