Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enriching item api #232

Open
wants to merge 3 commits into
base: 13.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
183 changes: 170 additions & 13 deletions NwPluginAPI/Core/Items/ItemPickup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@ namespace PluginAPI.Core.Items
using Core;
using System.Collections.Generic;
using UnityEngine;
using PluginAPI.Core.Zones;
using MapGeneration;

public class ItemPickup
{
private static readonly Dictionary<ushort, ItemPickup> CachedItems = new Dictionary<ushort, ItemPickup>();
private PickupStandardPhysics _pickupStandardPhysics;
private PickupStandardPhysics? _pickupStandardPhysics;

// This values are from ItemPickupBase
private const float MinimalPickupTime = 0.245f;
private const float WeightToTime = 0.175f;

/// <summary>
/// The base-game object.
Expand All @@ -26,7 +32,7 @@ public class ItemPickup
/// <summary>
/// Gets the pickup's previous owner.
/// </summary>
public Player LastOwner => OriginalObject.PreviousOwner.Hub != null ? Player.Get<Player>(OriginalObject.PreviousOwner.Hub) : null;
public Player? LastOwner => OriginalObject.PreviousOwner.Hub != null ? Player.Get<Player>(OriginalObject.PreviousOwner.Hub) : null;

/// <summary>
/// Gets the pickup's serial.
Expand All @@ -51,23 +57,32 @@ public bool IsLocked
set => OriginalObject.Info.Locked = value;
}

/// <summary>
/// Gets or sets whether the item pickup is currently in use.
/// </summary>
/// <value>True if the item pickup is in use; otherwise, false.</value>
public bool InUse
{
get => OriginalObject.Info.InUse;
set => OriginalObject.Info.InUse = value;
}

/// <summary>
/// Gets the pickup's <see cref="PickupStandardPhysics"/>.
/// </summary>
public PickupStandardPhysics PickupStandardPhysics
public PickupStandardPhysics? PickupStandardPhysics
{
get
{
if (_pickupStandardPhysics == null)
_pickupStandardPhysics = OriginalObject.PhysicsModule as PickupStandardPhysics;
_pickupStandardPhysics ??= (PickupStandardPhysics)OriginalObject.PhysicsModule;
return _pickupStandardPhysics;
}
}

/// <summary>
/// Gets the pickup's <see cref="UnityEngine.Rigidbody"/>.
/// </summary>
public Rigidbody Rigidbody => PickupStandardPhysics.Rb;
public Rigidbody? Rigidbody => PickupStandardPhysics?.Rb;

/// <summary>
/// Gets the pickup's <see cref="UnityEngine.Rigidbody"/>.
Expand All @@ -80,15 +95,83 @@ public PickupStandardPhysics PickupStandardPhysics
public GameObject GameObject => OriginalObject.gameObject;

/// <summary>
/// Gets the pickup's position.
/// Gets or sets the pickup's position.
/// </summary>
public Vector3 Position
{
get => OriginalObject.Position;
set => OriginalObject.Position = value;
}

/// <summary>
/// Gets or sets the rotation of the pickup.
/// </summary>
public Vector3 Position => Transform.position;
/// <value>The rotation of the item pickup as a <see cref="Quaternion"/>.</value>
public Quaternion Rotation
{
get => OriginalObject.Rotation;
set => OriginalObject.Rotation = value;
}

/// <summary>
/// Gets the pickup's rotation.
/// Gets or sets the scale of the pickup.
/// </summary>
public Quaternion Rotation => Transform.rotation;
/// <value>The scale of the pickup as a <see cref="Vector3"/>.</value>
public Vector3 Scale
{
get => OriginalObject.transform.localScale;
set
{
// If the provided scale matches the current scale, no action is taken.
if (value == OriginalObject.transform.localScale)
return;

// If the object has not been spawned on the network, set the scale directly.
if (!NetworkServer.spawned.ContainsKey(OriginalObject.netId))
{
OriginalObject.transform.localScale = value;
}
else
{
// Unspawn the item pickup, set the scale, and then spawn it again.
UnSpawn();
OriginalObject.transform.localScale = value;
Spawn();
}
}
}

/// <summary>
/// Gets or sets the time it takes to pick up this pickup based on its weight.
/// </summary>
/// <value>The pickup time in seconds.</value>
public float PickupTime
{
get => MinimalPickupTime + (WeightToTime * Weight);
set => Weight = MinimalPickupTime - (WeightToTime / value);
}

