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

48 lines
1.8 KiB
C#
Raw Normal View History

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
internal sealed class GeneratePixelsTickStep(
IEnumerable<IDrawStep> drawSteps,
IEnumerable<IFrameConsumer> consumers
2024-04-07 20:16:22 +02:00
) : ITickStep
2024-04-07 13:02:49 +02:00
{
private GamePixelGrid _lastGamePixelGrid = new(MapService.PixelsPerRow, MapService.PixelsPerColumn);
2024-10-16 20:15:32 +02:00
private Bitmap _lastObserverPixelGrid = Bitmap.New(MapService.PixelsPerRow, MapService.PixelsPerColumn);
private GamePixelGrid _gamePixelGrid = new(MapService.PixelsPerRow, MapService.PixelsPerColumn);
2024-10-16 20:15:32 +02:00
private Bitmap _observerPixelGrid = Bitmap.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);
2024-05-13 01:23:34 +02:00
if (_observerPixelGrid.Data.SequenceEqual(_lastObserverPixelGrid.Data))
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 (var y = 0; y < MapService.PixelsPerColumn; y++)
for (var x = 0; x < MapService.PixelsPerRow; x++)
{
if (gamePixelGrid[x, y].EntityType.HasValue)
observerPixelGrid[(ushort)x, (ushort)y] = true;
}
2024-04-07 13:02:49 +02:00
}
}