fgm24/Assets/Scripts/Rumbling/PlayerRumbling.cs

85 lines
1.8 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerRumbling : MonoBehaviour
{
[SerializeField] private float RopeRubleTolerance = 0.5f;
private PlayerInput pInput;
private Gamepad pad;
// Rope
[SerializeField] private RopeSimulator rope;
private void Start()
{
Invoke("LateStart", 0.1f);
}
void LateStart()
{
pInput = GetComponent<PlayerInput>();
pad = Gamepad.all.ElementAtOrDefault(pInput.PlayerNum);
if (pad == null)
{
this.enabled = false;
}
hasInit = true;
}
bool hasInit = false;
private void Update()
{
if (!hasInit) return;
var rumble = new RumbleData();
float ropeClamed = Mathf.Max(0, rope.Overshoot);
if (ropeClamed > RopeRubleTolerance)
{
float mapped = ropeClamed.Remap(0.5f, 1f, 0f, 1f);
rumble.Max(mapped, mapped);
}
rumble.Commit(pad);
}
}
public class RumbleData
{
private bool modified = false;
private float lowFreq = 0;
private float highFreq = 0;
public void Max(float low, float high)
{
modified = true;
lowFreq = Mathf.Max(lowFreq, low);
highFreq = Mathf.Max(highFreq, high);
}
public void Min(float low, float high)
{
modified = true;
lowFreq = Mathf.Min(lowFreq, low);
highFreq = Mathf.Min(highFreq, high);
}
public void Add(float low, float high)
{
modified = true;
lowFreq += low;
highFreq += high;
}
public void Commit(Gamepad target)
{
modified = false;
target.SetMotorSpeeds(lowFreq, highFreq);
}
}