Skip to content

Commit

Permalink
Merge pull request #4 from Vl4dimyr/develop
Browse files Browse the repository at this point in the history
Added: contoller support & configuration
  • Loading branch information
Vl4dimyr authored Sep 5, 2020
2 parents 89a4664 + aaee007 commit da36877
Show file tree
Hide file tree
Showing 6 changed files with 319 additions and 41 deletions.
6 changes: 4 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"cSpell.words": [
"bepis",
"thunderstore"
"captainshotgunmodes",
"thunderstore",
"userstorm"
]
}
}
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.1.0] - 2020-9-5
### Added
- Controller support (tested with xbox 360 controller)
- Cycle through modes with mouse wheel or DPad
- Configuration for default mode and enable/disable modes selection
- README.md information about cycle through logic and controller support
- README.md Config and example config

## [1.0.0] - 2020-8-30
### Added
- All project files (GitHub)
Expand Down
230 changes: 195 additions & 35 deletions CaptainShotgunModesPlugin.cs
Original file line number Diff line number Diff line change
@@ -1,45 +1,66 @@
using BepInEx;
using System;
using BepInEx;
using BepInEx.Configuration;
using UnityEngine;
using EntityStates.Captain.Weapon;
using R2API.Utils;
using RoR2.UI;
using RoR2;
using On_ChargeCaptainShotgun = On.EntityStates.Captain.Weapon.ChargeCaptainShotgun;

