JoshuaReason 5 years ago
parent
commit
c84e426e4b
20 changed files with 276 additions and 33 deletions
  1. +3
    -0
      Assets/Data/Networking/Client/Realtime/ConnectClients.asset
  2. +8
    -0
      Assets/Data/Networking/Client/Realtime/ConnectClients.asset.meta
  3. +2
    -2
      Assets/Data/Networking/Client/Realtime/LocalClient.asset
  4. +2
    -2
      Assets/Data/Networking/Server/Realtime/ClientList.asset
  5. +2
    -2
      Assets/Scenes/Client Scenes/LoginScreen.unity
  6. +3
    -0
      Assets/Scenes/RaceTrack 4-player alpha.unity
  7. +7
    -0
      Assets/Scenes/RaceTrack 4-player alpha.unity.meta
  8. +35
    -0
      Assets/Scripts/Block.cs
  9. +8
    -0
      Assets/Scripts/Character.meta
  10. +10
    -0
      Assets/Scripts/Character/CharacterData.cs
  11. +11
    -0
      Assets/Scripts/Character/CharacterData.cs.meta
  12. +8
    -0
      Assets/Scripts/Components.meta
  13. +7
    -2
      Assets/Scripts/Managers/GameManager.cs
  14. +6
    -0
      Assets/Scripts/Networking/Client/ClientObject.cs
  15. +102
    -0
      Assets/Scripts/Networking/Client/ConnectedClients.cs
  16. +11
    -0
      Assets/Scripts/Networking/Client/ConnectedClients.cs.meta
  17. +2
    -0
      Assets/Scripts/Networking/Core/Protocols/LoginProtocols.cs
  18. +29
    -0
      Assets/Scripts/Networking/Server/ClientList.cs
  19. +2
    -18
      Assets/Scripts/UI/Client/LoginUIManager.cs
  20. +18
    -7
      Assets/Scripts/animalSelection.cs

+ 3
- 0
Assets/Data/Networking/Client/Realtime/ConnectClients.asset View File

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:4d22d2621c905effb48e71fa0091fb0f569b358a644fc77182dbd6dd0776af75
size 478

+ 8
- 0
Assets/Data/Networking/Client/Realtime/ConnectClients.asset.meta View File

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

+ 2
- 2
Assets/Data/Networking/Client/Realtime/LocalClient.asset View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1 version https://git-lfs.github.com/spec/v1
oid sha256:9e95c47ee245273f574075cf296e80d083a1401111c78ecb2025c2ebb78c6f49
size 607
oid sha256:cdf01e4c58d4afd9740fc7252436c4a0c7383081d565b0dc65378000fce8c4bb
size 743

+ 2
- 2
Assets/Data/Networking/Server/Realtime/ClientList.asset View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1 version https://git-lfs.github.com/spec/v1
oid sha256:ad3462c6f94c1dd783ef1b25cdb98524845edaa36a879324db1ff597811de687
size 776
oid sha256:e3ee5388094941012a17d8eac09e9009610e3bcbcb69a307ce928fbc133b6d3c
size 834

+ 2
- 2
Assets/Scenes/Client Scenes/LoginScreen.unity View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1 version https://git-lfs.github.com/spec/v1
oid sha256:921c3031e05c70f24f9c7d294a58f76e4b8ebb7405d1cc6ab4c2bfd9684d79ea
size 259080
oid sha256:b7d072ed5fffb0afdf3ee9090c2b01468dba1779c7c97d3a8d961897c5b8f1f1
size 259079

+ 3
- 0
Assets/Scenes/RaceTrack 4-player alpha.unity View File

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:44b4324088c8dade10402a7c0554434853b15ee4d88936e007a70499bd05f4d3
size 2604605

+ 7
- 0
Assets/Scenes/RaceTrack 4-player alpha.unity.meta View File

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 9dc1300c8b04e8a4b89eebfa931c970a
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

+ 35
- 0
Assets/Scripts/Block.cs View File

