194 lines
6.2 KiB
C#
194 lines
6.2 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using XInputDotNetPure;
|
|
using System.IO;
|
|
|
|
public class RumbleManager : MonoBehaviour
|
|
{
|
|
PlayerIndex playerIndex;
|
|
public bool shouldRumble = true;
|
|
public bool IsVibrating;
|
|
public bool heartBeatIsPlaying;
|
|
public bool isUpdatingRumble;
|
|
|
|
Coroutine RumbleHeartbeatRythmCoroutine;
|
|
private bool rumbleHeatBeatRythmCoroutineIsPlaying;
|
|
|
|
public SettingsData settingsData;
|
|
private string settingsPath;
|
|
|
|
private static RumbleManager _instance;
|
|
public static RumbleManager GetInstance() { return _instance; }
|
|
|
|
private void Awake()
|
|
{
|
|
if (!_instance)
|
|
{
|
|
_instance = this;
|
|
}
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
|
|
#region Try to load Settings for camera sensitivity
|
|
settingsPath = Application.persistentDataPath + "/settings.json";
|
|
if (File.Exists(settingsPath))
|
|
{
|
|
string json = File.ReadAllText(settingsPath);
|
|
settingsData = JsonUtility.FromJson<SettingsData>(json);
|
|
Debug.Log("Settings file exists");
|
|
shouldRumble = settingsData.shouldRumble;
|
|
Debug.Log("Settings for camera sensitivity loaded");
|
|
}
|
|
#endregion
|
|
}
|
|
|
|
public void StartRumble(float leftMotor, float rightMotor)
|
|
{
|
|
if (!UIManager.GetInstance().userInterfaceIsOnScreen)
|
|
{
|
|
if (shouldRumble)
|
|
{
|
|
if (InputControlManager.Getinstance().IsUsingJoystick && !IsVibrating)
|
|
{
|
|
GamePad.SetVibration(playerIndex, leftMotor, rightMotor);
|
|
IsVibrating = true;
|
|
}
|
|
else if (!InputControlManager.Getinstance().IsUsingJoystick && IsVibrating) //If not using joystick:
|
|
{
|
|
GamePad.SetVibration(playerIndex, 0f, 0f);
|
|
IsVibrating = false;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
StopRumble();
|
|
}
|
|
}
|
|
|
|
public void RumbleUpdate(float leftMotor, float rightMotor)
|
|
{
|
|
isUpdatingRumble = true;
|
|
StartCoroutine(UpdateRumble(leftMotor, rightMotor));
|
|
}
|
|
|
|
IEnumerator UpdateRumble(float leftMotor, float rightMotor)
|
|
{
|
|
while (isUpdatingRumble)
|
|
{
|
|
if (!UIManager.GetInstance().userInterfaceIsOnScreen)
|
|
{
|
|
if (shouldRumble)
|
|
{
|
|
if (InputControlManager.Getinstance().IsUsingJoystick && !IsVibrating)
|
|
{
|
|
GamePad.SetVibration(playerIndex, leftMotor, rightMotor);
|
|
IsVibrating = true;
|
|
}
|
|
else if (!InputControlManager.Getinstance().IsUsingJoystick && IsVibrating) //If not using joystick:
|
|
{
|
|
GamePad.SetVibration(playerIndex, 0f, 0f);
|
|
IsVibrating = false;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
StopRumble();
|
|
}
|
|
yield return null;
|
|
}
|
|
yield return null;
|
|
|
|
}
|
|
|
|
public void StopRumble()
|
|
{
|
|
print("Stop controller rumble");
|
|
GamePad.SetVibration(playerIndex, 0f, 0f);
|
|
IsVibrating = false;
|
|
}
|
|
|
|
public void StartHeartBeatRumble(float sanityDropTarget)
|
|
{
|
|
if (!heartBeatIsPlaying)
|
|
{
|
|
StartCoroutine(HeartBeatRumble(sanityDropTarget));
|
|
heartBeatIsPlaying = true;
|
|
}
|
|
}
|
|
|
|
IEnumerator HeartBeatRumble(float sanityDropTarget)
|
|
{
|
|
while (Sanity.GetInstance().currentSanity >= sanityDropTarget)
|
|
{
|
|
if (!UIManager.GetInstance().userInterfaceIsOnScreen)
|
|
{
|
|
if (shouldRumble)
|
|
{
|
|
if (InputControlManager.Getinstance().IsUsingJoystick)
|
|
{
|
|
if (rumbleHeatBeatRythmCoroutineIsPlaying == false)
|
|
{
|
|
RumbleHeartbeatRythmCoroutine = StartCoroutine(RumbleHeartbeatRythm(sanityDropTarget));
|
|
rumbleHeatBeatRythmCoroutineIsPlaying = true;
|
|
}
|
|
}
|
|
else if (!InputControlManager.Getinstance().IsUsingJoystick) //If not using joystick:
|
|
{
|
|
if (RumbleHeartbeatRythmCoroutine != null)
|
|
{
|
|
StopCoroutine(RumbleHeartbeatRythmCoroutine);
|
|
RumbleHeartbeatRythmCoroutine = null;
|
|
rumbleHeatBeatRythmCoroutineIsPlaying = false;
|
|
}
|
|
GamePad.SetVibration(playerIndex, 0f, 0f);
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (InputControlManager.Getinstance().IsUsingJoystick)
|
|
{
|
|
if (RumbleHeartbeatRythmCoroutine != null)
|
|
{
|
|
StopCoroutine(RumbleHeartbeatRythmCoroutine);
|
|
RumbleHeartbeatRythmCoroutine = null;
|
|
rumbleHeatBeatRythmCoroutineIsPlaying = false;
|
|
}
|
|
StopRumble();
|
|
}
|
|
}
|
|
yield return null;
|
|
}
|
|
yield return null;
|
|
heartBeatIsPlaying = false;
|
|
if (RumbleHeartbeatRythmCoroutine != null)
|
|
{
|
|
StopCoroutine(RumbleHeartbeatRythmCoroutine);
|
|
RumbleHeartbeatRythmCoroutine = null;
|
|
}
|
|
rumbleHeatBeatRythmCoroutineIsPlaying = false;
|
|
}
|
|
|
|
IEnumerator RumbleHeartbeatRythm(float sanityDropTarget)
|
|
{
|
|
while (Sanity.GetInstance().currentSanity >= sanityDropTarget)
|
|
{
|
|
GamePad.SetVibration(playerIndex, 0.2f, 0.4f);
|
|
yield return new WaitForSeconds(0.3f);
|
|
GamePad.SetVibration(playerIndex, 0f, 0f);
|
|
yield return new WaitForSeconds(0.1f);
|
|
GamePad.SetVibration(playerIndex, 0.3f, 0.2f);
|
|
yield return new WaitForSeconds(0.4f);
|
|
GamePad.SetVibration(playerIndex, 0f, 0f);
|
|
yield return new WaitForSeconds(0.2f);
|
|
yield return null;
|
|
}
|
|
yield return null;
|
|
}
|
|
}
|