Implemented updateQueues

This commit is contained in:
Pascal Serrarens 2025-05-01 17:39:50 +02:00
parent 19a6e22b16
commit 71adf127d9
5 changed files with 48 additions and 85 deletions

View File

@ -27,16 +27,12 @@ namespace RoboidControl.Unity {
private void HandleThingEvent(RoboidControl.Participant.UpdateEvent e) { private void HandleThingEvent(RoboidControl.Participant.UpdateEvent e) {
switch (e.thing) { switch (e.thing) {
case RoboidControl.TouchSensor coreTouchSensor: case RoboidControl.TouchSensor coreTouchSensor:
GameObject touchObj = new("Touch Sensor"); TouchSensor touchSensor = TouchSensor.Create(coreTouchSensor);
touchObj.transform.SetParent(this.transform); coreTouchSensor.component = touchSensor;
TouchSensor touchSensor = touchObj.AddComponent<TouchSensor>();
touchSensor.coreSensor = coreTouchSensor;
break; break;
case RoboidControl.Thing coreThing: case RoboidControl.Thing coreThing:
GameObject thingObj = new("Thingg"); Thing thing = Thing.Create(coreThing);
thingObj.transform.SetParent(this.transform); coreThing.component = thing;
Thing thing = thingObj.AddComponent<Thing>();
thing.core = coreThing;
break; break;
} }
} }

View File

@ -33,7 +33,6 @@ namespace RoboidControl.Unity {
return; return;
} }
siteServer.site.Add(thing); siteServer.site.Add(thing);
core.OnPoseChanged += PoseChanged;
} }
public static Thing Create(RoboidControl.Thing core) { public static Thing Create(RoboidControl.Thing core) {
@ -56,8 +55,6 @@ namespace RoboidControl.Unity {
this.transform.localPosition = core.position.ToVector3(); this.transform.localPosition = core.position.ToVector3();
if (core.orientation != null) if (core.orientation != null)
this.transform.localRotation = core.orientation.ToQuaternion(); this.transform.localRotation = core.orientation.ToQuaternion();
core.OnPoseChanged += this.PoseChanged;
} }
/// <summary> /// <summary>
@ -82,36 +79,30 @@ namespace RoboidControl.Unity {
Vector3 axis = core.angularVelocity.direction.ToVector3(); Vector3 axis = core.angularVelocity.direction.ToVector3();
this.transform.localRotation *= Quaternion.AngleAxis(core.angularVelocity.distance * Time.deltaTime, axis); 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) { private void HandleUpdateEvent(RoboidControl.Thing.UpdateEvent e) {
switch (e.messageId) { switch (e.messageId) {
case ThingMsg.id: 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; break;
case PoseMsg.Id: case PoseMsg.Id:
if (core.linearVelocity.distance == 0)
this.transform.localPosition = core.position.ToVector3(); this.transform.localPosition = core.position.ToVector3();
if (core.angularVelocity.distance == 0)
this.transform.localRotation = core.orientation.ToQuaternion(); this.transform.localRotation = core.orientation.ToQuaternion();
break; break;
} }

View File

@ -17,7 +17,7 @@ namespace RoboidControl.Unity {
set => base.core = value; set => base.core = value;
} }
SphereCollider collider = null; SphereCollider touchCollider = null;
/// <summary> /// <summary>
/// Start the Unity represention /// Start the Unity represention
@ -27,7 +27,7 @@ namespace RoboidControl.Unity {
participant = FindAnyObjectByType<SiteServer>(); participant = FindAnyObjectByType<SiteServer>();
SetCoreThing(new RoboidControl.TouchSensor(participant.site)); SetCoreThing(new RoboidControl.TouchSensor(participant.site));
} }
collider = GetComponent<SphereCollider>(); touchCollider = GetComponent<SphereCollider>();
} }
/// <summary> /// <summary>
@ -59,10 +59,10 @@ namespace RoboidControl.Unity {
protected override void Update() { protected override void Update() {
base.Update(); base.Update();
if (collider.radius == 0.01f && if (touchCollider.radius == 0.01f &&
this.transform.parent != null && this.transform.localPosition.magnitude > 0 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; this.transform.position = (this.transform.position + this.transform.parent.position) / 2;
} }
} }

View File

@ -111,7 +111,7 @@ namespace RoboidControl {
protected override void Process(Participant sender, NetworkIdMsg msg) { } protected override void Process(Participant sender, NetworkIdMsg msg) { }
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($"{this.name}: Process thing [{msg.networkId}/{msg.thingId}] {msg.thingType} {msg.parentId} ");
Thing thing = sender.Get(msg.thingId); Thing thing = sender.Get(msg.thingId);
if (thing == null) { if (thing == null) {

View File

@ -58,7 +58,6 @@ namespace RoboidControl {
if (this.owner != null) if (this.owner != null)
this.owner.Add(this); this.owner.Add(this);
if (invokeEvent) { if (invokeEvent) {
//InvokeNewThing(this);
Participant.UpdateEvent e = new() { Participant.UpdateEvent e = new() {
messageId = ThingMsg.id, messageId = ThingMsg.id,
thing = this thing = this
@ -138,20 +137,25 @@ namespace RoboidControl {
if (_name != value) { if (_name != value) {
_name = value; _name = value;
nameChanged = true; nameChanged = true;
OnNameChanged?.Invoke(); this.updateQueue.Enqueue(new UpdateEvent(NameMsg.Id));
} }
} }
} }
/// <summary>
/// Event which is triggered when the name changes
/// </summary>
public event ChangeHandler OnNameChanged = delegate { };
public bool nameChanged = false; public bool nameChanged = false;
private string _modelUrl = "";
/// <summary> /// <summary>
/// An URL pointing to the location where a model of the thing can be found /// An URL pointing to the location where a model of the thing can be found
/// </summary> /// </summary>
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 #if UNITY_5_3_OR_NEWER
/// <summary> /// <summary>
@ -183,6 +187,7 @@ namespace RoboidControl {
value.AddChild(this); value.AddChild(this);
} }
this.hierarchyChanged = true; this.hierarchyChanged = true;
this.updateQueue.Enqueue(new UpdateEvent(ThingMsg.id));
} }
} }
@ -277,17 +282,11 @@ namespace RoboidControl {
if (_position != value) { if (_position != value) {
_position = value; _position = value;
positionUpdated = true; positionUpdated = true;
//OnPositionChanged?.Invoke(); updateQueue.Enqueue(new UpdateEvent(PoseMsg.Id));
UpdateEvent e = new() { messageId = PoseMsg.Id };
updateQueue.Enqueue(e);
} }
} }
} }
/// <summary> /// <summary>
/// Event triggered when the pose has changed
/// </summary>
public event ChangeHandler OnPoseChanged = delegate { };
/// <summary>
/// Boolean indicating that the thing has an updated position /// Boolean indicating that the thing has an updated position
/// </summary> /// </summary>
public bool positionUpdated = false; public bool positionUpdated = false;
@ -321,15 +320,11 @@ namespace RoboidControl {
if (_linearVelocity != value) { if (_linearVelocity != value) {
_linearVelocity = value; _linearVelocity = value;
linearVelocityUpdated = true; linearVelocityUpdated = true;
OnLinearVelocityChanged?.Invoke(_linearVelocity); updateQueue.Enqueue(new UpdateEvent(PoseMsg.Id));
} }
} }
} }
/// <summary> /// <summary>
/// Event triggered when the linear velocity has changed
/// </summary>
public event SphericalHandler OnLinearVelocityChanged = delegate { };
/// <summary>
/// Boolean indicating the thing has an updated linear velocity /// Boolean indicating the thing has an updated linear velocity
/// </summary> /// </summary>
public bool linearVelocityUpdated = false; public bool linearVelocityUpdated = false;
@ -344,15 +339,11 @@ namespace RoboidControl {
if (_angularVelocity != value) { if (_angularVelocity != value) {
_angularVelocity = value; _angularVelocity = value;
angularVelocityUpdated = true; angularVelocityUpdated = true;
OnAngularVelocityChanged?.Invoke(_angularVelocity); updateQueue.Enqueue(new UpdateEvent(PoseMsg.Id));
} }
} }
} }
/// <summary> /// <summary>
/// Event triggered when the angular velocity has changed
/// </summary>
public event SphericalHandler OnAngularVelocityChanged = delegate { };
/// <summary>
/// Boolean indicating the thing has an updated angular velocity /// Boolean indicating the thing has an updated angular velocity
/// </summary> /// </summary>
public bool angularVelocityUpdated = false; public bool angularVelocityUpdated = false;
@ -387,8 +378,6 @@ namespace RoboidControl {
/// <param name="currentTime">he current clock time in milliseconds; if this is zero, the current time is retrieved automatically</param> /// <param name="currentTime">he current clock time in milliseconds; if this is zero, the current time is retrieved automatically</param>
/// <param name="recurse">When true, this will Update the descendants recursively</param> /// <param name="recurse">When true, this will Update the descendants recursively</param>
public virtual void Update(ulong currentTimeMs, bool recurse = false) { public virtual void Update(ulong currentTimeMs, bool recurse = false) {
if (this.positionUpdated || this.orientationUpdated)
OnPoseChanged?.Invoke();
this.positionUpdated = false; this.positionUpdated = false;
this.orientationUpdated = false; this.orientationUpdated = false;
this.linearVelocityUpdated = 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);
/// <summary>
/// Event triggered when a new thing has been created
/// </summary>
public static event ThingHandler OnNewThing = delegate { };
/// <summary>
/// Trigger the creation for the given thing
/// </summary>
/// <param name="thing">The created thing</param>
public static void InvokeNewThing(Thing thing) {
OnNewThing?.Invoke(thing);
}
public class UpdateEvent { public class UpdateEvent {
public UpdateEvent(int messageId) {
this.messageId = messageId;
}
public int messageId; // see the communication messages public int messageId; // see the communication messages
}; };
public ConcurrentQueue<UpdateEvent> updateQueue = new(); public ConcurrentQueue<UpdateEvent> updateQueue = new();