add explosive bullet power up

This commit is contained in:
Vinzenz Schroeter 2024-04-17 19:34:19 +02:00
parent 3f4a301993
commit a2d46bda92
30 changed files with 407 additions and 253 deletions

View file

@ -2,11 +2,11 @@ using TanksServer.GameLogic;
namespace TanksServer.Graphics;
internal sealed class DrawBulletsStep(BulletManager bullets) : IDrawStep
internal sealed class DrawBulletsStep(MapEntityManager entityManager) : IDrawStep
{
public void Draw(GamePixelGrid pixels)
{
foreach (var bullet in bullets.GetAll())
foreach (var bullet in entityManager.Bullets)
{
var position = bullet.Position.ToPixelPosition();
pixels[position.X, position.Y].EntityType = GamePixelEntityType.Bullet;

View file

@ -0,0 +1,52 @@
using System.Diagnostics;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using TanksServer.GameLogic;
namespace TanksServer.Graphics;
internal sealed class DrawPowerUpsStep : IDrawStep
{
private readonly MapEntityManager _entityManager;
private readonly bool?[,] _explosiveSprite;
public DrawPowerUpsStep(MapEntityManager entityManager)
{
_entityManager = entityManager;
using var tankImage = Image.Load<Rgba32>("assets/powerup_explosive.png");
Debug.Assert(tankImage.Width == tankImage.Height && tankImage.Width == MapService.TileSize);
_explosiveSprite = new bool?[tankImage.Width, tankImage.Height];
var whitePixel = new Rgba32(byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue);
for (var y = 0; y < tankImage.Height; y++)
for (var x = 0; x < tankImage.Width; x++)
{
var pixelValue = tankImage[x, y];
_explosiveSprite[x, y] = pixelValue.A == 0
? null
: pixelValue == whitePixel;
}
}
public void Draw(GamePixelGrid pixels)
{
foreach (var powerUp in _entityManager.PowerUps)
{
var position = powerUp.Bounds.TopLeft;
for (byte dy = 0; dy < MapService.TileSize; dy++)
for (byte dx = 0; dx < MapService.TileSize; dx++)
{
var pixelState = _explosiveSprite[dx, dy];
if (!pixelState.HasValue)
continue;
var (x, y) = position.GetPixelRelative(dx, dy);
pixels[x, y].EntityType = pixelState.Value
? GamePixelEntityType.PowerUp
: null;
}
}
}
}

View file

@ -6,13 +6,13 @@ namespace TanksServer.Graphics;
internal sealed class DrawTanksStep : IDrawStep
{
private readonly TankManager _tanks;
private readonly MapEntityManager _entityManager;
private readonly bool[] _tankSprite;
private readonly int _tankSpriteWidth;
public DrawTanksStep(TankManager tanks)
public DrawTanksStep(MapEntityManager entityManager)
{
_tanks = tanks;
_entityManager = entityManager;
using var tankImage = Image.Load<Rgba32>("assets/tank.png");
_tankSprite = new bool[tankImage.Height * tankImage.Width];
@ -28,7 +28,7 @@ internal sealed class DrawTanksStep : IDrawStep
public void Draw(GamePixelGrid pixels)
{
foreach (var tank in _tanks)
foreach (var tank in _entityManager.Tanks)
{
var tankPosition = tank.Bounds.TopLeft;

View file

@ -4,5 +4,6 @@ internal enum GamePixelEntityType : byte
{
Wall = 0x0,
Tank = 0x1,
Bullet = 0x2
Bullet = 0x2,
PowerUp = 0x3
}