diff --git a/Unity/Participant.cs b/Unity/Participant.cs index 8b0c5f5..1921ef2 100644 --- a/Unity/Participant.cs +++ b/Unity/Participant.cs @@ -27,16 +27,12 @@ namespace RoboidControl.Unity { 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; + TouchSensor touchSensor = TouchSensor.Create(coreTouchSensor); + coreTouchSensor.component = touchSensor; break; case RoboidControl.Thing coreThing: - GameObject thingObj = new("Thingg"); - thingObj.transform.SetParent(this.transform); - Thing thing = thingObj.AddComponent(); - thing.core = coreThing; + Thing thing = Thing.Create(coreThing); + coreThing.component = thing; break; } } diff --git a/Unity/Thing.cs b/Unity/Thing.cs index 5ac2117..700ca87 100644 --- a/Unity/Thing.cs +++ b/Unity/Thing.cs @@ -33,7 +33,6 @@ namespace RoboidControl.Unity { return; } siteServer.site.Add(thing); - core.OnPoseChanged += PoseChanged; } public static Thing Create(RoboidControl.Thing core) { @@ -56,8 +55,6 @@ namespace RoboidControl.Unity { this.transform.localPosition = core.position.ToVector3(); if (core.orientation != null) this.transform.localRotation = core.orientation.ToQuaternion(); - - core.OnPoseChanged += this.PoseChanged; } /// @@ -71,7 +68,7 @@ namespace RoboidControl.Unity { } if (core.updateQueue.TryDequeue(out RoboidControl.Thing.UpdateEvent e)) - HandleUpdateEvent(e); + HandleUpdateEvent(e); if (core.linearVelocity != null && core.linearVelocity.distance != 0) { Vector3 direction = Quaternion.AngleAxis(core.linearVelocity.direction.horizontal, Vector3.up) * Vector3.forward; @@ -82,37 +79,31 @@ namespace RoboidControl.Unity { Vector3 axis = core.angularVelocity.direction.ToVector3(); this.transform.localRotation *= Quaternion.AngleAxis(core.angularVelocity.distance * Time.deltaTime, axis); } - - if (!string.IsNullOrEmpty(core.modelUrl) && this.modelUrl == null) { - string extension = core.modelUrl.Substring(core.modelUrl.LastIndexOf(".")); - if (extension == ".jpg" || extension == ".png") { - StartCoroutine(LoadJPG()); - } - - this.modelUrl = core.modelUrl; - } - if (core.nameChanged) { - if (this.gameObject.name != core.name) - this.gameObject.name = core.name; - core.nameChanged = false; - } - if (core.hierarchyChanged) { - // Debug.Log("Parent changed"); - if (core.parent == null) - this.transform.SetParent(null, true); - else - this.transform.SetParent(core.parent.component.transform, true); - core.hierarchyChanged = false; - } } private void HandleUpdateEvent(RoboidControl.Thing.UpdateEvent e) { - switch(e.messageId) { - case ThingMsg.id: + switch (e.messageId) { + case ThingMsg.id: + if (core.parent == null) + this.transform.SetParent(null, true); + else if (core.parent.component != null) + this.transform.SetParent(core.parent.component.transform, true); + break; + case NameMsg.Id: + this.gameObject.name = core.name; + break; + case ModelUrlMsg.Id: + string extension = core.modelUrl[core.modelUrl.LastIndexOf(".")..]; + if (extension == ".jpg" || extension == ".png") + StartCoroutine(LoadJPG()); + + this.modelUrl = core.modelUrl; break; case PoseMsg.Id: - this.transform.localPosition = core.position.ToVector3(); - this.transform.localRotation = core.orientation.ToQuaternion(); + if (core.linearVelocity.distance == 0) + this.transform.localPosition = core.position.ToVector3(); + if (core.angularVelocity.distance == 0) + this.transform.localRotation = core.orientation.ToQuaternion(); break; } } diff --git a/Unity/TouchSensor.cs b/Unity/TouchSensor.cs index da2f0e7..0c4e593 100644 --- a/Unity/TouchSensor.cs +++ b/Unity/TouchSensor.cs @@ -17,7 +17,7 @@ namespace RoboidControl.Unity { set => base.core = value; } - SphereCollider collider = null; + SphereCollider touchCollider = null; /// /// Start the Unity represention @@ -27,7 +27,7 @@ namespace RoboidControl.Unity { participant = FindAnyObjectByType(); SetCoreThing(new RoboidControl.TouchSensor(participant.site)); } - collider = GetComponent(); + touchCollider = GetComponent(); } /// @@ -59,10 +59,10 @@ namespace RoboidControl.Unity { protected override void Update() { base.Update(); - if (collider.radius == 0.01f && + if (touchCollider.radius == 0.01f && this.transform.parent != null && this.transform.localPosition.magnitude > 0 ) { - collider.radius = Vector3.Distance(this.transform.position, this.transform.parent.position) / 2; + touchCollider.radius = Vector3.Distance(this.transform.position, this.transform.parent.position) / 2; this.transform.position = (this.transform.position + this.transform.parent.position) / 2; } } diff --git a/src/Participants/SiteServer.cs b/src/Participants/SiteServer.cs index 6c5c795..c97154a 100644 --- a/src/Participants/SiteServer.cs +++ b/src/Participants/SiteServer.cs @@ -111,7 +111,7 @@ namespace RoboidControl { protected override void Process(Participant sender, NetworkIdMsg msg) { } protected override void Process(Participant sender, ThingMsg msg) { - Console.WriteLine($"SiteServer: Process thing [{msg.networkId}/{msg.thingId}] {msg.thingType} {msg.parentId} "); + Console.WriteLine($"{this.name}: Process thing [{msg.networkId}/{msg.thingId}] {msg.thingType} {msg.parentId} "); Thing thing = sender.Get(msg.thingId); if (thing == null) { diff --git a/src/Thing.cs b/src/Thing.cs index 184cc09..cee404f 100644 --- a/src/Thing.cs +++ b/src/Thing.cs @@ -58,7 +58,6 @@ namespace RoboidControl { if (this.owner != null) this.owner.Add(this); if (invokeEvent) { - //InvokeNewThing(this); Participant.UpdateEvent e = new() { messageId = ThingMsg.id, thing = this @@ -138,20 +137,25 @@ namespace RoboidControl { if (_name != value) { _name = value; nameChanged = true; - OnNameChanged?.Invoke(); + this.updateQueue.Enqueue(new UpdateEvent(NameMsg.Id)); } } } - /// - /// Event which is triggered when the name changes - /// - public event ChangeHandler OnNameChanged = delegate { }; public bool nameChanged = false; + private string _modelUrl = ""; /// /// An URL pointing to the location where a model of the thing can be found /// - public string modelUrl = ""; + public string modelUrl { + get => _modelUrl; + set { + if (_modelUrl != value) { + _modelUrl = value; + this.updateQueue.Enqueue(new UpdateEvent(ModelUrlMsg.Id)); + } + } + } #if UNITY_5_3_OR_NEWER /// @@ -183,6 +187,7 @@ namespace RoboidControl { value.AddChild(this); } this.hierarchyChanged = true; + this.updateQueue.Enqueue(new UpdateEvent(ThingMsg.id)); } } @@ -277,17 +282,11 @@ namespace RoboidControl { if (_position != value) { _position = value; positionUpdated = true; - //OnPositionChanged?.Invoke(); - UpdateEvent e = new() { messageId = PoseMsg.Id }; - updateQueue.Enqueue(e); + updateQueue.Enqueue(new UpdateEvent(PoseMsg.Id)); } } } /// - /// Event triggered when the pose has changed - /// - public event ChangeHandler OnPoseChanged = delegate { }; - /// /// Boolean indicating that the thing has an updated position /// public bool positionUpdated = false; @@ -321,15 +320,11 @@ namespace RoboidControl { if (_linearVelocity != value) { _linearVelocity = value; linearVelocityUpdated = true; - OnLinearVelocityChanged?.Invoke(_linearVelocity); + updateQueue.Enqueue(new UpdateEvent(PoseMsg.Id)); } } } /// - /// Event triggered when the linear velocity has changed - /// - public event SphericalHandler OnLinearVelocityChanged = delegate { }; - /// /// Boolean indicating the thing has an updated linear velocity /// public bool linearVelocityUpdated = false; @@ -344,15 +339,11 @@ namespace RoboidControl { if (_angularVelocity != value) { _angularVelocity = value; angularVelocityUpdated = true; - OnAngularVelocityChanged?.Invoke(_angularVelocity); + updateQueue.Enqueue(new UpdateEvent(PoseMsg.Id)); } } } /// - /// Event triggered when the angular velocity has changed - /// - public event SphericalHandler OnAngularVelocityChanged = delegate { }; - /// /// Boolean indicating the thing has an updated angular velocity /// public bool angularVelocityUpdated = false; @@ -387,8 +378,6 @@ namespace RoboidControl { /// he current clock time in milliseconds; if this is zero, the current time is retrieved automatically /// When true, this will Update the descendants recursively public virtual void Update(ulong currentTimeMs, bool recurse = false) { - if (this.positionUpdated || this.orientationUpdated) - OnPoseChanged?.Invoke(); this.positionUpdated = false; this.orientationUpdated = false; this.linearVelocityUpdated = false; @@ -406,23 +395,10 @@ namespace RoboidControl { } } - public delegate void ChangeHandler(); - public delegate void SphericalHandler(Spherical v); - public delegate void ThingHandler(Thing t); - - /// - /// Event triggered when a new thing has been created - /// - public static event ThingHandler OnNewThing = delegate { }; - /// - /// Trigger the creation for the given thing - /// - /// The created thing - public static void InvokeNewThing(Thing thing) { - OnNewThing?.Invoke(thing); - } - public class UpdateEvent { + public UpdateEvent(int messageId) { + this.messageId = messageId; + } public int messageId; // see the communication messages }; public ConcurrentQueue updateQueue = new();