Skip to content

Commit

Permalink
Additions (#29)
Browse files Browse the repository at this point in the history
* Add a bunch

* Fix

* Security

* Make changes

* remove unused usings

* Getting inventory

* oops

* Dev commit

* use exiled

---------

Co-authored-by: Yamato <66829532+louis1706@users.noreply.github.com>
  • Loading branch information
Misfiy and louis1706 authored Aug 11, 2024
1 parent 08a5deb commit a3595e1
Show file tree
Hide file tree
Showing 14 changed files with 290 additions and 81 deletions.
37 changes: 37 additions & 0 deletions EXILED/Exiled.API/Enums/HazardType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// -----------------------------------------------------------------------
// <copyright file="HazardType.cs" company="Exiled Team">
// Copyright (c) Exiled Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
// </copyright>
// -----------------------------------------------------------------------

namespace Exiled.API.Enums
{
using Exiled.API.Features.Hazards;

/// <summary>
/// Unique identifier for a <see cref="Hazard"/>.
/// </summary>
public enum HazardType
{
/// <summary>
/// SCP-939 amnestic cloud.
/// </summary>
AmnesticCloud,

/// <summary>
/// Sinkhole spawned at start of round.
/// </summary>
Sinkhole,

/// <summary>
/// SCP-173 tantrum.
/// </summary>
Tantrum,

/// <summary>
/// Should never happen
/// </summary>
Unknown,
}
}
63 changes: 63 additions & 0 deletions EXILED/Exiled.API/Extensions/BitwiseExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// -----------------------------------------------------------------------
// <copyright file="BitwiseExtensions.cs" company="Exiled Team">
// Copyright (c) Exiled Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
// </copyright>
// -----------------------------------------------------------------------

namespace Exiled.API.Extensions
{
using System;

/// <summary>
/// Extensions for bitwise operations.
/// </summary>
public static class BitwiseExtensions
{
/// <summary>
/// Adds the specified flags to the given enum value.
/// </summary>
/// <typeparam name="T">The type of the enum.</typeparam>
/// <param name="flags">The enum value to add flags to.</param>
/// <param name="newFlags">The flags to add.</param>
/// <returns>The enum value with the specified flags added.</returns>
public static T AddFlags<T>(this T flags, params T[] newFlags)
where T : Enum => flags.ModifyFlags(true, newFlags);

/// <summary>
/// Removes the specified flags from the given enum value.
/// </summary>
/// <typeparam name="T">The type of the enum.</typeparam>
/// <param name="flags">The enum value to remove flags from.</param>
/// <param name="oldFlags">The flags to remove.</param>
/// <returns>The enum value with the specified flags removed.</returns>
public static T RemoveFlags<T>(this T flags, params T[] oldFlags)
where T : Enum => flags.ModifyFlags(false, oldFlags);

/// <summary>
/// Sets the specified flag to the given value, default is true.
/// </summary>
/// <param name="flags">The flags enum to modify.</param>
/// <param name="value">The value to set the flag to.</param>
/// <param name="changeFlags">The flags to modify.</param>
/// <typeparam name="T">The type of the enum.</typeparam>
/// <returns>The flags enum with the flag set to the given value.</returns>
public static T ModifyFlags<T>(this T flags, bool value, params T[] changeFlags)
where T : Enum
{
long currentValue = Convert.ToInt64(flags);

foreach (T flag in changeFlags)
{
long flagValue = Convert.ToInt64(flag);

if (value)
currentValue |= flagValue;
else
currentValue &= ~flagValue;
}

return (T)Enum.ToObject(typeof(T), currentValue);
}
}
}
23 changes: 13 additions & 10 deletions EXILED/Exiled.API/Extensions/RoleExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,18 +140,22 @@ public static SpawnLocation GetRandomSpawnLocation(this RoleTypeId roleType)
return null;
}

/// <summary>
/// Gets the starting <see cref="InventoryRoleInfo"/> of a <see cref="RoleTypeId"/>.
/// </summary>
/// <param name="role">The <see cref="RoleTypeId"/>.</param>
/// <returns>The <see cref="InventoryRoleInfo"/> that the role receives on spawn. </returns>
public static InventoryRoleInfo GetInventory(this RoleTypeId role)
=> StartingInventories.DefinedInventories.TryGetValue(role, out InventoryRoleInfo info)
? info
: new(Array.Empty<ItemType>(), new());

