diff --git a/Unity/Thing.cs b/Unity/Thing.cs index 7804c3b..676a335 100644 --- a/Unity/Thing.cs +++ b/Unity/Thing.cs @@ -35,6 +35,7 @@ namespace RoboidControl.Unity { } siteServer.site.Add(thing); core.OnPoseChanged += PoseChanged; + core.OnHierarchyChanged += ParentChanged; } public static Thing Create(RoboidControl.Thing core) { @@ -59,6 +60,7 @@ namespace RoboidControl.Unity { this.transform.localRotation = core.orientation.ToQuaternion(); core.OnPoseChanged += this.PoseChanged; + core.OnHierarchyChanged += ParentChanged; } /// @@ -72,18 +74,11 @@ namespace RoboidControl.Unity { Vector3 direction = Quaternion.AngleAxis(core.linearVelocity.direction.horizontal, Vector3.up) * Vector3.forward; this.transform.Translate(core.linearVelocity.distance * Time.deltaTime * direction, Space.Self); } - // else if (core.positionUpdated) - // this.transform.localPosition = core.position.ToVector3(); if (core.angularVelocity != null && core.angularVelocity.distance != 0) { - // Vector3 angularVelocity = core.angularVelocity.ToVector3(); - // Debug.Log(angularVelocity); Vector3 axis = core.angularVelocity.direction.ToVector3(); this.transform.localRotation *= Quaternion.AngleAxis(core.angularVelocity.distance * Time.deltaTime, axis); - //this.transform.localRotation *= Quaternion.Euler(angularVelocity * Time.deltaTime); } - // else if (core.orientationUpdated) - // this.transform.localRotation = core.orientation.ToQuaternion(); if (!string.IsNullOrEmpty(core.modelUrl) && this.modelUrl == null) { string extension = core.modelUrl.Substring(core.modelUrl.LastIndexOf(".")); @@ -98,7 +93,14 @@ namespace RoboidControl.Unity { 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 PoseChanged() { @@ -109,6 +111,14 @@ namespace RoboidControl.Unity { this.transform.localRotation = core.orientation.ToQuaternion(); } + private void ParentChanged() { + + // if (core.parent == null) + // this.transform.SetParent(null, true); + // else + // this.transform.SetParent(core.parent.component.transform, true); + } + private IEnumerator LoadJPG() { UnityWebRequest request = UnityWebRequestTexture.GetTexture(core.modelUrl); yield return request.SendWebRequest(); diff --git a/src/ParticipantUDP.cs b/src/ParticipantUDP.cs index 2f261b9..e6aeb5d 100644 --- a/src/ParticipantUDP.cs +++ b/src/ParticipantUDP.cs @@ -94,27 +94,6 @@ namespace RoboidControl { #region Update - protected void ReceiveUDP(IAsyncResult result) { - // UnityEngine.Debug.Log("received"); - if (this.udpClient == null) // || this.endPoint == null) - return; - - byte[] data = this.udpClient.EndReceive(result, ref endPoint); - // This does not yet take multi-packet messages into account! - if (endPoint == null) - return; - - // We can receive our own publish (broadcast) packages. How do we recognize them???? - // It is hard to determine our source port - string ipAddress = endPoint.Address.ToString(); - Participant remoteParticipant = GetParticipant(ipAddress, endPoint.Port); - remoteParticipant ??= AddParticipant(ipAddress, endPoint.Port); - - ReceiveData(data, remoteParticipant); - - udpClient.BeginReceive(new AsyncCallback(callbackResult => ReceiveUDP(callbackResult)), null); - } - protected ulong nextPublishMe = 0; public override void Update(ulong currentTimeMS = 0) { if (currentTimeMS == 0) { @@ -139,6 +118,11 @@ namespace RoboidControl { Thing thing = this.things[ix]; if (thing == null || thing.parent != null) continue; + + if (thing.hierarchyChanged && !(this.isIsolated || this.networkId == 0)) { + ThingMsg thingMsg = new(this.networkId, thing); + this.Send(this.remoteSite, thingMsg); + } thing.Update(currentTimeMS, true); if (this.isIsolated || this.networkId == 0) @@ -172,10 +156,6 @@ namespace RoboidControl { } - public virtual void Publish() { - this.Publish(new ParticipantMsg(this.networkId)); - } - #endregion Update #region Send @@ -199,6 +179,10 @@ namespace RoboidControl { return true; } + public virtual void Publish() { + this.Publish(new ParticipantMsg(this.networkId)); + } + public void PublishThingInfo(Thing thing) { // Console.WriteLine("Publish thing info"); this.Publish(new ThingMsg(this.networkId, thing)); @@ -239,6 +223,27 @@ namespace RoboidControl { #region Receive + protected void ReceiveUDP(IAsyncResult result) { + // UnityEngine.Debug.Log("received"); + if (this.udpClient == null) // || this.endPoint == null) + return; + + byte[] data = this.udpClient.EndReceive(result, ref endPoint); + // This does not yet take multi-packet messages into account! + if (endPoint == null) + return; + + // We can receive our own publish (broadcast) packages. How do we recognize them???? + // It is hard to determine our source port + string ipAddress = endPoint.Address.ToString(); + Participant remoteParticipant = GetParticipant(ipAddress, endPoint.Port); + remoteParticipant ??= AddParticipant(ipAddress, endPoint.Port); + + ReceiveData(data, remoteParticipant); + + udpClient.BeginReceive(new AsyncCallback(callbackResult => ReceiveUDP(callbackResult)), null); + } + public void ReceiveData(byte[] data, Participant sender) { byte msgId = data[0]; if (msgId == 0xFF) { diff --git a/src/SiteServer.cs b/src/SiteServer.cs index 51abbfb..6a7c905 100644 --- a/src/SiteServer.cs +++ b/src/SiteServer.cs @@ -81,7 +81,11 @@ namespace RoboidControl { if (parentThing == null) Console.WriteLine($"Could not find parent [{msg.networkId}/{msg.parentId}]"); else - thing.parent = parentThing; + thing.parent = parentThing; + } + else { + Console.Write($"Dropped {thing.id}"); + thing.parent = null; } } } diff --git a/src/Thing.cs b/src/Thing.cs index 4c75727..856277d 100644 --- a/src/Thing.cs +++ b/src/Thing.cs @@ -145,14 +145,15 @@ namespace RoboidControl { } else { value.AddChild(this); - OnParentChanged?.Invoke(); } + OnHierarchyChanged?.Invoke(); + this.hierarchyChanged = true; } } /// /// Event which is triggered when the parent changes /// - public event ChangeHandler OnParentChanged = delegate { }; + public event ChangeHandler OnHierarchyChanged = delegate { }; /// /// Add a child Thing to this Thing @@ -219,6 +220,11 @@ namespace RoboidControl { return null; } + /// + /// Indicator that the hierarchy of the thing has changed + /// + public bool hierarchyChanged = true; + /// /// The children of this thing /// @@ -368,7 +374,8 @@ namespace RoboidControl { this.positionUpdated = false; this.orientationUpdated = false; this.linearVelocityUpdated = false; - this.angularVelocityUpdated = false; + this.angularVelocityUpdated = false; + //this.hierarchyChanged = false; // should recurse over children... if (recursively) {