@ -50,8 +50,42 @@ public class Block : MonoBehaviour
//checks if there is no block above this one and that this is tagged as walkable //checks if there is no block above this one and that this is tagged as walkable
return (is_Walkable && !isBlockAtPosition(position + Vector3.up, 1, layerMask)); return (is_Walkable && !isBlockAtPosition(position + Vector3.up, 1, layerMask));
} }
/// <summary>
/// Is called after all players have taken one move
///
/// Should be implemented by a derived class
/// </summary>
public virtual void OnPlayersMoved()
{
}
/// <summary>
/// Is called when a player moves onto this block
///
/// Should be implemented by a derived class
/// </summary>
/// <param name="player">Player which moved on to block</param>
public virtual void OnWalkedOnByPlayer(PlayerData player)
{
}
/// <summary>
/// Called after all players have finished all their moves
///
/// Should be implemented by a derived class
/// </summary>
public virtual void OnRoundEnd()
{
}
#endregion Public Functions #endregion Public Functions
#region Editor Functions
private void OnDrawGizmos() private void OnDrawGizmos()
{ {
if (!isSpawnable) if (!isSpawnable)
@ -74,6 +108,7 @@ public class Block : MonoBehaviour
DebugExtensions.DrawCube(earPosition - Perp * 0.3f, earScale, Color.magenta, 0); DebugExtensions.DrawCube(earPosition - Perp * 0.3f, earScale, Color.magenta, 0);
} }
#endregion Editor Functions
#region Static Functions #region Static Functions

+ 8
- 0
Assets/Scripts/Character.meta View File

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

+ 10
- 0
Assets/Scripts/Character/CharacterData.cs View File

@ -0,0 +1,10 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Networking.Server;
public class CharacterData
{
public Character Character;
public ClientData Client;
}

+ 11
- 0
Assets/Scripts/Character/CharacterData.cs.meta View File

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

+ 8
- 0
Assets/Scripts/Components.meta View File

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

+ 7
- 2
Assets/Scripts/Managers/GameManager.cs View File

