2024-05-26 15:06:09 +02:00
|
|
|
using ServicePoint;
|
2024-04-10 19:25:45 +02:00
|
|
|
using TanksServer.GameLogic;
|
2024-05-03 14:45:41 +02:00
|
|
|
using TanksServer.Interactivity;
|
2024-04-07 13:02:49 +02:00
|
|
|
|
2024-04-10 19:25:45 +02:00
|
|
|
namespace TanksServer.Graphics;
|
2024-04-07 13:02:49 +02:00
|
|
|
|
2024-04-13 14:07:14 +02:00
|
|
|
internal sealed class GeneratePixelsTickStep(
|
|
|
|
IEnumerable<IDrawStep> drawSteps,
|
2024-04-15 20:34:23 +02:00
|
|
|
IEnumerable<IFrameConsumer> consumers
|
2024-10-19 16:07:41 +02:00
|
|
|
) : ITickStep, IDisposable
|
2024-04-07 13:02:49 +02:00
|
|
|
{
|
2024-04-30 23:49:39 +02:00
|
|
|
private GamePixelGrid _lastGamePixelGrid = new(MapService.PixelsPerRow, MapService.PixelsPerColumn);
|
2024-10-19 16:07:41 +02:00
|
|
|
private Bitmap _lastObserverPixelGrid = new(MapService.PixelsPerRow, MapService.PixelsPerColumn);
|
2024-04-30 23:49:39 +02:00
|
|
|
private GamePixelGrid _gamePixelGrid = new(MapService.PixelsPerRow, MapService.PixelsPerColumn);
|
2024-10-19 16:07:41 +02:00
|
|
|
private Bitmap _observerPixelGrid = new(MapService.PixelsPerRow, MapService.PixelsPerColumn);
|
2024-04-30 23:49:39 +02:00
|
|
|
|
2024-04-07 19:05:50 +02:00
|
|
|
private readonly List<IDrawStep> _drawSteps = drawSteps.ToList();
|
2024-04-15 20:34:23 +02:00
|
|
|
private readonly List<IFrameConsumer> _consumers = consumers.ToList();
|
2024-04-07 13:02:49 +02:00
|
|
|
|
2024-04-28 15:34:32 +02:00
|
|
|
public async ValueTask TickAsync(TimeSpan _)
|
2024-04-07 13:02:49 +02:00
|
|
|
{
|
2024-04-30 23:49:39 +02:00
|
|
|
Draw(_gamePixelGrid, _observerPixelGrid);
|
2024-11-12 18:27:04 +01:00
|
|
|
if (_observerPixelGrid.Equals(_lastObserverPixelGrid))
|
2024-04-30 23:49:39 +02:00
|
|
|
return;
|
|
|
|
|
2024-05-03 14:45:41 +02:00
|
|
|
await _consumers.Select(c => c.OnFrameDoneAsync(_gamePixelGrid, _observerPixelGrid))
|
|
|
|
.WhenAll();
|
2024-04-30 23:49:39 +02:00
|
|
|
|
|
|
|
(_lastGamePixelGrid, _gamePixelGrid) = (_gamePixelGrid, _lastGamePixelGrid);
|
|
|
|
(_lastObserverPixelGrid, _observerPixelGrid) = (_observerPixelGrid, _lastObserverPixelGrid);
|
|
|
|
}
|
2024-04-28 12:53:18 +02:00
|
|
|
|
2024-10-16 20:15:32 +02:00
|
|
|
private void Draw(GamePixelGrid gamePixelGrid, Bitmap observerPixelGrid)
|
2024-04-30 23:49:39 +02:00
|
|
|
{
|
|
|
|
gamePixelGrid.Clear();
|
2024-04-07 20:16:22 +02:00
|
|
|
foreach (var step in _drawSteps)
|
2024-04-30 23:49:39 +02:00
|
|
|
step.Draw(gamePixelGrid);
|
2024-04-15 20:34:23 +02:00
|
|
|
|
2024-05-13 01:23:34 +02:00
|
|
|
observerPixelGrid.Fill(false);
|
2024-11-12 18:27:04 +01:00
|
|
|
for (ulong y = 0; y < MapService.PixelsPerColumn; y++)
|
|
|
|
for (ulong x = 0; x < MapService.PixelsPerRow; x++)
|
|
|
|
{
|
|
|
|
if (gamePixelGrid[x, y].EntityType.HasValue)
|
|
|
|
observerPixelGrid.Set(x, y, true);
|
|
|
|
}
|
2024-04-07 13:02:49 +02:00
|
|
|
}
|
2024-10-19 16:07:41 +02:00
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
_lastObserverPixelGrid.Dispose();
|
|
|
|
_observerPixelGrid.Dispose();
|
|
|
|
}
|
2024-04-07 13:02:49 +02:00
|
|
|
}
|