#if UNITY_EDITOR
using ProjectM.Simulation;
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
namespace ProjectM.Server
{
///
/// 07-20 sandbox combat target: keeps ONE planted dummy alive in the DevSandbox
/// (scene-name gate — the established dev-script convention; GymSub bakes no WaveDirector, so this is the
/// sandbox's enemy source by design). The dummy is a real enemy ghost instantiated from a baked prefab
/// (grunt-shaped preferred), parked at the first player's position + a fixed offset, HP fat enough to eat a
/// few chains; it dies through the NORMAL death path (Dying corpse window → destroy → kill pop on the client)
/// and respawns at the same anchor shortly after — kill feedback stays testable on repeat. Editor-only.
///
[WorldSystemFilter(WorldSystemFilterFlags.ServerSimulation)]
public partial class TargetDummySpawnSystem : SystemBase
{
const float RespawnDelaySec = 1.5f;
const float DummyHealth = 400f;
static readonly float3 AnchorOffset = new float3(3f, 0f, 1.5f);
float _respawnTimer;
float3 _anchor;
bool _anchored;
protected override void OnUpdate()
{
if (UnityEngine.SceneManagement.SceneManager.GetActiveScene().name != "DevSandbox")
return;
// A dummy exists (alive OR in its Dying corpse window) -> nothing to do; the corpse window
// conveniently delays the respawn timer until the body is gone.
var dummyQ = SystemAPI.QueryBuilder().WithAll().Build();
if (!dummyQ.IsEmpty) { _respawnTimer = 0f; return; }
// Anchor once on the first player seen (spawn point + offset = a consistent training spot).
if (!_anchored)
{
foreach (var xf in SystemAPI.Query>().WithAll())
{
_anchor = xf.ValueRO.Position + AnchorOffset;
_anchored = true;
break;
}
if (!_anchored) return; // no player yet (world still connecting)
}
_respawnTimer += SystemAPI.Time.DeltaTime; // wall-frame is fine: dev spawner in the plain server group
if (_respawnTimer < RespawnDelaySec) return;
_respawnTimer = 0f;
// Grunt-shaped prefab preferred (no lunge/spit machinery on a training dummy); fall back to any enemy.
var prefQ = EntityManager.CreateEntityQuery(new EntityQueryDesc
{
All = new ComponentType[] { typeof(EnemyTag), typeof(Prefab) },
None = new ComponentType[] { typeof(LungeState), typeof(SpitterState) },
Options = EntityQueryOptions.IncludePrefab,
});
var prefabs = prefQ.ToEntityArray(Allocator.Temp);
if (prefabs.Length == 0)
{
prefabs.Dispose();
var anyQ = EntityManager.CreateEntityQuery(new EntityQueryDesc
{
All = new ComponentType[] { typeof(EnemyTag), typeof(Prefab) },
Options = EntityQueryOptions.IncludePrefab,
});
prefabs = anyQ.ToEntityArray(Allocator.Temp);
if (prefabs.Length == 0) { prefabs.Dispose(); return; } // no enemy ghosts in this world
}
var prefab = prefabs[0];
prefabs.Dispose();
var inst = EntityManager.Instantiate(prefab);
var baked = EntityManager.GetComponentData(prefab);
EntityManager.SetComponentData(inst, baked.WithPosition(_anchor)); // WithPosition: never FromPosition (Scale reset)
EntityManager.SetComponentData(inst, new Health { Current = DummyHealth, Max = DummyHealth });
EntityManager.AddComponent(inst); // server-only: plants it (every AI pass excludes the tag)
}
}
}
#endif