Skip to content

Commit

Permalink
Improvements: only one virtual cyclops for all cyclops, openables no …
Browse files Browse the repository at this point in the history
…longer push players, disconnect event is now fired
  • Loading branch information
tornac1234 committed Aug 7, 2024
1 parent aa6588d commit 8e512c5
Show file tree
Hide file tree
Showing 12 changed files with 213 additions and 121 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using NitroxClient.Communication.Packets.Processors.Abstract;
using NitroxClient.Communication.Packets.Processors.Abstract;
using NitroxClient.GameLogic;
using NitroxClient.GameLogic.HUD;
using NitroxModel.DataStructures.Util;
Expand Down Expand Up @@ -26,6 +26,7 @@ public override void Process(Disconnect disconnect)
Optional<RemotePlayer> remotePlayer = remotePlayerManager.Find(disconnect.PlayerId);
if (remotePlayer.HasValue)
{
remotePlayer.Value.PlayerDisconnectEvent.Trigger(remotePlayer.Value);
remotePlayerManager.RemovePlayer(disconnect.PlayerId);
Log.Info($"{remotePlayer.Value.PlayerName} disconnected");
Log.InGame(Language.main.Get("Nitrox_PlayerDisconnected").Replace("{PLAYER}", remotePlayer.Value.PlayerName));
Expand Down
39 changes: 29 additions & 10 deletions NitroxClient/GameLogic/CyclopsPawn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public class CyclopsPawn
public static readonly int PLAYER_LAYER = 1 << LayerMask.NameToLayer("Player");

private readonly INitroxPlayer player;
private readonly VirtualCyclops virtualCyclops;
private readonly Transform parentTransform;
private readonly NitroxCyclops cyclops;
private readonly Transform virtualTransform;
private readonly Transform realCyclopsTransform;
private readonly bool isLocalPlayer;
public readonly GameObject RealObject;
Expand All @@ -32,12 +32,12 @@ public Vector3 Position
set { Handle.transform.position = value; }
}

public CyclopsPawn(INitroxPlayer player, VirtualCyclops virtualCyclops, Transform realCyclopsTransform)
public CyclopsPawn(INitroxPlayer player, NitroxCyclops cyclops)
{
this.player = player;
this.virtualCyclops = virtualCyclops;
parentTransform = virtualCyclops.transform;
this.realCyclopsTransform = realCyclopsTransform;
this.cyclops = cyclops;
virtualTransform = VirtualCyclops.Instance.transform;
realCyclopsTransform = cyclops.transform;

if (player is ILocalNitroxPlayer)
{
Expand All @@ -64,7 +64,7 @@ public void Initialize(string name, Vector3 localPosition)
Handle = GameObject.CreatePrimitive(PrimitiveType.Capsule);
Handle.layer = 1 << PLAYER_LAYER;
Handle.name = name;
Handle.transform.parent = parentTransform;
Handle.transform.parent = virtualTransform;
Handle.transform.localPosition = localPosition;
GameObject.DestroyImmediate(Handle.GetComponent<Collider>());

Expand All @@ -81,6 +81,8 @@ public void Initialize(string name, Vector3 localPosition)

RegisterController();

Handle.AddComponent<CyclopsPawnIdentifier>().Pawn = this;

Log.Debug($"Pawn: height: {Controller.height}, center {center}, radius: {Controller.radius}, skinWidth: {Controller.skinWidth}");
}

Expand Down Expand Up @@ -114,22 +116,39 @@ public void MaintainPosition()

public void Unregister()
{
if (virtualCyclops)
if (cyclops)
{
if (isLocalPlayer)
{
virtualCyclops.Cyclops.OnLocalPlayerExit();
cyclops.OnLocalPlayerExit();
}
else
{
virtualCyclops.Cyclops.OnPlayerExit((RemotePlayer)player);
cyclops.OnPlayerExit((RemotePlayer)player);
}
}
}

/// <summary>
/// Replicates openable being blocked only if the pawn causing the block is in the cyclops associated to the virtual one.
/// </summary>
public void BlockOpenable(Openable openable, bool blockState)
{
if (cyclops.Virtual)
{
openable.blocked = blockState;
cyclops.Virtual.ReplicateBlock(openable, blockState);
}
}

public void Terminate()
{
controllers.Remove(Controller);
GameObject.Destroy(Handle);
}
}

public class CyclopsPawnIdentifier : MonoBehaviour
{
public CyclopsPawn Pawn;
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public static IEnumerator RestoreModule(Transform parent, ModuleEntity moduleEnt
moduleTransform.localScale = moduleEntity.Transform.LocalScale.ToUnity();
ApplyModuleData(moduleEntity, moduleObject, result);

if (parent && parent.TryGetComponent(out NitroxCyclops nitroxCyclops))
if (parent && parent.TryGetComponent(out NitroxCyclops nitroxCyclops) && nitroxCyclops.Virtual)
{
nitroxCyclops.Virtual.ReplicateConstructable(moduleObject.GetComponent<Constructable>());
}
Expand Down
4 changes: 2 additions & 2 deletions NitroxClient/MonoBehaviours/Cyclops/CyclopsMotor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ public void Initialize(GroundMotor reference)
RecalculateConstants();
}

public void SetCyclops(SubRoot subRoot, CyclopsPawn pawn)
public void SetCyclops(NitroxCyclops cyclops, SubRoot subRoot, CyclopsPawn pawn)
{
cyclops = subRoot.GetComponent<NitroxCyclops>();
this.cyclops = cyclops;
sub = subRoot;
realAxis = sub.subAxis;
virtualAxis = cyclops.Virtual.axis;
Expand Down
61 changes: 37 additions & 24 deletions NitroxClient/MonoBehaviours/Cyclops/NitroxCyclops.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Generic;
using NitroxClient.GameLogic;
using NitroxClient.GameLogic.PlayerLogic;
using NitroxClient.GameLogic.PlayerLogic.PlayerModel.Abstract;
Expand All @@ -10,7 +11,7 @@ namespace NitroxClient.MonoBehaviours.Cyclops;
/// </summary>
public class NitroxCyclops : MonoBehaviour
{
public VirtualCyclops Virtual { get; private set; }
public VirtualCyclops Virtual;
private CyclopsMotor cyclopsMotor;
private SubRoot subRoot;
private SubControl subControl;
Expand All @@ -20,7 +21,7 @@ public class NitroxCyclops : MonoBehaviour
private CharacterController controller;
private int ballasts;

public SubControl.Mode Mode;
public readonly Dictionary<INitroxPlayer, CyclopsPawn> Pawns = [];

public void Start()
{
Expand All @@ -36,18 +37,6 @@ public void Start()
UWE.Utils.SetIsKinematicAndUpdateInterpolation(rigidbody, false, true);

GetComponent<SubFire>().enabled = false;
SetReceiving();

Virtual = VirtualCyclops.CreateVirtualInstance(gameObject);
}

/// <remarks>
/// Triggered by <see cref="LiveMixin"/> sending a "OnKill" message when cyclops is destroyed.
/// This might need to be adapted once the "restore" command is synced (for now a destroyed cyclops can't be restored)
/// </remarks>
public void OnKill()
{
VirtualCyclops.Terminate(gameObject);
}

/// <summary>
Expand All @@ -64,47 +53,52 @@ public void RemoveAllPlayers()
}

/// <summary>
/// Parents local player to the cyclops and registers it in the virtual cyclops.
/// Parents local player to the cyclops and registers it in the current cyclops.
/// </summary>
public void OnLocalPlayerEnter()
{
Virtual = VirtualCyclops.Instance;
Virtual.SetCurrentCyclops(this);

Player.mainObject.transform.parent = subRoot.transform;
CyclopsPawn pawn = Virtual.AddPawnForPlayer(this.Resolve<ILocalNitroxPlayer>());
cyclopsMotor.SetCyclops(subRoot, pawn);
CyclopsPawn pawn = AddPawnForPlayer(this.Resolve<ILocalNitroxPlayer>());
cyclopsMotor.SetCyclops(this, subRoot, pawn);
cyclopsMotor.ToggleCyclopsMotor(true);
}

/// <summary>
/// Unregisters the local player from the cyclops (and from the virtual one). Ensures the player is not weirdly rotated when it leaves the cyclops.
/// Unregisters the local player from the current cyclops. Ensures the player is not weirdly rotated when it leaves the cyclops.
/// </summary>
public void OnLocalPlayerExit()
{
Virtual.RemovePawnForPlayer(this.Resolve<ILocalNitroxPlayer>());
RemovePawnForPlayer(this.Resolve<ILocalNitroxPlayer>());
Player.main.transform.parent = null;
Player.main.transform.rotation = Quaternion.identity;
cyclopsMotor.ToggleCyclopsMotor(false);

Virtual.SetCurrentCyclops(null);
}

/// <summary>
/// Registers a remote player in the virtual cyclops.
/// Registers a remote player for it to get a pawn in the current cyclops.
/// </summary>
public void OnPlayerEnter(RemotePlayer remotePlayer)
{
remotePlayer.Pawn = Virtual.AddPawnForPlayer(remotePlayer);
remotePlayer.Pawn = AddPawnForPlayer(remotePlayer);
}

/// <summary>
/// Unregisters a remote player from the virtual cyclops.
/// Unregisters a remote player from the current cyclops.
/// </summary>
public void OnPlayerExit(RemotePlayer remotePlayer)
{
Virtual.RemovePawnForPlayer(remotePlayer);
RemovePawnForPlayer(remotePlayer);
remotePlayer.Pawn = null;
}

public void MaintainPawns()
{
foreach (CyclopsPawn pawn in Virtual.Pawns.Values)
foreach (CyclopsPawn pawn in Pawns.Values)
{
if (pawn.MaintainPredicate())
{
Expand All @@ -113,6 +107,25 @@ public void MaintainPawns()
}
}

public CyclopsPawn AddPawnForPlayer(INitroxPlayer player)
{
if (!Pawns.TryGetValue(player, out CyclopsPawn pawn))
{
pawn = new(player, this);
Pawns.Add(player, pawn);
}
return pawn;
}

public void RemovePawnForPlayer(INitroxPlayer player)
{
if (Pawns.TryGetValue(player, out CyclopsPawn pawn))
{
pawn.Terminate();
}
Pawns.Remove(player);
}

public void SetBroadcasting()
{
worldForces.OnDisable();
Expand Down
Loading

0 comments on commit 8e512c5

Please sign in to comment.