68 lines
2 KiB
C#
68 lines
2 KiB
C#
|
|
using Unity.Burst;
|
||
|
|
using Unity.Entities;
|
||
|
|
using Unity.Mathematics;
|
||
|
|
using Unity.Transforms;
|
||
|
|
|
||
|
|
namespace HappyForest.Prototypen
|
||
|
|
{
|
||
|
|
// Bewegt alle Pillen und lässt sie an den (aus den "Wall"-Objekten berechneten)
|
||
|
|
// Feld-Grenzen abprallen. Läuft komplett als Burst-kompilierter Parallel-Job.
|
||
|
|
[BurstCompile]
|
||
|
|
public partial struct PilleMovementSystem : ISystem
|
||
|
|
{
|
||
|
|
public void OnCreate(ref SystemState state)
|
||
|
|
{
|
||
|
|
state.RequireForUpdate<PilleSimulationConfig>();
|
||
|
|
}
|
||
|
|
|
||
|
|
[BurstCompile]
|
||
|
|
public void OnUpdate(ref SystemState state)
|
||
|
|
{
|
||
|
|
var config = SystemAPI.GetSingleton<PilleSimulationConfig>();
|
||
|
|
float deltaTime = SystemAPI.Time.DeltaTime;
|
||
|
|
|
||
|
|
var job = new MoveAndBounceJob
|
||
|
|
{
|
||
|
|
DeltaTime = deltaTime,
|
||
|
|
BoundsMin = config.BoundsMin,
|
||
|
|
BoundsMax = config.BoundsMax,
|
||
|
|
PlayHeight = config.PlayHeight
|
||
|
|
};
|
||
|
|
|
||
|
|
state.Dependency = job.ScheduleParallel(state.Dependency);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
[BurstCompile]
|
||
|
|
public partial struct MoveAndBounceJob : IJobEntity
|
||
|
|
{
|
||
|
|
public float DeltaTime;
|
||
|
|
public float3 BoundsMin;
|
||
|
|
public float3 BoundsMax;
|
||
|
|
public float PlayHeight;
|
||
|
|
|
||
|
|
void Execute(ref LocalTransform transform, ref PilleComponent pille)
|
||
|
|
{
|
||
|
|
if (math.lengthsq(pille.Velocity) < 0.0001f)
|
||
|
|
pille.Velocity = new float3(1f, 0f, 0f);
|
||
|
|
|
||
|
|
float3 pos = transform.Position + pille.Velocity * pille.Speed * DeltaTime;
|
||
|
|
|
||
|
|
if (pos.x < BoundsMin.x || pos.x > BoundsMax.x)
|
||
|
|
{
|
||
|
|
pille.Velocity.x *= -1f;
|
||
|
|
pos.x = math.clamp(pos.x, BoundsMin.x, BoundsMax.x);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (pos.z < BoundsMin.z || pos.z > BoundsMax.z)
|
||
|
|
{
|
||
|
|
pille.Velocity.z *= -1f;
|
||
|
|
pos.z = math.clamp(pos.z, BoundsMin.z, BoundsMax.z);
|
||
|
|
}
|
||
|
|
|
||
|
|
pos.y = PlayHeight;
|
||
|
|
transform.Position = pos;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|