5Y5T3M/Assets/Scripts/GameManager.cs

120 lines
3.2 KiB
C#
Raw Normal View History

2025-09-15 23:01:07 +02:00
using System;
2025-09-16 00:08:50 +02:00
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEditor;
using UnityEngine;
using UnityEngine.Rendering;
2025-09-15 23:01:07 +02:00
2025-09-16 00:08:50 +02:00
[ExecuteAlways]
2025-09-15 23:01:07 +02:00
public class GameManager : MonoBehaviour
{
[HideInInspector]
public static GameManager instance;
2025-09-16 00:08:50 +02:00
[SerializeField]
private Transform NodeParent;
[SerializeField]
private GameObject NodePrefab;
2025-09-15 23:01:07 +02:00
[Header("Parameters")]
[Range(0, 100)]
public float maxConnectionLength;
2025-09-16 00:08:50 +02:00
private List<Node> nodes = new List<Node>();
2025-09-15 23:01:07 +02:00
[SerializeField]
public List<Connection> connections = new List<Connection>();
[Serializable]
public struct Connection
2025-09-16 00:08:50 +02:00
{
2025-09-15 23:01:07 +02:00
public Node nodeA, nodeB;
public bool allowed;
}
2025-09-16 00:08:50 +02:00
[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>());
}
}
2025-09-15 23:01:07 +02:00
2025-09-16 00:08:50 +02:00
[ContextMenu("Generate Connections")]
void GenerateConnections()
{
connections.Clear();
2025-09-15 23:01:07 +02:00
foreach (Node nodeA in nodes)
{
2025-09-16 00:08:50 +02:00
if (nodeA == null) continue;
2025-09-15 23:01:07 +02:00
foreach (Node nodeB in nodes)
{
2025-09-16 00:08:50 +02:00
if (nodeB == null) continue;
2025-09-15 23:01:07 +02:00
bool conExists = false;
2025-09-16 00:08:50 +02:00
if (nodeA == nodeB || Math.Abs(Vector3.Distance(nodeA.transform.position, nodeB.transform.position)) > maxConnectionLength)
continue;
2025-09-15 23:01:07 +02:00
foreach (Connection con in connections)
{
if ((con.nodeA == nodeA && con.nodeB == nodeB) || (con.nodeA == nodeB && con.nodeB == nodeA))
{
conExists = true;
break;
}
}
2025-09-16 00:08:50 +02:00
if (!conExists)
connections.Add(new Connection { nodeA = nodeA, nodeB = nodeB });
2025-09-15 23:01:07 +02:00
}
2025-09-16 00:08:50 +02:00
}
2025-09-15 23:01:07 +02:00
}
2025-09-16 00:08:50 +02:00
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
2025-09-15 23:01:07 +02:00
{
2025-09-16 00:08:50 +02:00
2025-09-15 23:01:07 +02:00
}
2025-09-16 00:08:50 +02:00
#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
2025-09-15 23:01:07 +02:00
}