Skip to content

Commit

Permalink
Move over some more content
Browse files Browse the repository at this point in the history
  • Loading branch information
simonkellly committed Dec 20, 2023
1 parent da3312d commit 7237269
Show file tree
Hide file tree
Showing 19 changed files with 654 additions and 45 deletions.
191 changes: 191 additions & 0 deletions Jungle/Buttons/CooldownButton.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Jungle.Utils;
using Reactor.Utilities.Attributes;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

namespace Jungle.Buttons;

[RegisterInIl2Cpp]
public class CooldownButton : MonoBehaviour
{
public static float XDistFromEdge = 1.1f;
public static float YDistFromEdge = 1.1f;

private static readonly int Percent = Shader.PropertyToID("_Percent");
private static readonly int Desat = Shader.PropertyToID("_Desat");

public static List<CooldownButton> Buttons { get; } = new();

public static T Create<T>() where T : CooldownButton
{
var buttonObj = new GameObject(typeof(T).Name) {layer = 5};
buttonObj.transform.parent = HudManager.Instance.transform;
return buttonObj.AddComponent<T>();
}

public CooldownButton(IntPtr ptr) : base(ptr) { }

public SpriteRenderer Renderer;
public TextMeshPro TimerText;
public AspectPosition Aspect;
public PassiveButton Button;
public Action OnClickAction;

public float CurrentTime;
public float Cooldown;

#region Event Loop

public void Awake()
{
Buttons.Add(this);

var buttonObj = gameObject;

// SpriteRenderer
Renderer = buttonObj.AddComponent<SpriteRenderer>();
Renderer.material = new Material(Shader.Find("Unlit/CooldownShader"));

// TextMeshPro
TimerText = Instantiate(HudManager.Instance.KillButton.cooldownTimerText, buttonObj.transform);
TimerText.transform.localPosition = new Vector3(0, 0.07f, -0.001f);
TimerText.gameObject.SetActive(true);

// AspectPosition
Aspect = buttonObj.AddComponent<AspectPosition>();
Aspect.parentCam = HudManager.Instance.UICamera;

// PassiveButton
var buttonColl = buttonObj.AddComponent<BoxCollider2D>();
buttonColl.size = new Vector2(1.15f, 1.15f);

Button = buttonObj.AddComponent<PassiveButton>();
Button.OnMouseOut = Button.OnMouseOver = new Button.ButtonClickedEvent();
Button.Colliders = new[] { buttonColl };
Button.OnClick = new Button.ButtonClickedEvent();
Button.OnClick.AddListener((Action) InvokePerformClick);

SetPosition(AspectPosition.EdgeAlignments.LeftBottom);
}

public virtual void Update()
{
Button!.enabled = Renderer!.enabled = TimerText!.enabled = ShouldBeVisible();
// ReSharper disable once CompareOfFloatsByEqualityOperator -> Its set to this value manually
if (CurrentTime == int.MaxValue) return;
if (MeetingHud.Instance || ExileController.Instance) CurrentTime = Cooldown;

if (!Unlocked())
{
SetButtonSaturation(false);
SetButtonCooldownLevel(1);
TimerText.enabled = false;
return;
}

CurrentTime -= Time.deltaTime;

SetButtonCooldownLevel(Cooldown == 0 ? 0 : Mathf.Clamp01(CurrentTime / Cooldown));
SetButtonSaturation(true);

if (CurrentTime <= 0)
{
CurrentTime = 0;
TimerText.enabled = false;
return;
}

TimerText.text = Mathf.CeilToInt(CurrentTime).ToString();
}

public void OnDestroy()
{
var myHashCode = GetHashCode();
Buttons.RemoveAll(b => b.GetHashCode() == myHashCode);
}

#endregion

#region Appearance

public void SetPosition(AspectPosition.EdgeAlignments alignment, int buttonIndex = Int32.MaxValue)
{
var myHashCode = GetHashCode();
if (buttonIndex == Int32.MaxValue) buttonIndex = Buttons.Count(c => c.GetHashCode() != myHashCode && c.Aspect!.Alignment == alignment);

Vector3 distanceFromEdge = new(0.7f, 0.7f, -5f);

distanceFromEdge.x += XDistFromEdge * Mathf.FloorToInt(buttonIndex / 3f);
distanceFromEdge.y += YDistFromEdge * (buttonIndex % 3);
distanceFromEdge.z = -5f;

Aspect!.Alignment = alignment;
Aspect.DistanceFromEdge = distanceFromEdge;
Aspect.AdjustPosition();
}

public void SetSprite(string spriteName, float ppu = 100) => SetSprite(AssetManager.LoadSprite(spriteName, ppu));

public void SetSprite(Sprite sprite)
{
Renderer!.sprite = sprite;
// ReSharper disable once InvokeAsExtensionMethod
CooldownHelpers.SetCooldownNormalizedUvs(Renderer);
}

public void SetButtonCooldownLevel(float amount)
{
Renderer!.material.SetFloat(Percent, amount);
}

public void SetButtonSaturation(bool saturated)
{
Renderer!.material.SetFloat(Desat, saturated ? 0 : 1);
Renderer.color = saturated ? Palette.EnabledColor : Palette.DisabledClear;
}

#endregion

#region Usability

public virtual bool Unlocked() => true;

public virtual bool ShouldBeVisible()
{
if (!ShipStatus.Instance) return false;
if (!HudManager.Instance.UseButton.gameObject.active) return false;
var localPlayer = PlayerControl.LocalPlayer;
if (!localPlayer || localPlayer.Data == null) return false;
if (localPlayer.Data.IsDead) return false;
return true;
}

public virtual bool ShouldCooldown() => PlayerControl.LocalPlayer.CanMove;

public virtual bool CanUse()
{
if (!isActiveAndEnabled || CurrentTime > 0) return false;
if (!Unlocked()) return false;
if (!ShipStatus.Instance || MeetingHud.Instance || Minigame.Instance) return false;
if (PlayerControl.LocalPlayer.inVent) return false;
return true;
}

public void InvokePerformClick()
{
PerformClick();
}

public virtual void PerformClick()
{
if (!ShouldCooldown() || !ShouldBeVisible() || !CanUse()) return;

CurrentTime = Cooldown;
OnClickAction?.Invoke();
}
#endregion
}
2 changes: 1 addition & 1 deletion Jungle/Config/MapConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static void SetupConfig()
{
HudManager.Instance.ShadowQuad.material.color = Color.clear;

foreach (Collider2D collider2D in Object.FindObjectsOfType<Collider2D>())
foreach (var collider2D in Object.FindObjectsOfType<Collider2D>())
{
if (collider2D.gameObject.layer == 10) collider2D.isTrigger = true;
}
Expand Down
4 changes: 2 additions & 2 deletions Jungle/Debugging/BaseDebugTab.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ internal static void Initialize()

private static void Register(Assembly assembly)
{
foreach (Type type in assembly.GetTypes().Where(t => t.IsSubclassOf(typeof(BaseDebugTab)) && !t.IsAbstract))
foreach (var type in assembly.GetTypes().Where(t => t.IsSubclassOf(typeof(BaseDebugTab)) && !t.IsAbstract))
{
BaseDebugTab debugger = (BaseDebugTab)Activator.CreateInstance(type)!;
var debugger = (BaseDebugTab)Activator.CreateInstance(type)!;
DebugWindow.Tabs.Add(debugger);
}
}
Expand Down
2 changes: 1 addition & 1 deletion Jungle/Debugging/DebugWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private void BuildWindow()

GUILayout.BeginHorizontal();

foreach (BaseDebugTab tab in Tabs)
foreach (var tab in Tabs)
{
if (GUILayout.Toggle(SelectedTab == tab, tab.Name, GUI.skin.button))
{
Expand Down
22 changes: 22 additions & 0 deletions Jungle/Debugging/GeneralTab.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using Jungle.Buttons;
using Jungle.HUDMap;
using Jungle.Utils;
using UnityEngine;

Expand All @@ -17,6 +19,26 @@ public override void BuildUI()
ResolutionManager.SetResolution(2560, 1080, true);
}

if (GUILayout.Button("Test Button"))
{
CooldownButton.Create<ClimbButton>();
}

if (GUILayout.Button("Teleport"))
{
void MouseUpEvent(CustomMapBehaviour instance, int mousebutton, Vector2 worldposition)
{
if (mousebutton != 1) return;
instance.MouseUpEvent -= MouseUpEvent;
instance.Parent!.Close();
PlayerControl.LocalPlayer.moveable = true;
Message(PlayerControl.LocalPlayer.NetTransform.transform.position.ToString());
PlayerControl.LocalPlayer.NetTransform.RpcSnapTo(worldposition);
Message(PlayerControl.LocalPlayer.NetTransform.transform.position.ToString());
}
CustomMapBehaviour.ShowWithAllPlayers(new Color32(158, 240, 103, 255), MouseUpEvent);
}

if (CameraZoomController.Instance != null)
{
GUILayout.Label($"Camera Zoom: {CameraZoomController.Instance.OrthographicSize:F2}");
Expand Down
2 changes: 1 addition & 1 deletion Jungle/Debugging/TaskTab.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public override void BuildUI()
{
if (!PlayerControl.LocalPlayer) return;

foreach (PlayerTask dataTask in PlayerControl.LocalPlayer.myTasks)
foreach (var dataTask in PlayerControl.LocalPlayer.myTasks)
{
if (!dataTask.IsComplete && dataTask.TryCast<NormalPlayerTask>() is { } task)
{
Expand Down
30 changes: 30 additions & 0 deletions Jungle/ExampleButton.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using Jungle.Buttons;
using Reactor.Utilities.Attributes;
using UnityEngine;

namespace Jungle;

[RegisterInIl2Cpp]
public class ClimbButton : CooldownButton
{
public ClimbButton(IntPtr ptr) : base(ptr)
{
}

public void Start()
{
SetSprite("Climb", 300);
Cooldown = 5f;
OnClickAction += () =>
{
PlayerControl.LocalPlayer.NetTransform.RpcSnapTo(PlayerControl.LocalPlayer.transform.position + Vector3.up * 2f);
};
}
public override bool Unlocked()
{
return true;
}

public override bool ShouldBeVisible() => base.ShouldBeVisible() && Unlocked();
}
6 changes: 3 additions & 3 deletions Jungle/Extensions/AnimationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ public static class AnimationExtensions
{
public static IEnumerator Wait(this AnimationClip clip, Action<AnimationEvent> onEvent = null)
{
float length = clip.length;
var length = clip.length;

foreach (AnimationEvent animationEvent in clip.events)
foreach (var animationEvent in clip.events)
{
float time = animationEvent.time;
var time = animationEvent.time;
length -= time;

yield return new WaitForSeconds(time);
Expand Down
12 changes: 6 additions & 6 deletions Jungle/Extensions/CollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static class CollectionExtensions
public static I.List<T> ToIl2CppList<T>(this List<T> systemList)
{
I.List<T> iList = new();
foreach (T item in systemList) iList.Add(item);
foreach (var item in systemList) iList.Add(item);
return iList;
}

Expand All @@ -23,9 +23,9 @@ public static I.List<T> ToIl2CppList<T>(this List<T> systemList)
/// </summary>
public static void Shuffle<T>(this IList<T> self)
{
for (int i = self.Count - 1; i > 0; i--)
for (var i = self.Count - 1; i > 0; i--)
{
int j = UnityEngine.Random.Range(0, i + 1);
var j = UnityEngine.Random.Range(0, i + 1);
(self[i], self[j]) = (self[j], self[i]);
}
}
Expand All @@ -35,9 +35,9 @@ public static void Shuffle<T>(this IList<T> self)
/// </summary>
public static void Shuffle<T>(this IList<T> self, Random random)
{
for (int i = self.Count - 1; i > 0; i--)
for (var i = self.Count - 1; i > 0; i--)
{
int j = random.Next(i + 1);
var j = random.Next(i + 1);
(self[i], self[j]) = (self[j], self[i]);
}
}
Expand All @@ -47,7 +47,7 @@ public static void Shuffle<T>(this IList<T> self, Random random)
/// </summary>
public static IEnumerable<T> OfIl2CppType<T>(this IEnumerable source) where T : Il2CppObjectBase
{
foreach (object obj in source)
foreach (var obj in source)
{
if (obj is Il2CppObjectBase il2CppObject)
{
Expand Down
8 changes: 4 additions & 4 deletions Jungle/Extensions/ComponentExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static class ComponentExtensions
/// </summary>
public static T EnsureComponent<T>(this GameObject obj) where T : Component
{
T comp = obj.GetComponent<T>();
var comp = obj.GetComponent<T>();
if (!comp) comp = obj.AddComponent<T>();
return comp;
}
Expand All @@ -24,15 +24,15 @@ public static T EnsureComponent<T>(this GameObject obj) where T : Component

public static T GetCachedComponent<T>(this GameObject gameObject) where T : Component
{
if (CachedComponentStore<T>.Map.TryGetValue(gameObject, out T result))
if (CachedComponentStore<T>.Map.TryGetValue(gameObject, out var result))
{
return result;
}

T component = gameObject.GetComponent<T>();
var component = gameObject.GetComponent<T>();
if (!component) return component;

Dictionary<GameObject, T> map = CachedComponentStore<T>.Map;
var map = CachedComponentStore<T>.Map;
map[gameObject] = component;

gameObject.EnsureComponent<DestroyEventListener>().OnDestroyEvent += () => map.Remove(gameObject);
Expand Down
Loading

0 comments on commit 7237269

Please sign in to comment.