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 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
46 changes: 46 additions & 0 deletions EXILED/Exiled.API/Enums/ObjectiveType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// -----------------------------------------------------------------------
// <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,

/// <summary>
/// Objective that is completed when player escapes.
/// </summary>
Escape,
}
}
58 changes: 58 additions & 0 deletions EXILED/Exiled.API/Features/Objectives/EscapeObjective.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// -----------------------------------------------------------------------
// <copyright file="EscapeObjective.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 PlayerRoles;
using Respawning.Objectives;

using BaseObjective = Respawning.Objectives.EscapeObjective;

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

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

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

/// <summary>
/// Fakes player's escape and tries to achieve this objective.
/// </summary>
/// <param name="player">Target that has escaped.</param>
/// <param name="newRole">Role that target will get after escaping.</param>
public void Escape(Player player, RoleTypeId newRole = RoleTypeId.None)
{
if (newRole == RoleTypeId.None)
{
if (player.Role == RoleTypeId.ClassD)
newRole = RoleTypeId.ChaosConscript;
else if (player.Role == RoleTypeId.Scientist)
newRole = RoleTypeId.NtfSpecialist;
else
newRole = player.Role;
}
Comment on lines +45 to +53

Choose a reason for hiding this comment

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

remove this it's not logical for what name you gave as argument newRole


Base.OnServerRoleSet(player.ReferenceHub, newRole, RoleChangeReason.Escaped);
}
}
}
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>
/// Fakes generator activation and tries to achieve this objective.
/// </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>
/// Fakes player's damage and tries to achieve this objective.
/// </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>
/// Fakes player's damage and tries to achieve this objective.
/// </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>
/// Fakes player's kill and tries to achieve this objective.
/// </summary>
/// <param name="damageHandler">An <see cref="AttackerDamageHandler"/> instance.</param>
public void Kill(AttackerDamageHandler damageHandler) => Base.OnKill(damageHandler.Target.ReferenceHub, damageHandler.Base);

/// <summary>
/// Fakes player's kill and tries to achieve this objective.
/// </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));
}
}
104 changes: 104 additions & 0 deletions EXILED/Exiled.API/Features/Objectives/HumanObjective.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// -----------------------------------------------------------------------
// <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, new()
{
/// <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>
/// <remarks>Can be <c>null</c>. It's being set by game only before achieving.</remarks>
public T ObjectiveFootprint
{
get => (T)Base.ObjectiveFootprint;
set => Base.ObjectiveFootprint = value;
}

/// <summary>
/// Gets or sets the time reward.
/// </summary>
/// <remarks>
/// Can be <c>0</c> if <see cref="ObjectiveFootprint"/> is <c>null</c>.
/// Setter affects only client notification.
/// </remarks>
public float TimeReward
{
get => ObjectiveFootprint?.TimeReward ?? 0;
set
{
ObjectiveFootprint ??= new T();
ObjectiveFootprint.TimeReward = value;
}
}

/// <summary>
/// Gets or sets the influence reward.
/// </summary>
/// <remarks>
/// Can be <c>0</c> if <see cref="ObjectiveFootprint"/> is <c>null</c>.
/// Setter affects only client notification.
/// </remarks>
public float InfluenceReward
{
get => ObjectiveFootprint?.InfluenceReward ?? 0;
set
{
ObjectiveFootprint ??= new T();
ObjectiveFootprint.InfluenceReward = value;
}
}

/// <summary>
/// Gets or sets the achiever.
/// </summary>
/// <remarks>
/// Can be <c>null</c> if <see cref="ObjectiveFootprint"/> is <c>null</c>.
/// Setter affects only client notification.
/// </remarks>
public Player Achiever
{
get => ObjectiveFootprint == null ? null : Player.Get(ObjectiveFootprint.AchievingPlayer.Nickname);
set
{
ObjectiveFootprint ??= new T();
ObjectiveFootprint.AchievingPlayer = new(value.Footprint);
}
}

/// <summary>
/// Achieves the objective.
/// </summary>
/// <param name="objectiveFootprint">An objective footprint instance.</param>
public void Achieve(T objectiveFootprint)
{
ObjectiveFootprint = objectiveFootprint;
Achieve();
}
}
}
Loading
Loading