Skip to content

Commit

Permalink
Movement turbo
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirefel committed Aug 11, 2024
1 parent afd95a3 commit 68441f7
Showing 1 changed file with 91 additions and 25 deletions.
116 changes: 91 additions & 25 deletions OriModding.BF.Speedrun/Turbo.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
Expand All @@ -9,19 +10,79 @@ namespace OriModding.BF.Speedrun;

public class TurboController : MonoBehaviour
{
class TurboButton(IButtonInput button, global::Core.Input.InputButtonProcessor target)
enum SpecialTargets
{
readonly IButtonInput button = button;
readonly global::Core.Input.InputButtonProcessor target = target;
None,
LSLeft,
LSRight,
LSUp,
LSDown,
RSLeft,
RSRight,
RSUp,
RSDown,
DPadLeft,
DPadRight,
DPadUp,
DPadDown
}

class TurboButton(IButtonInput button, global::Core.Input.InputButtonProcessor target, SpecialTargets specialTarget = SpecialTargets.None)
{
private readonly IButtonInput button = button;
private readonly global::Core.Input.InputButtonProcessor target = target;
private readonly SpecialTargets specialTarget = specialTarget;
int value = 0;

public void Update()
{
if (button.GetButton())
{
target.Update(false);
if (value == 0)
target.Update(true);
switch (specialTarget)
{
case SpecialTargets.None:
target.Update(false);
if (value == 0)
target.Update(true);
break;

case SpecialTargets.LSLeft:
global::Core.Input.HorizontalAnalogLeft = value == 0 ? -1 : 0;
break;
case SpecialTargets.LSRight:
global::Core.Input.HorizontalAnalogLeft = value == 0 ? 1 : 0;
break;
case SpecialTargets.LSUp:
global::Core.Input.VerticalAnalogLeft = value == 0 ? 1 : 0;
break;
case SpecialTargets.LSDown:
global::Core.Input.VerticalAnalogLeft = value == 0 ? -1 : 0;
break;
case SpecialTargets.RSLeft:
global::Core.Input.HorizontalAnalogRight = value == 0 ? -1 : 0;
break;
case SpecialTargets.RSRight:
global::Core.Input.HorizontalAnalogRight = value == 0 ? 1 : 0;
break;
case SpecialTargets.RSUp:
global::Core.Input.VerticalAnalogRight = value == 0 ? 1 : 0;
break;
case SpecialTargets.RSDown:
global::Core.Input.VerticalAnalogRight = value == 0 ? -1 : 0;
break;
case SpecialTargets.DPadLeft:
global::Core.Input.HorizontalDigiPad = value == 0 ? -1 : 0;
break;
case SpecialTargets.DPadRight:
global::Core.Input.HorizontalDigiPad = value == 0 ? 1 : 0;
break;
case SpecialTargets.DPadUp:
global::Core.Input.VerticalDigiPad = value == 0 ? 1 : 0;
break;
case SpecialTargets.DPadDown:
global::Core.Input.VerticalDigiPad = value == 0 ? -1 : 0;
break;
}

value = (value + 1) % 3;
}
Expand All @@ -32,7 +93,7 @@ public void Update()
}
}

List<TurboButton> turboButtons = new List<TurboButton>();
List<TurboButton> turboButtons;

void Awake()
{
Expand All @@ -44,6 +105,7 @@ void Awake()
orig(self);
foreach (var button in turboButtons)
button.Update();
self.RefreshControls();
};
}

Expand Down Expand Up @@ -78,12 +140,6 @@ private void Load(string filepath)
continue;
}

var targetInput = targets.FirstOrDefault(x => x.Name.ToLower() == line[0].ToLower());
if (targetInput == null)
{
Plugin.Logger.LogWarning($"Invalid turbo configuration (target): {line}");
continue;
}

var input = new CompoundButtonInput(InputLib.CustomInput.ParseButtons(line[1]));
if (input.Buttons.Length != line[1].Split(new[] { ',' }, System.StringSplitOptions.RemoveEmptyEntries).Length)
Expand All @@ -92,17 +148,27 @@ private void Load(string filepath)
continue;
}

turboButtons.Add(new TurboButton(input, targetInput.GetValue(null) as global::Core.Input.InputButtonProcessor));
Plugin.Logger.LogInfo($"Added turbo: {line[0]}:{line[1]}");
}
}

void OnGUI()
{
if (global::Core.Input.SpiritFlame.IsPressed)
{

GUI.Box(new Rect(5, 5, 50, 50), "SPIRITFLAME");
var targetInput = targets.FirstOrDefault(x => x.Name.ToLower() == line[0].ToLower());
if (targetInput != null)
{
turboButtons.Add(new TurboButton(input, targetInput.GetValue(null) as global::Core.Input.InputButtonProcessor));
Plugin.Logger.LogInfo($"Added turbo: {line[0]}:{line[1]}");
}
else
{
// Could be a movement one
try
{
var t = (SpecialTargets)Enum.Parse(typeof(SpecialTargets), line[0], true);
turboButtons.Add(new TurboButton(input, null, t));
Plugin.Logger.LogInfo($"Added turbo: {line[0]}:{line[1]}");
}
catch (Exception)
{
Plugin.Logger.LogWarning($"Invalid turbo configuration (target): {line}");
continue;
}
}
}
}
}

0 comments on commit 68441f7

Please sign in to comment.