mirror of
https://github.com/All-Of-Us-Mods/MiraAPI.git
synced 2026-10-19 15:51:48 +00:00
modifiers UI
This commit is contained in:
1 parent
a88646465b
commit
dfa5591188
8 files changed
+118
-8
No files matched your search
@@ -0,0 +1,16 @@
|
||||
using MiraAPI.Roles;
|
||||
using MiraAPI.Utilities.Assets;
|
||||
using UnityEngine;
|
||||
|
||||
namespace MiraAPI.Example;
|
||||
|
||||
[RegisterCustomRole]
|
||||
public class ExampleRole : ImpostorRole, ICustomRole
|
||||
{
|
||||
public string RoleName => "fortnite killer";
|
||||
public string RoleLongDescription => "ok so your objective is to eliminate everyone \nwith your fortnite tactical shotgun";
|
||||
public string RoleDescription => RoleLongDescription;
|
||||
public Color RoleColor => Palette.Orange;
|
||||
public ModdedRoleTeams Team => ModdedRoleTeams.Impostor;
|
||||
public LoadableAsset<Sprite> OptionsScreenshot => MiraAssets.Empty;
|
||||
}
|
||||
@@ -15,11 +15,11 @@ public class MiraDebugWindow : MonoBehaviour
|
||||
{
|
||||
if (GUILayout.Button("Test modifier"))
|
||||
{
|
||||
PlayerControl.LocalPlayer.AddModifier<ModifierExample>();
|
||||
PlayerControl.LocalPlayer.AddModifier<ModifierTimerExample>();
|
||||
}
|
||||
if (GUILayout.Button("Remove modifier"))
|
||||
{
|
||||
PlayerControl.LocalPlayer.RemoveModifier<ModifierExample>();
|
||||
PlayerControl.LocalPlayer.RemoveModifier<ModifierTimerExample>();
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ namespace MiraAPI.Example;
|
||||
[RegisterModifier]
|
||||
public class ModifierExample : GameModifier
|
||||
{
|
||||
public override string ModifierName => "Example";
|
||||
public override string ModifierName => "Example Modifier";
|
||||
|
||||
public override bool CanVent()
|
||||
{
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
using MiraAPI.Modifiers.Types;
|
||||
|
||||
namespace MiraAPI.Example;
|
||||
|
||||
[RegisterModifier]
|
||||
public class ModifierTimerExample : TimedModifier
|
||||
{
|
||||
public override string ModifierName => "Color Changer";
|
||||
public override bool HideOnUi => true;
|
||||
public override float Duration => 10f;
|
||||
public override bool AutoStart => true;
|
||||
|
||||
private System.Random _rand = new();
|
||||
|
||||
public override void OnTimerComplete()
|
||||
{
|
||||
Player.SetColor((byte)_rand.Next(0, Palette.PlayerColors.Count));
|
||||
StartTimer();
|
||||
}
|
||||
}
|
||||
@@ -2,11 +2,13 @@
|
||||
public abstract class BaseModifier
|
||||
{
|
||||
public abstract string ModifierName { get; }
|
||||
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;
|
||||
}
|
||||
@@ -6,6 +6,9 @@ using Reactor.Utilities;
|
||||
using Reactor.Utilities.Attributes;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
|
||||
namespace MiraAPI.Modifiers;
|
||||
@@ -15,12 +18,27 @@ public class ModifierComponent(IntPtr ptr) : MonoBehaviour(ptr)
|
||||
{
|
||||
public List<BaseModifier> ActiveModifiers { get; private set; }
|
||||
|
||||
public PlayerControl player;
|
||||
public PlayerControl Player;
|
||||
|
||||
private TextMeshPro _modifierText;
|
||||
|
||||
public void Start()
|
||||
{
|
||||
player = GetComponent<PlayerControl>();
|
||||
Player = GetComponent<PlayerControl>();
|
||||
ActiveModifiers = new List<BaseModifier>();
|
||||
|
||||
if (!Player.AmOwner) return;
|
||||
|
||||
_modifierText = Helpers.CreateTextLabel("ModifierText", HudManager.Instance.transform, AspectPosition.EdgeAlignments.RightTop, new Vector3(10.1f, 3.5f, 0), textAlignment: TextAlignmentOptions.Right);
|
||||
_modifierText.verticalAlignment = VerticalAlignmentOptions.Top;
|
||||
}
|
||||
|
||||
public void FixedUpdate()
|
||||
{
|
||||
foreach (var modifier in ActiveModifiers)
|
||||
{
|
||||
modifier.FixedUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
public void Update()
|
||||
@@ -29,6 +47,26 @@ public class ModifierComponent(IntPtr ptr) : MonoBehaviour(ptr)
|
||||
{
|
||||
modifier.Update();
|
||||
}
|
||||
|
||||
var filteredModifiers = ActiveModifiers.Where(mod => !mod.HideOnUi);
|
||||
|
||||
if (Player.AmOwner && filteredModifiers.Count() > 0)
|
||||
{
|
||||
var stringBuild = new StringBuilder();
|
||||
foreach (var mod in filteredModifiers)
|
||||
{
|
||||
stringBuild.Append($"\n{mod.ModifierName}");
|
||||
if (mod is TimedModifier timer)
|
||||
{
|
||||
stringBuild.Append($" <size=70%>({Math.Round(timer.Duration - timer.TimeRemaining, 0)}s/{timer.Duration}s)</size>");
|
||||
}
|
||||
}
|
||||
_modifierText.text = $"<b><size=130%>Modifiers:</b></size>{stringBuild}";
|
||||
}
|
||||
else if (Player.AmOwner && filteredModifiers.Count() == 0 && _modifierText.text != string.Empty)
|
||||
{
|
||||
_modifierText.text = string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
public static void RemoveModifier(PlayerControl target, uint modifierId)
|
||||
@@ -66,6 +104,12 @@ public class ModifierComponent(IntPtr ptr) : MonoBehaviour(ptr)
|
||||
return;
|
||||
}
|
||||
|
||||
if (target.HasModifier(modifierId))
|
||||
{
|
||||
Logger<MiraApiPlugin>.Error($"Player already has modifier with id {modifierId}!");
|
||||
return;
|
||||
}
|
||||
|
||||
var modifier = (BaseModifier)Activator.CreateInstance(type);
|
||||
|
||||
var modifierComponent = target.GetModifierComponent();
|
||||
@@ -77,7 +121,7 @@ public class ModifierComponent(IntPtr ptr) : MonoBehaviour(ptr)
|
||||
}
|
||||
|
||||
modifierComponent.ActiveModifiers.Add(modifier);
|
||||
modifier.Player = modifierComponent.player;
|
||||
modifier.Player = modifierComponent.Player;
|
||||
modifier.ModifierId = modifierId;
|
||||
modifier.OnActivate();
|
||||
|
||||
|
||||
@@ -11,13 +11,13 @@ public abstract class TimedModifier : BaseModifier
|
||||
public bool TimerActive = false;
|
||||
public float TimeRemaining;
|
||||
|
||||
public override void Update()
|
||||
public override void FixedUpdate()
|
||||
{
|
||||
if (!Player.AmOwner) return;
|
||||
|
||||
if (TimeRemaining > 0 && TimerActive)
|
||||
{
|
||||
TimeRemaining -= Time.deltaTime;
|
||||
TimeRemaining -= Time.fixedDeltaTime;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using MiraAPI.Roles;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
|
||||
namespace MiraAPI.Utilities;
|
||||
@@ -13,6 +14,33 @@ public static class Helpers
|
||||
return ShipStatus.Instance.AllRooms.ToList().Find(room => room.roomArea.OverlapPoint(pos));
|
||||
}
|
||||
|
||||
public static TextMeshPro CreateTextLabel(string name, Transform parent,
|
||||
AspectPosition.EdgeAlignments alignment, Vector3 distance, float fontSize = 2f,
|
||||
TextAlignmentOptions textAlignment = TextAlignmentOptions.Center)
|
||||
{
|
||||
var textObj = new GameObject(name)
|
||||
{
|
||||
transform =
|
||||
{
|
||||
parent = parent
|
||||
},
|
||||
layer = LayerMask.NameToLayer("UI")
|
||||
};
|
||||
|
||||
var textMeshPro = textObj.AddComponent<TextMeshPro>();
|
||||
textMeshPro.fontSize = fontSize;
|
||||
textMeshPro.alignment = textAlignment;
|
||||
textMeshPro.font = HudManager.Instance.TaskPanel.taskText.font;
|
||||
textMeshPro.fontMaterial = HudManager.Instance.TaskPanel.taskText.fontMaterial;
|
||||
|
||||
var aspectPosition = textObj.AddComponent<AspectPosition>();
|
||||
aspectPosition.Alignment = alignment;
|
||||
aspectPosition.DistanceFromEdge = distance;
|
||||
aspectPosition.AdjustPosition();
|
||||
|
||||
return textMeshPro;
|
||||
}
|
||||
|
||||
public static DeadBody GetBodyById(byte id)
|
||||
{
|
||||
return Object.FindObjectsOfType<DeadBody>().FirstOrDefault(body => body.ParentId == id);
|
||||
|
||||
Reference in new issue
Block a user