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

refractor!: ReworkingAdminToyCreate #309

Draft
wants to merge 8 commits into
base: dev
Choose a base branch
from
Draft
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
14 changes: 12 additions & 2 deletions EXILED/Exiled.API/Enums/AdminToyType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,19 @@ public enum AdminToyType
LightSource,

/// <summary>
/// ShootingTarget toy.
/// ShootingTarget Radial sport target.
/// </summary>
ShootingTarget,
ShootingTargetSport,

/// <summary>
/// ShootingTarget D-Class target.
/// </summary>
ShootingTargetClassD,

/// <summary>
/// ShootingTarget Binary target.
/// </summary>
ShootingTargetBinary,

/// <summary>
/// Speaker toy.
Expand Down
36 changes: 0 additions & 36 deletions EXILED/Exiled.API/Enums/ShootingTargetType.cs
Original file line number Diff line number Diff line change
@@ -1,36 +0,0 @@
// -----------------------------------------------------------------------
// <copyright file="ShootingTargetType.cs" company="ExMod Team">
// Copyright (c) ExMod Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
// </copyright>
// -----------------------------------------------------------------------

namespace Exiled.API.Enums
{
/// <summary>
/// Shooting target types present in the game.
/// </summary>
/// <seealso cref="Features.Toys.ShootingTargetToy.Type"/>
public enum ShootingTargetType
{
/// <summary>
/// Unknown target.
/// </summary>
Unknown,

/// <summary>
/// Radial sport target.
/// </summary>
Sport,

/// <summary>
/// D-Class target.
/// </summary>
ClassD,

/// <summary>
/// Binary target.
/// </summary>
Binary,
}
}
47 changes: 44 additions & 3 deletions EXILED/Exiled.API/Features/Toys/AdminToy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ public abstract class AdminToy : IWorldSpace
/// </summary>
internal static readonly Dictionary<AdminToyBase, AdminToy> BaseToAdminToy = new(new ComponentsEqualityComparer());

private static readonly Dictionary<AdminToyType, PrefabType> TypeLookup = new()
{
{ AdminToyType.ShootingTargetSport, PrefabType.SportTarget },
{ AdminToyType.ShootingTargetClassD, PrefabType.DBoyTarget },
{ AdminToyType.ShootingTargetBinary, PrefabType.BinaryTarget },
{ AdminToyType.LightSource, PrefabType.LightSourceToy },
{ AdminToyType.PrimitiveObject, PrefabType.PrimitiveObjectToy },
{ AdminToyType.Speaker, PrefabType.SpeakerToy },
};

/// <summary>
/// Initializes a new instance of the <see cref="AdminToy"/> class.
/// </summary>
Expand Down Expand Up @@ -152,14 +162,34 @@ public static AdminToy Get(AdminToyBase adminToyBase)

return adminToyBase switch
{
LightSourceToy lightSourceToy => new Light(lightSourceToy),
PrimitiveObjectToy primitiveObjectToy => new Primitive(primitiveObjectToy),
ShootingTarget shootingTarget => new ShootingTargetToy(shootingTarget),
LightSourceToy lightSourceToy => new Light(lightSourceToy),
SpeakerToy speakerToy => new Speaker(speakerToy),
_ => throw new System.NotImplementedException()
ShootingTarget shootingTarget => shootingTarget.gameObject.name switch
{
"TargetBinary" => new ShootingTargetToy(shootingTarget, AdminToyType.ShootingTargetBinary),
"TargetClassD" => new ShootingTargetToy(shootingTarget, AdminToyType.ShootingTargetClassD),
"TargetSport" => new ShootingTargetToy(shootingTarget, AdminToyType.ShootingTargetSport),
Comment on lines +170 to +172
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't the valid name

_ => throw new System.NotImplementedException($"ShootingTarget Name: {shootingTarget.gameObject.name}"),
},
_ => throw new System.NotImplementedException($"AdminToyBase Name: {adminToyBase.gameObject.name}")
};
}

