From f9cb5adfd81f0d1aecdab130ecde018fbed9a9d5 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Sat, 1 Feb 2020 21:27:47 -0600 Subject: [PATCH] some basic server interaction --- Assets/Networking.asset | 17 +++++++ Assets/Networking.asset.meta | 8 ++++ Assets/Plugins.meta | 8 ++++ Assets/Prefabs/Player.prefab | 18 +++++++ Assets/Prefabs/Soul.prefab | 27 +++++++++-- Assets/Prefabs/spike.prefab | 23 +++++++-- Assets/Scripts/MoveController.cs | 16 +------ Assets/Scripts/Networking.cs | 75 ++++++++++++++++++++++++++++++ Assets/Scripts/Networking.cs.meta | 11 +++++ Assets/Scripts/PlayerController.cs | 39 +++++++++++++--- Assets/Scripts/SoulController.cs | 5 ++ 11 files changed, 218 insertions(+), 29 deletions(-) create mode 100644 Assets/Networking.asset create mode 100644 Assets/Networking.asset.meta create mode 100644 Assets/Plugins.meta create mode 100644 Assets/Scripts/Networking.cs create mode 100644 Assets/Scripts/Networking.cs.meta diff --git a/Assets/Networking.asset b/Assets/Networking.asset new file mode 100644 index 0000000..71cdf42 --- /dev/null +++ b/Assets/Networking.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: 9d1527f3b62a45143867c15cefe61e7b, type: 3} + m_Name: Networking + m_EditorClassIdentifier: + host: cdm.jordanorelli.com + path: kloam + port: 9001 diff --git a/Assets/Networking.asset.meta b/Assets/Networking.asset.meta new file mode 100644 index 0000000..3913c10 --- /dev/null +++ b/Assets/Networking.asset.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: bb2274d4e981ac14fb9a2e043d04e95f +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 11400000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Plugins.meta b/Assets/Plugins.meta new file mode 100644 index 0000000..9cea810 --- /dev/null +++ b/Assets/Plugins.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 3fe9140992331f24890a308c4520b4a2 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Prefabs/Player.prefab b/Assets/Prefabs/Player.prefab index ef1cb59..8487011 100644 --- a/Assets/Prefabs/Player.prefab +++ b/Assets/Prefabs/Player.prefab @@ -12,6 +12,7 @@ GameObject: - component: {fileID: 8462409473881997938} - component: {fileID: 6891912346901077427} - component: {fileID: 5588890751565926871} + - component: {fileID: 2492546993917640295} m_Layer: 10 m_Name: Player m_TagString: Player @@ -85,6 +86,23 @@ MonoBehaviour: hud: {fileID: 0} soulPrefab: {fileID: 923676258187016684, guid: 80b838e4e488ed64b9ac358ee000f444, type: 3} + networking: {fileID: 11400000, guid: bb2274d4e981ac14fb9a2e043d04e95f, type: 2} +--- !u!54 &2492546993917640295 +Rigidbody: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 6383496741192187632} + serializedVersion: 2 + m_Mass: 1 + m_Drag: 0 + m_AngularDrag: 0.05 + m_UseGravity: 0 + m_IsKinematic: 1 + m_Interpolate: 0 + m_Constraints: 0 + m_CollisionDetection: 0 --- !u!1 &6383496741659177281 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Prefabs/Soul.prefab b/Assets/Prefabs/Soul.prefab index 77f82a3..fdcd518 100644 --- a/Assets/Prefabs/Soul.prefab +++ b/Assets/Prefabs/Soul.prefab @@ -11,7 +11,8 @@ GameObject: - component: {fileID: 3487225793045142114} - component: {fileID: 945971871285315277} - component: {fileID: 2986043981050272749} - m_Layer: 11 + - component: {fileID: 4236194107275681819} + m_Layer: 0 m_Name: Soul m_TagString: Soul m_Icon: {fileID: 0} @@ -57,11 +58,27 @@ SphereCollider: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 923676258187016684} m_Material: {fileID: 0} - m_IsTrigger: 0 + m_IsTrigger: 1 m_Enabled: 1 serializedVersion: 2 m_Radius: 0.25 m_Center: {x: 0, y: 0, z: 0} +--- !u!54 &4236194107275681819 +Rigidbody: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 923676258187016684} + serializedVersion: 2 + m_Mass: 1 + m_Drag: 0 + m_AngularDrag: 0.05 + m_UseGravity: 0 + m_IsKinematic: 1 + m_Interpolate: 0 + m_Constraints: 0 + m_CollisionDetection: 0 --- !u!1 &2059173772660875122 GameObject: m_ObjectHideFlags: 0 @@ -73,7 +90,7 @@ GameObject: - component: {fileID: 4604280620177553672} - component: {fileID: 6542300211569968197} - component: {fileID: 3343412635193291118} - m_Layer: 11 + m_Layer: 0 m_Name: Name Text m_TagString: Untagged m_Icon: {fileID: 0} @@ -151,7 +168,7 @@ GameObject: - component: {fileID: 323817472649017284} - component: {fileID: 3964294279211880244} - component: {fileID: 6841750811663349008} - m_Layer: 11 + m_Layer: 0 m_Name: Canvas m_TagString: Untagged m_Icon: {fileID: 0} @@ -249,7 +266,7 @@ GameObject: - component: {fileID: 8132939646274158128} - component: {fileID: 8132939646274960912} - component: {fileID: 8132939646276058320} - m_Layer: 11 + m_Layer: 0 m_Name: soul m_TagString: Untagged m_Icon: {fileID: 0} diff --git a/Assets/Prefabs/spike.prefab b/Assets/Prefabs/spike.prefab index 3343a32..eff9302 100644 --- a/Assets/Prefabs/spike.prefab +++ b/Assets/Prefabs/spike.prefab @@ -43,7 +43,8 @@ GameObject: - component: {fileID: 4679091611350652119} - component: {fileID: 4679091611349653015} - component: {fileID: 4393206124236303816} - m_Layer: 11 + - component: {fileID: 2633173143634587846} + m_Layer: 0 m_Name: spike m_TagString: Fatal m_Icon: {fileID: 0} @@ -117,9 +118,25 @@ MeshCollider: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 4679091611351845079} m_Material: {fileID: 0} - m_IsTrigger: 0 + m_IsTrigger: 1 m_Enabled: 1 serializedVersion: 3 - m_Convex: 0 + m_Convex: 1 m_CookingOptions: 14 m_Mesh: {fileID: 4300000, guid: 9aced54bab99cb6499f16b696076404a, type: 3} +--- !u!54 &2633173143634587846 +Rigidbody: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 4679091611351845079} + serializedVersion: 2 + m_Mass: 1 + m_Drag: 0 + m_AngularDrag: 0.05 + m_UseGravity: 0 + m_IsKinematic: 1 + m_Interpolate: 0 + m_Constraints: 0 + m_CollisionDetection: 0 diff --git a/Assets/Scripts/MoveController.cs b/Assets/Scripts/MoveController.cs index 286b6be..1da79ae 100644 --- a/Assets/Scripts/MoveController.cs +++ b/Assets/Scripts/MoveController.cs @@ -61,11 +61,6 @@ public class MoveController : MonoBehaviour return; } - if (hit.collider.gameObject.CompareTag("Soul")) { - Destroy(hit.collider.gameObject); - return; - } - float slopeAngle = Vector3.Angle(hit.normal, Vector3.up); if (i == 0 && slopeAngle <= maxClimbAngle) { float distanceToSlopeStart = 0f; @@ -95,6 +90,7 @@ public class MoveController : MonoBehaviour } } } + return; } private void VerticalCollisions(ref Vector3 velocity) { @@ -116,16 +112,6 @@ public class MoveController : MonoBehaviour } } - if (hit.collider.gameObject.CompareTag("Fatal")) { - Destroy(gameObject); - return; - } - - if (hit.collider.gameObject.CompareTag("Soul")) { - Destroy(hit.collider.gameObject); - return; - } - velocity.y = (hit.distance - skinWidth) * directionY; // Debug.LogFormat("with RayLength {0} MinHitDist {1} setting velocity.y to {2}", rayLength, hit.distance, velocity.y); rayLength = hit.distance; diff --git a/Assets/Scripts/Networking.cs b/Assets/Scripts/Networking.cs new file mode 100644 index 0000000..c750f03 --- /dev/null +++ b/Assets/Scripts/Networking.cs @@ -0,0 +1,75 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Net; +using System.Net.WebSockets; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +using UnityEngine; +using UnityEngine.Networking; + +[CreateAssetMenu(fileName = "Data", menuName = "ScriptableObjects/Networking", order = 1)] +public class Networking : ScriptableObject { + public string host; + public string path; + public int port; + + private ClientWebSocket sock; + private Task connectTask; + private Task writeTask; + private bool isConnected; + private int seq; + + public void Connect() { + sock = new ClientWebSocket(); + UriBuilder b = new UriBuilder(); + b.Scheme = "ws"; + b.Host = host; + b.Port = port; + b.Path = path; + Debug.LogFormat("Connecting to: {0}", b.Uri); + connectTask = sock.ConnectAsync(b.Uri, CancellationToken.None); + connectTask.ContinueWith((x) => { + Debug.LogFormat("Finished connection task with status: {0}", sock.State); + isConnected = sock.State == WebSocketState.Open; + }); + } + + public void SendCollectSoul(string playerName, Vector3 position) { + seq++; + CollectSoul info; + info.seq = seq; + info.cmd = "collect-soul"; + info.playerName = playerName; + info.position = position; + string msg = JsonUtility.ToJson(info); + ArraySegment buf = new ArraySegment(Encoding.UTF8.GetBytes(msg)); + sock.SendAsync(buf, WebSocketMessageType.Text, true, CancellationToken.None); + } + + public void SendDeath(Vector3 position) { + seq++; + Death death; + death.seq = seq; + death.cmd = "death"; + death.position = position; + string msg = JsonUtility.ToJson(death); + ArraySegment buf = new ArraySegment(Encoding.UTF8.GetBytes(msg)); + sock.SendAsync(buf, WebSocketMessageType.Text, true, CancellationToken.None); + } + + private struct CollectSoul { + public int seq; + public string cmd; + public string playerName; + public Vector3 position; + } + + private struct Death { + public int seq; + public string cmd; + public Vector3 position; + } +} diff --git a/Assets/Scripts/Networking.cs.meta b/Assets/Scripts/Networking.cs.meta new file mode 100644 index 0000000..8664dff --- /dev/null +++ b/Assets/Scripts/Networking.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9d1527f3b62a45143867c15cefe61e7b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/PlayerController.cs b/Assets/Scripts/PlayerController.cs index 8723dc8..4630119 100644 --- a/Assets/Scripts/PlayerController.cs +++ b/Assets/Scripts/PlayerController.cs @@ -11,18 +11,22 @@ public class PlayerController : MonoBehaviour { public float accelerationTimeGrounded = 0.05f; public Canvas hud; public GameObject soulPrefab; + public Networking networking; private Vector3 velocity; private float jumpVelocity; private MoveController moveController; private float gravity; private float moveXSmoothing; + private bool quitting; void Start() { moveController = GetComponent(); gravity = -(2*jumpHeight)/Mathf.Pow(jumpDuration, 2f); jumpVelocity = Mathf.Abs(gravity) * jumpDuration; + + networking.Connect(); } void Update() { @@ -49,12 +53,35 @@ public class PlayerController : MonoBehaviour { void OnDestroy() { Debug.Log("I'm dead"); - hud.gameObject.SetActive(true); - HudController c = hud.gameObject.GetComponent(); - if (c) { - c.SetDead(true); + // hud.gameObject.SetActive(true); + // HudController c = hud.gameObject.GetComponent(); + // if (c) { + // c.SetDead(true); + // } + if (!quitting) { + GameObject soul = Instantiate(soulPrefab, transform.position + Vector3.up * 0.5f, transform.rotation); + soul.GetComponent().playerName = "fartface"; } - GameObject soul = Instantiate(soulPrefab, transform.position + Vector3.up * 0.5f, transform.rotation); - soul.GetComponent().playerName = "fartface"; + } + + void OnCollisionEnter(Collision other) { + Debug.LogFormat("Player collided with {0}", other); + } + + void OnTriggerEnter(Collider other) { + Debug.LogFormat("Player triggered other: {0}", other); + if (other.CompareTag("Soul")) { + SoulController soul = other.GetComponent(); + networking.SendCollectSoul(soul.playerName, other.transform.position); + Destroy(other.gameObject); + } + if (other.CompareTag("Fatal")) { + networking.SendDeath(transform.position); + Destroy(gameObject); + } + } + + void OnApplicationQuit() { + quitting = true; } } diff --git a/Assets/Scripts/SoulController.cs b/Assets/Scripts/SoulController.cs index 3c4182f..a031b29 100644 --- a/Assets/Scripts/SoulController.cs +++ b/Assets/Scripts/SoulController.cs @@ -24,4 +24,9 @@ public class SoulController : MonoBehaviour { void Update() { transform.rotation = cam.transform.rotation * fixedRotation; } + + public struct PickupInfo { + public string playerName; + public Vector3 position; + } }