Oprava spawnování monster, optimalizace v CommandProcessor a EventProcessor. Přesunutí některých tříd do vlastních namespaců, pročištění kódu, úpravy formátování, odstranění nepoužívaných souborů a zakomentovaného kódu
This commit is contained in:
parent
e5d746d597
commit
243f071a43
62 changed files with 873 additions and 1217 deletions
60
ONDServer/Enemies/OpportunityTimer.cs
Normal file
60
ONDServer/Enemies/OpportunityTimer.cs
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
using System.Diagnostics;
|
||||
|
||||
namespace ONDServer.Enemies;
|
||||
|
||||
public class OpportunityTimer {
|
||||
public int Interval{ get; set; }
|
||||
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 OpportunityTimer(int intervalMs, double movementChance) {
|
||||
Interval = intervalMs;
|
||||
MovementChance = movementChance;
|
||||
}
|
||||
|
||||
public OpportunityTimer(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;
|
||||
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