Projekt přejmenován. Neko nastaven na výchozí pozici
This commit is contained in:
parent
1a27dd6fab
commit
ceac37920b
104 changed files with 873 additions and 208 deletions
62
ONDServer/Enemies/MovementOpportunity.cs
Normal file
62
ONDServer/Enemies/MovementOpportunity.cs
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
using System.Diagnostics;
|
||||
|
||||
namespace ONDServer.Enemies;
|
||||
|
||||
public class MovementOpportunity {
|
||||
public int Interval{ get; set; }
|
||||
// public double ChanceDenominator;
|
||||
public double MovementChance{
|
||||
get;
|
||||
set{
|
||||
field = value;
|
||||
GuaranteeSuccess(value >= 1);
|
||||
}
|
||||
}
|
||||
|
||||
public bool Running => stopwatch.IsRunning;
|
||||
|
||||
private Stopwatch stopwatch = new();
|
||||
private long stopwatchOffset = 0;
|
||||
|
||||
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() {
|
||||
if (stopwatch.ElapsedMilliseconds - stopwatchOffset >= Interval){
|
||||
int overshoot = (int)(stopwatch.ElapsedMilliseconds - stopwatchOffset - Interval);
|
||||
stopwatchOffset = stopwatch.ElapsedMilliseconds - overshoot;
|
||||
// stopwatch.Restart();
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue