1
Fork 0
mirror of https://github.com/Steffo99/better-tee.git synced 2024-11-26 00:34:18 +00:00
better-tee/Assets/Code/Viewer/ViewerMainController.cs

114 lines
4.1 KiB
C#
Raw Normal View History

2019-09-19 10:28:02 +00:00
using System;
using UnityEngine;
using Mirror;
namespace BetterTee.Viewer
{
public class ViewerMainController : MonoBehaviour
{
2019-09-21 00:24:30 +00:00
[Header("WIP")]
public string address = "127.0.0.1";
public string viewerName = "Unknown";
public string gamePassword = "ASDF";
void Start() {
viewerName = Environment.MachineName;
ConnectToServer(address, viewerName);
}
[Header("Objects")]
public ActViewer currentAct = null;
public LobbyController lobbyController = null;
[Header("Prefabs")]
public GameObject lobbyControllerPrefab = null;
public GameObject drawingViewerPrefab = null;
public GameObject typingViewerPrefab = null;
[Serializable]
public class InvalidActTypeException : Exception {
public readonly string actType;
public InvalidActTypeException(string actType) {
this.actType = actType;
}
};
public void LoadAct(ActSettings settings) {
2019-09-22 17:58:06 +00:00
if(settings.type == "Drawing") {
currentAct = Instantiate(drawingViewerPrefab).GetComponent<ActViewer>();
currentAct.settings = settings;
currentAct.ActInit();
}
else if(settings.type == "Typing") {
currentAct = Instantiate(typingViewerPrefab).GetComponent<ActViewer>();
currentAct.settings = settings;
currentAct.ActInit();
}
2019-09-21 00:24:30 +00:00
throw new InvalidActTypeException(settings.type);
}
public void ConnectToServer(string address, string playerName) {
LogFilter.Debug = true;
Transport.activeTransport = GetComponent<TelepathyTransport>();
NetworkClient.RegisterHandler<ConnectMessage>(OnConnect);
NetworkClient.RegisterHandler<DisconnectMessage>(OnDisconnect);
NetworkClient.RegisterHandler<NetMsg.Server.LobbyStatusChange>(OnLobbyStatusChange);
NetworkClient.RegisterHandler<NetMsg.Server.LobbyEnd>(OnLobbyEnd);
NetworkClient.RegisterHandler<NetMsg.Server.GameEnd>(OnGameEnd);
NetworkClient.RegisterHandler<NetMsg.Server.ActInit>(OnActInit);
NetworkClient.RegisterHandler<NetMsg.Server.ActStart>(OnActStart);
NetworkClient.RegisterHandler<NetMsg.Server.ActEnd>(OnActEnd);
NetworkClient.Connect(address);
}
public void StartGame() {
NetworkClient.Send<NetMsg.Viewer.GameStart>(new NetMsg.Viewer.GameStart {});
}
#region Network Events
protected void OnConnect(NetworkConnection connection, ConnectMessage message) {
connection.Send<NetMsg.Viewer.ViewerLink>(new NetMsg.Viewer.ViewerLink {
viewerName = viewerName,
gamePassword = gamePassword
});
lobbyController = Instantiate(lobbyControllerPrefab, transform).GetComponent<LobbyController>();
lobbyController.startGameAction = StartGame;
}
protected void OnDisconnect(NetworkConnection connection, DisconnectMessage message) {
Debug.LogWarning("Lost connection");
}
protected void OnLobbyStatusChange(NetworkConnection connection, NetMsg.Server.LobbyStatusChange message) {
2019-09-22 17:58:06 +00:00
lobbyController.OnLobbyStatusChange(message.players, message.viewers, message.canStart);
2019-09-21 00:24:30 +00:00
}
2019-09-22 17:58:06 +00:00
protected void OnLobbyEnd(NetworkConnection connection, NetMsg.Server.LobbyEnd message) {
Destroy(lobbyController.gameObject);
}
2019-09-21 00:24:30 +00:00
protected void OnGameEnd(NetworkConnection connection, NetMsg.Server.GameEnd message) {}
protected void OnActInit(NetworkConnection connection, NetMsg.Server.ActInit message) {
LoadAct(message.settings);
}
protected void OnActStart(NetworkConnection connection, NetMsg.Server.ActStart message) {
2019-09-22 17:58:06 +00:00
currentAct.ActStart();
2019-09-21 00:24:30 +00:00
}
protected void OnActEnd(NetworkConnection connection, NetMsg.Server.ActEnd message) {
2019-09-22 17:58:06 +00:00
currentAct.ActEnd();
Destroy(currentAct.gameObject);
2019-09-21 00:24:30 +00:00
}
#endregion
2019-09-19 10:28:02 +00:00
}
}