/// <summary>
/// Gets the time it takes to pick up this item for a specific <see cref="Player"/>.
/// </summary>
/// <param name="player">The <see cref="Player"/> attempting to pick up the item.</param>
/// <returns>The pickup time in seconds for the specified player.</returns>
public float PickupTimeForPlayer(Player player)
{
return OriginalObject.SearchTimeForPlayer(player.ReferenceHub);
}

/// <summary>
/// Gets the room in which this item pickup is located.
/// </summary>
/// <value>The <see cref="RoomIdentifier"/> representing the room or null if not found.</value>
public RoomIdentifier? Room => RoomIdUtils.RoomAtPositionRaycasts(Position);

/// <summary>
/// Gets an existing <see cref="ItemPickup"/> associated with the given <see cref="ItemPickupBase"/> or adds a new one if not found.
/// </summary>
/// <param name="item">The <see cref="ItemPickupBase"/> to look up or add.</param>
/// <returns>The existing or newly created <see cref="ItemPickup"/> associated with the input item.</returns>
private static ItemPickup GetOrAdd(ItemPickupBase item)
{
if (CachedItems.TryGetValue(item.Info.Serial, out ItemPickup it))
Expand All @@ -99,9 +182,15 @@ private static ItemPickup GetOrAdd(ItemPickupBase item)
return newItem;
}

/// <summary>
/// Removes an existing <see cref="ItemPickup"/> associated with the given <see cref="ItemPickupBase"/>.
/// </summary>
/// <param name="item">The <see cref="ItemPickupBase"/> to remove from the cache.</param>
/// <returns>True if an associated <see cref="ItemPickup"/> was found and removed; otherwise, false.</returns>
public static bool Remove(ItemPickupBase item)
{
if (!CachedItems.TryGetValue(item.Info.Serial, out ItemPickup it)) return false;
if (!CachedItems.TryGetValue(item.Info.Serial, out _))
return false;

return CachedItems.Remove(item.Info.Serial);
}
Expand All @@ -113,23 +202,73 @@ public static bool Remove(ItemPickupBase item)
/// <param name="position">The position.</param>
/// <param name="rotation">The rotation.</param>
/// <returns>The created <see cref="ItemPickup"/>.</returns>
public static ItemPickup Create(ItemType item, Vector3 position, Quaternion rotation)
public static ItemPickup? Create(ItemType item, Vector3 position, Quaternion rotation = default)
{
if (item == ItemType.None || !InventoryItemLoader.AvailableItems.TryGetValue(item, out ItemBase ib))
return null;

PickupSyncInfo syncInfo = new PickupSyncInfo()
PickupSyncInfo syncInfo = new()
{
ItemId = item,
Serial = ItemSerialGenerator.GenerateNext(),
WeightKg = ib.Weight
};

ItemPickupBase newPickup = InventoryExtensions.ServerCreatePickup(ib, syncInfo, position, rotation, false);
return GetOrAdd(newPickup);
}

/// <summary>
/// Creates a new <see cref="ItemPickup"/> and spawns it.
/// </summary>
/// <param name="type">The <see cref="ItemType"/> of the item.</param>
/// <param name="position">The position where the pickup should be created.</param>
/// <param name="rotation">The rotation of the pickup (optional).</param>
/// <returns>The created and spawned <see cref="ItemPickup"/> if successful, otherwise null.</returns>
public static ItemPickup? CreateAndSpawn(ItemType type, Vector3 position, Quaternion rotation = default)
{
var pickup = Create(type, position, rotation);
pickup?.Spawn();
return pickup;
}

/// <summary>
/// Creates a new <see cref="ItemPickup"/>.
/// </summary>
/// <param name="type">The <see cref="ItemType"/> of the item.</param>
/// <param name="position">The position where the pickup should be created.</param>
/// <param name="pickupInfo">Additional pickup information.</param>
/// <param name="rotation">The rotation of the pickup (optional).</param>
/// <returns>The created <see cref="ItemPickup"/> if successful, otherwise null.</returns>
public static ItemPickup? Create(ItemType type, Vector3 position, PickupSyncInfo pickupInfo)
{
if (type == ItemType.None || !InventoryItemLoader.AvailableItems.TryGetValue(type, out ItemBase ib))
return null;

if(pickupInfo.Serial == 0 || pickupInfo.ItemId == ItemType.None)
return null;

ItemPickupBase newPickup = InventoryExtensions.ServerCreatePickup(ib, pickupInfo, position, false);

return GetOrAdd(newPickup);
}

