2026-03-08 16:55:49 +01:00
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
|
|
|
|
namespace FNAF_Server.Enemies;
|
|
|
|
|
|
|
|
|
|
public class MovementOpportunity {
|
|
|
|
|
public int Interval{ get; set; }
|
|
|
|
|
// public double ChanceDenominator;
|
2026-03-17 20:14:29 +01:00
|
|
|
public double MovementChance{
|
|
|
|
|
get;
|
|
|
|
|
set{
|
|
|
|
|
field = value;
|
|
|
|
|
GuaranteeSuccess(value >= 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-03-08 16:55:49 +01:00
|
|
|
|
|
|
|
|
public bool Running => stopwatch.IsRunning;
|
|
|
|
|
|
|
|
|
|
private Stopwatch stopwatch = new();
|
2026-03-12 22:33:35 +01:00
|
|
|
private long stopwatchOffset = 0;
|
2026-03-08 16:55:49 +01:00
|
|
|
|
|
|
|
|
private Random random = new();
|
|
|
|
|
|
|
|
|
|
public MovementOpportunity(int intervalMs, double movementChance) {
|
|
|
|
|
Interval = intervalMs;
|
|
|
|
|
MovementChance = movementChance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public MovementOpportunity(int intervalMs) {
|
|
|
|
|
Interval = intervalMs;
|
|
|
|
|
MovementChance = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Start() {
|
|
|
|
|
stopwatch.Start();
|
|
|
|
|
}
|
|
|
|
|
public void Stop() {
|
|
|
|
|
stopwatch.Stop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Check() {
|
2026-03-12 22:33:35 +01:00
|
|
|
if (stopwatch.ElapsedMilliseconds - stopwatchOffset >= Interval){
|
|
|
|
|
int overshoot = (int)(stopwatch.ElapsedMilliseconds - stopwatchOffset - Interval);
|
|
|
|
|
stopwatchOffset = stopwatch.ElapsedMilliseconds - overshoot;
|
|
|
|
|
// stopwatch.Restart();
|
2026-03-08 16:55:49 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Roll() => roll();
|
|
|
|
|
private Func<bool> roll = () => true;
|
|
|
|
|
|
|
|
|
|
public bool CheckAndRoll() {
|
|
|
|
|
if (Check()) return Roll();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void GuaranteeSuccess(bool value) {
|
|
|
|
|
roll = value ? () => true : () => random.NextDouble() <= MovementChance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|