mirror of
https://github.com/All-Of-Us-Mods/MiraAPI.git
synced 2026-10-19 15:51:48 +00:00
Idk
This commit is contained in:
1 parent
a077cabea1
commit
5c24488049
5 files changed
+56
-15
No files matched your search
@@ -1,8 +1,15 @@
|
||||
namespace MiraAPI.Example;
|
||||
using MiraAPI.Modifiers.Types;
|
||||
|
||||
public class ModifierExample
|
||||
namespace MiraAPI.Example;
|
||||
|
||||
[RegisterModifier]
|
||||
public class ModifierExample : TimedModifier
|
||||
{
|
||||
//name, role specific, etc
|
||||
public override string ModifierName => "Timed modifier example";
|
||||
public override float Duration => 10;
|
||||
|
||||
// mono behaviour stuff here (ModdedPlayerModifier inherits monobehaviour)
|
||||
public override void OnTimerComplete()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,8 @@
|
||||
public abstract class BaseModifier
|
||||
{
|
||||
public abstract string ModifierName { get; }
|
||||
public uint ModifierId { get; set; }
|
||||
public uint ModifierId { get; internal set; }
|
||||
public PlayerControl Player { get; set; }
|
||||
public virtual void OnActivate() { }
|
||||
public virtual void OnDeactivate() { }
|
||||
public virtual void Update() { }
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
using MiraAPI.Networking;
|
||||
using MiraAPI.Modifiers.Types;
|
||||
using MiraAPI.Networking;
|
||||
using MiraAPI.Utilities;
|
||||
using Reactor.Networking.Attributes;
|
||||
using Reactor.Utilities;
|
||||
using Reactor.Utilities.Attributes;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
@@ -12,7 +14,7 @@ namespace MiraAPI.Modifiers;
|
||||
[RegisterInIl2Cpp]
|
||||
public class ModifierManager(IntPtr ptr) : MonoBehaviour(ptr)
|
||||
{
|
||||
public List<uint> activeModifiersIds = new();
|
||||
public Dictionary<uint, BaseModifier> activeModifiers = new();
|
||||
public static List<BaseModifier> registeredModifiers = new();
|
||||
|
||||
public static void RegisterModifier(Type modifierType)
|
||||
@@ -27,11 +29,25 @@ public class ModifierManager(IntPtr ptr) : MonoBehaviour(ptr)
|
||||
registeredModifiers.Add(newMod);
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
foreach (var modifier in activeModifiers.Values)
|
||||
{
|
||||
modifier.Update();
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerator ModifierTimer(TimedModifier modifier)
|
||||
{
|
||||
yield return new WaitForSeconds(modifier.Duration);
|
||||
modifier.timerOngoing = false;
|
||||
modifier.OnTimerComplete();
|
||||
}
|
||||
|
||||
public bool HasModifier<T>(PlayerControl target) where T : BaseModifier
|
||||
{
|
||||
uint id = registeredModifiers.Find(mod => mod.GetType() == typeof(T)).ModifierId;
|
||||
return target.GetModifierManager().activeModifiersIds.Contains(id);
|
||||
return target.GetModifierManager().activeModifiers.ContainsKey(id);
|
||||
}
|
||||
|
||||
public void AddModifier<T>(PlayerControl target) where T : BaseModifier
|
||||
@@ -43,7 +59,7 @@ public class ModifierManager(IntPtr ptr) : MonoBehaviour(ptr)
|
||||
public void RemoveModifier<T>(PlayerControl target) where T : BaseModifier
|
||||
{
|
||||
uint id = registeredModifiers.Find(mod => mod.GetType() == typeof(T)).ModifierId;
|
||||
if (!activeModifiersIds.Contains(id))
|
||||
if (!activeModifiers.ContainsKey(id))
|
||||
{
|
||||
Logger<MiraApiPlugin>.Error($"Cannot remove modifier {typeof(T).Name} because it is not active.");
|
||||
return;
|
||||
@@ -59,7 +75,7 @@ public class ModifierManager(IntPtr ptr) : MonoBehaviour(ptr)
|
||||
if (modifier is null) return;
|
||||
|
||||
modifier.OnDeactivate();
|
||||
target.GetModifierManager().activeModifiersIds.Remove(modifierId);
|
||||
target.GetModifierManager().activeModifiers.Remove(modifierId);
|
||||
}
|
||||
|
||||
[MethodRpc((uint)MiraRpc.AddModifier)]
|
||||
@@ -68,7 +84,8 @@ public class ModifierManager(IntPtr ptr) : MonoBehaviour(ptr)
|
||||
BaseModifier modifier = registeredModifiers.Find(mod => mod.ModifierId == modifierId);
|
||||
if (modifier is null) return;
|
||||
|
||||
target.GetModifierManager().activeModifiersIds.Add(modifierId);
|
||||
target.GetModifierManager().activeModifiers.Add(modifierId, modifier);
|
||||
modifier.Player = target;
|
||||
modifier.OnActivate();
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,22 @@
|
||||
namespace MiraAPI.Modifiers.Types;
|
||||
using Reactor.Utilities;
|
||||
|
||||
namespace MiraAPI.Modifiers.Types;
|
||||
public abstract class TimedModifier : BaseModifier
|
||||
{
|
||||
public abstract float Duration { get; }
|
||||
public abstract bool StartOnAssignment { get; }
|
||||
public abstract bool RemoveOnCompletion { get; }
|
||||
public abstract void OnTimerComplete();
|
||||
|
||||
internal bool timerOngoing = false;
|
||||
|
||||
public void StartTimer(ModifierManager manager)
|
||||
{
|
||||
if (!timerOngoing)
|
||||
{
|
||||
Logger<MiraApiPlugin>.Error("Can't start a timer that is already ongoing.");
|
||||
return;
|
||||
}
|
||||
|
||||
timerOngoing = true;
|
||||
Coroutines.Start(manager.ModifierTimer(this));
|
||||
}
|
||||
}
|
||||
@@ -21,11 +21,12 @@ public static class Extensions
|
||||
|
||||
public static void AddModifier<T>(this PlayerControl player) where T : BaseModifier
|
||||
{
|
||||
player.GetModifierManager().AddModifier<T>();
|
||||
player.GetModifierManager().AddModifier<T>(player);
|
||||
}
|
||||
|
||||
public static void RemoveModifier<T>(this PlayerControl player) where T : BaseModifier
|
||||
{
|
||||
player.GetModifierManager().RemoveModifier<T>(player);
|
||||
}
|
||||
|
||||
public static Color DarkenColor(this Color color)
|
||||
|
||||
Reference in new issue
Block a user