RoboidControl-csharp/Unity/SiteServer.cs

53 lines
1.6 KiB
C#

#if UNITY_5_3_OR_NEWER
using System;
using System.Collections.Generic;
using UnityEngine;
namespace RoboidControl.Unity {
public class SiteServer : Participant {
//public RoboidControl.SiteServer site;
public RoboidControl.SiteServer site => this.coreParticipant as RoboidControl.SiteServer;
//public Queue<RoboidControl.Thing> thingQueue = new();
protected virtual void Awake() {
Console.SetOut(new UnityLogWriter());
this.coreParticipant = new RoboidControl.SiteServer(port);
}
void OnApplicationQuit() {
if (site != null)
site.Close();
}
protected override void Update() {
if (site == null)
return;
while (site.updateQueue.TryDequeue(out RoboidControl.Participant.UpdateEvent e))
HandleUpdateEvent(e);
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:
HandleThingEvent(e);
break;
}
}
}
}
#endif