Object creatation/destruction works
This commit is contained in:
		
							parent
							
								
									6e85ef163d
								
							
						
					
					
						commit
						1225ee1097
					
				| @ -35,7 +35,6 @@ namespace RoboidControl.Unity { | |||||||
|             } |             } | ||||||
|             siteServer.site.Add(thing); |             siteServer.site.Add(thing); | ||||||
|             core.OnPoseChanged += PoseChanged; |             core.OnPoseChanged += PoseChanged; | ||||||
|             core.OnHierarchyChanged += ParentChanged; |  | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         public static Thing Create(RoboidControl.Thing core) { |         public static Thing Create(RoboidControl.Thing core) { | ||||||
| @ -60,15 +59,17 @@ namespace RoboidControl.Unity { | |||||||
|                 this.transform.localRotation = core.orientation.ToQuaternion(); |                 this.transform.localRotation = core.orientation.ToQuaternion(); | ||||||
| 
 | 
 | ||||||
|             core.OnPoseChanged += this.PoseChanged; |             core.OnPoseChanged += this.PoseChanged; | ||||||
|             core.OnHierarchyChanged += ParentChanged; |  | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// Update the Unity representation |         /// Update the Unity representation | ||||||
|         /// </summary> |         /// </summary> | ||||||
|         protected virtual void Update() { |         protected virtual void Update() { | ||||||
|             if (core == null) |             if (core == null) { | ||||||
|  |                 Debug.Log("Core thing is gone, self destruct in 0 seconds..."); | ||||||
|  |                 Destroy(this); | ||||||
|                 return; |                 return; | ||||||
|  |             } | ||||||
| 
 | 
 | ||||||
|             if (core.linearVelocity != null && core.linearVelocity.distance != 0) { |             if (core.linearVelocity != null && core.linearVelocity.distance != 0) { | ||||||
|                 Vector3 direction = Quaternion.AngleAxis(core.linearVelocity.direction.horizontal, Vector3.up) * Vector3.forward; |                 Vector3 direction = Quaternion.AngleAxis(core.linearVelocity.direction.horizontal, Vector3.up) * Vector3.forward; | ||||||
| @ -111,14 +112,6 @@ namespace RoboidControl.Unity { | |||||||
|                 this.transform.localRotation = core.orientation.ToQuaternion(); |                 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() { |         private IEnumerator LoadJPG() { | ||||||
|             UnityWebRequest request = UnityWebRequestTexture.GetTexture(core.modelUrl); |             UnityWebRequest request = UnityWebRequestTexture.GetTexture(core.modelUrl); | ||||||
|             yield return request.SendWebRequest(); |             yield return request.SendWebRequest(); | ||||||
|  | |||||||
| @ -26,9 +26,9 @@ namespace RoboidControl { | |||||||
|         /// </summary> |         /// </summary> | ||||||
|         /// <param name="networkId">The network ID of the thing</param> |         /// <param name="networkId">The network ID of the thing</param> | ||||||
|         /// <param name="thingId">The ID of the thing</param> |         /// <param name="thingId">The ID of the thing</param> | ||||||
|         public DestroyMsg(byte networkId, byte thingId) { |         public DestroyMsg(byte networkId, Thing thing) { | ||||||
|             this.networkId = networkId; |             this.networkId = networkId; | ||||||
|             this.thingId = thingId; |             this.thingId = thing.id; | ||||||
|         } |         } | ||||||
|         /// @copydoc Passer::RoboidControl::IMessage::IMessage(byte[] buffer) |         /// @copydoc Passer::RoboidControl::IMessage::IMessage(byte[] buffer) | ||||||
|         public DestroyMsg(byte[] buffer) : base(buffer) { |         public DestroyMsg(byte[] buffer) : base(buffer) { | ||||||
|  | |||||||
| @ -95,28 +95,30 @@ namespace RoboidControl { | |||||||
|         #region Update |         #region Update | ||||||
| 
 | 
 | ||||||
|         protected ulong nextPublishMe = 0; |         protected ulong nextPublishMe = 0; | ||||||
|         public override void Update(ulong currentTimeMS = 0) { |  | ||||||
|             if (currentTimeMS == 0) { |  | ||||||
| #if UNITY_5_3_OR_NEWER |  | ||||||
|                 currentTimeMS = (ulong)(UnityEngine.Time.time * 1000); |  | ||||||
| #endif |  | ||||||
|             } |  | ||||||
| 
 | 
 | ||||||
|  |         public override void Update(ulong currentTimeMS = 0) { | ||||||
|  |             if (currentTimeMS == 0) | ||||||
|  |                 currentTimeMS = Thing.GetTimeMs(); | ||||||
|  | 
 | ||||||
|  |             if (this.isIsolated == false) { | ||||||
|                 if (this.publishInterval > 0 && currentTimeMS > this.nextPublishMe) { |                 if (this.publishInterval > 0 && currentTimeMS > this.nextPublishMe) { | ||||||
|                 Publish(); |                     ParticipantMsg msg = new ParticipantMsg(this.networkId); | ||||||
|                 // Console.WriteLine($"{this.name} Publish ClientMsg {this.networkId}"); |                     if (this.remoteSite == null) | ||||||
|  |                         this.Publish(msg); | ||||||
|  |                     else | ||||||
|  |                         this.Send(this.remoteSite, msg); | ||||||
|  | 
 | ||||||
|                     this.nextPublishMe = currentTimeMS + this.publishInterval; |                     this.nextPublishMe = currentTimeMS + this.publishInterval; | ||||||
|                 } |                 } | ||||||
|  |             } | ||||||
| 
 | 
 | ||||||
|             UpdateMyThings(currentTimeMS); |             UpdateMyThings(currentTimeMS); | ||||||
|             UpdateOtherThings(currentTimeMS); |             UpdateOtherThings(currentTimeMS); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         protected virtual void UpdateMyThings(ulong currentTimeMS) { |         protected virtual void UpdateMyThings(ulong currentTimeMS) { | ||||||
|             int n = this.things.Count; |             foreach (Thing thing in this.things) { | ||||||
|             for (int ix = 0; ix < n; ix++) { |                 if (thing == null) | ||||||
|                 Thing thing = this.things[ix]; |  | ||||||
|                 if (thing == null || thing.parent != null) |  | ||||||
|                     continue; |                     continue; | ||||||
| 
 | 
 | ||||||
|                 if (thing.hierarchyChanged && !(this.isIsolated || this.networkId == 0)) { |                 if (thing.hierarchyChanged && !(this.isIsolated || this.networkId == 0)) { | ||||||
| @ -124,10 +126,17 @@ namespace RoboidControl { | |||||||
|                     this.Send(this.remoteSite, thingMsg); |                     this.Send(this.remoteSite, thingMsg); | ||||||
|                 } |                 } | ||||||
| 
 | 
 | ||||||
|                 thing.Update(currentTimeMS, true); |                 // Why don't we do recursive? | ||||||
|                 if (this.isIsolated || this.networkId == 0) |                 // Because when a thing creates a thing in the update, | ||||||
|                     continue; |                 // that new thing is not sent out (because of hierarchyChanged) | ||||||
| 
 |                 // before it is updated itself: it is immediatedly updated! | ||||||
|  |                 thing.Update(currentTimeMS, false); | ||||||
|  |                 if (!(this.isIsolated || this.networkId == 0)) { | ||||||
|  |                     if (thing.terminate) { | ||||||
|  |                         DestroyMsg destroyMsg = new(this.networkId, thing); | ||||||
|  |                         this.Send(this.remoteSite, destroyMsg); | ||||||
|  |                     } | ||||||
|  |                     else { | ||||||
|                         // Send to remote site |                         // Send to remote site | ||||||
|                         PoseMsg poseMsg = new(thing.owner.networkId, thing); |                         PoseMsg poseMsg = new(thing.owner.networkId, thing); | ||||||
|                         this.Send(this.remoteSite, poseMsg); |                         this.Send(this.remoteSite, poseMsg); | ||||||
| @ -135,6 +144,10 @@ namespace RoboidControl { | |||||||
|                         this.Send(this.remoteSite, binaryMsg); |                         this.Send(this.remoteSite, binaryMsg); | ||||||
|                     } |                     } | ||||||
|                 } |                 } | ||||||
|  |                 if (thing.terminate) | ||||||
|  |                     this.Remove(thing); | ||||||
|  |             } | ||||||
|  |         } | ||||||
| 
 | 
 | ||||||
|         protected virtual void UpdateOtherThings(ulong currentTimeMS) { |         protected virtual void UpdateOtherThings(ulong currentTimeMS) { | ||||||
|             for (int ownerIx = 0; ownerIx < Participant.participants.Count; ownerIx++) { |             for (int ownerIx = 0; ownerIx < Participant.participants.Count; ownerIx++) { | ||||||
| @ -147,6 +160,9 @@ namespace RoboidControl { | |||||||
|                     continue; |                     continue; | ||||||
| 
 | 
 | ||||||
|                 foreach (Thing thing in participant.things) { |                 foreach (Thing thing in participant.things) { | ||||||
|  |                     if (thing == null) | ||||||
|  |                         continue; | ||||||
|  | 
 | ||||||
|                     PoseMsg poseMsg = new(thing.owner.networkId, thing); |                     PoseMsg poseMsg = new(thing.owner.networkId, thing); | ||||||
|                     this.Send(participant, poseMsg); |                     this.Send(participant, poseMsg); | ||||||
|                     BinaryMsg binaryMsg = new(thing.owner.networkId, thing); |                     BinaryMsg binaryMsg = new(thing.owner.networkId, thing); | ||||||
| @ -179,10 +195,6 @@ namespace RoboidControl { | |||||||
|             return true; |             return true; | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         public virtual void Publish() { |  | ||||||
|             this.Publish(new ParticipantMsg(this.networkId)); |  | ||||||
|         } |  | ||||||
| 
 |  | ||||||
|         public void PublishThingInfo(Thing thing) { |         public void PublishThingInfo(Thing thing) { | ||||||
|             // Console.WriteLine("Publish thing info"); |             // Console.WriteLine("Publish thing info"); | ||||||
|             this.Publish(new ThingMsg(this.networkId, thing)); |             this.Publish(new ThingMsg(this.networkId, thing)); | ||||||
| @ -383,7 +395,10 @@ namespace RoboidControl { | |||||||
| #if DEBUG | #if DEBUG | ||||||
|             Console.WriteLine($"Participant: Process Destroy Msg [{msg.networkId}/{msg.thingId}]"); |             Console.WriteLine($"Participant: Process Destroy Msg [{msg.networkId}/{msg.thingId}]"); | ||||||
| #endif             | #endif             | ||||||
| 
 |             Thing thing = sender.Get(msg.networkId, msg.thingId); | ||||||
|  |             if (thing != null) | ||||||
|  |                 this.Remove(thing); | ||||||
|  |             thing.component.core = null; | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         private void ForwardMessage(IMessage msg) { |         private void ForwardMessage(IMessage msg) { | ||||||
| @ -30,17 +30,16 @@ namespace RoboidControl { | |||||||
|             this.udpClient?.Close(); |             this.udpClient?.Close(); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|  |         #region Update | ||||||
|  | 
 | ||||||
|         protected override void UpdateMyThings(ulong currentTimeMS) { |         protected override void UpdateMyThings(ulong currentTimeMS) { | ||||||
|             int n = this.things.Count; |             foreach (Thing thing in this.things) { | ||||||
|             for (int ix = 0; ix < n; ix++) { |                 if (thing == null) | ||||||
|                 Thing thing = this.things[ix]; |  | ||||||
|                 if (thing == null || thing.parent != null) |  | ||||||
|                     continue; |                     continue; | ||||||
| 
 | 
 | ||||||
|                 thing.Update(currentTimeMS, true); |                 thing.Update(currentTimeMS, false); | ||||||
|                 if (this.isIsolated || this.networkId == 0) |  | ||||||
|                     continue; |  | ||||||
| 
 | 
 | ||||||
|  |                 if (this.isIsolated == false) { | ||||||
|                     // Send to all other participants |                     // Send to all other participants | ||||||
|                     foreach (Participant participant in Participant.participants) { |                     foreach (Participant participant in Participant.participants) { | ||||||
|                         if (participant == null || participant == this) |                         if (participant == null || participant == this) | ||||||
| @ -53,16 +52,18 @@ namespace RoboidControl { | |||||||
|                     } |                     } | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
| 
 |  | ||||||
|         public override void Publish() { |  | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|  |         #endregion Update | ||||||
|  | 
 | ||||||
|  |         #region Receive | ||||||
|  | 
 | ||||||
|         protected override void Process(Participant sender, ParticipantMsg msg) { |         protected override void Process(Participant sender, ParticipantMsg msg) { | ||||||
|             base.Process(sender, msg); |             base.Process(sender, msg); | ||||||
|             //if (msg.networkId == 0) { |             if (msg.networkId != sender.networkId) { | ||||||
|                 //Console.WriteLine($"{this.name} received New Participant -> {sender.networkId}"); |                 //Console.WriteLine($"{this.name} received New Participant -> {sender.networkId}"); | ||||||
|                 this.Send(sender, new NetworkIdMsg(sender.networkId)); |                 this.Send(sender, new NetworkIdMsg(sender.networkId)); | ||||||
|             //} |             } | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         protected override void Process(Participant sender, NetworkIdMsg msg) { } |         protected override void Process(Participant sender, NetworkIdMsg msg) { } | ||||||
| @ -70,23 +71,22 @@ namespace RoboidControl { | |||||||
|         protected override void Process(Participant sender, ThingMsg msg) { |         protected override void Process(Participant sender, ThingMsg msg) { | ||||||
|             Console.WriteLine($"SiteServer: Process thing [{msg.networkId}/{msg.thingId}] {msg.thingType} {msg.parentId} "); |             Console.WriteLine($"SiteServer: Process thing [{msg.networkId}/{msg.thingId}] {msg.thingType} {msg.parentId} "); | ||||||
|             Thing thing = sender.Get(msg.networkId, msg.thingId); |             Thing thing = sender.Get(msg.networkId, msg.thingId); | ||||||
|             if (thing == null) { |             if (thing == null) | ||||||
|                 thing = new Thing(sender, msg.networkId, msg.thingId, msg.thingType);             |                 thing = new Thing(sender, msg.networkId, msg.thingId, msg.thingType);             | ||||||
|                 // Console.WriteLine("Created generic new core thing"); |  | ||||||
|                 // } |  | ||||||
|             } |  | ||||||
| 
 | 
 | ||||||
|             if (msg.parentId != 0) { |             if (msg.parentId != 0) { | ||||||
|                 Thing parentThing = Get(msg.networkId, msg.parentId); |                 thing.parent = Get(msg.networkId, msg.parentId); | ||||||
|                 if (parentThing == null) |                 if (thing.parent == null) | ||||||
|                     Console.WriteLine($"Could not find parent [{msg.networkId}/{msg.parentId}]"); |                     Console.WriteLine($"Could not find parent [{msg.networkId}/{msg.parentId}]"); | ||||||
|                 else |  | ||||||
|                     thing.parent = parentThing;                     |  | ||||||
|             } |             } | ||||||
|             else { |             else { | ||||||
|                 Console.Write($"Dropped {thing.id}"); |                 Console.Write($"Dropped {thing.id}"); | ||||||
|                 thing.parent = null; |                 thing.parent = null; | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|  | 
 | ||||||
|  |         #endregion Receive | ||||||
|  | 
 | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
| } | } | ||||||
							
								
								
									
										13
									
								
								src/Thing.cs
									
									
									
									
									
								
							
							
						
						
									
										13
									
								
								src/Thing.cs
									
									
									
									
									
								
							| @ -146,14 +146,9 @@ namespace RoboidControl { | |||||||
|                 else { |                 else { | ||||||
|                     value.AddChild(this); |                     value.AddChild(this); | ||||||
|                 } |                 } | ||||||
|                 OnHierarchyChanged?.Invoke(); |  | ||||||
|                 this.hierarchyChanged = true; |                 this.hierarchyChanged = true; | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|         /// <summary> |  | ||||||
|         /// Event which is triggered when the parent changes |  | ||||||
|         /// </summary> |  | ||||||
|         public event ChangeHandler OnHierarchyChanged = delegate { }; |  | ||||||
| 
 | 
 | ||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// Add a child Thing to this Thing |         /// Add a child Thing to this Thing | ||||||
| @ -352,10 +347,18 @@ namespace RoboidControl { | |||||||
|         public Unity.Thing component = null; |         public Unity.Thing component = null; | ||||||
| #endif | #endif | ||||||
| 
 | 
 | ||||||
|  |         public bool terminate = false; | ||||||
|  | 
 | ||||||
|         #endregion Properties |         #endregion Properties | ||||||
| 
 | 
 | ||||||
|         #region Methods |         #region Methods | ||||||
| 
 | 
 | ||||||
|  |         public static ulong GetTimeMs() { | ||||||
|  | #if UNITY_5_3_OR_NEWER | ||||||
|  |             return (ulong)(UnityEngine.Time.time * 1000); | ||||||
|  | #endif | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// Update de state of the thing |         /// Update de state of the thing | ||||||
|         /// </summary> |         /// </summary> | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user