socket fixes, fix tank jumping
This commit is contained in:
		
							parent
							
								
									9620703efc
								
							
						
					
					
						commit
						b3dacbd6f6
					
				
					 13 changed files with 44 additions and 46 deletions
				
			
		|  | @ -52,7 +52,8 @@ public sealed class Cp437Grid(ushort width, ushort height) : IEquatable<Cp437Gri | |||
|     { | ||||
|         ReadOnlySpan<byte> valueBytes = stackalloc byte[] { b }; | ||||
|         Span<char> resultStr = stackalloc char[1]; | ||||
|         _encoding.GetChars(valueBytes, resultStr); | ||||
|         var consumed = _encoding.GetChars(valueBytes, resultStr); | ||||
|         Debug.Assert(consumed == 1); | ||||
|         return resultStr[0]; | ||||
|     } | ||||
| 
 | ||||
|  |  | |||
|  | @ -32,7 +32,7 @@ internal sealed class SpawnNewTanks( | |||
|             if (map.IsCurrentlyWall(tile)) | ||||
|                 continue; | ||||
| 
 | ||||
|             var tilePixelCenter = tile.GetPixelRelative(4, 4).ToFloatPosition(); | ||||
|             var tilePixelCenter = tile.ToPixelPosition().GetPixelRelative(4, 4).ToFloatPosition(); | ||||
| 
 | ||||
|             var minDistance = bullets.GetAll() | ||||
|                 .Cast<IMapEntity>() | ||||
|  |  | |||
|  | @ -8,6 +8,6 @@ internal sealed class DrawBulletsStep(BulletManager bullets) : IDrawStep | |||
|     public void Draw(PixelGrid buffer) | ||||
|     { | ||||
|         foreach (var position in bullets.GetAll().Select(b => b.Position.ToPixelPosition())) | ||||
|             buffer[position.X, position.Y] = true; | ||||
|             buffer[(ushort)position.X, (ushort)position.Y] = true; | ||||
|     } | ||||
| } | ||||
|  |  | |||
|  | @ -17,8 +17,8 @@ internal sealed class DrawMapStep(MapService map) : IDrawStep | |||
|             for (byte pixelInTileY = 0; pixelInTileY < MapService.TileSize; pixelInTileY++) | ||||
|             for (byte pixelInTileX = 0; pixelInTileX < MapService.TileSize; pixelInTileX++) | ||||
|             { | ||||
|                 var position = tile.GetPixelRelative(pixelInTileX, pixelInTileY); | ||||
|                 buffer[position.X, position.Y] = pixelInTileX % 2 == pixelInTileY % 2; | ||||
|                 var (x, y) = tile.ToPixelPosition().GetPixelRelative(pixelInTileX, pixelInTileY); | ||||
|                 buffer[(ushort)x, (ushort)y] = pixelInTileX % 2 == pixelInTileY % 2; | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|  |  | |||
|  | @ -40,8 +40,8 @@ internal sealed class DrawTanksStep : IDrawStep | |||
|                 if (!TankSpriteAt(dx, dy, orientation)) | ||||
|                     continue; | ||||
| 
 | ||||
|                 var position = tankPosition.GetPixelRelative(dx, dy); | ||||
|                 buffer[position.X, position.Y] = true; | ||||
|                 var (x, y) = tankPosition.GetPixelRelative(dx, dy); | ||||
|                 buffer[(ushort)x, (ushort)y] = true; | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|  |  | |||
|  | @ -18,7 +18,7 @@ internal sealed class ByteChannelWebSocket(WebSocket socket, ILogger logger, int | |||
|                 yield return _buffer.ToArray(); | ||||
|         } | ||||
| 
 | ||||
|         if (socket.State != WebSocketState.Closed) | ||||
|         if (socket.State is not WebSocketState.Closed and not WebSocketState.Aborted) | ||||
|             Debugger.Break(); | ||||
|     } | ||||
| 
 | ||||
|  |  | |||
|  | @ -93,9 +93,9 @@ internal sealed class ClientScreenServer( | |||
|                 await _channel.SendAsync(pixels.Data); | ||||
|                 _lastSentPixels = pixels; | ||||
|             } | ||||
|             catch (ChannelClosedException) | ||||
|             catch (WebSocketException ex) | ||||
|             { | ||||
|                 _logger.LogWarning("send failed, channel is closed"); | ||||
|                 _logger.LogWarning(ex, "send failed"); | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|  |  | |||
|  | @ -1,9 +1,11 @@ | |||
| using System.Diagnostics; | ||||
| using TanksServer.GameLogic; | ||||
| 
 | ||||
| namespace TanksServer.Models; | ||||
| 
 | ||||
| [DebuggerDisplay("({X} | {Y})")] | ||||
| internal readonly struct FloatPosition(double x, double y) | ||||
| { | ||||
|     public double X { get; } = (x + MapService.PixelsPerRow) % MapService.PixelsPerRow; | ||||
|     public double Y { get; } = (y + MapService.PixelsPerColumn) % MapService.PixelsPerColumn; | ||||
| } | ||||
| } | ||||
|  |  | |||
|  | @ -1,3 +1,6 @@ | |||
| using System.Diagnostics; | ||||
| 
 | ||||
| namespace TanksServer.Models; | ||||
| 
 | ||||