/// <summary>
/// Gets the starting items of a <see cref="RoleTypeId"/>.
/// </summary>
/// <param name="roleType">The <see cref="RoleTypeId"/>.</param>
/// <returns>An <see cref="Array"/> of <see cref="ItemType"/> that the role receives on spawn. Will be empty for classes that do not spawn with items.</returns>
public static ItemType[] GetStartingInventory(this RoleTypeId roleType)
{
if (StartingInventories.DefinedInventories.TryGetValue(roleType, out InventoryRoleInfo info))
return info.Items;

return Array.Empty<ItemType>();
}
public static ItemType[] GetStartingInventory(this RoleTypeId roleType) => GetInventory(roleType).Items;

/// <summary>
/// Gets the starting ammo of a <see cref="RoleTypeId"/>.
Expand All @@ -160,10 +164,9 @@ public static ItemType[] GetStartingInventory(this RoleTypeId roleType)
/// <returns>An <see cref="Array"/> of <see cref="ItemType"/> that the role receives on spawn. Will be empty for classes that do not spawn with ammo.</returns>
public static Dictionary<AmmoType, ushort> GetStartingAmmo(this RoleTypeId roleType)
{
if (StartingInventories.DefinedInventories.TryGetValue(roleType, out InventoryRoleInfo info))
return info.Ammo.ToDictionary(kvp => kvp.Key.GetAmmoType(), kvp => kvp.Value);
InventoryRoleInfo info = roleType.GetInventory();

return new();
return info.Ammo.ToDictionary(kvp => kvp.Key.GetAmmoType(), kvp => kvp.Value);
}
}
}
22 changes: 21 additions & 1 deletion EXILED/Exiled.API/Features/Hazards/AmnesticCloudHazard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@
// Copyright (c) Exiled Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
// </copyright>
// -----------------------------------------------------------------------
// ------------------------------------------------------------------------

namespace Exiled.API.Features.Hazards
{
using Exiled.API.Enums;
using PlayerRoles.PlayableScps.Scp939;

/// <summary>
/// A wrapper for SCP-939's amnestic cloud.
/// </summary>
public class AmnesticCloudHazard : TemporaryHazard
{
private static Scp939AmnesticCloudInstance amnesticCloudPrefab;

/// <summary>
/// Initializes a new instance of the <see cref="AmnesticCloudHazard"/> class.
/// </summary>
Expand All @@ -26,9 +29,26 @@ public AmnesticCloudHazard(Scp939AmnesticCloudInstance hazard)
Owner = Player.Get(Ability.Owner);
}

/// <summary>
/// Gets the amnestic cloud prefab.
/// </summary>
public static Scp939AmnesticCloudInstance AmnesticCloudPrefab
{
get
{
if (amnesticCloudPrefab == null)
amnesticCloudPrefab = PrefabHelper.GetPrefab<Scp939AmnesticCloudInstance>(PrefabType.AmnesticCloudHazard);

return amnesticCloudPrefab;
}
}

/// <inheritdoc cref="Hazard.Base"/>
public new Scp939AmnesticCloudInstance Base { get; }

/// <inheritdoc />
public override HazardType Type => HazardType.AmnesticCloud;

/// <summary>
/// Gets the <see cref="Scp939AmnesticCloudAbility"/> for this instance.
/// </summary>
Expand Down
13 changes: 13 additions & 0 deletions EXILED/Exiled.API/Features/Hazards/Hazard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace Exiled.API.Features.Hazards
using System.Collections.Generic;
using System.Linq;

using Exiled.API.Enums;
using Exiled.API.Features.Core;
using Exiled.API.Interfaces;
using global::Hazards;
Expand Down Expand Up @@ -48,6 +49,11 @@ public Hazard(EnvironmentalHazard hazard)
/// </summary>
public EnvironmentalHazard Base { get; }

/// <summary>
/// Gets the <see cref="HazardType"/> associated with the current Hazard.
/// </summary>
public virtual HazardType Type { get; } = HazardType.Unknown;

/// <summary>
/// Gets or sets the list with all affected by this hazard players.
/// </summary>
Expand Down Expand Up @@ -153,6 +159,13 @@ public static T Get<T>(EnvironmentalHazard environmentalHazard)
/// <returns><see cref="IEnumerable{T}"/> of <see cref="Hazard"/> based on predicate.</returns>
public static IEnumerable<Hazard> Get(Func<Hazard, bool> predicate) => List.Where(predicate);

/// <summary>
/// Gets an <see cref="IEnumerable{T}"/> of <see cref="Hazard"/>.
/// </summary>
/// <param name="type">The <see cref="HazardType"/> to get.</param>
/// <returns><see cref="IEnumerable{T}"/> of <see cref="Hazard"/> based on type.</returns>
public static IEnumerable<Hazard> Get(HazardType type) => Get(h => h.Type == type);

/// <summary>
/// Checks if player is in hazard zone.
/// </summary>
Expand Down
4 changes: 4 additions & 0 deletions EXILED/Exiled.API/Features/Hazards/SinkholeHazard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Exiled.API.Features.Hazards
{
using Exiled.API.Enums;
using global::Hazards;

/// <summary>
Expand All @@ -28,5 +29,8 @@ public SinkholeHazard(SinkholeEnvironmentalHazard hazard)
/// Gets the <see cref="SinkholeEnvironmentalHazard"/>.
/// </summary>
public new SinkholeEnvironmentalHazard Base { get; }

/// <inheritdoc />
public override HazardType Type => HazardType.Sinkhole;
}
}
44 changes: 44 additions & 0 deletions EXILED/Exiled.API/Features/Hazards/TantrumHazard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

