lots of staff

This commit is contained in:
2024-10-23 18:49:13 +02:00
parent ff23702c15
commit a98a463531
8 changed files with 216 additions and 25 deletions
+49 -16
View File
@@ -1,10 +1,10 @@
using model;
using System.Collections.ObjectModel;
using System.IO.Pipes;
using System.Numerics;
using System.Diagnostics;
namespace Snake.Model;
public class SnakeLevel{
public class SnakeLevel : IDisposable{
public static readonly TimeSpan TickTimeSpan = TimeSpan.FromMilliseconds(500);
public int Size {get; }
@@ -43,8 +43,7 @@ public class SnakeLevel{
public int NewEggRound { get; }
public int EggLimit { get; }
private int _round;
private PeriodicTimer _timer;
private ITimer timer;
public int Score { get; private set; }
public event EventHandler? GameUpdate;
@@ -57,12 +56,18 @@ public class SnakeLevel{
{
_state = value;
GameStateUpdate?.Invoke(this, _state);
if(_state == GameState.Dead)
{
timer.Change(Timeout.InfiniteTimeSpan, TickTimeSpan);
}
}
}
private SnakeDirection LastDirection = SnakeDirection.Down;
private SnakeDirection _SnakeHeadDirection;
private bool disposedValue;
public SnakeDirection SnakeHeadDirection
{
get => _SnakeHeadDirection;
@@ -96,11 +101,17 @@ public class SnakeLevel{
_SnakeHeadDirection = SnakeDirection.Down;
LastDirection = SnakeDirection.Down;
_timer = new PeriodicTimer(TickTimeSpan, time_provider);
timer = time_provider.CreateTimer(new TimerCallback(_timer_Tick), null, Timeout.InfiniteTimeSpan, TickTimeSpan);
_state = GameState.Paused;
}
private void _timer_Tick(object? sender)
{
Tick();
GameUpdate?.Invoke(this, EventArgs.Empty);
}
public SnakeLevel(int size, IEnumerable<Point> obstacles, int snake_start_length, int new_egg_round, int egg_limit) : this(size, obstacles, snake_start_length, new_egg_round, egg_limit, TimeProvider.System)
{
}
@@ -187,21 +198,12 @@ public class SnakeLevel{
LastDirection = SnakeHeadDirection;
}
private async Task TickerTask()
{
while(State == GameState.Running && await _timer.WaitForNextTickAsync())
{
Tick();
GameUpdate?.Invoke(this, EventArgs.Empty);
}
}
public void Start()
{
if(State == GameState.Paused)
{
State = GameState.Running;
Task.Run(TickerTask);
timer.Change(TickTimeSpan, TickTimeSpan);
}
else
{
@@ -214,10 +216,41 @@ public class SnakeLevel{
if(State == GameState.Running)
{
State = GameState.Paused;
timer.Change(Timeout.InfiniteTimeSpan, TickTimeSpan);
}
else
{
throw new Exception("Game is not running");
}
}
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
// TODO: dispose managed state (managed objects)
timer.Dispose();
}
// TODO: free unmanaged resources (unmanaged objects) and override finalizer
// TODO: set large fields to null
disposedValue = true;
}
}
// // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
// ~SnakeLevel()
// {
// // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
// Dispose(disposing: false);
// }
public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
+70
View File
@@ -0,0 +1,70 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace model
{
/*internal class Timer : IDisposable
{
private PeriodicTimer _timer;
public event EventHandler<EventArgs>? Tick;
public Timer(TimeSpan tick, TimeProvider _time_provider)
{
_timer = new PeriodicTimer(tick, _time_provider);
}
private CancellationTokenSource? _cts;
private bool disposedValue;
private async Task TickerTask()
{
_cts = new CancellationTokenSource();
while (await _timer.WaitForNextTickAsync(_cts.Token))
{
Tick?.Invoke(this, EventArgs.Empty);
}
}
public void Start()
{
Task.Run(TickerTask);
}
public void Stop()
{
_cts?.Cancel();
}
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
// TODO: dispose managed state (managed objects)
_cts?.Dispose();
}
// TODO: free unmanaged resources (unmanaged objects) and override finalizer
// TODO: set large fields to null
disposedValue = true;
}
}
// // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
// ~Timer()
// {
// // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
// Dispose(disposing: false);
// }
public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}*/
}
+7
View File
@@ -6,4 +6,11 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="ELTE.FI.SARuleSet" Version="3.1.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>