@ -110,10 +110,15 @@ public class GameManager : MonoBehaviour
{ {
//Debug.Log("One Move"); //Debug.Log("One Move");
foreach (PlayerData player in playerArray) foreach (PlayerData player in playerArray)
RunOnce(player);
{
Debug.Log(player.client.Name);
StartCoroutine(RunOnce(player));
yield return new WaitUntil(() => player.waiting);
}
//wait until all players have finished //wait until all players have finished
yield return new WaitUntil(() => playerArray.All(p => p.waiting));
//yield return new WaitUntil(() => playerArray.All(p => p.waiting));
gamemode.FinishedMove(playerArray.ToArray()); gamemode.FinishedMove(playerArray.ToArray());
playerArray.ForEach(p => p.client.SendScore()); playerArray.ForEach(p => p.client.SendScore());

+ 6
- 0
Assets/Scripts/Networking/Client/ClientObject.cs View File

@ -49,6 +49,10 @@ namespace Networking.Client
[Tooltip("Current Score in Round")] [Tooltip("Current Score in Round")]
public float RoundScore; public float RoundScore;
[SerializeField]
[Tooltip("A list of all connected clients")]
public ConnectedClients ConnectedClients;
#endregion Inspector Fields #endregion Inspector Fields
@ -66,6 +70,8 @@ namespace Networking.Client
client = new NetworkClient(); client = new NetworkClient();
client.Configure(TransportConfigure.CreateConfigure(), 1); client.Configure(TransportConfigure.CreateConfigure(), 1);
ConnectedClients.SetUp(this);
client.Connect(serverAddress, port); client.Connect(serverAddress, port);
Inventory.Reset(); Inventory.Reset();

+ 102
- 0
Assets/Scripts/Networking/Client/ConnectedClients.cs View File

@ -0,0 +1,102 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Networking;
using Networking;
namespace Networking.Client
{
[CreateAssetMenu(menuName = "Major Project/Networking/Client/OtherClientsList", order = 150)]
public class ConnectedClients : ScriptableObject
{
#if UNITY_EDITOR
[SerializeField]
[Multiline]
private string DevNotes;
#endif
/// <summary>
/// All Clients which are Currently Connected;
/// </summary>
[SerializeField]
public List<ClientData> AllClients;
/// <summary>
/// Called when a client connects or disconnet
/// </summary>
public System.Action<List<ClientData>> OnClientsChange;
public void SetUp(ClientObject client)
{
Reset();
client.client.RegisterHandler(LoginProtocols.OtherClientConnected, OnClientConnect);
client.client.RegisterHandler(LoginProtocols.OtherClientDisconnected, OnClientDisconnect);
}
public void Reset()
{
AllClients = new List<ClientData>();
OnClientsChange?.Invoke(AllClients);
}
public void OnClientConnect(NetworkMessage msg)
{
LoginProtocols.LoginMsg loginMsg;
if (!msg.TryRead(out loginMsg))
return;
AllClients.Add(new ClientData(loginMsg));
OnClientsChange?.Invoke(AllClients);
}
public void OnClientDisconnect(NetworkMessage msg)
{
LoginProtocols.LoginMsg loginMsg;
if (!msg.TryRead(out loginMsg))
return;
AllClients.RemoveAll(p => (p.Name == loginMsg.Name) && (p.Color == loginMsg.Color) && (p.characterAnimal == loginMsg.Animal));
OnClientsChange?.Invoke(AllClients);
}
}
[System.Serializable]
public class ClientData
{
/// <summary>
/// Client Name
/// </summary>
public string Name;
/// <summary>
/// Color which represents player, picked by player
/// </summary>
public Color Color;
/// <summary>
/// Clients character animal - needed for instantiation
/// </summary>
public string characterAnimal = "Bear";
public ClientData(LoginProtocols.LoginMsg msg)
{
this.Name = msg.Name;
this.Color = msg.Color;
this.characterAnimal = msg.Animal;
}
}
}

+ 11
- 0
Assets/Scripts/Networking/Client/ConnectedClients.cs.meta View File

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

+ 2
- 0
Assets/Scripts/Networking/Core/Protocols/LoginProtocols.cs View File

@ -11,6 +11,8 @@ public class LoginProtocols
public const short DetailSucess = 105; public const short DetailSucess = 105;
public const short LoginFail = 103; public const short LoginFail = 103;
public const short SceneChange = 104; public const short SceneChange = 104;
public const short OtherClientConnected = 105;
public const short OtherClientDisconnected = 106;
public class EmptyMsg : MessageBase { } public class EmptyMsg : MessageBase { }

+ 29
- 0
Assets/Scripts/Networking/Server/ClientList.cs View File

@ -11,6 +11,14 @@ namespace Networking.Server
public class ClientList : ScriptableObject, IEnumerable<ClientData> public class ClientList : ScriptableObject, IEnumerable<ClientData>
{ {
#if UNITY_EDITOR
[SerializeField]
[Multiline]
private string DevNotes;
#endif
[SerializeField] [SerializeField]
private Inventory StartInventory; private Inventory StartInventory;
@ -28,6 +36,8 @@ namespace Networking.Server
public List<ClientData> DisconnectedClients; public List<ClientData> DisconnectedClients;
private List<NetworkConnection> PotentialClients = new List<NetworkConnection>();
/// <summary> /// <summary>
/// Called when a client connects or disconnet /// Called when a client connects or disconnet
/// </summary> /// </summary>
@ -48,6 +58,7 @@ namespace Networking.Server
{ {
ConnectedClients = new List<ClientData>(); ConnectedClients = new List<ClientData>();
DisconnectedClients = new List<ClientData>(); DisconnectedClients = new List<ClientData>();
PotentialClients = new List<NetworkConnection>();
} }
/// <summary> /// <summary>
@ -73,11 +84,18 @@ namespace Networking.Server
return; return;
} }
if (!PotentialClients.Contains(msg.conn))
PotentialClients.Add(msg.conn);
msg.conn.Send(LoginProtocols.RequestLoginDetails, new LoginProtocols.EmptyMsg()); msg.conn.Send(LoginProtocols.RequestLoginDetails, new LoginProtocols.EmptyMsg());
ConnectedClients.ForEach(p => msg.conn.Send(LoginProtocols.OtherClientConnected, new LoginProtocols.LoginMsg(p.Name, p.Color, p.characterAnimal)));
} }
public void OnClientDisconnect(NetworkMessage msg) public void OnClientDisconnect(NetworkMessage msg)
{ {
PotentialClients.RemoveAll(p => p == msg.conn);
if (!ConnectedClients.Any(p => p.ID == msg.conn.connectionId)) if (!ConnectedClients.Any(p => p.ID == msg.conn.connectionId))
{ {
Debug.LogError("Unknown client disconnect [" + msg.conn.connectionId + "]"); Debug.LogError("Unknown client disconnect [" + msg.conn.connectionId + "]");
@ -88,6 +106,9 @@ namespace Networking.Server
ConnectedClients.Remove(client); ConnectedClients.Remove(client);
DisconnectedClients.Add(client); DisconnectedClients.Add(client);
ConnectedClients.ForEach(p => p.conn.Send(LoginProtocols.OtherClientDisconnected, new LoginProtocols.LoginMsg(client.Name, client.Color, client.characterAnimal)));
PotentialClients.ForEach(p => p.Send(LoginProtocols.OtherClientDisconnected, new LoginProtocols.LoginMsg(client.Name, client.Color, client.characterAnimal)));
Debug.Log("Disconnected: " + client.Name); Debug.Log("Disconnected: " + client.Name);
OnClientsChange.Invoke(ConnectedClients); OnClientsChange.Invoke(ConnectedClients);
} }
@ -127,13 +148,21 @@ namespace Networking.Server
newClient.Name = loginMsg.Name; newClient.Name = loginMsg.Name;
newClient.conn = msg.conn; newClient.conn = msg.conn;
ConnectedClients.ForEach(p => p.conn.Send(LoginProtocols.OtherClientConnected, new LoginProtocols.LoginMsg(newClient.Name, newClient.Color, newClient.characterAnimal)));
ConnectedClients.Add(newClient); ConnectedClients.Add(newClient);
PotentialClients.RemoveAll(p => p == newClient.conn);
PotentialClients.ForEach(p => p.Send(LoginProtocols.OtherClientConnected, new LoginProtocols.LoginMsg(newClient.Name, newClient.Color, newClient.characterAnimal)));
newClient.conn.Send(LoginProtocols.LoginSuccess, new LoginProtocols.LoginMsg(newClient.Name, newClient.Color, newClient.characterAnimal)); newClient.conn.Send(LoginProtocols.LoginSuccess, new LoginProtocols.LoginMsg(newClient.Name, newClient.Color, newClient.characterAnimal));
OnClientsChange.Invoke(ConnectedClients); OnClientsChange.Invoke(ConnectedClients);
newClient.SendInventory(); newClient.SendInventory();
newClient.SendScore(); newClient.SendScore();
newClient.ChangeScene("WaitScene"); newClient.ChangeScene("WaitScene");
} }
#region IEnumerable Implementation #region IEnumerable Implementation

+ 2
- 18
Assets/Scripts/UI/Client/LoginUIManager.cs View File

@ -41,8 +41,8 @@ public class LoginUIManager : MonoBehaviour
private string playerAnimal; private string playerAnimal;
private Color playerColor; private Color playerColor;
bool animalSelected = true;
bool colorSelected = true;
bool animalSelected = false;
bool colorSelected = false;
#region Unity Functions #region Unity Functions
@ -130,22 +130,6 @@ public class LoginUIManager : MonoBehaviour
} }
} }
public void OnClick_Color(Color playerColor)
{
this.playerColor = playerColor;
}
public void OnClick_Colour(string hex)
{
Color newColor = new Color();
if (ColorUtility.TryParseHtmlString(hex, out newColor)){
this.playerColor = newColor;
colorSelected = true;
}
clientManager.SendPlayerCDetails(playerColor);
}
public void OnClick_Animal(string playerAnimal) public void OnClick_Animal(string playerAnimal)
{ {
this.playerAnimal = playerAnimal; this.playerAnimal = playerAnimal;

+ 18
- 7
Assets/Scripts/animalSelection.cs View File

@ -1,24 +1,35 @@
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using Networking.Server;
using Networking.Client;
using UnityEngine.UI; using UnityEngine.UI;
public class animalSelection : MonoBehaviour public class animalSelection : MonoBehaviour
{ {
public ClientList Clients;
public ConnectedClients Clients;
public List<Toggle> Animals; public List<Toggle> Animals;
void Update()
//change function to react on push from the server
//private void OnEnable()
//{
// Clients.OnClientsChange += UpdateAnimals;
//}
//
//private void OnDisable()
//{
// Clients.OnClientsChange -= UpdateAnimals;
//}
public void Update()
{ {
if (Clients.ConnectedClients.Count > 0)
if (Clients.AllClients.Count > 0)
{ {
for (int i = 0; i < Clients.ConnectedClients.Count; i++)
for (int i = 0; i < Clients.AllClients.Count; i++)
{ {
for (int j = 0; j < Animals.Count; j++) for (int j = 0; j < Animals.Count; j++)
{ {
if (Clients.ConnectedClients[i].characterAnimal == Animals[j].name)
if (Clients.AllClients[i].characterAnimal == Animals[j].name)
{ {
Animals[j].GetComponent<Image>().color = Color.black; Animals[j].GetComponent<Image>().color = Color.black;
Animals[j].enabled = false; Animals[j].enabled = false;

Loading…
Cancel
Save