/// <summary>
/// Create the <see cref="AdminToy"/> belonging to the <see cref="AdminToys.AdminToyBase"/>.
/// </summary>
/// <param name="adminToyType">The <see cref="AdminToyType"/>.</param>
/// <param name="position">The <see cref="Vector3"/> position where the <see cref="AdminToy"/> will spawn.</param>
/// <param name="rotation">The <see cref="Quaternion"/> rotation of the <see cref="AdminToy"/>.</param>
/// <returns>The corresponding <see cref="AdminToy"/>.</returns>
public static AdminToy Create(AdminToyType adminToyType, Vector3 position = default, Quaternion? rotation = null)
{
if (!TypeLookup.TryGetValue(adminToyType, out PrefabType prefabType))
throw new System.NotImplementedException($"AdminToy::Create(AdminToyType, Vector3, Quaternion?) AdminToyType: {adminToyType}");
return Get(PrefabHelper.Spawn<AdminToyBase>(prefabType, position, rotation));
}

/// <summary>
/// Gets the <see cref="AdminToy"/> by <see cref="AdminToys.AdminToyBase"/>.
/// </summary>
Expand All @@ -169,6 +199,17 @@ public static AdminToy Get(AdminToyBase adminToyBase)
public static T Get<T>(AdminToyBase adminToyBase)
where T : AdminToy => Get(adminToyBase) as T;

/// <summary>
/// Gets the <see cref="AdminToy"/> by <see cref="AdminToys.AdminToyBase"/>.
/// </summary>
/// <param name="adminToyType">The <see cref="AdminToyType"/> to convert into an admintoy.</param>
/// <param name="position">The <see cref="Vector3"/> position where the <see cref="AdminToy"/> will spawn.</param>
/// <param name="rotation">The <see cref="Quaternion"/> rotation of the <see cref="AdminToy"/>.</param>
/// <typeparam name="T">The specified <see cref="AdminToy"/> type.</typeparam>
/// <returns>The admintoy wrapper for the given <see cref="AdminToys.AdminToyBase"/>.</returns>
public static T Create<T>(AdminToyType adminToyType, Vector3 position = default, Quaternion? rotation = null)
where T : AdminToy => Create(adminToyType, position, rotation) as T;

/// <summary>
/// Spawns the toy into the game. Use <see cref="UnSpawn"/> to remove it.
/// </summary>
Expand Down
48 changes: 0 additions & 48 deletions EXILED/Exiled.API/Features/Toys/Light.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,53 +121,5 @@ public LightShadows ShadowType
get => Base.NetworkShadowType;
set => Base.NetworkShadowType = value;
}

/// <summary>
/// Creates a new <see cref="Light"/>.
/// </summary>
/// <param name="position">The position of the <see cref="Light"/>.</param>
/// <param name="rotation">The rotation of the <see cref="Light"/>.</param>
/// <param name="scale">The scale of the <see cref="Light"/>.</param>
/// <param name="spawn">Whether the <see cref="Light"/> should be initially spawned.</param>
/// <returns>The new <see cref="Light"/>.</returns>
public static Light Create(Vector3? position = null, Vector3? rotation = null, Vector3? scale = null, bool spawn = true)
=> Create(position, rotation, scale, spawn, null);

/// <summary>
/// Creates a new <see cref="Light"/>.
/// </summary>
/// <param name="position">The position of the <see cref="Light"/>.</param>
/// <param name="rotation">The rotation of the <see cref="Light"/>.</param>
/// <param name="scale">The scale of the <see cref="Light"/>.</param>
/// <param name="spawn">Whether the <see cref="Light"/> should be initially spawned.</param>
/// <param name="color">The color of the <see cref="Light"/>.</param>
/// <returns>The new <see cref="Light"/>.</returns>
public static Light Create(Vector3? position /*= null*/, Vector3? rotation /*= null*/, Vector3? scale /*= null*/, bool spawn /*= true*/, Color? color /*= null*/)
{
Light light = new(UnityEngine.Object.Instantiate(Prefab))
{
Position = position ?? Vector3.zero,
Rotation = Quaternion.Euler(rotation ?? Vector3.zero),
Scale = scale ?? Vector3.one,
};

if (spawn)
light.Spawn();

light.Color = color ?? Color.gray;

return light;
}