namespace CaptainShotgunModes
{
enum FireMode { Normal, Auto, AutoCharge }
enum FireMode { Normal, Auto, AutoCharge, Count }

[BepInDependency("com.bepis.r2api", BepInDependency.DependencyFlags.HardDependency)]
[BepInPlugin("de.userstorm.captainshotgunmodes", "CaptainShotgunModes", "{VERSION}")]
public class CaptainShotgunModesPlugin : BaseUnityPlugin
{
public static ConfigEntry<string> DefaultMode { get; set; }
public static ConfigEntry<bool> EnableModeSelectionWithNumberKeys { get; set; }
public static ConfigEntry<bool> EnableModeSelectionWithMouseWheel { get; set; }
public static ConfigEntry<bool> EnableModeSelectionWithDPad { get; set; }

private FireMode fireMode = FireMode.Normal;
private float fixedAge = 0;

public void FixedUpdateHook(
On.EntityStates.Captain.Weapon.ChargeCaptainShotgun.orig_FixedUpdate orig,
ChargeCaptainShotgun self
)
private void SingleFireMode(On_ChargeCaptainShotgun.orig_FixedUpdate orig, ChargeCaptainShotgun self)
{
fixedAge += Time.fixedDeltaTime;
orig.Invoke(self);

if (fireMode.Equals(FireMode.Normal))
if (self.GetFieldValue<bool>("released"))
{
orig.Invoke(self);
fixedAge = 0;
}
}

if (self.GetFieldValue<bool>("released")) {
fixedAge = 0;
}
private void AutoFireMode(On_ChargeCaptainShotgun.orig_FixedUpdate orig, ChargeCaptainShotgun self)
{
var didFire = false;
var released = self.GetFieldValue<bool>("released");

return;
if (!released)
{
didFire = true;
fixedAge = 0;
self.SetFieldValue("released", true);
}

bool didFire = false;
bool charge = fireMode.Equals(FireMode.AutoCharge);
bool released = self.GetFieldValue<bool>("released");
float chargeDuration = self.GetFieldValue<float>("chargeDuration");
orig.Invoke(self);

if (!released && (!charge || (charge && fixedAge >= chargeDuration)))
if (didFire)
{
self.SetFieldValue("released", false);
}
}

private void AutoFireChargeMode(On_ChargeCaptainShotgun.orig_FixedUpdate orig, ChargeCaptainShotgun self)
{
var didFire = false;
var released = self.GetFieldValue<bool>("released");
var chargeDuration = self.GetFieldValue<float>("chargeDuration");

if (!released && fixedAge >= chargeDuration)
{
didFire = true;
fixedAge = 0;
Expand All @@ -54,53 +75,192 @@ ChargeCaptainShotgun self
}
}

public void UpdateHook(On.RoR2.UI.SkillIcon.orig_Update orig, SkillIcon self)
private void CycleFireMode (bool forward = true)
{
orig.Invoke(self);
FireMode newFireMode = fireMode + (forward ? 1 : -1);

if (self.targetSkill && self.targetSkillSlot.Equals(SkillSlot.Primary))
if (newFireMode == FireMode.Count)
{
SurvivorIndex survivorIndex =
SurvivorCatalog.GetSurvivorIndexFromBodyIndex(self.targetSkill.characterBody.bodyIndex);
newFireMode = FireMode.Normal;
}

if (survivorIndex.Equals(SurvivorIndex.Captain))
{
self.stockText.gameObject.SetActive(true);
self.stockText.fontSize = 12f;
self.stockText.SetText(fireMode.ToString());
}
if ((int)newFireMode == -1)
{
newFireMode = FireMode.Count - 1;
}

fireMode = newFireMode;
}

public void Awake()
private void InitConfig()
{
On.EntityStates.Captain.Weapon.ChargeCaptainShotgun.FixedUpdate += FixedUpdateHook;
On.RoR2.UI.SkillIcon.Update += UpdateHook;
DefaultMode = Config.Bind<string>(
"Settings",
"DefaultMode",
"Normal",
"The mode that is selected on game start. Modes: Normal, Auto, AutoCharge"
);

EnableModeSelectionWithNumberKeys = Config.Bind<bool>(
"Settings",
"EnableModeSelectionWithNumberKeys",
true,
"When set to true modes can be selected using the number keys"
);

EnableModeSelectionWithMouseWheel = Config.Bind<bool>(
"Settings",
"EnableModeSelectionWithMouseWheel",
true,
"When set to true modes can be cycled through using the mouse wheel"
);


EnableModeSelectionWithDPad = Config.Bind<bool>(
"Settings",
"EnableModeSelectionWithDPad",
true,
"When set to true modes can be cycled through using the DPad (controller)"
);
}

public void Update()
private void HandleConfig()
{
try
{
fireMode = (FireMode)Enum.Parse(typeof(FireMode), DefaultMode.Value);
}
catch (Exception)
{
fireMode = FireMode.Normal;
}
}

private void SelectFireModeWithNumberKeys() {
if (!EnableModeSelectionWithNumberKeys.Value) {
return;
}

if (Input.GetKeyDown(KeyCode.Alpha1))
{
fireMode = FireMode.Normal;

return;
}

if (Input.GetKeyDown(KeyCode.Alpha2))
{
fireMode = FireMode.Auto;

return;
}

if (Input.GetKeyDown(KeyCode.Alpha3))
{
fireMode = FireMode.AutoCharge;
}
}

private void SelectFireModeWithMouseWheel() {
if (!EnableModeSelectionWithMouseWheel.Value) {
return;
}

float wheel = Input.GetAxis("Mouse ScrollWheel");

if (wheel == 0) {
return;
}

// scroll down => forward; scroll up => backward
CycleFireMode(wheel < 0f);
}

private void SelectFireModeWithDPad()
{
if (!EnableModeSelectionWithDPad.Value) {
return;
}

if (DPad.GetInputDown(DPadInput.Right) || DPad.GetInputDown(DPadInput.Down))
{
CycleFireMode();

return;
}

if (DPad.GetInputDown(DPadInput.Left) || DPad.GetInputDown(DPadInput.Up))
{
CycleFireMode(false);
}
}

private void SelectFireMode()
{
SelectFireModeWithNumberKeys();
SelectFireModeWithMouseWheel();
SelectFireModeWithDPad();
}

public void FixedUpdateHook(On_ChargeCaptainShotgun.orig_FixedUpdate orig, ChargeCaptainShotgun self)
{
fixedAge += Time.fixedDeltaTime;

switch (fireMode)
{
case FireMode.Normal:
SingleFireMode(orig, self);
break;
case FireMode.Auto:
AutoFireMode(orig, self);
break;
case FireMode.AutoCharge:
AutoFireChargeMode(orig, self);
break;
default:
// fallback to single fire mode
SingleFireMode(orig, self);
break;
}
}

public void UpdateHook(On.RoR2.UI.SkillIcon.orig_Update orig, SkillIcon self)
{
orig.Invoke(self);

if (self.targetSkill && self.targetSkillSlot == SkillSlot.Primary)
{
int bodyIndex = self.targetSkill.characterBody.bodyIndex;
SurvivorIndex survivorIndex = SurvivorCatalog.GetSurvivorIndexFromBodyIndex(bodyIndex);

if (survivorIndex == SurvivorIndex.Captain)
{
self.stockText.gameObject.SetActive(true);
self.stockText.fontSize = 12f;
self.stockText.SetText(fireMode.ToString());
}
}
}

public void Awake()
{
InitConfig();
HandleConfig();

On_ChargeCaptainShotgun.FixedUpdate += FixedUpdateHook;
On.RoR2.UI.SkillIcon.Update += UpdateHook;
}

public void Update()
{
DPad.Update();

// TODO: add gamepad button to cycle through fire modes
SelectFireMode();
}

public void OnDestroy()
{
On.EntityStates.Captain.Weapon.ChargeCaptainShotgun.FixedUpdate -= FixedUpdateHook;
On_ChargeCaptainShotgun.FixedUpdate -= FixedUpdateHook;
On.RoR2.UI.SkillIcon.Update -= UpdateHook;
}
}
Expand Down
61 changes: 61 additions & 0 deletions DPad.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using UnityEngine;

namespace CaptainShotgunModes
{
enum DPadInput { Left, Right, Up, Down, Count }
enum DPadAxis { Horizontal, Vertical, Count }

class DPad
{
private static bool[] inputDownList = new bool [(int)DPadInput.Count];
private static bool[] inputUpList = new bool [(int)DPadInput.Count];
private static float[] axisValueList = new float [(int)DPadAxis.Count];

public static void Update()
{
// TODO: this might only work with xbox 360 and xbox one contollers
float horizontal = Input.GetAxis("Joy1Axis6");
float vertical = Input.GetAxis("Joy1Axis7");

inputDownList[(int)DPadInput.Left] = horizontal < 0f && axisValueList[(int)DPadAxis.Horizontal] == 0f;
inputDownList[(int)DPadInput.Right] = horizontal > 0f && axisValueList[(int)DPadAxis.Horizontal] == 0f;
inputDownList[(int)DPadInput.Up] = vertical > 0f && axisValueList[(int)DPadAxis.Vertical] == 0f;
inputDownList[(int)DPadInput.Down] = vertical < 0f && axisValueList[(int)DPadAxis.Vertical] == 0f;

inputUpList[(int)DPadInput.Left] = horizontal == 0f && axisValueList[(int)DPadAxis.Horizontal] < 0f;
inputUpList[(int)DPadInput.Right] = horizontal == 0f && axisValueList[(int)DPadAxis.Horizontal] > 0f;
inputUpList[(int)DPadInput.Up] = vertical == 0f && axisValueList[(int)DPadAxis.Vertical] > 0f;
inputUpList[(int)DPadInput.Down] = vertical == 0f && axisValueList[(int)DPadAxis.Vertical] < 0f;

axisValueList[(int)DPadAxis.Horizontal] = horizontal;
axisValueList[(int)DPadAxis.Vertical] = vertical;
}

public static bool GetInputDown(DPadInput input)
{
if (input < 0 || input >= DPadInput.Count) {
return false;
}

return inputDownList[(int)input];
}

public static bool GetInputUp(DPadInput input)
{
if (input < 0 || input >= DPadInput.Count) {
return false;
}

return inputUpList[(int)input];
}

public static float GetAxis(DPadAxis axis)
{
if (axis < 0 || axis >= DPadAxis.Count) {
return 0f;
}

return axisValueList[(int)axis];
}
}
}
Loading

0 comments on commit da36877

Please sign in to comment.