/// <summary>
/// Creates a clone of the current <see cref="ItemPickup"/> with a new serial.
/// </summary>
/// <returns>The cloned <see cref="ItemPickup"/>.</returns>
public ItemPickup? Clone()
{
PickupSyncInfo newInfo = new()
{
ItemId = Type,
Serial = ItemSerialGenerator.GenerateNext(),
WeightKg = Weight
};

return Create(Type, Position, newInfo);
}

/// <summary>
/// Spawns the pickup.
/// </summary>
Expand All @@ -138,6 +277,14 @@ public void Spawn()
NetworkServer.Spawn(GameObject);
}

/// <summary>
/// Unspawn the pickup.
/// </summary>
public void UnSpawn()
{
NetworkServer.UnSpawn(GameObject);
}

/// <summary>
/// Destroys the pickup.
/// </summary>
Expand All @@ -146,9 +293,19 @@ public void Destroy()
OriginalObject.DestroySelf();
}

/// <summary>
/// Initializes a new instance of the <see cref="ItemPickup"/> class based on an existing <see cref="ItemPickupBase"/>.
/// </summary>
/// <param name="item">The original <see cref="ItemPickupBase"/> to associate with this <see cref="ItemPickup"/>.</param>
public ItemPickup(ItemPickupBase item)
{
OriginalObject = item;
}

/// <summary>
/// Returns a string representation of the item pickup, including important related data.
/// </summary>
/// <returns>A string containing information about the item pickup.</returns>
public override string ToString() => $"{Type} - {Serial} | {Weight} - *{Scale}* | {Position} | {IsLocked} - {InUse}";
}
}
17 changes: 11 additions & 6 deletions NwPluginAPI/Core/Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public static DecontaminationController.DecontaminationStatus DecontaminationSta

#region Facility rooms tools

#region GetRandomRoom
#region Get random room

/// <summary>
/// Get a random room from the specified zone.
Expand All @@ -129,7 +129,7 @@ public static RoomIdentifier GetRandomRoom(FacilityZone zone)

#endregion

#region Light Flicker
#region Light flicker

/// <summary>
/// Turns off the lights in the specified zone, for a period of time.
Expand Down Expand Up @@ -173,7 +173,7 @@ public static void FlickerAllLights(float duration)

#endregion

#region Turn On Lights
#region Turn on lights

/// <summary>
/// Turn on all the lights on the map
Expand Down Expand Up @@ -223,7 +223,10 @@ public static void ChangeColorOfAllLights(Color color)
{
foreach (var controller in RoomLightController.Instances)
{
// this is only for save the default color.
controller.Room.ApiRoom.Lights.LightColor = color;

controller.NetworkOverrideColor = color;
}
}

Expand All @@ -236,8 +239,10 @@ public static void ChangeColorOfLights(Color color, FacilityZone zone)
{
if (controller.Room.Zone != zone)
continue;

// this is only for save the default color.
controller.Room.ApiRoom.Lights.LightColor = color;

controller.NetworkOverrideColor = color;
}
}

Expand All @@ -264,7 +269,7 @@ public static void ResetColorOfAllLights()
foreach (var controller in RoomLightController.Instances)
{
if (controller.Room.ApiRoom.Lights.LightColor != controller.Room.ApiRoom.Lights.DefaultColor)
controller.Room.ApiRoom.Lights.LightColor = controller.Room.ApiRoom.Lights.DefaultColor;
controller.NetworkOverrideColor = controller.Room.ApiRoom.Lights.DefaultColor;
}
}

Expand All @@ -279,7 +284,7 @@ public static void ResetColorOfLights(FacilityZone zone)
if (controller.Room.Zone != zone) continue;

if (controller.Room.ApiRoom.Lights.LightColor != controller.Room.ApiRoom.Lights.DefaultColor)
controller.Room.ApiRoom.Lights.LightColor = controller.Room.ApiRoom.Lights.DefaultColor;
controller.NetworkOverrideColor = controller.Room.ApiRoom.Lights.DefaultColor;
}
}

Expand Down
1 change: 1 addition & 0 deletions NwPluginAPI/NwPluginAPI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFramework>net48</TargetFramework>
<PlatformTarget>x64</PlatformTarget>
<OutputType>Library</OutputType>
<Nullable>Enable</Nullable>

<AssemblyName>PluginAPI</AssemblyName>
<RootNamespace>PluginAPI</RootNamespace>
Expand Down