160 lines
No EOL
5.4 KiB
C#
160 lines
No EOL
5.4 KiB
C#
using System.Collections.Generic;
|
|
using Unity.Entities;
|
|
using Unity.Transforms;
|
|
using UnityEngine;
|
|
|
|
namespace HappyForest.Prototypen
|
|
{
|
|
// --- DOTS DATEN FÜR DEN MANAGER ---
|
|
public struct SpawnerData : IComponentData
|
|
{
|
|
public Entity DotsPrefabEntity;
|
|
}
|
|
|
|
// --- DER OOP MANAGER ---
|
|
public class ManagerScript : MonoBehaviour
|
|
{
|
|
[Header("Architektur Switch")]
|
|
[Tooltip("Schaltet um auf das rasend schnelle Entity Component System")]
|
|
public bool nutzeDOTS = false;
|
|
public GameObject dotsPrefab; // Zieh hier dein NEUES DOTS-Prefab rein
|
|
|
|
[Header("OOP Spawner Einstellungen")]
|
|
public GameObject oopPrefab; // Dein altes Prefab
|
|
public bool pillenKollidieren = true;
|
|
|
|
[Header("Tasten-Einstellungen")]
|
|
public float aktionsVerzoegerung = 0.1f;
|
|
private float letzterAktionsZeitpunkt = 0f;
|
|
|
|
[HideInInspector]
|
|
public List<GameObject> aktivePillen = new List<GameObject>();
|
|
|
|
// DOTS Referenzen
|
|
private EntityManager entityManager;
|
|
private EntityQuery spawnerQuery;
|
|
private int dotsPillenCount = 0;
|
|
|
|
void Start()
|
|
{
|
|
// Initialisiere DOTS Welt
|
|
if (World.DefaultGameObjectInjectionWorld != null)
|
|
{
|
|
entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
|
|
spawnerQuery = entityManager.CreateEntityQuery(typeof(SpawnerData));
|
|
}
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
// Tote OOP-Pillen aussortieren (falls sie über -10 gefallen sind)
|
|
aktivePillen.RemoveAll(pille => pille == null);
|
|
|
|
// Tasten gedrückt halten (mit Cooldown)
|
|
if (Time.time - letzterAktionsZeitpunkt > aktionsVerzoegerung)
|
|
{
|
|
// HIER SIND DIE NEUEN TASTEN FÜR DEINE TASTATUR
|
|
if (Input.GetKey(KeyCode.Space))
|
|
{
|
|
Debug.Log("Leertaste gedrückt!"); // <--- NEU
|
|
SpawnPille(1);
|
|
letzterAktionsZeitpunkt = Time.time;
|
|
}
|
|
else if (Input.GetKey(KeyCode.M))
|
|
{
|
|
PilleEntfernen();
|
|
letzterAktionsZeitpunkt = Time.time;
|
|
}
|
|
else if (Input.GetKey(KeyCode.C))
|
|
{
|
|
SpawnPille(100);
|
|
letzterAktionsZeitpunkt = Time.time;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void SpawnPille(int anzahl)
|
|
{
|
|
if (nutzeDOTS)
|
|
{
|
|
// --- DOTS SPAWN ---
|
|
if (!spawnerQuery.HasSingleton<SpawnerData>())
|
|
{
|
|
Debug.LogError("Abbruch: Manager findet SpawnerData nicht!"); // <--- NEU
|
|
return;
|
|
}
|
|
|
|
Entity prefab = spawnerQuery.GetSingleton<SpawnerData>().DotsPrefabEntity;
|
|
|
|
for (int i = 0; i < anzahl; i++)
|
|
{
|
|
Entity newEntity = entityManager.Instantiate(prefab);
|
|
|
|
// Optionale Startposition (z.B. aus einer bestehenden Pille) könnte man hier über LocalTransform setzen
|
|
entityManager.SetComponentData(newEntity, LocalTransform.FromPosition(Vector3.zero));
|
|
dotsPillenCount++;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// --- OOP SPAWN (Dein alter Code) ---
|
|
for (int i = 0; i < anzahl; i++)
|
|
{
|
|
Vector3 spawnPos = Vector3.zero;
|
|
if (aktivePillen.Count > 0)
|
|
{
|
|
GameObject randomPille = aktivePillen[Random.Range(0, aktivePillen.Count)];
|
|
spawnPos = randomPille.transform.position;
|
|
}
|
|
|
|
if (oopPrefab != null)
|
|
{
|
|
GameObject neu = Instantiate(oopPrefab, spawnPos, Quaternion.identity);
|
|
aktivePillen.Add(neu);
|
|
|
|
// Kollision umschalten
|
|
if (!pillenKollidieren) neu.layer = LayerMask.NameToLayer("Pille");
|
|
else neu.layer = LayerMask.NameToLayer("Default");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void PilleEntfernen()
|
|
{
|
|
if (nutzeDOTS)
|
|
{
|
|
// In DOTS müsste man hier eine Query machen und eine Entity löschen.
|
|
// Für diesen Test reduzieren wir nur den Zähler zur Veranschaulichung.
|
|
Debug.Log("Löschen in DOTS benötigt einen kleinen Extra-Job!");
|
|
}
|
|
else
|
|
{
|
|
if (aktivePillen.Count > 0)
|
|
{
|
|
GameObject pilleToDelete = aktivePillen[0];
|
|
aktivePillen.RemoveAt(0);
|
|
Destroy(pilleToDelete);
|
|
}
|
|
}
|
|
}
|
|
|
|
void OnGUI()
|
|
{
|
|
GUI.color = Color.yellow;
|
|
GUI.skin.label.fontSize = 20;
|
|
|
|
int fps = (int)(1f / Time.unscaledDeltaTime);
|
|
GUI.Label(new Rect(10, 10, 400, 30), $"FPS: {fps}");
|
|
|
|
if (nutzeDOTS)
|
|
{
|
|
GUI.Label(new Rect(10, 40, 400, 30), $"Aktive DOTS Pillen (Gespawnt): {dotsPillenCount}");
|
|
}
|
|
else
|
|
{
|
|
GUI.Label(new Rect(10, 40, 400, 30), $"Aktive OOP Pillen: {aktivePillen.Count}");
|
|
}
|
|
}
|
|
}
|
|
} |