Skip to content

Commit 5cbbb6d

Browse files
authored
Merge pull request #43 from CrowdedMods/features/shapeshifter
Rework paging handling & support shapeshifter UI
2 parents 10ffb34 + e459fed commit 5cbbb6d

5 files changed

Lines changed: 177 additions & 68 deletions

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
using UnityEngine;
3+
4+
namespace CrowdedMod.Components;
5+
6+
// Interface until unhollower implements generic il2cpp (if it's possible)
7+
/// <summary>
8+
/// This class is not actually abstract because unhollower does not support it <br/>
9+
/// You need to implement <see cref="OnPageChanged"/> and <see cref="MaxPage"/>
10+
/// </summary>
11+
public class AbstractPagingBehaviour : MonoBehaviour
12+
{
13+
public AbstractPagingBehaviour(IntPtr ptr) : base(ptr)
14+
{
15+
}
16+
17+
private int _page;
18+
19+
public virtual int MaxPerPage => 15;
20+
// public virtual IEnumerable<T> Targets { get; }
21+
22+
public virtual int Page
23+
{
24+
get => _page;
25+
set
26+
{
27+
_page = value;
28+
OnPageChanged();
29+
}
30+
}
31+
32+
public virtual int MaxPage => throw new NotImplementedException();
33+
// public virtual int MaxPages => Targets.Count() / MaxPerPage;
34+
35+
public virtual void OnPageChanged() => throw new NotImplementedException();
36+
37+
public void Start()
38+
{
39+
OnPageChanged();
40+
}
41+
42+
public virtual void Update()
43+
{
44+
if (Input.GetKeyDown(KeyCode.UpArrow) || Input.mouseScrollDelta.y > 0f)
45+
{
46+
Page = Mathf.Clamp(Page - 1, 0, MaxPage);
47+
}
48+
else if (Input.GetKeyDown(KeyCode.DownArrow) || Input.mouseScrollDelta.y < 0f)
49+
{
50+
Page = Mathf.Clamp(Page + 1, 0, MaxPage);
51+
}
52+
}
53+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using Reactor;
5+
using UnhollowerBaseLib.Attributes;
6+
using UnityEngine;
7+
8+
namespace CrowdedMod.Components;
9+
10+
[RegisterInIl2Cpp]
11+
public class MeetingHudPagingBehaviour : AbstractPagingBehaviour
12+
{
13+
public MeetingHudPagingBehaviour(IntPtr ptr) : base(ptr)
14+
{
15+
}
16+
17+
public MeetingHud MeetingHud = null!;
18+
19+
[HideFromIl2Cpp]
20+
public IEnumerable<PlayerVoteArea> Targets => MeetingHud.playerStates.OrderBy(p => p.AmDead);
21+
public override int MaxPage => Targets.Count() / MaxPerPage;
22+
23+
public override void Update()
24+
{
25+
base.Update();
26+
MeetingHud.TimerText.text += $" ({Page + 1}/{MaxPage + 1})";
27+
}
28+
29+
public override void OnPageChanged()
30+
{
31+
var i = 0;
32+
33+
foreach (var button in Targets) {
34+
if (i >= Page * MaxPerPage && i < (Page + 1) * MaxPerPage) {
35+
button.gameObject.SetActive(true);
36+
37+
var relativeIndex = i % MaxPerPage;
38+
var row = relativeIndex / 3;
39+
var buttonTransform = button.transform;
40+
buttonTransform.localPosition = MeetingHud.VoteOrigin +
41+
new Vector3(
42+
MeetingHud.VoteButtonOffsets.x * (relativeIndex % 3),
43+
MeetingHud.VoteButtonOffsets.y * row,
44+
buttonTransform.localPosition.z
45+
);
46+
} else {
47+
button.gameObject.SetActive(false);
48+
}
49+
i++;
50+
}
51+
}
52+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using Reactor;
5+
using UnhollowerBaseLib.Attributes;
6+
using UnityEngine;
7+
8+
namespace CrowdedMod.Components;
9+
10+
[RegisterInIl2Cpp]
11+
public class ShapeShifterPagingBehaviour : AbstractPagingBehaviour
12+
{
13+
public ShapeShifterPagingBehaviour(IntPtr ptr) : base(ptr)
14+
{
15+
}
16+
17+
public ShapeshifterMinigame ShapeshifterMinigame = null!;
18+
[HideFromIl2Cpp]
19+
public IEnumerable<ShapeshifterPanel> Targets => ShapeshifterMinigame.potentialVictims.ToArray();
20+
21+
public override int MaxPage => Targets.Count() / MaxPerPage;
22+
23+
public override void OnPageChanged()
24+
{
25+
var i = 0;
26+
27+
foreach (var panel in Targets)
28+
{
29+
if (i >= Page * MaxPerPage && i < (Page + 1) * MaxPerPage) {
30+
panel.gameObject.SetActive(true);
31+
32+
var relativeIndex = i % MaxPerPage;
33+
var row = relativeIndex / 3;
34+
var buttonTransform = panel.transform;
35+
buttonTransform.localPosition = new Vector3(
36+
ShapeshifterMinigame.XStart + ShapeshifterMinigame.XOffset * (relativeIndex % 3),
37+
ShapeshifterMinigame.YStart + ShapeshifterMinigame.YOffset * row,
38+
buttonTransform.localPosition.z
39+
);
40+
} else {
41+
panel.gameObject.SetActive(false);
42+
}
43+
44+
i++;
45+
}
46+
}
47+
}

src/CrowdedMod/Patches/MeetingHudPatches.cs

Lines changed: 0 additions & 68 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using HarmonyLib;
2+
using CrowdedMod.Components;
3+
4+
namespace CrowdedMod.Patches {
5+
internal static class PagingPatches
6+
{
7+
[HarmonyPatch(typeof(MeetingHud), nameof(MeetingHud.Start))]
8+
public static class MeetingHudStartPatch
9+
{
10+
public static void Postfix(MeetingHud __instance)
11+
{
12+
__instance.gameObject.AddComponent<MeetingHudPagingBehaviour>().MeetingHud = __instance;
13+
}
14+
}
15+
16+
[HarmonyPatch(typeof(ShapeshifterMinigame), nameof(ShapeshifterMinigame.Begin))]
17+
public static class ShapeshifterMinigameBeginPatch
18+
{
19+
public static void Postfix(ShapeshifterMinigame __instance)
20+
{
21+
__instance.gameObject.AddComponent<ShapeShifterPagingBehaviour>().ShapeshifterMinigame = __instance;
22+
}
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)