Skip to content

Commit

Permalink
Implement patterns (constant and sine wave)
Browse files Browse the repository at this point in the history
  • Loading branch information
Furimanejo committed Oct 9, 2021
1 parent c48d25a commit 92454f1
Show file tree
Hide file tree
Showing 9 changed files with 1,785 additions and 201 deletions.
1,795 changes: 1,648 additions & 147 deletions ButtplugOfLegendsUnity/Assets/Scenes/MainScene.unity

Large diffs are not rendered by default.

48 changes: 17 additions & 31 deletions ButtplugOfLegendsUnity/Assets/Scripts/Buttplug/ButtplugClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,26 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Threading.Tasks;
using Buttplug;
using ButtplugUnity;
using UnityEngine.UI;

public class ButtplugClient : MonoBehaviour
{
[SerializeField] string clientDisplayName;
[SerializeField] string clientDisplayName;
[SerializeField] Text connectionStatusLabel;
ButtplugUnityClient client = null;
float transmissionTimer = 0;
[SerializeField] float transmissionTimerPeriod = .2f;
float value = 0f;

[SerializeField] InputField maxDeviceStrength;


private void Start()
{
client = new ButtplugUnityClient(clientDisplayName);
ButtplugAntiCrash.clientList.Add(client);
client.ServerDisconnect += Client_ServerDisconnect;
ButtplugAntiCrash.clientList.Add(client);
}

private void Client_ServerDisconnect(object sender, EventArgs e)
{
connectionStatusLabel.text = "Status: Disconnected";
}

public async void TryConnect()
Expand All @@ -29,39 +30,24 @@ public async void TryConnect()
{
var connector = new ButtplugWebsocketConnectorOptions(new Uri("ws://localhost:12345/buttplug"));
await client.ConnectAsync(connector);
if(client.Connected)
if (client.Connected)
{
connectionStatusLabel.text = "Status: Connected";
await client.StartScanningAsync();
}
}
}

void Update()
{
transmissionTimer += Time.deltaTime;
if (transmissionTimer > transmissionTimerPeriod)
{
transmissionTimer = 0;
SendValue();
}
}

public void SetValue(float _value)
{
value = _value;
value = Mathf.Clamp(value, 0f, 1f);
}

void SendValue()
public void SendValue(float value,ServerMessage.Types.MessageAttributeType messageAttributeType)
{
if (client == null || client.Connected == false)
return;

var valueToBeSent = value * float.Parse(maxDeviceStrength.text) / 100;

foreach (var device in client.Devices)
{
if (device.AllowedMessages.ContainsKey(ServerMessage.Types.MessageAttributeType.VibrateCmd))
if (device.AllowedMessages.ContainsKey(messageAttributeType))
{
device.SendVibrateCmd(valueToBeSent);
device.SendVibrateCmd(value);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public abstract class ButtplugController : MonoBehaviour
{
[SerializeField] protected ButtplugClient client;
[SerializeField] float transmissionTimerPeriod = .2f;
float transmissionTimer = 0;
protected float value;

protected abstract void SendValueToClient();

public virtual void SetValue(float _value)
{
value = _value;
value = Mathf.Clamp(value, 0f, 1f);
}

private void Update()
{
transmissionTimer += Time.deltaTime;
if (transmissionTimer > transmissionTimerPeriod)
{
transmissionTimer = 0;
SendValueToClient();
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class ButtplugOfLegendsUnity : MonoBehaviour
string playerName = string.Empty;
int countOfEventsInLastEvaluation = 0;

[SerializeField] ButtplugClient buttplugClient;
[SerializeField] List<ButtplugController> controllers;
[SerializeField] ScoreManager scoreManager;

void Start()
Expand All @@ -24,7 +24,8 @@ void Start()

void Update()
{
buttplugClient.SetValue(scoreManager.GetScore()/100f);
foreach(var controller in controllers)
controller.SetValue(scoreManager.GetScore()/100f);
}

IEnumerator GetClientDataRoutine()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ButtplugVibrationController : ButtplugController
{
[SerializeField] InputField maxDeviceStrength;
int pattern = 0;
[SerializeField] InputField frequencyBPM;
const float BPM_TO_RADS = 0.10471975499997f;

protected override void SendValueToClient()
{
var sendValue = value * float.Parse(maxDeviceStrength.text) / 100;
switch (pattern)
{
case 1:
sendValue *= .5f + .5f * Mathf.Sin(float.Parse(frequencyBPM.text) * Time.realtimeSinceStartup * BPM_TO_RADS);
break;
default:
break;
}
Debug.Log(sendValue);
client.SendValue(sendValue, Buttplug.ServerMessage.Types.MessageAttributeType.VibrateCmd);
}

public void OnChangePattern(int newPattern)
{
pattern = newPattern;
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,33 @@ public class ClickThroughController : MonoBehaviour
[SerializeField] OverlayController overlayController = null;
[SerializeField] EventSystem eventSystem = null;
[SerializeField] GraphicRaycaster graphicRaycaster = null;
bool status = true;

void Update()
private void Start()
{
StartCoroutine(CheckRaycast());
}

IEnumerator CheckRaycast()
{
//Set up the new Pointer Event
var m_PointerEventData = new PointerEventData(eventSystem);
//Set the Pointer Event Position to that of the mouse position
m_PointerEventData.position = Input.mousePosition;
//Create a list of Raycast Results
List<RaycastResult> results = new List<RaycastResult>();

//Raycast using the Graphics Raycaster and mouse click position
graphicRaycaster.Raycast(m_PointerEventData, results);
while (true)
{
//Set the Pointer Event Position to that of the mouse position
m_PointerEventData.position = Input.mousePosition;
//Create a list of Raycast Results
List<RaycastResult> results = new List<RaycastResult>();
//Raycast using the Graphics Raycaster and mouse click position
graphicRaycaster.Raycast(m_PointerEventData, results);

bool hit = results.Count > 0;
overlayController.SetClickThrough(!hit);
bool hit = results.Count > 0;
if(hit != status)
{
status = hit;
overlayController.SetClickThrough(!hit);
}
yield return new WaitForSeconds(0.1f);
}
}
}
22 changes: 11 additions & 11 deletions ButtplugOfLegendsUnity/Assets/UI/Labeled Int Input Field.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ MonoBehaviour:
m_Calls: []
m_FontData:
m_Font: {fileID: 10102, guid: 0000000000000000e000000000000000, type: 0}
m_FontSize: 14
m_FontSize: 28
m_FontStyle: 0
m_BestFit: 0
m_MinSize: 10
m_MinSize: 2
m_MaxSize: 40
m_Alignment: 0
m_Alignment: 4
m_AlignByGeometry: 0
m_RichText: 0
m_HorizontalOverflow: 1
Expand Down Expand Up @@ -114,7 +114,7 @@ RectTransform:
m_AnchorMin: {x: 0, y: 1}
m_AnchorMax: {x: 0, y: 1}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 150, y: 30}
m_SizeDelta: {x: 300, y: 60}
m_Pivot: {x: 0, y: 1}
--- !u!222 &8325702996894195228
CanvasRenderer:
Expand Down Expand Up @@ -146,10 +146,10 @@ MonoBehaviour:
m_Calls: []
m_FontData:
m_Font: {fileID: 10102, guid: 0000000000000000e000000000000000, type: 0}
m_FontSize: 14
m_FontSize: 28
m_FontStyle: 0
m_BestFit: 0
m_MinSize: 10
m_MinSize: 2
m_MaxSize: 40
m_Alignment: 3
m_AlignByGeometry: 0
Expand Down Expand Up @@ -192,9 +192,9 @@ RectTransform:
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 1}
m_AnchorMax: {x: 0, y: 1}
m_AnchoredPosition: {x: 480, y: -270}
m_SizeDelta: {x: 200, y: 30}
m_Pivot: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 400, y: 60}
m_Pivot: {x: 0, y: 1}
--- !u!1 &8325702997181569118
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -232,8 +232,8 @@ RectTransform:
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 1}
m_AnchorMax: {x: 0, y: 1}
m_AnchoredPosition: {x: 150, y: 0}
m_SizeDelta: {x: 50, y: 30}
m_AnchoredPosition: {x: 300, y: 0}
m_SizeDelta: {x: 100, y: 60}
m_Pivot: {x: 0, y: 1}
--- !u!222 &8325702997181569114
CanvasRenderer:
Expand Down

0 comments on commit 92454f1

Please sign in to comment.