refactoring the networking stuff

master
Jordan Orelli 5 years ago
parent c641df65af
commit fcaf7122ea

@ -102,26 +102,46 @@ public class Networking : ScriptableObject {
return; return;
} }
GameObject soul;
switch (parts[0]) { switch (parts[0]) {
case "spawn-soul": case "spawn-soul":
Debug.LogFormat("spawn a soul: {0}", parts[1]); SpawnSoul spawned = JsonUtility.FromJson<SpawnSoul>(parts[1]);
SpawnSoul ss = JsonUtility.FromJson<SpawnSoul>(parts[1]); onSpawnSoul(spawned);
soul = Instantiate(soulPrefab, ss.position, Quaternion.identity); break;
soul.name = ss.playerName;
case "soul-collected":
CollectSoul collected = JsonUtility.FromJson<CollectSoul>(parts[1]);
onSoulCollected(collected);
break;
case "login-result":
LoginResult login = JsonUtility.FromJson<LoginResult>(parts[1]);
onLoginResult(login);
break;
case "tick":
break;
default:
Debug.LogFormat("also can't handle this one: {0} {1}", parts[0], parts[1]);
break;
}
}
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"); GameObject allSouls = GameObject.Find("Souls");
soul.transform.SetParent(allSouls.transform); soul.transform.SetParent(allSouls.transform);
SoulController sc = soul.GetComponent<SoulController>(); SoulController sc = soul.GetComponent<SoulController>();
sc.playerName = ss.playerName; sc.playerName = spawn.playerName;
break; }
case "soul-collected": private void onSoulCollected(CollectSoul collected) {
Debug.LogFormat("a soul was collected: {0}", parts[1]); Debug.LogFormat("a soul was collected: {0}", collected);
CollectSoul collected = JsonUtility.FromJson<CollectSoul>(parts[1]); GameObject soul = GameObject.Find("Souls/"+collected.playerName);
soul = GameObject.Find("Souls/"+collected.playerName);
Destroy(soul); Destroy(soul);
if (collected.playerName == loginInfo.playerName) { if (collected.playerName == loginInfo.playerName) {
@ -133,26 +153,16 @@ public class Networking : ScriptableObject {
cc.player = player.transform; cc.player = player.transform;
} }
} }
break; }
case "login-result": private void onLoginResult(LoginResult result) {
Debug.LogFormat("received login result: {0}", parts[1]); Debug.LogFormat("received login result: {0}", result);
loginInfo.sentLogin = false; loginInfo.sentLogin = false;
LoginResult login = JsonUtility.FromJson<LoginResult>(parts[1]); if (result.passed) {
if (login.passed) {
loginInfo.isLoggedIn = true; loginInfo.isLoggedIn = true;
} else { } else {
loginInfo.loginError = login.error; loginInfo.loginError = result.error;
Debug.LogErrorFormat("failed login: {0}", login.error); Debug.LogErrorFormat("failed login: {0}", result.error);
}
break;
case "tick":
break;
default:
Debug.LogFormat("also can't handle this one: {0} {1}", parts[0], parts[1]);
break;
} }
} }

Loading…
Cancel
Save