move upgrades to tank, serialize objects directly
This commit is contained in:
parent
b1df817ece
commit
827b3a9330
16 changed files with 135 additions and 180 deletions
|
@ -2,19 +2,27 @@ using System.Diagnostics;
|
|||
|
||||
namespace TanksServer.GameLogic;
|
||||
|
||||
internal sealed class CollectPowerUp(
|
||||
MapEntityManager entityManager
|
||||
) : ITickStep
|
||||
internal sealed class CollectPowerUp : ITickStep
|
||||
{
|
||||
private readonly Predicate<PowerUp> _collectPredicate = b => TryCollect(b, entityManager.Tanks);
|
||||
private readonly Predicate<PowerUp> _collectPredicate;
|
||||
private readonly GameRules _rules;
|
||||
private readonly MapEntityManager _entityManager;
|
||||
|
||||
public CollectPowerUp(MapEntityManager entityManager,
|
||||
IOptions<GameRules> options)
|
||||
{
|
||||
_entityManager = entityManager;
|
||||
_rules = options.Value;
|
||||
_collectPredicate = b => TryCollect(b, entityManager.Tanks);
|
||||
}
|
||||
|
||||
public ValueTask TickAsync(TimeSpan delta)
|
||||
{
|
||||
entityManager.RemoveWhere(_collectPredicate);
|
||||
_entityManager.RemoveWhere(_collectPredicate);
|
||||
return ValueTask.CompletedTask;
|
||||
}
|
||||
|
||||
private static bool TryCollect(PowerUp powerUp, IEnumerable<Tank> tanks)
|
||||
private bool TryCollect(PowerUp powerUp, IEnumerable<Tank> tanks)
|
||||
{
|
||||
var position = powerUp.Position;
|
||||
foreach (var tank in tanks)
|
||||
|
@ -34,32 +42,38 @@ internal sealed class CollectPowerUp(
|
|||
return false;
|
||||
}
|
||||
|
||||
private static void ApplyPowerUpEffect(PowerUp powerUp, Tank tank)
|
||||
private void ApplyPowerUpEffect(PowerUp powerUp, Tank tank)
|
||||
{
|
||||
switch (powerUp.Type)
|
||||
{
|
||||
case PowerUpType.MagazineType:
|
||||
if (powerUp.MagazineType == null)
|
||||
throw new UnreachableException();
|
||||
|
||||
tank.Magazine = tank.Magazine with
|
||||
{
|
||||
Type = tank.Magazine.Type | powerUp.MagazineType.Value,
|
||||
UsedBullets = 0
|
||||
};
|
||||
|
||||
if (tank.ReloadingUntil >= DateTime.Now)
|
||||
tank.ReloadingUntil = DateTime.Now;
|
||||
|
||||
break;
|
||||
case PowerUpType.MagazineSize:
|
||||
tank.Magazine = tank.Magazine with
|
||||
tank.MaxBullets = (byte)int.Clamp(tank.MaxBullets + 1, 1, 32);
|
||||
break;
|
||||
|
||||
case PowerUpType.BulletAcceleration:
|
||||
tank.BulletStats = tank.BulletStats with
|
||||
{
|
||||
MaxBullets = (byte)int.Clamp(tank.Magazine.MaxBullets + 1, 1, 32)
|
||||
Acceleration = tank.BulletStats.Acceleration * _rules.BulletAccelerationUpgradeStrength
|
||||
};
|
||||
break;
|
||||
|
||||
case PowerUpType.ExplosiveBullets:
|
||||
tank.BulletStats = tank.BulletStats with { Explosive = true };
|
||||
break;
|
||||
|
||||
case PowerUpType.SmartBullets:
|
||||
tank.BulletStats = tank.BulletStats with { Smart = true };
|
||||
break;
|
||||
|
||||
case PowerUpType.BulletSpeed:
|
||||
tank.BulletStats = tank.BulletStats with
|
||||
{
|
||||
Speed = tank.BulletStats.Speed * _rules.BulletSpeedUpgradeStrength
|
||||
};
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new UnreachableException();
|
||||
throw new NotImplementedException($"unknown type {powerUp.Type}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -35,7 +35,7 @@ internal sealed class CollideBullets : ITickStep
|
|||
if (bullet.Timeout > DateTime.Now)
|
||||
return false;
|
||||
|
||||
ExplodeAt(bullet.Position.ToPixelPosition(), bullet.IsExplosive, bullet.Owner);
|
||||
ExplodeAt(bullet.Position.ToPixelPosition(), bullet.Stats.Explosive, bullet.Owner);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -45,7 +45,7 @@ internal sealed class CollideBullets : ITickStep
|
|||
if (!_map.Current.IsWall(pixel))
|
||||
return false;
|
||||
|
||||
ExplodeAt(pixel, bullet.IsExplosive, bullet.Owner);
|
||||
ExplodeAt(pixel, bullet.Stats.Explosive, bullet.Owner);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -55,7 +55,7 @@ internal sealed class CollideBullets : ITickStep
|
|||
if (hitTank == null)
|
||||
return false;
|
||||
|
||||
ExplodeAt(bullet.Position.ToPixelPosition(), bullet.IsExplosive, bullet.Owner);
|
||||
ExplodeAt(bullet.Position.ToPixelPosition(), bullet.Stats.Explosive, bullet.Owner);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -28,5 +28,7 @@ internal sealed class GameRules
|
|||
|
||||
public double SmartBulletInertia { get; set; } = 1;
|
||||
|
||||
public double FastBulletAcceleration { get; set; } = 0.25;
|
||||
public double BulletAccelerationUpgradeStrength { get; set; } = 0.1;
|
||||
|
||||
public double BulletSpeedUpgradeStrength { get; set; } = 0.1;
|
||||
}
|
||||
|
|
|
@ -15,19 +15,17 @@ internal sealed class MapEntityManager(
|
|||
public IEnumerable<Tank> Tanks => _playerTanks.Values;
|
||||
public IEnumerable<PowerUp> PowerUps => _powerUps;
|
||||
|
||||
public void SpawnBullet(Player tankOwner, FloatPosition position, double rotation, MagazineType type)
|
||||
public void SpawnBullet(Player tankOwner, FloatPosition position, double rotation, BulletStats stats)
|
||||
{
|
||||
_bullets.Add(new Bullet
|
||||
{
|
||||
Owner = tankOwner,
|
||||
Position = position,
|
||||
Rotation = rotation,
|
||||
IsExplosive = type.HasFlag(MagazineType.Explosive),
|
||||
Timeout = DateTime.Now + _bulletTimeout,
|
||||
OwnerCollisionAfter = DateTime.Now + TimeSpan.FromSeconds(1),
|
||||
Speed = _rules.BulletSpeed,
|
||||
IsSmart = type.HasFlag(MagazineType.Smart),
|
||||
Acceleration = type.HasFlag(MagazineType.Fast) ? _rules.FastBulletAcceleration : 0d
|
||||
Stats = stats
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -35,24 +33,23 @@ internal sealed class MapEntityManager(
|
|||
|
||||
public void SpawnTank(Player player, FloatPosition position)
|
||||
{
|
||||
var tank = new Tank
|
||||
var tank = new Tank(player)
|
||||
{
|
||||
Owner = player,
|
||||
Position = position,
|
||||
Rotation = Random.Shared.NextDouble(),
|
||||
Magazine = new Magazine(MagazineType.Basic, 0, _rules.MagazineSize)
|
||||
MaxBullets = _rules.MagazineSize,
|
||||
BulletStats =new BulletStats(_rules.BulletSpeed, 0, false, false)
|
||||
};
|
||||
_playerTanks[player] = tank;
|
||||
logger.LogInformation("Tank added for player {}", player.Name);
|
||||
}
|
||||
|
||||
public void SpawnPowerUp(FloatPosition position, PowerUpType type, MagazineType? magazineType)
|
||||
public void SpawnPowerUp(FloatPosition position, PowerUpType type)
|
||||
{
|
||||
var powerUp = new PowerUp
|
||||
{
|
||||
Position = position,
|
||||
Type = type,
|
||||
MagazineType = magazineType
|
||||
Type = type
|
||||
};
|
||||
_powerUps.Add(powerUp);
|
||||
}
|
||||
|
|
|
@ -17,14 +17,15 @@ internal sealed class MoveBullets(
|
|||
|
||||
private void MoveBullet(Bullet bullet, TimeSpan delta)
|
||||
{
|
||||
if (bullet.IsSmart && TryGetSmartRotation(bullet.Position, bullet.Owner, out var wantedRotation))
|
||||
if (bullet.Stats.Smart && TryGetSmartRotation(bullet.Position, bullet.Owner, out var wantedRotation))
|
||||
{
|
||||
var inertiaFactor = _smartBulletInertia * delta.TotalSeconds;
|
||||
var difference = wantedRotation - bullet.Rotation;
|
||||
bullet.Rotation += difference * inertiaFactor;
|
||||
}
|
||||
|
||||
bullet.Speed *= 1 + (bullet.Acceleration * delta.TotalSeconds);
|
||||
bullet.Speed = double.Clamp(bullet.Speed * (1 + (bullet.Stats.Acceleration * delta.TotalSeconds)), 0d,
|
||||
MapService.TileSize * 10);
|
||||
|
||||
var speed = bullet.Speed * delta.TotalSeconds;
|
||||
var angle = bullet.Rotation * 2 * Math.PI;
|
||||
|
|
|
@ -26,24 +26,17 @@ internal sealed class ShootFromTanks(
|
|||
if (tank.ReloadingUntil >= now)
|
||||
return;
|
||||
|
||||
if (tank.Magazine.Empty)
|
||||
if (tank.UsedBullets >= tank.MaxBullets)
|
||||
{
|
||||
tank.ReloadingUntil = now.AddMilliseconds(_config.ReloadDelayMs);
|
||||
tank.Magazine = tank.Magazine with
|
||||
{
|
||||
UsedBullets = 0,
|
||||
Type = MagazineType.Basic
|
||||
};
|
||||
tank.UsedBullets = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
tank.NextShotAfter = now.AddMilliseconds(_config.ShootDelayMs);
|
||||
tank.Magazine = tank.Magazine with
|
||||
{
|
||||
UsedBullets = (byte)(tank.Magazine.UsedBullets + 1)
|
||||
};
|
||||
tank.UsedBullets++;
|
||||
|
||||
tank.Owner.Scores.ShotsFired++;
|
||||
entityManager.SpawnBullet(tank.Owner, tank.Position, tank.Orientation / 16d, tank.Magazine.Type);
|
||||
entityManager.SpawnBullet(tank.Owner, tank.Position, tank.Orientation / 16d, tank.BulletStats);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,25 +18,9 @@ internal sealed class SpawnPowerUp(
|
|||
if (Random.Shared.NextDouble() > _spawnChance * delta.TotalSeconds)
|
||||
return ValueTask.CompletedTask;
|
||||
|
||||
|
||||
var type = Random.Shared.Next(4) == 0
|
||||
? PowerUpType.MagazineSize
|
||||
: PowerUpType.MagazineType;
|
||||
|
||||
MagazineType? magazineType = type switch
|
||||
{
|
||||
PowerUpType.MagazineType => Random.Shared.Next(0, 3) switch
|
||||
{
|
||||
0 => MagazineType.Fast,
|
||||
1 => MagazineType.Explosive,
|
||||
2 => MagazineType.Smart,
|
||||
_ => throw new UnreachableException()
|
||||
},
|
||||
_ => null
|
||||
};
|
||||
|
||||
var type = (PowerUpType)Random.Shared.Next((int)Enum.GetValues<PowerUpType>().Max());
|
||||
var position = emptyTileFinder.ChooseEmptyTile().GetCenter().ToFloatPosition();
|
||||
entityManager.SpawnPowerUp(position, type, magazineType);
|
||||
entityManager.SpawnPowerUp(position, type);
|
||||
return ValueTask.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue