some basic server interaction

master
Jordan Orelli 5 years ago
parent 40ef62d857
commit f9cb5adfd8

@ -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

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: bb2274d4e981ac14fb9a2e043d04e95f
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 11400000
userData:
assetBundleName:
assetBundleVariant:

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 3fe9140992331f24890a308c4520b4a2
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

@ -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

@ -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}

@ -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

@ -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;

@ -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<byte> buf = new ArraySegment<byte>(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<byte> buf = new ArraySegment<byte>(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;
}
}

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 9d1527f3b62a45143867c15cefe61e7b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

@ -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<MoveController>();
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<HudController>();
if (c) {
c.SetDead(true);
// hud.gameObject.SetActive(true);
// HudController c = hud.gameObject.GetComponent<HudController>();
// if (c) {
// c.SetDead(true);
// }
if (!quitting) {
GameObject soul = Instantiate(soulPrefab, transform.position + Vector3.up * 0.5f, transform.rotation);
soul.GetComponent<SoulController>().playerName = "fartface";
}
GameObject soul = Instantiate(soulPrefab, transform.position + Vector3.up * 0.5f, transform.rotation);
soul.GetComponent<SoulController>().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<SoulController>();
networking.SendCollectSoul(soul.playerName, other.transform.position);
Destroy(other.gameObject);
}
if (other.CompareTag("Fatal")) {
networking.SendDeath(transform.position);
Destroy(gameObject);
}
}
void OnApplicationQuit() {
quitting = true;
}
}

@ -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;
}
}

Loading…
Cancel
Save