servicepoint-tanks/tanks-backend/TanksServer/Graphics/GeneratePixelsTickStep.cs

53 lines
1.9 KiB
C#
Raw Permalink Normal View History

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
internal sealed class GeneratePixelsTickStep(
IEnumerable<IDrawStep> drawSteps,
IEnumerable<IFrameConsumer> consumers
) : ITickStep, IDisposable
2024-04-07 13:02:49 +02:00
{
private GamePixelGrid _lastGamePixelGrid = new(MapService.PixelsPerRow, MapService.PixelsPerColumn);
private Bitmap _lastObserverPixelGrid = new(MapService.PixelsPerRow, MapService.PixelsPerColumn);
private GamePixelGrid _gamePixelGrid = new(MapService.PixelsPerRow, MapService.PixelsPerColumn);
private Bitmap _observerPixelGrid = new(MapService.PixelsPerRow, MapService.PixelsPerColumn);
private readonly List<IDrawStep> _drawSteps = drawSteps.ToList();
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
{
Draw(_gamePixelGrid, _observerPixelGrid);
if (_observerPixelGrid.Equals(_lastObserverPixelGrid))
return;
2024-05-03 14:45:41 +02:00
await _consumers.Select(c => c.OnFrameDoneAsync(_gamePixelGrid, _observerPixelGrid))
.WhenAll();
(_lastGamePixelGrid, _gamePixelGrid) = (_gamePixelGrid, _lastGamePixelGrid);
(_lastObserverPixelGrid, _observerPixelGrid) = (_observerPixelGrid, _lastObserverPixelGrid);
}
2024-10-16 20:15:32 +02:00
private void Draw(GamePixelGrid gamePixelGrid, Bitmap observerPixelGrid)
{
gamePixelGrid.Clear();
2024-04-07 20:16:22 +02:00
foreach (var step in _drawSteps)
step.Draw(gamePixelGrid);
2024-05-13 01:23:34 +02:00
observerPixelGrid.Fill(false);
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
}
public void Dispose()
{
_lastObserverPixelGrid.Dispose();
_observerPixelGrid.Dispose();
}
2024-04-07 13:02:49 +02:00
}