players can now collect each other's souls

master
Jordan Orelli 5 years ago
parent f0c159e4fc
commit fbc72df7b4

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

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

@ -17,3 +17,6 @@ MonoBehaviour:
port: 9001 port: 9001
soulPrefab: {fileID: 923676258187016684, guid: 80b838e4e488ed64b9ac358ee000f444, soulPrefab: {fileID: 923676258187016684, guid: 80b838e4e488ed64b9ac358ee000f444,
type: 3} type: 3}
playerPrefab: {fileID: 6383496741192187632, guid: cd445451907d7274daea79c67e13a202,
type: 3}
loginInfo: {fileID: 11400000, guid: 3935bc2db09ef5f408342222f9490ded, type: 2}

@ -693,6 +693,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 2e685bfc1554e0c419dadae2bc4803aa, type: 3} m_Script: {fileID: 11500000, guid: 2e685bfc1554e0c419dadae2bc4803aa, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
loginInfo: {fileID: 11400000, guid: 3935bc2db09ef5f408342222f9490ded, type: 2}
networking: {fileID: 11400000, guid: bb2274d4e981ac14fb9a2e043d04e95f, type: 2} networking: {fileID: 11400000, guid: bb2274d4e981ac14fb9a2e043d04e95f, type: 2}
usernameField: {fileID: 1069633144627678397} usernameField: {fileID: 1069633144627678397}
passwordField: {fileID: 1069633145445280619} passwordField: {fileID: 1069633145445280619}

@ -2350,6 +2350,36 @@ PrefabInstance:
objectReference: {fileID: 0} objectReference: {fileID: 0}
m_RemovedComponents: [] m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: f97b2236cb22fbc46a20b65f13071959, type: 3} 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 --- !u!4 &428174253 stripped
Transform: Transform:
m_CorrespondingSourceObject: {fileID: 7525036764583245661, guid: f97b2236cb22fbc46a20b65f13071959, m_CorrespondingSourceObject: {fileID: 7525036764583245661, guid: f97b2236cb22fbc46a20b65f13071959,
@ -2952,6 +2982,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: af7a4f3a5c0c243498985148ee243e45, type: 3} m_Script: {fileID: 11500000, guid: af7a4f3a5c0c243498985148ee243e45, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
networking: {fileID: 11400000, guid: bb2274d4e981ac14fb9a2e043d04e95f, type: 2}
player: {fileID: 1063375671} player: {fileID: 1063375671}
--- !u!4 &573540321 stripped --- !u!4 &573540321 stripped
Transform: Transform:

@ -3,14 +3,18 @@ using System.Collections.Generic;
using UnityEngine; using UnityEngine;
public class CameraController : MonoBehaviour { public class CameraController : MonoBehaviour {
public Networking networking;
public Transform player; public Transform player;
// Start is called before the first frame update // Start is called before the first frame update
void Start() { void Start() {
networking.Connect();
} }
// Update is called once per frame // Update is called once per frame
void Update() { void Update() {
networking.CheckForMessages();
if (player) { if (player) {
transform.position = new Vector3(player.position.x-4, player.position.y+4, player.position.z-8); transform.position = new Vector3(player.position.x-4, player.position.y+4, player.position.z-8);
transform.LookAt(player); transform.LookAt(player);

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

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

@ -6,6 +6,7 @@ using UnityEngine.UI;
public class MainMenu : MonoBehaviour public class MainMenu : MonoBehaviour
{ {
public LoginInfo loginInfo;
public Networking networking; public Networking networking;
public InputField usernameField; public InputField usernameField;
public InputField passwordField; public InputField passwordField;
@ -28,7 +29,9 @@ public class MainMenu : MonoBehaviour
} }
usernameField.interactable = false; usernameField.interactable = false;
passwordField.interactable = false; passwordField.interactable = false;
networking.SendLogin(username, password); loginInfo.playerName = username;
loginInfo.password = password;
networking.SendLogin();
SceneManager.LoadScene("MainLevel"); SceneManager.LoadScene("MainLevel");
} }
} }

@ -16,6 +16,8 @@ public class Networking : ScriptableObject {
public string path; public string path;
public int port; public int port;
public GameObject soulPrefab; public GameObject soulPrefab;
public GameObject playerPrefab;
public LoginInfo loginInfo;
private ClientWebSocket sock; private ClientWebSocket sock;
private Task writeTask; private Task writeTask;
@ -24,6 +26,8 @@ public class Networking : ScriptableObject {
private int seq; private int seq;
async public void Connect() { async public void Connect() {
readBuffer = new ArraySegment<byte>(new byte[32000]);
if (sock != null) { if (sock != null) {
if (sock.State == WebSocketState.Open) { if (sock.State == WebSocketState.Open) {
return; return;
@ -31,7 +35,6 @@ public class Networking : ScriptableObject {
sock = null; sock = null;
} }
readBuffer = new ArraySegment<byte>(new byte[32000]);
Application.quitting += autoDisconnect; Application.quitting += autoDisconnect;
sock = new ClientWebSocket(); sock = new ClientWebSocket();
@ -68,13 +71,13 @@ public class Networking : ScriptableObject {
sock.SendAsync(buf, WebSocketMessageType.Text, true, CancellationToken.None); sock.SendAsync(buf, WebSocketMessageType.Text, true, CancellationToken.None);
} }
public void SendLogin(string username, string password) { public void SendLogin() {
seq++; seq++;
Login login; Login login;
login.seq = seq; login.seq = seq;
login.cmd = "login"; login.cmd = "login";
login.username = username; login.username = loginInfo.playerName;
login.password = password; login.password = loginInfo.password;
string msg = JsonUtility.ToJson(login); string msg = JsonUtility.ToJson(login);
ArraySegment<byte> buf = new ArraySegment<byte>(Encoding.UTF8.GetBytes(msg)); ArraySegment<byte> buf = new ArraySegment<byte>(Encoding.UTF8.GetBytes(msg));
sock.SendAsync(buf, WebSocketMessageType.Text, true, CancellationToken.None); sock.SendAsync(buf, WebSocketMessageType.Text, true, CancellationToken.None);
@ -84,6 +87,9 @@ public class Networking : ScriptableObject {
if (readTask != null) { if (readTask != null) {
return; return;
} }
if (sock == null) {
return;
}
readTask = sock.ReceiveAsync(readBuffer, CancellationToken.None); readTask = sock.ReceiveAsync(readBuffer, CancellationToken.None);
WebSocketReceiveResult result = await readTask; WebSocketReceiveResult result = await readTask;
readTask = null; readTask = null;
@ -94,15 +100,39 @@ 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]); Debug.LogFormat("spawn a soul: {0}", parts[1]);
SpawnSoul ss = JsonUtility.FromJson<SpawnSoul>(parts[1]); SpawnSoul ss = JsonUtility.FromJson<SpawnSoul>(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<SoulController>(); SoulController sc = soul.GetComponent<SoulController>();
sc.playerName = ss.playerName; sc.playerName = ss.playerName;
break; break;
case "soul-collected":
Debug.LogFormat("a soul was collected: {0}", parts[1]);
CollectSoul collected = JsonUtility.FromJson<CollectSoul>(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<CameraController>();
cc.player = player.transform;
}
}
break;
case "tick": case "tick":
break; break;

@ -25,13 +25,9 @@ public class PlayerController : MonoBehaviour {
gravity = -(2*jumpHeight)/Mathf.Pow(jumpDuration, 2f); gravity = -(2*jumpHeight)/Mathf.Pow(jumpDuration, 2f);
jumpVelocity = Mathf.Abs(gravity) * jumpDuration; jumpVelocity = Mathf.Abs(gravity) * jumpDuration;
networking.Connect();
} }
void Update() { void Update() {
networking.CheckForMessages();
if (moveController.collisions.above || moveController.collisions.below) { if (moveController.collisions.above || moveController.collisions.below) {
velocity.y = 0; velocity.y = 0;
} }

Loading…
Cancel
Save