forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CannonBallSystem.cs
63 lines (53 loc) · 1.8 KB
/
CannonBallSystem.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using Tutorials.Tanks.Step4;
using Unity.Burst;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
namespace Tutorials.Tanks.Step5
{
[BurstCompile]
partial struct CannonBallSystem : ISystem
{
[BurstCompile]
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate<Execute.CannonBall>();
}
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
var ecbSingleton = SystemAPI.GetSingleton<EndSimulationEntityCommandBufferSystem.Singleton>();
var cannonBallJob = new CannonBallJob
{
ECB = ecbSingleton.CreateCommandBuffer(state.WorldUnmanaged),
DeltaTime = SystemAPI.Time.DeltaTime
};
cannonBallJob.Schedule();
}
}
// IJobEntity relies on source generation to implicitly define a query from the signature of the Execute function.
[BurstCompile]
public partial struct CannonBallJob : IJobEntity
{
public EntityCommandBuffer ECB;
public float DeltaTime;
void Execute(Entity entity, ref CannonBall cannonBall, ref LocalTransform transform)
{
var gravity = new float3(0.0f, -9.82f, 0.0f);
var invertY = new float3(1.0f, -1.0f, 1.0f);
transform.Position += cannonBall.Velocity * DeltaTime;
// bounce on the ground
if (transform.Position.y < 0.0f)
{
transform.Position *= invertY;
cannonBall.Velocity *= invertY * 0.8f;
}
cannonBall.Velocity += gravity * DeltaTime;
var speed = math.lengthsq(cannonBall.Velocity);
if (speed < 0.1f)
{
ECB.DestroyEntity(entity);
}
}
}
}