|
using Improbable; |
|
using Improbable.Gdk.Core; |
|
using Improbable.Gdk.TransformSynchronization; |
|
using Improbable.Transform; |
|
using Unity.Collections; |
|
using Unity.Entities; |
|
using Quaternion = UnityEngine.Quaternion; |
|
|
|
namespace Faeborn.NavSystem |
|
{ |
|
[DisableAutoCreation] |
|
public class InitializationSystem : ComponentSystem |
|
{ |
|
private struct Data |
|
{ |
|
public readonly int Length; |
|
[ReadOnly] public EntityArray Entities; |
|
[ReadOnly] public ComponentDataArray<Metadata.Component> Metadata; |
|
[ReadOnly] public ComponentDataArray<Position.Component> Position; |
|
[ReadOnly] public ComponentDataArray<TransformInternal.Component> Transform; |
|
[ReadOnly] public ComponentDataArray<NewlyAddedSpatialOSEntity> DenotesNewSpatialOSEntity; |
|
} |
|
|
|
private readonly World world; |
|
private readonly NavSettings navSettings; |
|
private readonly TransformSynchronizationSendStrategy sendStrategy; |
|
|
|
[Inject] private EntityManager entityManager; |
|
[Inject] private WorkerSystem workerSystem; |
|
[Inject] private Data data; |
|
|
|
public NavInitializationSystem(World world, NavSettings navSettings, TransformSynchronizationSendStrategy sendStrategy) |
|
{ |
|
this.world = world; |
|
this.navSettings = navSettings; |
|
this.sendStrategy = sendStrategy; |
|
} |
|
|
|
protected override void OnUpdate() |
|
{ |
|
for (var i = 0; i < data.Length; i++) |
|
{ |
|
switch (data.Metadata[i].EntityType) |
|
{ |
|
case "Player": |
|
var entity = data.Entities[i]; |
|
var transform = data.Transform[i]; |
|
|
|
// Navigation components |
|
PostUpdateCommands.AddComponent(entity, new NavAgent( |
|
transform.Location.ToUnityVector3(), |
|
Quaternion.identity, |
|
navSettings.StoppingDistance, |
|
navSettings.MoveSpeed, |
|
navSettings.Acceleration, |
|
navSettings.RotationSpeed, |
|
navSettings.DefaultAreaMask)); |
|
PostUpdateCommands.AddComponent(entity, new SyncToImprobableTransformTag{origin = workerSystem.Origin}); |
|
|
|
// Improbable TransformSynchronization components |
|
PostUpdateCommands.AddComponent(entity, new TransformToSend |
|
{ |
|
Position = transform.Location.ToUnityVector3() - workerSystem.Origin, |
|
Velocity = transform.Velocity.ToUnityVector3(), |
|
Orientation = transform.Rotation.ToUnityQuaternion() |
|
}); |
|
PostUpdateCommands.AddComponent(entity, new TicksSinceLastTransformUpdate { NumberOfTicks = 0 }); |
|
PostUpdateCommands.AddComponent(entity, new LastTransformSentData |
|
{ |
|
// could set this to the max time if we want to immediately send something |
|
TimeSinceLastUpdate = 0.0f, |
|
Transform = data.Transform[i], |
|
}); |
|
PostUpdateCommands.AddComponent(entity, new LastPositionSentData |
|
{ |
|
// could set this to the max time if we want to immediately send something |
|
TimeSinceLastUpdate = 0.0f, |
|
Position = data.Position[i], |
|
}); |
|
|
|
sendStrategy.Apply(entity, world, PostUpdateCommands); |
|
break; |
|
} |
|
} |
|
} |
|
} |
|
} |
NavAgent module was taken from github and nearly 100% used as is with some very minor modifications: https://github.com/zulfajuniadi/unity-ecs-navmesh/