Files

71 lines
1.9 KiB
C#
Raw Permalink Normal View History

2021-04-24 02:47:17 +02:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
namespace EvilSatellites
{
public class VibrationManager
{
private PlayerIndex playerIndex;
List<Vibration> vibrations = new List<Vibration>();
public VibrationManager(PlayerIndex playerIndex)
{
this.playerIndex = playerIndex;
}
public void AddVibration(float leftMotor, float rightMotor, float durationMS)
{
2021-04-24 14:34:15 +02:00
if (GameSettings.UseForceFeedback)
{
vibrations.Add(new Vibration(leftMotor, rightMotor, durationMS));
}
2021-04-24 02:47:17 +02:00
}
public void Update(float msElapsed)
{
2021-04-24 14:34:15 +02:00
if (!GameSettings.UseForceFeedback) return;
2021-04-24 02:47:17 +02:00
List<Vibration> toDelete = new List<Vibration>();
foreach (Vibration vibration in vibrations)
{
vibration.MSLeft -= msElapsed;
if (vibration.MSLeft < 0.0f)
{
toDelete.Add(vibration);
}
}
foreach (Vibration vibration in toDelete)
{
vibrations.Remove(vibration);
}
float leftMotor;
float rightMotor;
GetVibration(out leftMotor, out rightMotor);
GamePad.SetVibration(playerIndex, leftMotor, rightMotor);
}
public void GetVibration(out float leftMotor, out float rightMotor)
{
leftMotor = 0.0f;
rightMotor = 0.0f;
foreach (Vibration vibration in vibrations)
{
leftMotor = Math.Max(leftMotor, vibration.LeftMotor);
rightMotor = Math.Max(rightMotor, vibration.RightMotor);
}
}
}
}