2024-05-13 01:23:34 +02:00
|
|
|
using ServicePoint2;
|
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-04-07 20:16:22 +02:00
|
|
|
) : ITickStep
|
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-05-13 01:23:34 +02:00
|
|
|
private PixelGrid _lastObserverPixelGrid = PixelGrid.New(MapService.PixelsPerRow, MapService.PixelsPerColumn);
|
2024-04-30 23:49:39 +02:00
|
|
|
private GamePixelGrid _gamePixelGrid = new(MapService.PixelsPerRow, MapService.PixelsPerColumn);
|
2024-05-13 01:23:34 +02:00
|
|
|
private PixelGrid _observerPixelGrid = PixelGrid.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-05-13 01:23:34 +02:00
|
|
|
if (_observerPixelGrid.Data.SequenceEqual(_lastObserverPixelGrid.Data))
|
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-04-30 23:49:39 +02:00
|
|
|
private void Draw(GamePixelGrid gamePixelGrid, PixelGrid observerPixelGrid)
|
|
|
|
{
|
|
|
|
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-04-15 20:34:23 +02:00
|
|
|
for (var y = 0; y < MapService.PixelsPerColumn; y++)
|
|
|
|
for (var x = 0; x < MapService.PixelsPerRow; x++)
|
|
|
|
{
|
2024-04-30 23:49:39 +02:00
|
|
|
if (gamePixelGrid[x, y].EntityType.HasValue)
|
2024-04-28 12:53:18 +02:00
|
|
|
observerPixelGrid[(ushort)x, (ushort)y] = true;
|
2024-04-15 20:34:23 +02:00
|
|
|
}
|
2024-04-07 13:02:49 +02:00
|
|
|
}
|
|
|
|
}
|