Skip to content

Commit

Permalink
Fixed some leftovers (gamemode, item placing)
Browse files Browse the repository at this point in the history
  • Loading branch information
tornac1234 authored and dartasen committed Dec 3, 2023
1 parent 6f04e0a commit e5864ad
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 43 deletions.
33 changes: 14 additions & 19 deletions NitroxClient/GameLogic/Items.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class Items
{
private readonly IPacketSender packetSender;
private readonly Entities entities;
public GameObject PickingUpObject;
public GameObject PickingUpObject { get; private set; }

public Items(IPacketSender packetSender, Entities entities)
{
Expand Down Expand Up @@ -86,7 +86,7 @@ public void Dropped(GameObject gameObject, TechType? techType = null)
{
// We cast it to an entity type that is always seeable by clients
// therefore, the packet will be redirected to everyone
droppedItem = new GlobalRootEntity(gameObject.transform.ToWorldDto(), 0, classId, true, id, techType.Value.ToDto(), metadata.OrNull(), parentId, childrenEntities);
droppedItem = new GlobalRootEntity(gameObject.transform.ToLocalDto(), 0, classId, true, id, techType.Value.ToDto(), metadata.OrNull(), parentId, childrenEntities);
}
else if (gameObject.TryGetComponent(out OxygenPipe oxygenPipe))
{
Expand Down Expand Up @@ -137,28 +137,23 @@ public void Placed(GameObject gameObject, TechType techType)
string classId = gameObject.GetComponent<PrefabIdentifier>().ClassId;

List<Entity> childrenEntities = GetPrefabChildren(gameObject, id).ToList();
WorldEntity placedItem = new(gameObject.transform.ToLocalDto(), 0, classId, true, id, techType.ToDto(), metadata.OrNull(), null, childrenEntities);
WorldEntity placedItem;

// If the object is dropped in the water, it'll be parented to a CellRoot so we let it as WorldEntity (see Items.Dropped)
// PlaceTool's object is located under GlobalRoot or under a CellRoot (we differentiate both by giving a different type)
// Because objects under CellRoots must only spawn when visible while objects under GlobalRoot must be spawned at all times
if (IsGlobalRootObject(gameObject))
switch (gameObject.AliveOrNull())
{
placedItem = (GlobalRootEntity)placedItem;
}
else
{
SubRoot currentSub = Player.main.AliveOrNull()?.GetCurrentSub();
if (currentSub && currentSub.TryGetNitroxId(out NitroxId parentId))
{
placedItem.ParentId = parentId;
placedItem = (GlobalRootEntity)placedItem;
}
// If the object is not under a SubRoot nor in GlobalRoot, it'll be under a CellRoot but we still want to remember its state
else
{
placedItem = (PlacedWorldEntity)placedItem;
}
case not null when IsGlobalRootObject(gameObject):
placedItem = new GlobalRootEntity(gameObject.transform.ToWorldDto(), 0, classId, true, id, techType.ToDto(), metadata.OrNull(), null, childrenEntities);
break;
case not null when Player.main.AliveOrNull()?.GetCurrentSub().AliveOrNull()?.TryGetNitroxId(out NitroxId parentId) == true:
placedItem = new GlobalRootEntity(gameObject.transform.ToLocalDto(), 0, classId, true, id, techType.ToDto(), metadata.OrNull(), parentId, childrenEntities);
break;
default:
// If the object is not under a SubRoot nor in GlobalRoot, it'll be under a CellRoot but we still want to remember its state
placedItem = new PlacedWorldEntity(gameObject.transform.ToWorldDto(), 0, classId, true, id, techType.ToDto(), metadata.OrNull(), null, childrenEntities);
break;
}

Log.Debug($"Placed object: {placedItem}");
Expand Down
4 changes: 2 additions & 2 deletions NitroxClient/GameLogic/Spawning/Bases/BuildEntitySpawner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,12 @@ public static IEnumerator SetupBase(BuildEntity buildEntity, Base @base, Entitie
// While the rest must be spawned earlier for the base to load correctly (mostly InteriorPieceEntity)
// Which is why the spawn loops are separated by the SetActive instruction
// NB: We aim at spawning very precise entity types (InteriorPieceEntity, ModuleEntity and GlobalRootEntity)
// Thus we use GetType().Equals instead of "is GlobalRootEntity" so that derived types from it aren't selected
// Thus we use GetType() == instead of "is GlobalRootEntity" so that derived types from it aren't selected
List<GhostEntity> ghostChildrenEntities = new();
foreach (Entity childEntity in buildEntity.ChildEntities)
{
if (childEntity is InteriorPieceEntity || childEntity is ModuleEntity ||
childEntity.GetType().Equals(typeof(GlobalRootEntity)))
childEntity.GetType() == typeof(GlobalRootEntity))
{
if (childEntity is GhostEntity ghostEntity)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public FlareMetadataExtractor(Items items)

public override FlareMetadata Extract(Flare flare)
{
// If the flare is being picked up, its metadata must be set accordingly
// If the flare is thrown, set its activation time
if (flare.flareActiveState && items.PickingUpObject != flare.gameObject)
{
return new(flare.energyLeft, flare.hasBeenThrown, flare.flareActivateTime);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ public static void AdditionalSpawningSteps(GameObject gameObject)
UWE.Utils.SetIsKinematicAndUpdateInterpolation(rigidbody, true, false);
}
placeTool.OnPlace();
Log.Debug($"Applied additional steps to {gameObject}");
}
}

Expand Down
2 changes: 1 addition & 1 deletion NitroxClient/GameLogic/TimeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public double CurrentTime
/// <para>
/// Replaces <see cref="Time.deltaTime"/> because it is capped by <see cref="Time.maximumDeltaTime"/>
/// and may not reflect the real time which has passed between two frames once it's higher than the said maximum
/// [<see href="https://docs.unity3d.com/ScriptReference/Time-maximumDeltaTime.html"/>].
/// <br/>See <a href="https://docs.unity3d.com/ScriptReference/Time-maximumDeltaTime.html">Time.maximumDeltaTime</a>
/// </para>
/// <para>
/// This value is set to <c>0</c> when a time skip occurs to avoid undesired behaviours
Expand Down
4 changes: 2 additions & 2 deletions NitroxClient/MonoBehaviours/NitroxEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ public static void SetNewId(GameObject gameObject, NitroxId id)
{
// To avoid unrequired error spams, we do the id setting manually
// If the current UID was already registered, we unregister it
if (!string.IsNullOrEmpty(uniqueIdentifier.id)
&& UniqueIdentifier.identifiers.TryGetValue(uniqueIdentifier.Id, out UniqueIdentifier registeredIdentifier) &&
if (!string.IsNullOrEmpty(uniqueIdentifier.id) &&
UniqueIdentifier.identifiers.TryGetValue(uniqueIdentifier.Id, out UniqueIdentifier registeredIdentifier) &&
registeredIdentifier == uniqueIdentifier)
{
UniqueIdentifier.identifiers.Remove(uniqueIdentifier.id);
Expand Down
17 changes: 0 additions & 17 deletions NitroxModel/DataStructures/GameLogic/Entities/WorldEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,23 +85,6 @@ public WorldEntity(NitroxTransform transform, int level, string classId, bool sp
SpawnedByServer = spawnedByServer;
}

// TO exeriment
public T ConvertTo<T>() where T : WorldEntity, new()
{
return new T
{
Transform = Transform,
Level = Level,
ClassId = ClassId,
SpawnedByServer = SpawnedByServer,
Id = Id,
TechType = TechType,
Metadata = Metadata,
ParentId = ParentId,
ChildEntities = ChildEntities
};
}

public override string ToString()
{
return $"[{GetType().Name} Transform: {Transform} Level: {Level} ClassId: {ClassId} SpawnedByServer: {SpawnedByServer} {base.ToString()}]";
Expand Down
4 changes: 4 additions & 0 deletions NitroxServer/ConsoleCommands/ChangeServerGamemodeCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ protected override void Execute(CallArgs args)
{
serverConfig.GameMode = sgm;

foreach (Player player in playerManager.GetAllPlayers())
{
player.GameMode = sgm;
}
playerManager.SendPacketToAllPlayers(GameModeChanged.ForAllPlayers(sgm));
SendMessageToAllPlayers($"Server gamemode changed to \"{sgm}\" by {args.SenderName}");
}
Expand Down
1 change: 1 addition & 0 deletions NitroxServer/ConsoleCommands/GameModeCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ protected override void Execute(CallArgs args)
// The target player if not set, is the player who sent the command
targetPlayer ??= args.Sender.Value;

targetPlayer.GameMode = gameMode;
playerManager.SendPacketToAllPlayers(GameModeChanged.ForPlayer(targetPlayer.Id, gameMode));
SendMessage(targetPlayer, $"GameMode changed to {gameMode}");
if (args.IsConsole)
Expand Down

0 comments on commit e5864ad

Please sign in to comment.