namespace Exiled.API.Features.Hazards
{
using Exiled.API.Enums;
using global::Hazards;
using Mirror;
using RelativePositioning;
using UnityEngine;

Expand All @@ -16,6 +18,8 @@ namespace Exiled.API.Features.Hazards
/// </summary>
public class TantrumHazard : TemporaryHazard
{
private static TantrumEnvironmentalHazard tantrumPrefab;

/// <summary>
/// Initializes a new instance of the <see cref="TantrumHazard"/> class.
/// </summary>
Expand All @@ -26,11 +30,28 @@ public TantrumHazard(TantrumEnvironmentalHazard hazard)
Base = hazard;
}

/// <summary>
/// Gets the tantrum prefab.
/// </summary>
public static TantrumEnvironmentalHazard TantrumPrefab
{
get
{
if (tantrumPrefab == null)
tantrumPrefab = PrefabHelper.GetPrefab<TantrumEnvironmentalHazard>(PrefabType.TantrumObj);

return tantrumPrefab;
}
}

/// <summary>
/// Gets the <see cref="TantrumEnvironmentalHazard"/>.
/// </summary>
public new TantrumEnvironmentalHazard Base { get; }

/// <inheritdoc />
public override HazardType Type => HazardType.Tantrum;

/// <summary>
/// Gets or sets a value indicating whether or not sizzle should be played.
/// </summary>
Expand All @@ -57,5 +78,28 @@ public Transform CorrectPosition
get => Base._correctPosition;
set => Base._correctPosition = value;
}

/// <summary>
/// Places a Tantrum (SCP-173's ability) in the indicated position.
/// </summary>
/// <param name="position">The position where you want to spawn the Tantrum.</param>
/// <param name="isActive">Whether or not the tantrum will apply the <see cref="EffectType.Stained"/> effect.</param>
/// <remarks>If <paramref name="isActive"/> is <see langword="true"/>, the tantrum is moved slightly up from its original position. Otherwise, the collision will not be detected and the slowness will not work.</remarks>
/// <returns>The <see cref="TantrumHazard"/> instance.</returns>
public static TantrumHazard PlaceTantrum(Vector3 position, bool isActive = true)
{
TantrumEnvironmentalHazard tantrum = Object.Instantiate(TantrumPrefab);

if (!isActive)
tantrum.SynchronizedPosition = new(position);
else
tantrum.SynchronizedPosition = new(position + (Vector3.up * 0.25f));

tantrum._destroyed = !isActive;

NetworkServer.Spawn(tantrum.gameObject);

return Get<TantrumHazard>(tantrum);
}
}
}
Loading

0 comments on commit a3595e1

Please sign in to comment.