/// <summary>
/// Gets the <see cref="Light"/> belonging to the <see cref="LightSourceToy"/>.
/// </summary>
/// <param name="lightSourceToy">The <see cref="LightSourceToy"/> instance.</param>
/// <returns>The corresponding <see cref="LightSourceToy"/> instance.</returns>
public static Light Get(LightSourceToy lightSourceToy)
{
AdminToy adminToy = List.FirstOrDefault(x => x.AdminToyBase == lightSourceToy);
return adminToy is not null ? adminToy as Light : new(lightSourceToy);
}
}
}
143 changes: 0 additions & 143 deletions EXILED/Exiled.API/Features/Toys/Primitive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ public class Primitive : AdminToy, IWrapper<PrimitiveObjectToy>
internal Primitive(PrimitiveObjectToy toyAdminToyBase)
: base(toyAdminToyBase, AdminToyType.PrimitiveObject) => Base = toyAdminToyBase;

/// <summary>
/// Gets the prefab.
/// </summary>
public static PrimitiveObjectToy Prefab => PrefabHelper.GetPrefab<PrimitiveObjectToy>(PrefabType.PrimitiveObjectToy);

/// <summary>
/// Gets the base <see cref="PrimitiveObjectToy"/>.
/// </summary>
Expand Down Expand Up @@ -85,143 +80,5 @@ public PrimitiveFlags Flags
get => Base.NetworkPrimitiveFlags;
set => Base.NetworkPrimitiveFlags = value;
}

/// <summary>
/// Creates a new <see cref="Primitive"/>.
/// </summary>
/// <param name="position">The position of the <see cref="Primitive"/>.</param>
/// <param name="rotation">The rotation of the <see cref="Primitive"/>.</param>
/// <param name="scale">The scale of the <see cref="Primitive"/>.</param>
/// <param name="spawn">Whether the <see cref="Primitive"/> should be initially spawned.</param>
/// <returns>The new <see cref="Primitive"/>.</returns>
public static Primitive Create(Vector3? position = null, Vector3? rotation = null, Vector3? scale = null, bool spawn = true)
=> Create(position, rotation, scale, spawn, null);

/// <summary>
/// Creates a new <see cref="Primitive"/>.
/// </summary>
/// <param name="primitiveType">The type of primitive to spawn.</param>
/// <param name="position">The position of the <see cref="Primitive"/>.</param>
/// <param name="rotation">The rotation of the <see cref="Primitive"/>.</param>
/// <param name="scale">The scale of the <see cref="Primitive"/>.</param>
/// <param name="spawn">Whether the <see cref="Primitive"/> should be initially spawned.</param>
/// <returns>The new <see cref="Primitive"/>.</returns>
public static Primitive Create(PrimitiveType primitiveType = PrimitiveType.Sphere, Vector3? position = null, Vector3? rotation = null, Vector3? scale = null, bool spawn = true)
=> Create(primitiveType, position, rotation, scale, spawn, null);

/// <summary>
/// Creates a new <see cref="Primitive"/>.
/// </summary>
/// <param name="position">The position of the <see cref="Primitive"/>.</param>
/// <param name="rotation">The rotation of the <see cref="Primitive"/>.</param>
/// <param name="scale">The scale of the <see cref="Primitive"/>.</param>
/// <param name="spawn">Whether the <see cref="Primitive"/> should be initially spawned.</param>
/// <param name="color">The color of the <see cref="Primitive"/>.</param>
/// <returns>The new <see cref="Primitive"/>.</returns>
public static Primitive Create(Vector3? position /*= null*/, Vector3? rotation /*= null*/, Vector3? scale /*= null*/, bool spawn /*= true*/, Color? color /*= null*/)
{
Primitive primitive = new(Object.Instantiate(Prefab));

primitive.Position = position ?? Vector3.zero;
primitive.Rotation = Quaternion.Euler(rotation ?? Vector3.zero);
primitive.Scale = scale ?? Vector3.one;
primitive.Color = color ?? Color.gray;

if (spawn)
primitive.Spawn();

return primitive;
}

