66 lines
2.4 KiB
C#
66 lines
2.4 KiB
C#
using UnityEngine;
|
|
|
|
namespace RoboidControl.Unity {
|
|
|
|
public class Participant : MonoBehaviour {
|
|
public string ipAddress;
|
|
public int port;
|
|
|
|
public RoboidControl.Participant coreParticipant;
|
|
|
|
// public Thing GetThing(RoboidControl.Thing coreThing) {
|
|
// // Not efficient or full correct but will do for now
|
|
// Thing[] things = FindObjectsOfType<Thing>();
|
|
// foreach (Thing thing in things) {
|
|
// if (thing.core.id == coreThing.id)
|
|
// return thing;
|
|
// }
|
|
// return null;
|
|
// }
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
protected virtual void HandleThingEvent(RoboidControl.Participant.UpdateEvent e) {
|
|
switch (e.thing) {
|
|
case RoboidControl.TouchSensor coreTouchSensor:
|
|
Debug.Log("Handle TouchSensor");
|
|
TouchSensor touchSensor = TouchSensor.Create(coreTouchSensor);
|
|
coreTouchSensor.component = touchSensor;
|
|
break;
|
|
case RoboidControl.DifferentialDrive coreDrive:
|
|
Debug.Log("Handle Diff.Drive");
|
|
DifferentialDrive differentialDrive = DifferentialDrive.Create(coreDrive);
|
|
coreDrive.component = differentialDrive;
|
|
break;
|
|
case RoboidControl.Motor coreMotor:
|
|
Motor wheel = Motor.Create(coreMotor);
|
|
coreMotor.component = wheel;
|
|
// We need to know the details (though a binary msg)
|
|
// before we can create the wheel reliably
|
|
break;
|
|
case RoboidControl.Thing coreThing:
|
|
Debug.Log("Handle Thing");
|
|
if (coreThing.component == null) {
|
|
Thing thing = Thing.Create(coreThing);
|
|
coreThing.component = thing;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
}
|
|
} |