Skip to content

Commit

Permalink
add hud string method and code docs for modifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
XtraCube committed Aug 31, 2024
1 parent 264defe commit 1f357b5
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 17 deletions.
2 changes: 1 addition & 1 deletion MiraAPI.Example/Buttons/MeetingButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ protected override void OnClick()
PlayerControl.LocalPlayer.RpcRemoveModifier<CaptainModifier>();
}
}
}
}
3 changes: 2 additions & 1 deletion MiraAPI.Example/Modifiers/CaptainModifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace MiraAPI.Example.Modifiers;
public class CaptainModifier : GameModifier
{
public override string ModifierName => "Captain";

public override int GetAmountPerGame()
{
return 1;
Expand All @@ -16,4 +17,4 @@ public override int GetAssignmentChance()
{
return 100;
}
}
}
4 changes: 2 additions & 2 deletions MiraAPI.Example/Modifiers/Freezer/FreezeModifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ public class FreezeModifier : TimedModifier

public override void OnActivate()
{
if (Player.AmOwner)
if (Player?.AmOwner == true)
{
Player.moveable = false;
}
}

public override void OnTimerComplete()
{
if (Player.AmOwner)
if (Player?.AmOwner == true)
{
Player.moveable = true;
}
Expand Down
78 changes: 69 additions & 9 deletions MiraAPI/Modifiers/BaseModifier.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,74 @@
namespace MiraAPI.Modifiers;

/// <summary>
/// Base class for all modifiers.
/// </summary>
public abstract class BaseModifier
{
/// <summary>
/// Gets the player that the modifier is attached to.
/// </summary>
public PlayerControl? Player { get; internal set; }

/// <summary>
/// Gets the modifier id.
/// </summary>
public uint ModifierId { get; internal set; }

/// <summary>
/// Gets the modifier name.
/// </summary>
public abstract string ModifierName { get; }

/// <summary>
/// Gets a value indicating whether the modifier is hidden on the UI.
/// </summary>
public virtual bool HideOnUi => false;
public PlayerControl Player { get; internal set; }
public uint ModifierId { get; internal set; }
public virtual void OnActivate() { }
public virtual void OnDeactivate() { }
public virtual void Update() { }
public virtual void FixedUpdate() { }
public virtual void OnDeath() { }
public virtual bool CanVent() => Player.Data.Role.CanVent;
}

/// <summary>
/// Gets the HUD information for this modifier. Defaults to the modifier name. Does nothing if <see cref="HideOnUi"/> is true.
/// </summary>
/// <returns>The information string for the HUD.</returns>
public virtual string GetHudString() => ModifierName;

/// <summary>
/// Called when the modifier is activated.
/// </summary>
public virtual void OnActivate()
{
}

/// <summary>
/// Called when the modifier is deactivated.
/// </summary>
public virtual void OnDeactivate()
{
}

/// <summary>
/// Called when the modifier is updated. Attached to the ModifierComponent's Update method.
/// </summary>
public virtual void Update()
{
}

/// <summary>
/// Called when the modifier is updated. Attached to the ModifierComponent's FixedUpdate method.
/// </summary>
public virtual void FixedUpdate()
{
}

/// <summary>
/// Called when the player dies.
/// </summary>
public virtual void OnDeath()
{
}

/// <summary>
/// Determines whether the player can vent.
/// </summary>
/// <returns>True if the player can vent, false otherwise.</returns>
public virtual bool CanVent() => Player?.Data.Role.CanVent == true;
}
21 changes: 20 additions & 1 deletion MiraAPI/Modifiers/Types/GameModifier.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
namespace MiraAPI.Modifiers.Types;

/// <summary>
/// The base class for a game modifier. Game modifiers are applied at the start of the game on top of the player's role.
/// </summary>
public abstract class GameModifier : BaseModifier
{
/// <summary>
/// Gets the chance of the modifier being assigned to a player.
/// </summary>
/// <returns>An int value between 0 and 100 representing percent.</returns>
public abstract int GetAssignmentChance();

/// <summary>
/// Gets the amount of players that can have this modifier in a game.
/// </summary>
/// <returns>An int value greater than or equal to zero.</returns>
public abstract int GetAmountPerGame();

/// <summary>
/// Determines whether the modifier is valid on a role.
/// </summary>
/// <param name="role">The role to be checked.</param>
/// <returns>True if the modifier is valid on the role, otherwise false.</returns>
public virtual bool IsModifierValidOn(RoleBehaviour role) => true;
}
}
37 changes: 34 additions & 3 deletions MiraAPI/Modifiers/Types/TimedModifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,48 @@
using UnityEngine;

namespace MiraAPI.Modifiers.Types;

/// <summary>
/// The base class for all timed modifiers. Timed modifiers have a duration and can be started and stopped.
/// </summary>
public abstract class TimedModifier : BaseModifier
{
/// <summary>
/// Gets the duration of the modifier.
/// </summary>
public abstract float Duration { get; }

/// <summary>
/// Gets a value indicating whether the timer should start automatically when added.
/// </summary>
public virtual bool AutoStart => false;

/// <summary>
/// Gets a value indicating whether the modifier should be removed when the timer completes.
/// </summary>
public virtual bool RemoveOnComplete => true;

/// <summary>
/// Called when the timer completes.
/// </summary>
public abstract void OnTimerComplete();

/// <summary>
/// Gets or sets a value indicating whether the timer is active.
/// </summary>
public bool TimerActive { get; protected set; }

/// <summary>
/// Gets or sets the time remaining on the timer.
/// </summary>
public float TimeRemaining { get; protected set; }

/// <summary>
/// The FixedUpdate method for timed modifiers. Automatically handles the timer logic.
/// </summary>
public override void FixedUpdate()
{
if (!Player.AmOwner)
if (Player?.AmOwner == false)
{
return;
}
Expand All @@ -33,11 +61,14 @@ public override void FixedUpdate()

if (RemoveOnComplete)
{
Player.RpcRemoveModifier(ModifierId);
Player?.RpcRemoveModifier(ModifierId);
}
}
}

/// <summary>
/// Starts the timer.
/// </summary>
public void StartTimer()
{
if (TimerActive)
Expand All @@ -49,4 +80,4 @@ public void StartTimer()
TimerActive = true;
TimeRemaining = Duration;
}
}
}

0 comments on commit 1f357b5

Please sign in to comment.