Created
March 1, 2019 18:15
-
-
Save judah4/e14e3af55f0e9b8b050d5963b54e14db to your computer and use it in GitHub Desktop.
Read Kinematic Controller position data and sync it with SpatialOS. In order to set the position in th receiver, it needs a reference to the Motor. I have a player check to not set the players position directly unless the movement watchdog decides the player needs to resync
This file contains hidden or 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 System.Collections; | |
using System.Collections.Generic; | |
using Improbable.Core; | |
using Improbable.CoreLibrary.Transforms.Local; | |
using Improbable.Gdk.GameObjectRepresentation; | |
using Improbable.Worker.CInterop; | |
using UnityEngine; | |
public class KineTransformClientReceiver : KineTransformReceiver | |
{ | |
[Require] | |
protected ClientAuthorityCheck.Requirable.Writer ClientAuthorityCheckWriter; | |
protected override bool IsAnAuthoritativePlayer() | |
{ | |
var authority = ClientAuthorityCheckWriter.Authority; | |
//Debug.Log("Do I have authority to say were I want to go? " + authority); | |
return authority == Authority.Authoritative || authority == Authority.AuthorityLossImminent; | |
} | |
} |
This file contains hidden or 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.Core; | |
using Improbable.Gdk.GameObjectRepresentation; | |
using Improbable.Gdk.TransformSynchronization; | |
using Improbable.Transform; | |
using Improbable.Worker.CInterop; | |
using KinematicCharacterController; | |
using Scripts.Core; | |
using Twokinds.Assets._Game.Scripts.MovementKine; | |
using UnityEngine; | |
namespace Improbable.CoreLibrary.Transforms.Local | |
{ | |
/// <summary> | |
/// The RigidbodyTransformReceiver sets the unity position and velocity to correspond to the exact value | |
/// that the state is. It uses the TransformState. | |
/// | |
/// <seealso cref="ILocalModeBehaviour"/> | |
/// </summary> | |
public class KineTransformReceiver : MonoBehaviour | |
{ | |
[Require] protected TransformInternal.Requirable.Reader PositionReader; | |
//[Require] protected TransformVelocity.Requirable.Reader TransformVelocityReader; | |
protected KinematicCharacterMotor KineMovement { get; private set; } | |
[SerializeField] | |
private bool checkPlayerAuthority; | |
[SerializeField] private bool _debugPosition = false; | |
private SpatialOSComponent _spatialOsComponent; | |
protected virtual void Awake() | |
{ | |
KineMovement = GetComponent<KinematicCharacterMotor>(); | |
if (KineMovement == null) | |
{ | |
throw new MissingComponentException(string.Format("{0} on {1} expects a {2}.", GetType().Name, | |
gameObject.name, typeof (KineMovement).Name)); | |
} | |
} | |
protected virtual void OnEnable() | |
{ | |
_spatialOsComponent = GetComponent<SpatialOSComponent>(); | |
RegisterListeners(); | |
if (PositionReader.Authority == Authority.NotAuthoritative) | |
{ | |
KineMovement.SetPosition(PositionReader.Data.Location.ToCoordinates().ToLocalCoords().ToUnityVector() + _spatialOsComponent.Worker.Origin); | |
KineMovement.SetRotation(PositionReader.Data.Rotation.ToUnityQuaternion()); | |
} | |
} | |
protected virtual void OnDisable() | |
{ | |
RemoveListeners(); | |
} | |
protected virtual void HandleTransformUpdate(TransformInternal.Update update) | |
{ | |
if ((PositionReader.Authority == Authority.Authoritative || PositionReader.Authority == Authority.AuthorityLossImminent) || (checkPlayerAuthority &&IsAnAuthoritativePlayer() == true)) | |
return; | |
if (update.Location.HasValue) | |
{ | |
KineMovement.SetPosition(update.Location.Value.ToCoordinates().ToLocalCoords().ToUnityVector() + _spatialOsComponent.Worker.Origin); | |
} | |
if (update.Rotation.HasValue) | |
{ | |
KineMovement.SetRotation(update.Rotation.Value.ToUnityQuaternion()); | |
} | |
} | |
private void RegisterListeners() | |
{ | |
PositionReader.ComponentUpdated +=(HandleTransformUpdate); | |
} | |
private void RemoveListeners() | |
{ | |
} | |
protected virtual bool IsAnAuthoritativePlayer() | |
{ | |
return false; | |
} | |
void OnDrawGizmosSelected() | |
{ | |
if(_debugPosition == false) | |
return; | |
var offset = new Vector3(0, .5f, 0); | |
var capsuleTop = new Vector3(0, 1, 0); | |
var serverPosition = PositionReader.Data.Location.ToCoordinates().ToLocalCoords().ToUnityVector() + _spatialOsComponent.Worker.Origin; | |
Gizmos.color = Color.green; | |
Gizmos.DrawWireSphere(transform.position + offset, .5f); | |
Gizmos.DrawWireSphere(transform.position + offset + capsuleTop, .5f); | |
Gizmos.color = Color.red; | |
Gizmos.DrawSphere(serverPosition + offset, .5f); | |
Gizmos.DrawSphere(serverPosition + offset + capsuleTop, .5f); | |
} | |
} | |
} | |
This file contains hidden or 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 KinematicCharacterController; | |
using Scripts.Core; | |
namespace Assets.Gamelogic.Core | |
{ | |
public class KineTransformSender : TransformSender | |
{ | |
//[Require] protected TransformVelocity.Requirable.Writer TransformVelocityWriter; | |
protected KinematicCharacterMotor KineMovement { get; private set; } | |
protected override void DoOnEnable() | |
{ | |
if (KineMovement == null) | |
{ | |
KineMovement = GetComponent<KinematicCharacterMotor>(); | |
} | |
var pos = transformComponent.Data.Coords.ToLocalCoords().ToUnityVector() + SpatialOsComponent.Worker.Origin; | |
KineMovement.SetPosition(pos); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment