UpdateQueues First steps

This commit is contained in:
Pascal Serrarens 2025-05-01 16:04:25 +02:00
parent d6ac20f378
commit 19a6e22b16
7 changed files with 113 additions and 11 deletions

45
Unity/Participant.cs Normal file
View File

@ -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>();
touchSensor.coreSensor = coreTouchSensor;
break;
case RoboidControl.Thing coreThing:
GameObject thingObj = new("Thingg");
thingObj.transform.SetParent(this.transform);
Thing thing = thingObj.AddComponent<Thing>();
thing.core = coreThing;
break;
}
}
}
}

View File

@ -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>();
participant.coreParticipant = e.participant;
break;
case ThingMsg.id:
e.thing.CreateComponent();
break;
}
}
}
}

View File

@ -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)

View File

@ -14,6 +14,7 @@ namespace RoboidControl.Unity {
/// </summary>
public RoboidControl.TouchSensor coreSensor {
get => (RoboidControl.TouchSensor)base.core;
set => base.core = value;
}
SphereCollider collider = null;

View File

@ -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
/// <summary>
/// Update all things for this participant
/// </summary>
@ -91,6 +94,15 @@ namespace RoboidControl {
}
}
public class UpdateEvent {
public int messageId; // see the communication messages
public Thing thing;
public Participant participant;
}
public ConcurrentQueue<UpdateEvent> updateQueue = new();
#endregion Update
/// <summary>
/// The collection of known participants.
/// </summary>

View File

@ -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);
}
}

View File

@ -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);
}
}
/// <summary>
@ -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<UpdateEvent> updateQueue = new();
#endregion Update
/// <summary>