Vibrate on beat

This commit is contained in:
Lea 2024-05-20 21:30:27 +02:00
parent a28bebc626
commit 6fe683b4a9
Signed by: Lea
GPG key ID: 1BAFFE8347019C42
2 changed files with 26 additions and 6 deletions

View file

@ -1,4 +1,5 @@
using HarmonyLib; using HarmonyLib;
using MetalButtplug.Utils;
namespace MetalButtplug.Patches; namespace MetalButtplug.Patches;
@ -11,9 +12,11 @@ internal class UpdateBeatPatch {
var controller = player.ScoreController; var controller = player.ScoreController;
Plugin.Log.LogInfo( Plugin.Log.LogInfo(
$"Quarter beat, multiplier: {controller.GetCurrentTierIndex()}/{controller.GetNumTiers()} ({1 << controller.GetCurrentTierIndex()}x)" $"Quarter beat, multiplier: {controller.GetCurrentTierIndex()}/{controller.GetNumTiers()-1} ({1 << controller.GetCurrentTierIndex()}x)"
); );
ButtplugManager.Vibrate((float)controller.GetCurrentTierIndex() / ((float)controller.GetNumTiers() - 1), 100);
} }
} }
} }

View file

@ -1,11 +1,12 @@
using System; using System;
using System.Linq;
using System.Threading.Tasks;
using Buttplug.Client; using Buttplug.Client;
using Buttplug.Client.Connectors.WebsocketConnector; using Buttplug.Client.Connectors.WebsocketConnector;
namespace MetalButtplug.Utils; namespace MetalButtplug.Utils;
internal static class ButtplugManager { internal static class ButtplugManager {
public static bool enabled = false;
static ButtplugClient client; static ButtplugClient client;
public async static void Init() { public async static void Init() {
@ -17,7 +18,6 @@ internal static class ButtplugManager {
var connector = new ButtplugWebsocketConnector(new Uri("ws://localhost:12345")); var connector = new ButtplugWebsocketConnector(new Uri("ws://localhost:12345"));
await client.ConnectAsync(connector); await client.ConnectAsync(connector);
Plugin.Log.LogInfo("Connected to buttplug server!"); Plugin.Log.LogInfo("Connected to buttplug server!");
enabled = true;
} catch(Exception ex) { } catch(Exception ex) {
Plugin.Log.LogWarning($"Failed to connect: {ex.Message}\nDisabling buttplug integration - Is Intiface running?"); Plugin.Log.LogWarning($"Failed to connect: {ex.Message}\nDisabling buttplug integration - Is Intiface running?");
return; return;
@ -28,15 +28,32 @@ internal static class ButtplugManager {
Plugin.Log.LogInfo($"Found devices:"); Plugin.Log.LogInfo($"Found devices:");
foreach (ButtplugClientDevice device in client.Devices) { foreach (ButtplugClientDevice device in client.Devices) {
Plugin.Log.LogInfo($"- ${device.DisplayName}"); Plugin.Log.LogInfo($"- {device.Name}");
} }
} }
private static void OnDeviceAdd(object sender, DeviceAddedEventArgs e) { private static void OnDeviceAdd(object sender, DeviceAddedEventArgs e) {
Plugin.Log.LogInfo($"Device added: {e.Device.DisplayName}"); Plugin.Log.LogInfo($"Device added: {e.Device.Name}");
} }
private static void OnDeviceRemove(object sender, DeviceRemovedEventArgs e) { private static void OnDeviceRemove(object sender, DeviceRemovedEventArgs e) {
Plugin.Log.LogInfo($"Device removed: {e.Device.DisplayName}"); Plugin.Log.LogInfo($"Device removed: {e.Device.Name}");
}
public async static void Vibrate(float strength, int duration) {
if (!client.Connected) return;
if (strength > 1) strength = 1;
if (strength <= 0) return; // No need to waste time if we're not going to vibrate anyway
Task[] tasks = client.Devices
.Where((device) => device.VibrateAttributes.Count > 0)
.Select(async (device) => {
await device.VibrateAsync(strength);
await Task.Delay(duration);
await device.Stop();
})
.ToArray();
await Task.WhenAll(tasks);
} }
} }