1
Fork 0
mirror of https://github.com/Steffo99/better-tee.git synced 2024-11-22 15:24:18 +00:00
better-tee/Assets/Code/PlayerMainController.cs

93 lines
3.3 KiB
C#
Raw Normal View History

2019-09-14 16:30:16 +00:00
using System;
using UnityEngine;
2019-09-15 23:07:27 +00:00
using Mirror;
2019-09-14 16:30:16 +00:00
2019-09-14 19:42:44 +00:00
public class PlayerMainController : MonoBehaviour
2019-09-14 16:30:16 +00:00
{
2019-09-16 15:34:22 +00:00
[Header("WIP")]
2019-09-18 11:22:15 +00:00
public string address = "127.0.0.1";
2019-09-17 15:27:49 +00:00
public string playerName = "Steffo";
2019-09-18 11:22:15 +00:00
public string gamePassword = "ASDF";
2019-09-17 15:27:49 +00:00
2019-09-16 15:34:22 +00:00
void Start() {
2019-09-17 15:27:49 +00:00
ConnectToServer(address, playerName);
2019-09-16 15:34:22 +00:00
}
2019-09-14 16:30:16 +00:00
[Header("Objects")]
2019-09-16 15:34:22 +00:00
public ActController currentAct = null;
2019-09-14 16:30:16 +00:00
[Header("Prefabs")]
public GameObject drawingControllerPrefab;
public GameObject typingControllerPrefab;
[Serializable]
2019-09-15 23:07:27 +00:00
public class InvalidActTypeException : Exception {
public readonly string actType;
2019-09-14 16:30:16 +00:00
2019-09-15 23:07:27 +00:00
public InvalidActTypeException(string actType) {
this.actType = actType;
2019-09-14 16:30:16 +00:00
}
};
2019-09-15 23:07:27 +00:00
public void LoadAct(ActSettings settings) {
if(settings.type == "Drawing") {
2019-09-14 16:30:16 +00:00
currentAct = Instantiate(drawingControllerPrefab, transform).GetComponent<DrawingController>();
}
2019-09-15 23:07:27 +00:00
else if (settings.type == "Typing") {
2019-09-14 16:30:16 +00:00
currentAct = Instantiate(typingControllerPrefab, transform).GetComponent<TypingController>();
}
2019-09-15 23:07:27 +00:00
else throw new InvalidActTypeException(settings.type);
currentAct.settings = settings;
2019-09-14 16:30:16 +00:00
}
2019-09-15 23:07:27 +00:00
2019-09-17 15:27:49 +00:00
public void ConnectToServer(string address, string playerName) {
2019-09-17 16:03:01 +00:00
LogFilter.Debug = true;
2019-09-18 11:22:15 +00:00
Transport.activeTransport = GetComponent<TelepathyTransport>();
NetworkClient.RegisterHandler<ConnectMessage>(OnConnect);
2019-09-16 15:34:22 +00:00
NetworkClient.RegisterHandler<NetMessage.Connect.PlayerJoinSuccessful>(OnPlayerJoinSuccessful);
NetworkClient.RegisterHandler<NetMessage.Game.Settings>(OnGameSettings);
NetworkClient.RegisterHandler<NetMessage.Game.Start>(OnGameStart);
NetworkClient.RegisterHandler<NetMessage.Game.End>(OnGameEnd);
NetworkClient.RegisterHandler<NetMessage.Act.Init>(OnActInit);
NetworkClient.RegisterHandler<NetMessage.Act.Start>(OnActStart);
NetworkClient.RegisterHandler<NetMessage.Act.End>(OnActEnd);
2019-09-17 15:27:49 +00:00
NetworkClient.Connect(address);
2019-09-18 11:22:15 +00:00
}
2019-09-17 15:27:49 +00:00
2019-09-18 11:22:15 +00:00
public void OnConnect(NetworkConnection connection, ConnectMessage message) {
Debug.Log("Sending NetMessage.Connect.PlayerJoin");
2019-09-17 15:27:49 +00:00
NetMessage.Connect.PlayerJoin playerJoin = new NetMessage.Connect.PlayerJoin {
2019-09-18 11:22:15 +00:00
playerName = playerName,
gamePassword = gamePassword
2019-09-17 15:27:49 +00:00
};
2019-09-18 11:22:15 +00:00
connection.Send<NetMessage.Connect.PlayerJoin>(playerJoin);
2019-09-15 23:07:27 +00:00
}
2019-09-16 15:34:22 +00:00
protected void OnPlayerJoinSuccessful(NetworkConnection connection, NetMessage.Connect.PlayerJoinSuccessful message) {}
protected void OnGameSettings(NetworkConnection connection, NetMessage.Game.Settings message) {}
protected void OnGameStart(NetworkConnection connection, NetMessage.Game.Start message) {}
protected void OnGameEnd(NetworkConnection connection, NetMessage.Game.End message) {}
protected void OnActInit(NetworkConnection connection, NetMessage.Act.Init message) {
2019-09-15 23:07:27 +00:00
LoadAct(message.settings);
2019-09-16 15:34:22 +00:00
currentAct.ActInit();
}
protected void OnActStart(NetworkConnection connection, NetMessage.Act.Start message) {
currentAct.ActStart();
}
protected void OnActEnd(NetworkConnection connection, NetMessage.Act.End message) {
currentAct.ActEnd();
//SEND RESULTS HERE
//test this
Destroy(currentAct);
2019-09-15 23:07:27 +00:00
}
2019-09-14 16:30:16 +00:00
}