RoboidControl-csharp/Unity/SiteServer.cs

56 lines
1.8 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();
// 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;
participant.coreParticipant.component = participant;
participant.ipAddress = e.participant.ipAddress;
participant.port = e.participant.port;
break;
case ThingMsg.id:
HandleThingEvent(e);
break;
}
}
}
}
#endif