-
-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor how we keep track of the players data and model on the client.
- Loading branch information
Showing
24 changed files
with
202 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.