UpdateQueues First steps
This commit is contained in:
parent
d6ac20f378
commit
19a6e22b16
45
Unity/Participant.cs
Normal file
45
Unity/Participant.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -5,8 +5,7 @@ using UnityEngine;
|
|||||||
|
|
||||||
namespace RoboidControl.Unity {
|
namespace RoboidControl.Unity {
|
||||||
|
|
||||||
public class SiteServer : MonoBehaviour {
|
public class SiteServer : Participant {
|
||||||
public int port = 7681;
|
|
||||||
|
|
||||||
public RoboidControl.SiteServer site;
|
public RoboidControl.SiteServer site;
|
||||||
|
|
||||||
@ -16,7 +15,6 @@ namespace RoboidControl.Unity {
|
|||||||
Console.SetOut(new UnityLogWriter());
|
Console.SetOut(new UnityLogWriter());
|
||||||
|
|
||||||
site = new RoboidControl.SiteServer(port);
|
site = new RoboidControl.SiteServer(port);
|
||||||
RoboidControl.Thing.OnNewThing += HandleNewThing;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void OnApplicationQuit() {
|
void OnApplicationQuit() {
|
||||||
@ -24,17 +22,30 @@ namespace RoboidControl.Unity {
|
|||||||
site.Close();
|
site.Close();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void HandleNewThing(RoboidControl.Thing thing) {
|
protected override void Update() {
|
||||||
//Debug.Log($"Handle New thing event for {thing}");
|
if (site == null)
|
||||||
//site.Add(thing, false);
|
return;
|
||||||
thingQueue.Enqueue(thing);
|
|
||||||
}
|
if (site.updateQueue.TryDequeue(out RoboidControl.Participant.UpdateEvent e))
|
||||||
|
HandleUpdateEvent(e);
|
||||||
|
|
||||||
protected virtual void Update() {
|
|
||||||
site.Update((ulong)(Time.time * 1000));
|
site.Update((ulong)(Time.time * 1000));
|
||||||
while (thingQueue.TryDequeue(out RoboidControl.Thing thing))
|
while (thingQueue.TryDequeue(out RoboidControl.Thing thing))
|
||||||
thing.CreateComponent();
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -70,6 +70,9 @@ namespace RoboidControl.Unity {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (core.updateQueue.TryDequeue(out RoboidControl.Thing.UpdateEvent e))
|
||||||
|
HandleUpdateEvent(e);
|
||||||
|
|
||||||
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;
|
||||||
this.transform.Translate(core.linearVelocity.distance * Time.deltaTime * direction, Space.Self);
|
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() {
|
private void PoseChanged() {
|
||||||
//Debug.Log($"{this} pose changed");
|
//Debug.Log($"{this} pose changed");
|
||||||
if (core.positionUpdated)
|
if (core.positionUpdated)
|
||||||
|
@ -14,6 +14,7 @@ namespace RoboidControl.Unity {
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public RoboidControl.TouchSensor coreSensor {
|
public RoboidControl.TouchSensor coreSensor {
|
||||||
get => (RoboidControl.TouchSensor)base.core;
|
get => (RoboidControl.TouchSensor)base.core;
|
||||||
|
set => base.core = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
SphereCollider collider = null;
|
SphereCollider collider = null;
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Collections.Concurrent;
|
||||||
|
|
||||||
namespace RoboidControl {
|
namespace RoboidControl {
|
||||||
|
|
||||||
@ -78,6 +79,8 @@ namespace RoboidControl {
|
|||||||
this.things.Remove(thing);
|
this.things.Remove(thing);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#region Update
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Update all things for this participant
|
/// Update all things for this participant
|
||||||
/// </summary>
|
/// </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>
|
/// <summary>
|
||||||
/// The collection of known participants.
|
/// The collection of known participants.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -100,6 +100,11 @@ namespace RoboidControl {
|
|||||||
if (msg.networkId != sender.networkId) {
|
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));
|
||||||
|
UpdateEvent e = new() {
|
||||||
|
messageId = ParticipantMsg.Id,
|
||||||
|
participant = sender
|
||||||
|
};
|
||||||
|
this.updateQueue.Enqueue(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
18
src/Thing.cs
18
src/Thing.cs
@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Collections.Concurrent;
|
||||||
using LinearAlgebra;
|
using LinearAlgebra;
|
||||||
|
|
||||||
namespace RoboidControl {
|
namespace RoboidControl {
|
||||||
@ -56,8 +57,14 @@ namespace RoboidControl {
|
|||||||
this.type = thingType;
|
this.type = thingType;
|
||||||
if (this.owner != null)
|
if (this.owner != null)
|
||||||
this.owner.Add(this);
|
this.owner.Add(this);
|
||||||
if (invokeEvent)
|
if (invokeEvent) {
|
||||||
InvokeNewThing(this);
|
//InvokeNewThing(this);
|
||||||
|
Participant.UpdateEvent e = new() {
|
||||||
|
messageId = ThingMsg.id,
|
||||||
|
thing = this
|
||||||
|
};
|
||||||
|
this.owner.updateQueue.Enqueue(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -271,6 +278,8 @@ namespace RoboidControl {
|
|||||||
_position = value;
|
_position = value;
|
||||||
positionUpdated = true;
|
positionUpdated = true;
|
||||||
//OnPositionChanged?.Invoke();
|
//OnPositionChanged?.Invoke();
|
||||||
|
UpdateEvent e = new() { messageId = PoseMsg.Id };
|
||||||
|
updateQueue.Enqueue(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -413,6 +422,11 @@ namespace RoboidControl {
|
|||||||
OnNewThing?.Invoke(thing);
|
OnNewThing?.Invoke(thing);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class UpdateEvent {
|
||||||
|
public int messageId; // see the communication messages
|
||||||
|
};
|
||||||
|
public ConcurrentQueue<UpdateEvent> updateQueue = new();
|
||||||
|
|
||||||
#endregion Update
|
#endregion Update
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user