| [DebuggerDisplay("{TopLeft}, {BottomRight}")] | ||||
| internal record struct PixelBounds(PixelPosition TopLeft, PixelPosition BottomRight); | ||||
|  |  | |||
|  | @ -1,9 +1,17 @@ | |||
| using System.Diagnostics; | ||||
| using TanksServer.GameLogic; | ||||
| 
 | ||||
| namespace TanksServer.Models; | ||||
| 
 | ||||
| internal readonly struct PixelPosition(ushort x, ushort y) | ||||
| [DebuggerDisplay("({X} | {Y})")] | ||||
| internal readonly struct PixelPosition(int x, int y) | ||||
| { | ||||
|     public ushort X { get; } = (ushort)((x + MapService.PixelsPerRow) % MapService.PixelsPerRow); | ||||
|     public ushort Y { get; } = (ushort)((y + MapService.PixelsPerColumn) % MapService.PixelsPerColumn); | ||||
| } | ||||
|     public int X { get; } = (x + MapService.PixelsPerRow) % MapService.PixelsPerRow; | ||||
|     public int Y { get; } = (y + MapService.PixelsPerColumn) % MapService.PixelsPerColumn; | ||||
| 
 | ||||
|     public void Deconstruct(out int x, out int y) | ||||
|     { | ||||
|         x = X; | ||||
|         y = Y; | ||||
|     } | ||||
| } | ||||
|  |  | |||
|  | @ -1,31 +1,14 @@ | |||
| using System.Diagnostics; | ||||
| using TanksServer.GameLogic; | ||||
| 
 | ||||
| namespace TanksServer.Models; | ||||
| 
 | ||||
| internal static class PositionHelpers | ||||
| { | ||||
|     public static PixelPosition GetPixelRelative(this TilePosition position, byte subX, byte subY) | ||||
|     { | ||||
|         Debug.Assert(subX < 8); | ||||
|         Debug.Assert(subY < 8); | ||||
|         return new PixelPosition( | ||||
|             (ushort)(position.X * MapService.TileSize + subX), | ||||
|             (ushort)(position.Y * MapService.TileSize + subY) | ||||
|         ); | ||||
|     } | ||||
|     public static PixelPosition GetPixelRelative(this PixelPosition position, short subX, short subY) | ||||
|         => new(position.X + subX, position.Y + subY); | ||||
| 
 | ||||
|     public static PixelPosition GetPixelRelative(this PixelPosition position, byte subX, byte subY) | ||||
|     { | ||||
|         Debug.Assert(subX < 8); | ||||
|         Debug.Assert(subY < 8); | ||||
|         return new PixelPosition((ushort)(position.X + subX), (ushort)(position.Y + subY)); | ||||
|     } | ||||
| 
 | ||||
|     public static PixelPosition ToPixelPosition(this FloatPosition position) => new( | ||||
|         (ushort)((int)position.X % MapService.PixelsPerRow), | ||||
|         (ushort)((int)position.Y % MapService.PixelsPerRow) | ||||
|     ); | ||||
|     public static PixelPosition ToPixelPosition(this FloatPosition position) | ||||
|         => new((int)position.X, (int)position.Y); | ||||
| 
 | ||||
|     public static PixelPosition ToPixelPosition(this TilePosition position) => new( | ||||
|         (ushort)(position.X * MapService.TileSize), | ||||
|  |  | |||
|  | @ -28,13 +28,12 @@ internal sealed class Tank(Player player, FloatPosition spawnPosition) : IMapEnt | |||
| 
 | ||||
|     public PixelBounds Bounds => GetBoundsForCenter(Position); | ||||
| 
 | ||||
|     public static PixelBounds GetBoundsForCenter(FloatPosition position) => new( | ||||
|         new PixelPosition( | ||||
|             (ushort)(position.X - MapService.TileSize / 2d), | ||||
|             (ushort)(position.Y - MapService.TileSize / 2d) | ||||
|         ), new PixelPosition( | ||||
|             (ushort)(position.X + MapService.TileSize / 2d - 1d), | ||||
|             (ushort)(position.Y + MapService.TileSize / 2d - 1d) | ||||
|         ) | ||||
|     ); | ||||
|     public static PixelBounds GetBoundsForCenter(FloatPosition position) | ||||
|     { | ||||
|         var pixelPosition = position.ToPixelPosition(); | ||||
|         return new PixelBounds( | ||||
|             pixelPosition.GetPixelRelative(-3, -3), | ||||
|             pixelPosition.GetPixelRelative(4, 4) | ||||
|         ); | ||||
|     } | ||||
| } | ||||
|  |  | |||
|  | @ -1,9 +1,11 @@ | |||
| using System.Diagnostics; | ||||
| using TanksServer.GameLogic; | ||||
| 
 | ||||
| namespace TanksServer.Models; | ||||
| 
 | ||||
| [DebuggerDisplay("({X} | {Y})")] | ||||
| internal readonly struct TilePosition(ushort x, ushort y) | ||||
| { | ||||
|     public ushort X { get; } = (ushort)((x + MapService.TilesPerRow) % MapService.TilesPerRow); | ||||
|     public ushort Y { get; } = (ushort)((y + MapService.TilesPerColumn) % MapService.TilesPerColumn); | ||||
| } | ||||
| } | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Vinzenz Schroeter
						Vinzenz Schroeter