/// <summary>
/// Creates a new <see cref="Primitive"/>.
/// </summary>
/// <param name="primitiveType">The type of primitive to spawn.</param>
/// <param name="position">The position of the <see cref="Primitive"/>.</param>
/// <param name="rotation">The rotation of the <see cref="Primitive"/>.</param>
/// <param name="scale">The scale of the <see cref="Primitive"/>.</param>
/// <param name="spawn">Whether the <see cref="Primitive"/> should be initially spawned.</param>
/// <param name="color">The color of the <see cref="Primitive"/>.</param>
/// <returns>The new <see cref="Primitive"/>.</returns>
public static Primitive Create(PrimitiveType primitiveType /*= PrimitiveType.Sphere*/, Vector3? position /*= null*/, Vector3? rotation /*= null*/, Vector3? scale /*= null*/, bool spawn /*= true*/, Color? color /*= null*/)
{
Primitive primitive = new(Object.Instantiate(Prefab));

primitive.Position = position ?? Vector3.zero;
primitive.Rotation = Quaternion.Euler(rotation ?? Vector3.zero);
primitive.Scale = scale ?? Vector3.one;

primitive.Base.NetworkPrimitiveType = primitiveType;
primitive.Color = color ?? Color.gray;

if (spawn)
primitive.Spawn();

return primitive;
}

/// <summary>
/// Creates a new <see cref="Primitive"/>.
/// </summary>
/// <param name="primitiveType">The type of primitive to spawn.</param>
/// <param name="flags">The primitive flags.</param>
/// <param name="position">The position of the <see cref="Primitive"/>.</param>
/// <param name="rotation">The rotation of the <see cref="Primitive"/>.</param>
/// <param name="scale">The scale of the <see cref="Primitive"/>.</param>
/// <param name="spawn">Whether the <see cref="Primitive"/> should be initially spawned.</param>
/// <param name="color">The color of the <see cref="Primitive"/>.</param>
/// <returns>The new <see cref="Primitive"/>.</returns>
public static Primitive Create(PrimitiveType primitiveType /*= PrimitiveType.Sphere*/, PrimitiveFlags flags, Vector3? position /*= null*/, Vector3? rotation /*= null*/, Vector3? scale /*= null*/, bool spawn /*= true*/, Color? color /*= null*/)
{
Primitive primitive = new(Object.Instantiate(Prefab));

primitive.Position = position ?? Vector3.zero;
primitive.Rotation = Quaternion.Euler(rotation ?? Vector3.zero);
primitive.Scale = scale ?? Vector3.one;
primitive.Flags = flags;

primitive.Base.NetworkPrimitiveType = primitiveType;
primitive.Color = color ?? Color.gray;

if (spawn)
primitive.Spawn();

return primitive;
}

/// <summary>
/// Creates a new <see cref="Primitive"/>.
/// </summary>
/// <param name="primitiveSettings">The settings of the <see cref="Primitive"/>.</param>
/// <returns>The new <see cref="Primitive"/>.</returns>
public static Primitive Create(PrimitiveSettings primitiveSettings)
{
Primitive primitive = new(Object.Instantiate(Prefab));

primitive.Position = primitiveSettings.Position;
primitive.Rotation = Quaternion.Euler(primitiveSettings.Rotation);
primitive.Scale = primitiveSettings.Scale;
primitive.Flags = primitiveSettings.Flags;

primitive.Base.NetworkPrimitiveType = primitiveSettings.PrimitiveType;
primitive.Color = primitiveSettings.Color;
primitive.IsStatic = primitiveSettings.IsStatic;

if (primitiveSettings.Spawn)
primitive.Spawn();

return primitive;
}

/// <summary>
/// Gets the <see cref="Primitive"/> belonging to the <see cref="PrimitiveObjectToy"/>.
/// </summary>
/// <param name="primitiveObjectToy">The <see cref="PrimitiveObjectToy"/> instance.</param>
/// <returns>The corresponding <see cref="Primitive"/> instance.</returns>
public static Primitive Get(PrimitiveObjectToy primitiveObjectToy)
{
AdminToy adminToy = List.FirstOrDefault(x => x.AdminToyBase == primitiveObjectToy);
return adminToy is not null ? adminToy as Primitive : new(primitiveObjectToy);
}
}
}
Loading
Loading