diff --git a/Assets/Scripts/Networking.cs b/Assets/Scripts/Networking.cs index 98fafdf..380cd00 100644 --- a/Assets/Scripts/Networking.cs +++ b/Assets/Scripts/Networking.cs @@ -102,49 +102,20 @@ public class Networking : ScriptableObject { return; } - GameObject soul; - switch (parts[0]) { case "spawn-soul": - Debug.LogFormat("spawn a soul: {0}", parts[1]); - SpawnSoul ss = JsonUtility.FromJson(parts[1]); - soul = Instantiate(soulPrefab, ss.position, Quaternion.identity); - soul.name = ss.playerName; - - GameObject allSouls = GameObject.Find("Souls"); - soul.transform.SetParent(allSouls.transform); - - SoulController sc = soul.GetComponent(); - sc.playerName = ss.playerName; + SpawnSoul spawned = JsonUtility.FromJson(parts[1]); + onSpawnSoul(spawned); break; case "soul-collected": - Debug.LogFormat("a soul was collected: {0}", parts[1]); CollectSoul collected = JsonUtility.FromJson(parts[1]); - soul = GameObject.Find("Souls/"+collected.playerName); - Destroy(soul); - - if (collected.playerName == loginInfo.playerName) { - GameObject currentPlayer = GameObject.Find("Player"); - if (currentPlayer == null) { - GameObject player = Instantiate(playerPrefab, loginInfo.startPosition, Quaternion.identity); - Camera cam = Camera.main; - CameraController cc = cam.GetComponent(); - cc.player = player.transform; - } - } + onSoulCollected(collected); break; case "login-result": - Debug.LogFormat("received login result: {0}", parts[1]); - loginInfo.sentLogin = false; LoginResult login = JsonUtility.FromJson(parts[1]); - if (login.passed) { - loginInfo.isLoggedIn = true; - } else { - loginInfo.loginError = login.error; - Debug.LogErrorFormat("failed login: {0}", login.error); - } + onLoginResult(login); break; case "tick": @@ -156,6 +127,45 @@ public class Networking : ScriptableObject { } } + private void onSpawnSoul(SpawnSoul spawn) { + Debug.LogFormat("spawn a soul: {0}", spawn); + GameObject soul = Instantiate(soulPrefab, spawn.position, Quaternion.identity); + soul.name = spawn.playerName; + + GameObject allSouls = GameObject.Find("Souls"); + soul.transform.SetParent(allSouls.transform); + + SoulController sc = soul.GetComponent(); + sc.playerName = spawn.playerName; + } + + private void onSoulCollected(CollectSoul collected) { + Debug.LogFormat("a soul was collected: {0}", collected); + GameObject soul = GameObject.Find("Souls/"+collected.playerName); + Destroy(soul); + + if (collected.playerName == loginInfo.playerName) { + GameObject currentPlayer = GameObject.Find("Player"); + if (currentPlayer == null) { + GameObject player = Instantiate(playerPrefab, loginInfo.startPosition, Quaternion.identity); + Camera cam = Camera.main; + CameraController cc = cam.GetComponent(); + cc.player = player.transform; + } + } + } + + private void onLoginResult(LoginResult result) { + Debug.LogFormat("received login result: {0}", result); + loginInfo.sentLogin = false; + if (result.passed) { + loginInfo.isLoggedIn = true; + } else { + loginInfo.loginError = result.error; + Debug.LogErrorFormat("failed login: {0}", result.error); + } + } + private void autoDisconnect() { if (isConnected()) { Debug.Log("disconnecting websocket via autoDisconnect");