Happy_Forest/Assets/Scripts/PilleWanderSystem.cs

40 lines
No EOL
1.4 KiB
C#

using Unity.Burst;
using Unity.Entities;
using Unity.Transforms;
using Unity.Mathematics;
namespace HappyForest.Prototypen
{
[BurstCompile]
public partial struct PilleWanderSystem : ISystem
{
[BurstCompile]
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate<PilleWanderData>();
}
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
float deltaTime = SystemAPI.Time.DeltaTime;
// Ein EntityCommandBuffer wird benötigt, um Entities sicher zu zerstören,
// während wir gerade über sie iterieren.
var ecb = SystemAPI.GetSingleton<EndSimulationEntityCommandBufferSystem.Singleton>().CreateCommandBuffer(state.WorldUnmanaged);
// Iteriere über alle Entities, die eine Position (LocalTransform) und unsere WanderData haben
foreach (var (transform, wanderData, entity) in SystemAPI.Query<RefRW<LocalTransform>, RefRO<PilleWanderData>>().WithEntityAccess())
{
// 1. Bewegen (Aktuelle Position + Richtung * Geschwindigkeit * Zeit)
transform.ValueRW.Position += wanderData.ValueRO.Direction * wanderData.ValueRO.Speed * deltaTime;
// 2. Abgrund-Check (Runtergefallen?)
if (transform.ValueRO.Position.y < -10f)
{
ecb.DestroyEntity(entity);
}
}
}
}
}