implement different kinds of power ups (two bullet types not implemented yet)
This commit is contained in:
		
							parent
							
								
									a5a3ca3013
								
							
						
					
					
						commit
						21f7d1d5f4
					
				
					 5 changed files with 65 additions and 11 deletions
				
			
		| 
						 | 
				
			
			@ -1,3 +1,5 @@
 | 
			
		|||
using System.Diagnostics;
 | 
			
		||||
 | 
			
		||||
namespace TanksServer.GameLogic;
 | 
			
		||||
 | 
			
		||||
internal sealed class CollectPowerUp(
 | 
			
		||||
| 
						 | 
				
			
			@ -22,15 +24,32 @@ internal sealed class CollectPowerUp(
 | 
			
		|||
 | 
			
		||||
            // now the tank overlaps the power up by at least 0.5 tiles
 | 
			
		||||
 | 
			
		||||
            switch (obj.Type)
 | 
			
		||||
            {
 | 
			
		||||
                case PowerUpType.MagazineTypeUpgrade:
 | 
			
		||||
                    if (obj.MagazineType == null)
 | 
			
		||||
                        throw new UnreachableException();
 | 
			
		||||
 | 
			
		||||
                    tank.Magazine = tank.Magazine with
 | 
			
		||||
                    {
 | 
			
		||||
                UsedBullets = 0,
 | 
			
		||||
                Type = tank.Magazine.Type | MagazineType.Explosive
 | 
			
		||||
                        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++;
 | 
			
		||||
            return true;
 | 
			
		||||
        }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -46,7 +46,12 @@ internal sealed class MapEntityManager(
 | 
			
		|||
        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);
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,3 +1,5 @@
 | 
			
		|||
using System.Diagnostics;
 | 
			
		||||
 | 
			
		||||
namespace TanksServer.GameLogic;
 | 
			
		||||
 | 
			
		||||
internal sealed class SpawnPowerUp(
 | 
			
		||||
| 
						 | 
				
			
			@ -15,7 +17,25 @@ internal sealed class SpawnPowerUp(
 | 
			
		|||
        if (Random.Shared.NextDouble() > _spawnChance * delta.TotalSeconds)
 | 
			
		||||
            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;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -2,7 +2,7 @@ namespace TanksServer.Models;
 | 
			
		|||
 | 
			
		||||
internal interface IMapEntity
 | 
			
		||||
{
 | 
			
		||||
    FloatPosition Position { get; set; }
 | 
			
		||||
    FloatPosition Position { get; }
 | 
			
		||||
 | 
			
		||||
    PixelBounds Bounds { get; }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -2,9 +2,19 @@ using TanksServer.GameLogic;
 | 
			
		|||
 | 
			
		||||
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 required PowerUpType Type { get; init; }
 | 
			
		||||
 | 
			
		||||
    public MagazineType? MagazineType { get; init; }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue