Skip to content

Commit

Permalink
Refactor how we keep track of the players data and model on the client.
Browse files Browse the repository at this point in the history
  • Loading branch information
hubastard committed Mar 1, 2021
1 parent c1e76f4 commit 501b890
Show file tree
Hide file tree
Showing 24 changed files with 202 additions and 167 deletions.
14 changes: 4 additions & 10 deletions NebulaClient/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
using LiteNetLib.Utils;
using NebulaClient.MonoBehaviours;
using NebulaModel.Networking;
using NebulaModel.Packets;
using NebulaModel.Packets.Planet;
using NebulaModel.Packets.Session;
using NebulaModel.Utils;
using System;

Expand All @@ -15,7 +16,6 @@ public class Client

public bool IsConnected { get; protected set; }
public bool IsSessionJoined { get; protected set; }
public ushort LocalPlayerId { get; protected set; }

public NetPacketProcessor PacketProcessor { get; }

Expand All @@ -34,10 +34,10 @@ public Client()
PacketProcessor = new NetPacketProcessor();
LiteNetLibUtils.RegisterAllPacketNestedTypes(PacketProcessor);

PacketProcessor.SubscribeReusable<JoinSessionConfirmed>(OnJoinSessionConfirmed);
PacketProcessor.SubscribeReusable<JoinSessionConfirmed>((_) => { IsSessionJoined = true; });
}

public void Connect(string ip, int port)
public void Connect(string ip, int port)
{
client.Start();
client.Connect(ip, port, "nebula");
Expand Down Expand Up @@ -98,12 +98,6 @@ public void OnPeerDisconnected(NetPeer peer, DisconnectInfo disconnectInfo)
}));
}

private void OnJoinSessionConfirmed(JoinSessionConfirmed packet)
{
LocalPlayerId = packet.LocalPlayerId;
IsSessionJoined = true;
Console.WriteLine($"Client PlayerId is: {LocalPlayerId}");
}

//This function is called when the local player mines a vegetation
public void OnVegetationMined(int id, int planetID)
Expand Down
10 changes: 10 additions & 0 deletions NebulaClient/GameLogic/LocalPlayerModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using UnityEngine;

namespace NebulaClient.GameLogic
{
public class LocalPlayerModel
{
public global::Player Data => GameMain.mainPlayer;
public Transform Transform => GameMain.mainPlayer.transform;
}
}
12 changes: 12 additions & 0 deletions NebulaClient/GameLogic/Player.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace NebulaClient.GameLogic
{
public class Player
{
public ushort PlayerId { get; protected set; }

public Player(ushort playerId)
{
PlayerId = playerId;
}
}
}
79 changes: 79 additions & 0 deletions NebulaClient/GameLogic/PlayerManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using NebulaModel.Logger;
using System.Collections.Generic;

namespace NebulaClient.GameLogic
{
public class PlayerManager
{
Dictionary<ushort, Player> remotePlayers;
Dictionary<ushort, RemotePlayerModel> remotePlayerModels;

public Player LocalPlayer { get; protected set; }
public readonly LocalPlayerModel LocalPlayerModel = new LocalPlayerModel();

public PlayerManager()
{
remotePlayers = new Dictionary<ushort, Player>();
remotePlayerModels = new Dictionary<ushort, RemotePlayerModel>();
}

public void SetLocalPlayer(ushort localPlayerId)
{
LocalPlayer = new Player(localPlayerId);
}

public void AddRemotePlayer(ushort playerId)
{
if (remotePlayers.ContainsKey(playerId))
{
Log.Error($"RemotePlayerManager :: Already contains the playerId {playerId}");
return;
}

Player info = new Player(playerId);
remotePlayers.Add(playerId, info);

RemotePlayerModel model = new RemotePlayerModel(playerId);
remotePlayerModels.Add(playerId, model);
}

public void RemovePlayer(ushort playerId)
{
remotePlayers.Remove(playerId);

if (remotePlayerModels.ContainsKey(playerId))
{
remotePlayerModels[playerId].Destroy();
remotePlayerModels.Remove(playerId);
}
}

public void RemoveAll()
{
foreach(var playerModel in remotePlayerModels.Values)
{
playerModel.Destroy();
}
remotePlayerModels.Clear();
remotePlayers.Clear();
}

public Player GetPlayerById(ushort playerId)
{
if (remotePlayers.ContainsKey(playerId))
{
return remotePlayers[playerId];
}
return null;
}

public RemotePlayerModel GetPlayerModelById(ushort playerId)
{
if (remotePlayerModels.ContainsKey(playerId))
{
return remotePlayerModels[playerId];
}
return null;
}
}
}
49 changes: 49 additions & 0 deletions NebulaClient/GameLogic/RemotePlayerModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using NebulaClient.MonoBehaviours.Remote;
using UnityEngine;

namespace NebulaClient.GameLogic
{
public class RemotePlayerModel
{
const int PLAYER_PROTO_ID = 1;

public Transform PlayerTransform { get; set; }
public Transform PlayerModelTransform { get; set; }
public RemotePlayerMovement Movement { get; set; }
public RemotePlayerAnimation Animator { get; set; }

public RemotePlayerModel(ushort playerId)
{
// Spawn remote player model by cloning the player prefab and replacing local player script by remote player ones.
string playerPrefabPath = LDB.players.Select(PLAYER_PROTO_ID).PrefabPath;
if (playerPrefabPath != null)
{
PlayerTransform = Object.Instantiate(Resources.Load<Transform>(playerPrefabPath));
PlayerModelTransform = PlayerTransform.Find("Model");

// Remove local player components
Object.Destroy(PlayerTransform.GetComponent<PlayerFootsteps>());
Object.Destroy(PlayerTransform.GetComponent<PlayerEffect>());
Object.Destroy(PlayerTransform.GetComponent<PlayerAudio>());
Object.Destroy(PlayerTransform.GetComponent<PlayerAnimator>());
Object.Destroy(PlayerTransform.GetComponent<PlayerController>());
PlayerTransform.GetComponent<Rigidbody>().isKinematic = true;

// Add remote player components
Movement = PlayerTransform.gameObject.AddComponent<RemotePlayerMovement>();
Animator = PlayerTransform.gameObject.AddComponent<RemotePlayerAnimation>();
}

PlayerTransform.gameObject.name = $"Remote Player ({playerId})";
}

public void Destroy()
{
Object.Destroy(PlayerTransform.gameObject);
PlayerTransform = null;
PlayerModelTransform = null;
Movement = null;
Animator = null;
}
}
}
2 changes: 1 addition & 1 deletion NebulaClient/MonoBehaviours/Local/LocalPlayerAnimation.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using NebulaModel.DataStructures;
using NebulaModel.Packets;
using NebulaModel.Packets.Players;
using UnityEngine;

namespace NebulaClient.MonoBehaviours.Local
Expand Down
5 changes: 2 additions & 3 deletions NebulaClient/MonoBehaviours/Local/LocalPlayerMovement.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using NebulaModel.DataStructures;
using NebulaModel.Packets;
using NebulaModel.Packets.Players;
using UnityEngine;

namespace NebulaClient.MonoBehaviours.Local
Expand Down Expand Up @@ -30,7 +29,7 @@ void Update()
Vector3 rotation = rootTransform.eulerAngles;
Vector3 bodyRotation = bodyTransform.eulerAngles;

MultiplayerSession.instance.Client?.SendPacket(new Movement(position, rotation, bodyRotation), LiteNetLib.DeliveryMethod.Unreliable);
MultiplayerSession.instance.Client?.SendPacket(new Movement(position, rotation, bodyRotation), LiteNetLib.DeliveryMethod.Sequenced);
}
}
}
Expand Down
29 changes: 19 additions & 10 deletions NebulaClient/MonoBehaviours/MultiplayerSession.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using NebulaModel.Packets;
using NebulaClient.GameLogic;
using NebulaModel.Packets.Planet;
using NebulaModel.Packets.Players;
using NebulaModel.Packets.Session;
using UnityEngine;

namespace NebulaClient.MonoBehaviours
Expand All @@ -8,7 +11,7 @@ public class MultiplayerSession : MonoBehaviour
public static MultiplayerSession instance;

public Client Client { get; private set; }
public RemotePlayerManager RemotePlayerManager { get; private set; }
public PlayerManager PlayerManager { get; private set; }

private string serverIp;
private int serverPort;
Expand All @@ -18,10 +21,11 @@ void Awake()
instance = this;

Client = new Client();
Client.PacketProcessor.SubscribeReusable<Movement>(OnPlayerMovement);
Client.PacketProcessor.SubscribeReusable<PlayerAnimationUpdate>(OnPlayerAnimationUpdate);
Client.PacketProcessor.SubscribeReusable<JoinSessionConfirmed>(OnJoinSessionConfirmed);
Client.PacketProcessor.SubscribeReusable<RemotePlayerJoined>(OnRemotePlayerJoined);
Client.PacketProcessor.SubscribeReusable<PlayerDisconnected>(OnRemotePlayerDisconnect);
Client.PacketProcessor.SubscribeReusable<Movement>(OnPlayerMovement);
Client.PacketProcessor.SubscribeReusable<PlayerAnimationUpdate>(OnPlayerAnimationUpdate);
Client.PacketProcessor.SubscribeReusable<VegeMined>(OnVegeMined);
}

Expand All @@ -31,7 +35,7 @@ public void Connect(string ip, int port)
serverPort = port;
Client.Connect(ip, port);

RemotePlayerManager = new RemotePlayerManager();
PlayerManager = new PlayerManager();
}

public void TryToReconnect()
Expand Down Expand Up @@ -73,32 +77,37 @@ void OnDestroy()

void CleanupSession()
{
RemotePlayerManager.RemoveAll();
PlayerManager.RemoveAll();
}

void Update()
{
Client.Update();
}

private void OnJoinSessionConfirmed(JoinSessionConfirmed packet)
{
PlayerManager.SetLocalPlayer(packet.LocalPlayerId);
}

private void OnRemotePlayerJoined(RemotePlayerJoined packet)
{
RemotePlayerManager.AddPlayer(packet.PlayerId);
PlayerManager.AddRemotePlayer(packet.PlayerId);
}

private void OnRemotePlayerDisconnect(PlayerDisconnected packet)
{
RemotePlayerManager.RemovePlayer(packet.PlayerId);
PlayerManager.RemovePlayer(packet.PlayerId);
}

private void OnPlayerMovement(Movement packet)
{
RemotePlayerManager.GetPlayerById(packet.PlayerId)?.Movement.UpdatePosition(packet);
PlayerManager.GetPlayerModelById(packet.PlayerId)?.Movement.UpdatePosition(packet);
}

private void OnPlayerAnimationUpdate(PlayerAnimationUpdate packet)
{
RemotePlayerManager.GetPlayerById(packet.PlayerId)?.Animator.UpdateState(packet);
PlayerManager.GetPlayerModelById(packet.PlayerId)?.Animator.UpdateState(packet);
}

private void OnVegeMined(VegeMined packet)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using NebulaModel.DataStructures;
using NebulaModel.Packets;
using NebulaModel.Packets.Players;
using UnityEngine;

namespace NebulaClient.MonoBehaviours.Remote
Expand Down
2 changes: 1 addition & 1 deletion NebulaClient/MonoBehaviours/Remote/RemotePlayerMovement.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using NebulaClient.MonoBehaviours.Local;
using NebulaModel.DataStructures;
using NebulaModel.Packets;
using NebulaModel.Packets.Players;
using UnityEngine;

namespace NebulaClient.MonoBehaviours.Remote
Expand Down
6 changes: 4 additions & 2 deletions NebulaClient/NebulaClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,16 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Client.cs" />
<Compile Include="GameLogic\LocalPlayerModel.cs" />
<Compile Include="MonoBehaviours\Local\LocalPlayerAnimation.cs" />
<Compile Include="MonoBehaviours\Local\LocalPlayerMovement.cs" />
<Compile Include="MonoBehaviours\MultiplayerSession.cs" />
<Compile Include="MonoBehaviours\NebulaBootstrapper.cs" />
<Compile Include="MonoBehaviours\Remote\RemotePlayerMovement.cs" />
<Compile Include="MonoBehaviours\Remote\RemotePlayerAnimation.cs" />
<Compile Include="Player.cs" />
<Compile Include="RemotePlayerManager.cs" />
<Compile Include="GameLogic\Player.cs" />
<Compile Include="GameLogic\PlayerManager.cs" />
<Compile Include="GameLogic\RemotePlayerModel.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Extensions\MonoBehaviourUtils.cs" />
</ItemGroup>
Expand Down
24 changes: 0 additions & 24 deletions NebulaClient/Player.cs

This file was deleted.

Loading

0 comments on commit 501b890

Please sign in to comment.