implement different kinds of power ups (two bullet types not implemented yet)
This commit is contained in:
parent
a5a3ca3013
commit
21f7d1d5f4
|
@ -1,3 +1,5 @@
|
||||||
|
using System.Diagnostics;
|
||||||
|
|
||||||
namespace TanksServer.GameLogic;
|
namespace TanksServer.GameLogic;
|
||||||
|
|
||||||
internal sealed class CollectPowerUp(
|
internal sealed class CollectPowerUp(
|
||||||
|
@ -22,14 +24,31 @@ internal sealed class CollectPowerUp(
|
||||||
|
|
||||||
// now the tank overlaps the power up by at least 0.5 tiles
|
// now the tank overlaps the power up by at least 0.5 tiles
|
||||||
|
|
||||||
tank.Magazine = tank.Magazine with
|
switch (obj.Type)
|
||||||
{
|
{
|
||||||
UsedBullets = 0,
|
case PowerUpType.MagazineTypeUpgrade:
|
||||||
Type = tank.Magazine.Type | MagazineType.Explosive
|
if (obj.MagazineType == null)
|
||||||
};
|
throw new UnreachableException();
|
||||||
|
|
||||||
if (tank.ReloadingUntil >= DateTime.Now)
|
tank.Magazine = tank.Magazine with
|
||||||
tank.ReloadingUntil = DateTime.Now;
|
{
|
||||||
|
Type = tank.Magazine.Type | obj.MagazineType.Value,
|
||||||
|
UsedBullets = 0
|
||||||
|
};
|
||||||
|
|
||||||
|
if (tank.ReloadingUntil >= DateTime.Now)
|
||||||
|
tank.ReloadingUntil = DateTime.Now;
|
||||||
|
|
||||||
|
break;
|
||||||
|
case PowerUpType.MagazineSizeUpgrade:
|
||||||
|
tank.Magazine = tank.Magazine with
|
||||||
|
{
|
||||||
|
MaxBullets = (byte)int.Clamp(tank.Magazine.MaxBullets + 1, 1, 32)
|
||||||
|
};
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
tank.Owner.Scores.PowerUpsCollected++;
|
tank.Owner.Scores.PowerUpsCollected++;
|
||||||
return true;
|
return true;
|
||||||
|
|
|
@ -46,7 +46,12 @@ internal sealed class MapEntityManager(
|
||||||
logger.LogInformation("Tank added for player {}", player.Name);
|
logger.LogInformation("Tank added for player {}", player.Name);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SpawnPowerUp() => _powerUps.Add(new PowerUp(ChooseSpawnPosition()));
|
public void SpawnPowerUp(PowerUpType type, MagazineType? magazineType) => _powerUps.Add(new PowerUp
|
||||||
|
{
|
||||||
|
Position = ChooseSpawnPosition(),
|
||||||
|
Type = type,
|
||||||
|
MagazineType = magazineType
|
||||||
|
});
|
||||||
|
|
||||||
public void RemoveWhere(Predicate<PowerUp> predicate) => _powerUps.RemoveWhere(predicate);
|
public void RemoveWhere(Predicate<PowerUp> predicate) => _powerUps.RemoveWhere(predicate);
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
using System.Diagnostics;
|
||||||
|
|
||||||
namespace TanksServer.GameLogic;
|
namespace TanksServer.GameLogic;
|
||||||
|
|
||||||
internal sealed class SpawnPowerUp(
|
internal sealed class SpawnPowerUp(
|
||||||
|
@ -15,7 +17,25 @@ internal sealed class SpawnPowerUp(
|
||||||
if (Random.Shared.NextDouble() > _spawnChance * delta.TotalSeconds)
|
if (Random.Shared.NextDouble() > _spawnChance * delta.TotalSeconds)
|
||||||
return ValueTask.CompletedTask;
|
return ValueTask.CompletedTask;
|
||||||
|
|
||||||
entityManager.SpawnPowerUp();
|
|
||||||
|
var type = Random.Shared.Next(10) < 3
|
||||||
|
? PowerUpType.MagazineSizeUpgrade
|
||||||
|
: PowerUpType.MagazineTypeUpgrade;
|
||||||
|
|
||||||
|
MagazineType? magazineType = type switch
|
||||||
|
{
|
||||||
|
PowerUpType.MagazineTypeUpgrade => Random.Shared.Next(0, 4) switch
|
||||||
|
{
|
||||||
|
0 => MagazineType.Fast,
|
||||||
|
1 => MagazineType.Explosive,
|
||||||
|
2 => MagazineType.Smart,
|
||||||
|
3 => MagazineType.Mine,
|
||||||
|
_ => throw new UnreachableException()
|
||||||
|
},
|
||||||
|
_ => null
|
||||||
|
};
|
||||||
|
|
||||||
|
entityManager.SpawnPowerUp(type, magazineType);
|
||||||
return ValueTask.CompletedTask;
|
return ValueTask.CompletedTask;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@ namespace TanksServer.Models;
|
||||||
|
|
||||||
internal interface IMapEntity
|
internal interface IMapEntity
|
||||||
{
|
{
|
||||||
FloatPosition Position { get; set; }
|
FloatPosition Position { get; }
|
||||||
|
|
||||||
PixelBounds Bounds { get; }
|
PixelBounds Bounds { get; }
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,9 +2,19 @@ using TanksServer.GameLogic;
|
||||||
|
|
||||||
namespace TanksServer.Models;
|
namespace TanksServer.Models;
|
||||||
|
|
||||||
internal sealed class PowerUp(FloatPosition position): IMapEntity
|
internal enum PowerUpType
|
||||||
{
|
{
|
||||||
public FloatPosition Position { get; set; } = position;
|
MagazineTypeUpgrade,
|
||||||
|
MagazineSizeUpgrade
|
||||||
|
}
|
||||||
|
|
||||||
|
internal sealed class PowerUp: IMapEntity
|
||||||
|
{
|
||||||
|
public required FloatPosition Position { get; init; }
|
||||||
|
|
||||||
public PixelBounds Bounds => Position.GetBoundsForCenter(MapService.TileSize);
|
public PixelBounds Bounds => Position.GetBoundsForCenter(MapService.TileSize);
|
||||||
|
|
||||||
|
public required PowerUpType Type { get; init; }
|
||||||
|
|
||||||
|
public MagazineType? MagazineType { get; init; }
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue