diff --git a/Unity/Participant.cs b/Unity/Participant.cs new file mode 100644 index 0000000..8b0c5f5 --- /dev/null +++ b/Unity/Participant.cs @@ -0,0 +1,45 @@ +using UnityEngine; + +namespace RoboidControl.Unity { + + public class Participant : MonoBehaviour { + public string ipAddress; + public int port; + + public RoboidControl.Participant coreParticipant; + + protected virtual void Update() { + if (coreParticipant == null) + return; + + if (coreParticipant.updateQueue.TryDequeue(out RoboidControl.Participant.UpdateEvent e)) + HandleUpdateEvent(e); + } + + private void HandleUpdateEvent(RoboidControl.Participant.UpdateEvent e) { + switch (e.messageId) { + case ThingMsg.id: + HandleThingEvent(e); + break; + } + } + + private void HandleThingEvent(RoboidControl.Participant.UpdateEvent e) { + switch (e.thing) { + case RoboidControl.TouchSensor coreTouchSensor: + GameObject touchObj = new("Touch Sensor"); + touchObj.transform.SetParent(this.transform); + TouchSensor touchSensor = touchObj.AddComponent(); + touchSensor.coreSensor = coreTouchSensor; + break; + case RoboidControl.Thing coreThing: + GameObject thingObj = new("Thingg"); + thingObj.transform.SetParent(this.transform); + Thing thing = thingObj.AddComponent(); + thing.core = coreThing; + break; + } + } + + } +} \ No newline at end of file diff --git a/Unity/SiteServer.cs b/Unity/SiteServer.cs index 83e9870..b260f43 100644 --- a/Unity/SiteServer.cs +++ b/Unity/SiteServer.cs @@ -5,8 +5,7 @@ using UnityEngine; namespace RoboidControl.Unity { - public class SiteServer : MonoBehaviour { - public int port = 7681; + public class SiteServer : Participant { public RoboidControl.SiteServer site; @@ -16,7 +15,6 @@ namespace RoboidControl.Unity { Console.SetOut(new UnityLogWriter()); site = new RoboidControl.SiteServer(port); - RoboidControl.Thing.OnNewThing += HandleNewThing; } void OnApplicationQuit() { @@ -24,17 +22,30 @@ namespace RoboidControl.Unity { site.Close(); } - public void HandleNewThing(RoboidControl.Thing thing) { - //Debug.Log($"Handle New thing event for {thing}"); - //site.Add(thing, false); - thingQueue.Enqueue(thing); - } + protected override void Update() { + if (site == null) + return; + + if (site.updateQueue.TryDequeue(out RoboidControl.Participant.UpdateEvent e)) + HandleUpdateEvent(e); - protected virtual void Update() { site.Update((ulong)(Time.time * 1000)); while (thingQueue.TryDequeue(out RoboidControl.Thing thing)) thing.CreateComponent(); } + + private void HandleUpdateEvent(RoboidControl.Participant.UpdateEvent e) { + switch (e.messageId) { + case ParticipantMsg.Id: + GameObject remoteParticipant = new GameObject("RemoteParticipant"); + Participant participant = remoteParticipant.AddComponent(); + participant.coreParticipant = e.participant; + break; + case ThingMsg.id: + e.thing.CreateComponent(); + break; + } + } } } diff --git a/Unity/Thing.cs b/Unity/Thing.cs index 0045daa..5ac2117 100644 --- a/Unity/Thing.cs +++ b/Unity/Thing.cs @@ -70,6 +70,9 @@ namespace RoboidControl.Unity { return; } + if (core.updateQueue.TryDequeue(out RoboidControl.Thing.UpdateEvent e)) + HandleUpdateEvent(e); + if (core.linearVelocity != null && core.linearVelocity.distance != 0) { Vector3 direction = Quaternion.AngleAxis(core.linearVelocity.direction.horizontal, Vector3.up) * Vector3.forward; this.transform.Translate(core.linearVelocity.distance * Time.deltaTime * direction, Space.Self); @@ -103,6 +106,17 @@ namespace RoboidControl.Unity { } } + private void HandleUpdateEvent(RoboidControl.Thing.UpdateEvent e) { + switch(e.messageId) { + case ThingMsg.id: + break; + case PoseMsg.Id: + this.transform.localPosition = core.position.ToVector3(); + this.transform.localRotation = core.orientation.ToQuaternion(); + break; + } + } + private void PoseChanged() { //Debug.Log($"{this} pose changed"); if (core.positionUpdated) diff --git a/Unity/TouchSensor.cs b/Unity/TouchSensor.cs index 1b1286d..da2f0e7 100644 --- a/Unity/TouchSensor.cs +++ b/Unity/TouchSensor.cs @@ -14,6 +14,7 @@ namespace RoboidControl.Unity { /// public RoboidControl.TouchSensor coreSensor { get => (RoboidControl.TouchSensor)base.core; + set => base.core = value; } SphereCollider collider = null; diff --git a/src/Participant.cs b/src/Participant.cs index 3c13c1a..af3696f 100644 --- a/src/Participant.cs +++ b/src/Participant.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Collections.Concurrent; namespace RoboidControl { @@ -78,6 +79,8 @@ namespace RoboidControl { this.things.Remove(thing); } +#region Update + /// /// Update all things for this participant /// @@ -91,6 +94,15 @@ namespace RoboidControl { } } + public class UpdateEvent { + public int messageId; // see the communication messages + public Thing thing; + public Participant participant; + } + public ConcurrentQueue updateQueue = new(); + +#endregion Update + /// /// The collection of known participants. /// diff --git a/src/Participants/SiteServer.cs b/src/Participants/SiteServer.cs index 5b9b5ed..6c5c795 100644 --- a/src/Participants/SiteServer.cs +++ b/src/Participants/SiteServer.cs @@ -100,6 +100,11 @@ namespace RoboidControl { if (msg.networkId != sender.networkId) { //Console.WriteLine($"{this.name} received New Participant -> {sender.networkId}"); this.Send(sender, new NetworkIdMsg(sender.networkId)); + UpdateEvent e = new() { + messageId = ParticipantMsg.Id, + participant = sender + }; + this.updateQueue.Enqueue(e); } } diff --git a/src/Thing.cs b/src/Thing.cs index 6ea166e..184cc09 100644 --- a/src/Thing.cs +++ b/src/Thing.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Collections.Concurrent; using LinearAlgebra; namespace RoboidControl { @@ -56,8 +57,14 @@ namespace RoboidControl { this.type = thingType; if (this.owner != null) this.owner.Add(this); - if (invokeEvent) - InvokeNewThing(this); + if (invokeEvent) { + //InvokeNewThing(this); + Participant.UpdateEvent e = new() { + messageId = ThingMsg.id, + thing = this + }; + this.owner.updateQueue.Enqueue(e); + } } /// @@ -271,6 +278,8 @@ namespace RoboidControl { _position = value; positionUpdated = true; //OnPositionChanged?.Invoke(); + UpdateEvent e = new() { messageId = PoseMsg.Id }; + updateQueue.Enqueue(e); } } } @@ -413,6 +422,11 @@ namespace RoboidControl { OnNewThing?.Invoke(thing); } + public class UpdateEvent { + public int messageId; // see the communication messages + }; + public ConcurrentQueue updateQueue = new(); + #endregion Update ///