bullet hits tank, tank dies
This commit is contained in:
		
							parent
							
								
									190c0c3143
								
							
						
					
					
						commit
						dd33ec59ad
					
				
					 6 changed files with 45 additions and 9 deletions
				
			
		|  | @ -34,7 +34,7 @@ internal sealed class TankDrawer : IDrawStep | |||
|         foreach (var tank in _tanks) | ||||
|         { | ||||
|             var pos = tank.Position.ToPixelPosition(); | ||||
|             var rotationVariant = (int)Math.Floor(tank.Rotation); | ||||
|             var rotationVariant = (int)Math.Round(tank.Rotation) % 16; | ||||
|             for (var dy = 0; dy < MapService.TileSize; dy++) | ||||
|             { | ||||
|                 var rowStartIndex = (pos.Y + dy) * MapService.PixelsPerRow; | ||||
|  |  | |||
|  | @ -2,7 +2,7 @@ namespace TanksServer.Models; | |||
| 
 | ||||
| internal sealed class Bullet(Player tankOwner, FloatPosition position, double rotation) | ||||
| { | ||||
|     public Player TankOwner { get; } = tankOwner; | ||||
|     public Player Owner { get; } = tankOwner; | ||||
|      | ||||
|     public FloatPosition Position { get; set; } = position; | ||||
|      | ||||
|  |  | |||
|  | @ -7,6 +7,10 @@ internal sealed class Player(string name) | |||
|     public Guid Id { get; } = Guid.NewGuid(); | ||||
| 
 | ||||
|     public PlayerControls Controls { get; } = new(); | ||||
| 
 | ||||
|     public int Kills { get; set; } | ||||
|      | ||||
|     public int Deaths { get; set; } | ||||
| } | ||||
| 
 | ||||
| internal sealed class PlayerControls | ||||
|  |  | |||
|  | @ -1,3 +1,5 @@ | |||
| using TanksServer.Services; | ||||
| 
 | ||||
| namespace TanksServer.Models; | ||||
| 
 | ||||
| internal sealed class Tank(Player player, FloatPosition spawnPosition) | ||||
|  | @ -20,4 +22,13 @@ internal sealed class Tank(Player player, FloatPosition spawnPosition) | |||
|     public DateTime NextShotAfter { get; set; } | ||||
| 
 | ||||
|     public bool Moved { get; set; } | ||||
| 
 | ||||
|     public (FloatPosition TopLeft, FloatPosition BottomRight) GetBounds() | ||||
|     { | ||||
|         const int halfTile = MapService.TileSize / 2; | ||||
|         return ( | ||||
|             new FloatPosition(Position.X - halfTile, Position.Y - halfTile), | ||||
|             new FloatPosition(Position.X + halfTile, Position.Y + halfTile) | ||||
|         ); | ||||
|     } | ||||
| } | ||||
|  |  | |||
|  | @ -6,14 +6,20 @@ namespace TanksServer.Services; | |||
| 
 | ||||
| internal sealed class TankManager(ILogger<TankManager> logger) : IEnumerable<Tank> | ||||
| { | ||||
|     private readonly ConcurrentBag<Tank> _tanks = new(); | ||||
|     private readonly ConcurrentDictionary<Tank, byte> _tanks = new(); | ||||
| 
 | ||||
|     public void Add(Tank tank) | ||||
|     { | ||||
|         logger.LogInformation("Tank added for player {}", tank.Owner.Id); | ||||
|         _tanks.Add(tank); | ||||
|         _tanks.TryAdd(tank, 0); | ||||
|     } | ||||
| 
 | ||||
|     IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); | ||||
|     public IEnumerator<Tank> GetEnumerator() => _tanks.GetEnumerator(); | ||||
|     public IEnumerator<Tank> GetEnumerator() => _tanks.Keys.GetEnumerator(); | ||||
| 
 | ||||
|     public void Remove(Tank tank) | ||||
|     { | ||||
|         logger.LogInformation("Tank removed for player {}", tank.Owner.Id); | ||||
|         _tanks.Remove(tank, out _); | ||||
|     } | ||||
| } | ||||
|  |  | |||
|  | @ -3,7 +3,7 @@ using TanksServer.Services; | |||
| 
 | ||||
| namespace TanksServer.TickSteps; | ||||
| 
 | ||||
| internal sealed class CollideBulletsWithTanks(BulletManager bullets) : ITickStep | ||||
| internal sealed class CollideBulletsWithTanks(BulletManager bullets, TankManager tanks) : ITickStep | ||||
| { | ||||
|     public Task TickAsync() | ||||
|     { | ||||
|  | @ -13,6 +13,21 @@ internal sealed class CollideBulletsWithTanks(BulletManager bullets) : ITickStep | |||
| 
 | ||||
|     private bool BulletHitsTank(Bullet bullet) | ||||
|     { | ||||
|         return false; // TODO | ||||
|         foreach (var tank in tanks) | ||||
|         { | ||||
|             var (topLeft, bottomRight) = tank.GetBounds(); | ||||
|             if (bullet.Position.X <= topLeft.X || bullet.Position.X >= bottomRight.X || | ||||
|                 bullet.Position.Y <= topLeft.Y || bullet.Position.Y >= bottomRight.Y) | ||||
|                 continue; | ||||
| 
 | ||||
|             if (bullet.Owner != tank.Owner) | ||||
|                 bullet.Owner.Kills++; | ||||
|             tank.Owner.Deaths++; | ||||
|              | ||||
|             tanks.Remove(tank); | ||||
|             return true; | ||||
|         } | ||||
| 
 | ||||
|         return false; | ||||
|     } | ||||
| } | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Vinzenz Schroeter
						Vinzenz Schroeter