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

feat: Objective wrappers #316

Open
wants to merge 8 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
41 changes: 41 additions & 0 deletions EXILED/Exiled.API/Enums/ObjectiveType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// -----------------------------------------------------------------------
// <copyright file="ObjectiveType.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>
/// An enum representing the different types of objectives.
/// </summary>
/// <seealso cref="Exiled.API.Features.Objectives.Objective" />
public enum ObjectiveType
{
/// <summary>
/// Unknown objective.
/// </summary>
None,

/// <summary>
/// Objective that is completed when SCP item is picked up.
/// </summary>
ScpItemPickup,

/// <summary>
/// Objective that is completed when enemy military is damaged.
/// </summary>
HumanDamage,

/// <summary>
/// Objective that is completed when enemy military is killed.
/// </summary>
HumanKill,

/// <summary>
/// Objective that is completed when generator is activated.
/// </summary>
GeneratorActivation,
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// -----------------------------------------------------------------------
// <copyright file="GeneratorActivatedObjective.cs" company="ExMod Team">
// Copyright (c) ExMod Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
// </copyright>
// -----------------------------------------------------------------------

namespace Exiled.API.Features.Objectives
{
using Exiled.API.Enums;
using Exiled.API.Interfaces;
using Respawning.Objectives;

using BaseObjective = Respawning.Objectives.GeneratorActivatedObjective;

/// <summary>
/// Represents an objective that is completed when a generator is activated.
/// </summary>
public class GeneratorActivatedObjective : HumanObjective<GeneratorObjectiveFootprint>, IWrapper<BaseObjective>
{
/// <summary>
/// Initializes a new instance of the <see cref="GeneratorActivatedObjective"/> class.
/// </summary>
/// <param name="objectiveFootprintBase">A <see cref="BaseObjective"/> instance.</param>
public GeneratorActivatedObjective(BaseObjective objectiveFootprintBase)
: base(objectiveFootprintBase)
{
Base = objectiveFootprintBase;
}

/// <inheritdoc/>
public new BaseObjective Base { get; }

/// <inheritdoc/>
public override ObjectiveType Type { get; } = ObjectiveType.GeneratorActivation;

/// <summary>
/// TODO.
/// </summary>
/// <param name="generator">Generator that is activated.</param>
/// <param name="player">Player that activated the generator.</param>
public void Activate(Generator generator, Player player = null) => Base.OnGeneratorEngaged(generator.Base, (player ?? Server.Host).Footprint);
}
}
54 changes: 54 additions & 0 deletions EXILED/Exiled.API/Features/Objectives/HumanDamageObjective.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// -----------------------------------------------------------------------
// <copyright file="HumanDamageObjective.cs" company="ExMod Team">
// Copyright (c) ExMod Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
// </copyright>
// -----------------------------------------------------------------------

namespace Exiled.API.Features.Objectives
{
using Exiled.API.Enums;
using Exiled.API.Features.DamageHandlers;
using Exiled.API.Interfaces;
using Respawning.Objectives;

using BaseObjective = Respawning.Objectives.HumanDamageObjective;

/// <summary>
/// A wrapper for the human damage objective.
/// </summary>
public class HumanDamageObjective : HumanObjective<DamageObjectiveFootprint>, IWrapper<BaseObjective>
{
/// <summary>
/// Initializes a new instance of the <see cref="HumanDamageObjective"/> class.
/// </summary>
/// <param name="objectiveFootprintBase"><inheritdoc cref="Base"/></param>
internal HumanDamageObjective(BaseObjective objectiveFootprintBase)
: base(objectiveFootprintBase)
{
Base = objectiveFootprintBase;
}

/// <inheritdoc/>
public new BaseObjective Base { get; }

/// <inheritdoc/>
public override ObjectiveType Type { get; } = ObjectiveType.HumanDamage;

/// <summary>
/// TODO.
/// </summary>
/// <param name="attacker">Attacker.</param>
/// <param name="target">Target to damage.</param>
/// <param name="amount">Amount of damage.</param>
/// <param name="type">Type of damage.</param>
public void Damage(Player attacker, Player target, float amount, DamageType type = DamageType.Unknown)
=> Damage(new CustomDamageHandler(target, attacker, amount, type, string.Empty));

/// <summary>
/// TODO.
/// </summary>
/// <param name="damageHandler">An <see cref="AttackerDamageHandler"/> instance.</param>
public void Damage(AttackerDamageHandler damageHandler) => Base.OnPlayerDamaged(damageHandler.Attacker.ReferenceHub, damageHandler.Base);
}
}
69 changes: 69 additions & 0 deletions EXILED/Exiled.API/Features/Objectives/HumanKillObjective.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// -----------------------------------------------------------------------
// <copyright file="HumanKillObjective.cs" company="ExMod Team">
// Copyright (c) ExMod Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
// </copyright>
// -----------------------------------------------------------------------

namespace Exiled.API.Features.Objectives
{
using Exiled.API.Enums;
using Exiled.API.Features.DamageHandlers;
using Exiled.API.Interfaces;
using PlayerRoles;
using Respawning.Objectives;

using BaseObjective = Respawning.Objectives.HumanKillObjective;

/// <summary>
/// A wrapper for Human kill objective.
/// </summary>
public class HumanKillObjective : HumanObjective<KillObjectiveFootprint>, IWrapper<BaseObjective>
{
/// <summary>
/// Initializes a new instance of the <see cref="HumanKillObjective"/> class.
/// </summary>
/// <param name="objectiveFootprintBase"><inheritdoc cref="Base"/></param>
internal HumanKillObjective(BaseObjective objectiveFootprintBase)
: base(objectiveFootprintBase)
{
Base = objectiveFootprintBase;
}

/// <inheritdoc/>
public new BaseObjective Base { get; }

/// <inheritdoc/>
public override ObjectiveType Type { get; } = ObjectiveType.HumanKill;

/// <summary>
/// Checks if the role is an enemy role.
/// </summary>
/// <param name="target">Target role.</param>
/// <param name="player">Attacker.</param>
/// <returns><c>true</c> if role is an enemy role, <c>false</c> otherwise.</returns>
public bool IsValidEnemy(RoleTypeId target, Player player) => Base.IsValidEnemy(target, player.ReferenceHub);

/// <summary>
/// Checks if the player is an enemy.
/// </summary>
/// <param name="target">Target player.</param>
/// <param name="player">Attacker.</param>
/// <returns><c>true</c> if player is an enemy, <c>false</c> otherwise.</returns>
public bool IsValidEnemy(Player target, Player player) => IsValidEnemy(target.Role, player);

/// <summary>
/// TODO.
/// </summary>
/// <param name="damageHandler">An <see cref="AttackerDamageHandler"/> instance.</param>
public void Kill(AttackerDamageHandler damageHandler) => Base.OnKill(damageHandler.Target.ReferenceHub, damageHandler.Base);

/// <summary>
/// TODO.
/// </summary>
/// <param name="target">Target player.</param>
/// <param name="attacker">Attacker.</param>
/// <param name="damageType">Damage type.</param>
public void Kill(Player target, Player attacker, DamageType damageType = DamageType.Unknown) => Kill(new CustomDamageHandler(target, attacker, -1, damageType));
}
}
52 changes: 52 additions & 0 deletions EXILED/Exiled.API/Features/Objectives/HumanObjective.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// -----------------------------------------------------------------------
// <copyright file="HumanObjective.cs" company="ExMod Team">
// Copyright (c) ExMod Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
// </copyright>
// -----------------------------------------------------------------------

namespace Exiled.API.Features.Objectives
{
using Exiled.API.Interfaces;
using Respawning.Objectives;

/// <summary>
/// Represents a human objective.
/// </summary>
/// <typeparam name="T">An objective footprint type.</typeparam>
public class HumanObjective<T> : Objective, IWrapper<HumanObjectiveBase<T>>
where T : ObjectiveFootprintBase
{
/// <summary>
/// Initializes a new instance of the <see cref="HumanObjective{T}"/> class.
/// </summary>
/// <param name="objectiveFootprintBase">A <see cref="HumanObjectiveBase{T}"/> instance.</param>
internal HumanObjective(HumanObjectiveBase<T> objectiveFootprintBase)
: base(objectiveFootprintBase)
{
Base = objectiveFootprintBase;
}

/// <inheritdoc/>
public new HumanObjectiveBase<T> Base { get; }

/// <summary>
/// Gets or sets the objective footprint.
/// </summary>
public T ObjectiveFootprint
{
get => (T)Base.ObjectiveFootprint;
set => Base.ObjectiveFootprint = value;
}

/// <summary>
/// Achieves the objective.
/// </summary>
/// <param name="objectiveFootprint">An objective footprint instance.</param>
public void Achieve(T objectiveFootprint)
{
ObjectiveFootprint = objectiveFootprint;
Achieve();
}
}
}
112 changes: 112 additions & 0 deletions EXILED/Exiled.API/Features/Objectives/Objective.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// -----------------------------------------------------------------------
// <copyright file="Objective.cs" company="ExMod Team">
// Copyright (c) ExMod Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
// </copyright>
// -----------------------------------------------------------------------

namespace Exiled.API.Features.Objectives
{
using System.Collections.Generic;
using System.Linq;

using Exiled.API.Enums;
using Exiled.API.Features.Core;
using Exiled.API.Interfaces;
using PlayerRoles;
using Respawning;
using Respawning.Objectives;

using BaseHumanDamageObjective = Respawning.Objectives.HumanDamageObjective;
using BaseHumanKillObjective = Respawning.Objectives.HumanKillObjective;
using BaseScpPickupObjective = Respawning.Objectives.ScpItemPickupObjective;

/// <summary>
/// A wrapper for Faction objective.
/// </summary>
public class Objective : TypeCastObject<FactionObjectiveBase>, IWrapper<FactionObjectiveBase>
{
/// <summary>
/// A dictionary of all objectives.
/// </summary>
private static readonly Dictionary<ObjectiveType, Objective> Objectives = new();

/// <summary>
/// Initializes a new instance of the <see cref="Objective"/> class.
/// </summary>
/// <param name="objectiveFootprintBase"><inheritdoc cref="Base"/></param>
public Objective(FactionObjectiveBase objectiveFootprintBase)
{
Base = objectiveFootprintBase;
}

/// <summary>
/// Gets all objectives.
/// </summary>
public static IReadOnlyCollection<Objective> List => Objectives.Values;

/// <inheritdoc/>
public FactionObjectiveBase Base { get; }

/// <summary>
/// Gets the type of objective.
/// </summary>
public virtual ObjectiveType Type { get; } = ObjectiveType.None;

/// <summary>
/// Gets the objective by its type.
/// </summary>
/// <param name="type">Type of objective.</param>
/// <returns>An <see cref="Objective"/> instance if found, <c>null</c> otherwise.</returns>
public static Objective Get(ObjectiveType type)
{
if (Objectives.TryGetValue(type, out Objective objective))
return objective;

objective = type switch
{
ObjectiveType.ScpItemPickup => new ScpItemPickupObjective(FactionInfluenceManager.Objectives.OfType<BaseScpPickupObjective>().First()),
ObjectiveType.GeneratorActivation => new GeneratorActivatedObjective(FactionInfluenceManager.Objectives.OfType<Respawning.Objectives.GeneratorActivatedObjective>().First()),
ObjectiveType.HumanDamage => new HumanDamageObjective(FactionInfluenceManager.Objectives.OfType<BaseHumanDamageObjective>().First()),
ObjectiveType.HumanKill => new HumanKillObjective(FactionInfluenceManager.Objectives.OfType<BaseHumanKillObjective>().First()),
_ => null
};

Objectives.Add(type, objective);
return objective;
}

/// <summary>
/// Reduces timer for faction.
/// </summary>
/// <param name="faction">Faction to affect.</param>
/// <param name="seconds">Time to reduce in seconds.</param>
public void ReduceTimer(Faction faction, float seconds) => Base.ReduceTimer(faction, seconds);

/// <summary>
/// Grants influence to faction.
/// </summary>
/// <param name="faction">Faction to affect.</param>
/// <param name="amount">Amount of influence to grant.</param>
public void GrantInfluence(Faction faction, float amount) => Base.GrantInfluence(faction, amount);

/// <summary>
/// Achieves objective.
/// </summary>
public void Achieve() => Base.ServerSendUpdate();

/// <summary>
/// Checks if faction has this objective.
/// </summary>
/// <param name="faction">Faction to check.</param>
/// <returns><c>true</c> if faction has this objective, <c>false</c> otherwise.</returns>
public bool IsValidFaction(Faction faction) => Base.IsValidFaction(faction);

/// <summary>
/// Checks if player has this objective.
/// </summary>
/// <param name="player">Player to check.</param>
/// <returns><c>true</c> if player has this objective, <c>false</c> otherwise.</returns>
public bool IsValidFaction(Player player) => Base.IsValidFaction(player.ReferenceHub);
}
}
Loading
Loading