diff --git a/Assets/LoginInfo.asset b/Assets/LoginInfo.asset new file mode 100644 index 0000000..052d4ee --- /dev/null +++ b/Assets/LoginInfo.asset @@ -0,0 +1,17 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!114 &11400000 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: fb331b678c613734fa2ba6013e9cd499, type: 3} + m_Name: LoginInfo + m_EditorClassIdentifier: + playerName: DefaultPlayerName + password: defaultPassword + startPosition: {x: 0, y: 10, z: 0} diff --git a/Assets/LoginInfo.asset.meta b/Assets/LoginInfo.asset.meta new file mode 100644 index 0000000..d349199 --- /dev/null +++ b/Assets/LoginInfo.asset.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 3935bc2db09ef5f408342222f9490ded +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 11400000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Networking.asset b/Assets/Networking.asset index 7fb218c..b8286bb 100644 --- a/Assets/Networking.asset +++ b/Assets/Networking.asset @@ -17,3 +17,6 @@ MonoBehaviour: port: 9001 soulPrefab: {fileID: 923676258187016684, guid: 80b838e4e488ed64b9ac358ee000f444, type: 3} + playerPrefab: {fileID: 6383496741192187632, guid: cd445451907d7274daea79c67e13a202, + type: 3} + loginInfo: {fileID: 11400000, guid: 3935bc2db09ef5f408342222f9490ded, type: 2} diff --git a/Assets/Prefabs/Welcome Hud.prefab b/Assets/Prefabs/Welcome Hud.prefab index 8b0ec5f..1e91058 100644 --- a/Assets/Prefabs/Welcome Hud.prefab +++ b/Assets/Prefabs/Welcome Hud.prefab @@ -693,6 +693,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 2e685bfc1554e0c419dadae2bc4803aa, type: 3} m_Name: m_EditorClassIdentifier: + loginInfo: {fileID: 11400000, guid: 3935bc2db09ef5f408342222f9490ded, type: 2} networking: {fileID: 11400000, guid: bb2274d4e981ac14fb9a2e043d04e95f, type: 2} usernameField: {fileID: 1069633144627678397} passwordField: {fileID: 1069633145445280619} diff --git a/Assets/Scenes/MainLevel.unity b/Assets/Scenes/MainLevel.unity index d10bd53..5dd0043 100644 --- a/Assets/Scenes/MainLevel.unity +++ b/Assets/Scenes/MainLevel.unity @@ -2350,6 +2350,36 @@ PrefabInstance: objectReference: {fileID: 0} m_RemovedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: f97b2236cb22fbc46a20b65f13071959, type: 3} +--- !u!1 &427536309 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 427536310} + m_Layer: 0 + m_Name: Souls + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &427536310 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 427536309} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 14 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!4 &428174253 stripped Transform: m_CorrespondingSourceObject: {fileID: 7525036764583245661, guid: f97b2236cb22fbc46a20b65f13071959, @@ -2952,6 +2982,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: af7a4f3a5c0c243498985148ee243e45, type: 3} m_Name: m_EditorClassIdentifier: + networking: {fileID: 11400000, guid: bb2274d4e981ac14fb9a2e043d04e95f, type: 2} player: {fileID: 1063375671} --- !u!4 &573540321 stripped Transform: diff --git a/Assets/Scripts/CameraController.cs b/Assets/Scripts/CameraController.cs index 5d41a74..2050560 100644 --- a/Assets/Scripts/CameraController.cs +++ b/Assets/Scripts/CameraController.cs @@ -3,14 +3,18 @@ using System.Collections.Generic; using UnityEngine; public class CameraController : MonoBehaviour { + public Networking networking; public Transform player; // Start is called before the first frame update void Start() { + networking.Connect(); } // Update is called once per frame void Update() { + networking.CheckForMessages(); + if (player) { transform.position = new Vector3(player.position.x-4, player.position.y+4, player.position.z-8); transform.LookAt(player); diff --git a/Assets/Scripts/LoginInfo.cs b/Assets/Scripts/LoginInfo.cs new file mode 100644 index 0000000..94bc957 --- /dev/null +++ b/Assets/Scripts/LoginInfo.cs @@ -0,0 +1,10 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +[CreateAssetMenu(fileName = "LoginInfo", menuName = "ScriptableObjects/LoginInfo", order = 1)] +public class LoginInfo : ScriptableObject { + public string playerName; + public string password; + public Vector3 startPosition; +} diff --git a/Assets/Scripts/LoginInfo.cs.meta b/Assets/Scripts/LoginInfo.cs.meta new file mode 100644 index 0000000..92539b4 --- /dev/null +++ b/Assets/Scripts/LoginInfo.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: fb331b678c613734fa2ba6013e9cd499 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/MainMenu.cs b/Assets/Scripts/MainMenu.cs index fecf377..1002670 100644 --- a/Assets/Scripts/MainMenu.cs +++ b/Assets/Scripts/MainMenu.cs @@ -6,6 +6,7 @@ using UnityEngine.UI; public class MainMenu : MonoBehaviour { + public LoginInfo loginInfo; public Networking networking; public InputField usernameField; public InputField passwordField; @@ -28,7 +29,9 @@ public class MainMenu : MonoBehaviour } usernameField.interactable = false; passwordField.interactable = false; - networking.SendLogin(username, password); + loginInfo.playerName = username; + loginInfo.password = password; + networking.SendLogin(); SceneManager.LoadScene("MainLevel"); } } diff --git a/Assets/Scripts/Networking.cs b/Assets/Scripts/Networking.cs index 0f3bff7..0a35fa2 100644 --- a/Assets/Scripts/Networking.cs +++ b/Assets/Scripts/Networking.cs @@ -16,6 +16,8 @@ public class Networking : ScriptableObject { public string path; public int port; public GameObject soulPrefab; + public GameObject playerPrefab; + public LoginInfo loginInfo; private ClientWebSocket sock; private Task writeTask; @@ -24,6 +26,8 @@ public class Networking : ScriptableObject { private int seq; async public void Connect() { + readBuffer = new ArraySegment(new byte[32000]); + if (sock != null) { if (sock.State == WebSocketState.Open) { return; @@ -31,7 +35,6 @@ public class Networking : ScriptableObject { sock = null; } - readBuffer = new ArraySegment(new byte[32000]); Application.quitting += autoDisconnect; sock = new ClientWebSocket(); @@ -68,13 +71,13 @@ public class Networking : ScriptableObject { sock.SendAsync(buf, WebSocketMessageType.Text, true, CancellationToken.None); } - public void SendLogin(string username, string password) { + public void SendLogin() { seq++; Login login; login.seq = seq; login.cmd = "login"; - login.username = username; - login.password = password; + login.username = loginInfo.playerName; + login.password = loginInfo.password; string msg = JsonUtility.ToJson(login); ArraySegment buf = new ArraySegment(Encoding.UTF8.GetBytes(msg)); sock.SendAsync(buf, WebSocketMessageType.Text, true, CancellationToken.None); @@ -84,6 +87,9 @@ public class Networking : ScriptableObject { if (readTask != null) { return; } + if (sock == null) { + return; + } readTask = sock.ReceiveAsync(readBuffer, CancellationToken.None); WebSocketReceiveResult result = await readTask; readTask = null; @@ -94,15 +100,39 @@ 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]); - GameObject soul = Instantiate(soulPrefab, ss.position, Quaternion.identity); + 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; 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; + } + } + break; + case "tick": break; diff --git a/Assets/Scripts/PlayerController.cs b/Assets/Scripts/PlayerController.cs index 3659869..1e0284d 100644 --- a/Assets/Scripts/PlayerController.cs +++ b/Assets/Scripts/PlayerController.cs @@ -25,13 +25,9 @@ public class PlayerController : MonoBehaviour { gravity = -(2*jumpHeight)/Mathf.Pow(jumpDuration, 2f); jumpVelocity = Mathf.Abs(gravity) * jumpDuration; - - networking.Connect(); } void Update() { - networking.CheckForMessages(); - if (moveController.collisions.above || moveController.collisions.below) { velocity.y = 0; }