Instantly share code, notes, and snippets.
luna-duclos/NavAgentSyncSystem.cs Secret
Last active
February 4, 2019 08:52
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save luna-duclos/dc06674a877be04cece8788d3d3bae82 to your computer and use it in GitHub Desktop.
NavAgent synchronization with a game client using SpatialOS's TransformSynchronization module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Improbable.Gdk.TransformSynchronization; | |
using Unity.Burst; | |
using Unity.Collections; | |
using Unity.Entities; | |
using Unity.Jobs; | |
using Unity.Transforms; | |
using UnityEngine; | |
namespace Faeborn.NavSystem | |
{ | |
public class NavAgentToImprobableTransformSyncSystem : JobComponentSystem | |
{ | |
[BurstCompile] | |
private struct NavAgentToImprobableTransformSyncSystemJob : IJobProcessComponentData<TransformToSend, NavAgent, SyncToImprobableTransformTag> | |
{ | |
public void Execute (ref TransformToSend TransformToSend, [ReadOnly] ref NavAgent NavAgent, [ReadOnly] ref SyncToImprobableTransformTag SyncToImprobableTransformTag) | |
{ | |
TransformToSend.Position = NavAgent.position - SyncToImprobableTransformTag.origin; | |
TransformToSend.Orientation = NavAgent.rotation; | |
TransformToSend.Velocity = NavAgent.rotation * Vector3.forward * NavAgent.moveSpeed; | |
} | |
} | |
protected override JobHandle OnUpdate(JobHandle inputDeps) | |
{ | |
return new NavAgentToImprobableTransformSyncSystemJob().Schedule(this, inputDeps); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Unity.Entities; | |
using Unity.Mathematics; | |
namespace Faeborn.NavSystem | |
{ | |
public struct SyncToImprobableTransformTag : IComponentData | |
{ | |
public float3 origin; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Above code may be considered MIT licensed by anyone that finds this link and used within the restrictions of that license.