From 5e5c36c958c8f599096ecf22c35c72f9bd831751 Mon Sep 17 00:00:00 2001 From: cwtrucks Date: Fri, 16 Aug 2024 19:53:42 -0400 Subject: [PATCH 1/6] Free camera mode with LCtrl --- .gitignore | 3 ++- Blocktest/Blocktest.csproj | 18 ++++++++--------- Blocktest/Code/Scenes/GameScene.cs | 32 +++++++++++++++++++++++++----- Shared/Code/Packets/MovePlayer.cs | 7 ++++++- 4 files changed, 44 insertions(+), 16 deletions(-) diff --git a/.gitignore b/.gitignore index 9e6528f..4daaf92 100644 --- a/.gitignore +++ b/.gitignore @@ -13,4 +13,5 @@ riderModule.iml *.mgcb #Visual Studio -.vs/ \ No newline at end of file +.vs/ +/Blocktest/Content/Graphics/Player/placeholder-base.png diff --git a/Blocktest/Blocktest.csproj b/Blocktest/Blocktest.csproj index 3a73a1f..ec66860 100644 --- a/Blocktest/Blocktest.csproj +++ b/Blocktest/Blocktest.csproj @@ -11,12 +11,12 @@ Icon.ico - - + + - - + + true @@ -25,17 +25,17 @@ - + - + - + - - + + \ No newline at end of file diff --git a/Blocktest/Code/Scenes/GameScene.cs b/Blocktest/Code/Scenes/GameScene.cs index 297acc4..7ec85a7 100644 --- a/Blocktest/Code/Scenes/GameScene.cs +++ b/Blocktest/Code/Scenes/GameScene.cs @@ -1,3 +1,4 @@ +using System.Diagnostics; using System.Linq; using System.Net; using Blocktest.Block_System; @@ -27,6 +28,9 @@ public sealed class GameScene : IScene { private readonly Client _networkingClient; private readonly SpriteBatch _spriteBatch; + private readonly Vector2 _cameraPosition; + private Vector2 _cameraStayPosition; + private readonly WorldState _worldState = new(); private KeyboardState _previousKeyboardState; @@ -37,7 +41,8 @@ public GameScene(BlocktestGame game, bool doConnect, IPEndPoint? ip) { _spriteBatch = new SpriteBatch(game.GraphicsDevice); _game = game; - _camera = new Camera(Vector2.Zero, new Vector2(512, 256), game.GraphicsDevice); + _cameraPosition = Vector2.Zero; + _camera = new Camera(_cameraPosition, new Vector2(512, 256), game.GraphicsDevice); _backgroundTilemapSprites = new RenderableTilemap(_worldState.Foreground, _camera); _foregroundTilemapSprites = new RenderableTilemap(_worldState.Background, _camera); @@ -79,7 +84,7 @@ public void Draw(GameTime gameTime, GraphicsDevice graphicsDevice) { graphicsDevice.Clear(Color.CornflowerBlue); _camera.Draw(graphicsDevice, _spriteBatch); - const bool pixelPerfect = false; + const bool pixelPerfect = true; Rectangle destinationRectangle = pixelPerfect ? GetPixelPerfectRect() : GetFitRect(); _camera.RenderLocation = destinationRectangle; @@ -147,13 +152,15 @@ private void HandleInput() { moveVector.Y -= moveValue; } + int selfId = _networkingClient.Server?.RemoteId ?? 0; if (moveVector != Vector2.Zero) { - _camera.Position += moveVector; + //_camera.Position += moveVector; MovePlayer movementPacket = new() { TickNum = _networkingClient.LocalTickBuffer.CurrTick, - Position = (Vector2Int)_camera.Position, - SourceId = _networkingClient.Server?.RemoteId ?? 0 + //Position = (Vector2Int)_camera.Position, + AddToPosition = (Vector2Int)moveVector, + SourceId = selfId }; _networkingClient.LocalTickBuffer.AddPacket(movementPacket); if (_connect) { @@ -161,6 +168,21 @@ private void HandleInput() { } } + // allows free camera movement with lctrl, returns to player + Vector2 cameraMoveVector = Vector2.Zero; + if (currentKeyboardState.IsKeyDown(Keys.LeftControl)) { + if (_camera.RenderLocation.Contains(currentMouseState.Position)) { + cameraMoveVector.X = (currentMouseState.Position.X - _camera.RenderLocation.Center.X)/10; + cameraMoveVector.Y = -(currentMouseState.Position.Y - _camera.RenderLocation.Center.Y)/10; + } + if (cameraMoveVector != Vector2.Zero) { + _camera.Position += cameraMoveVector; + } + } else { + _camera.Position.X = _worldState.PlayerPositions[selfId].Position.X - _camera.RenderTarget.Width / 2; + _camera.Position.Y = _worldState.PlayerPositions[selfId].Position.Y - _camera.RenderTarget.Height / 2; + } + _previousKeyboardState = currentKeyboardState; if (currentMouseState.LeftButton != ButtonState.Pressed && diff --git a/Shared/Code/Packets/MovePlayer.cs b/Shared/Code/Packets/MovePlayer.cs index 03cd5ba..55eec3e 100644 --- a/Shared/Code/Packets/MovePlayer.cs +++ b/Shared/Code/Packets/MovePlayer.cs @@ -1,8 +1,12 @@ using LiteNetLib.Utils; +using System.Diagnostics; namespace Shared.Code.Packets; public sealed class MovePlayer : IPacket { public Vector2Int Position { get; set; } + + public Vector2Int AddToPosition { get; set; } + public ushort TickNum { get; init; } public int SourceId { get; init; } @@ -16,7 +20,8 @@ public void Deserialize(NetDataReader reader) { } public void Process(WorldState worldState) { - worldState.PlayerPositions[SourceId].Position = Position + new Vector2Int(256, 128); + //worldState.PlayerPositions[SourceId].Position = Position + new Vector2Int(256, 128); + worldState.PlayerPositions[SourceId].Position += AddToPosition; } public PacketType GetPacketType() => PacketType.MovePlayer; From ccc8915bd3955c0313b3edd6050dcf319750569f Mon Sep 17 00:00:00 2001 From: Cooper Trucks Date: Fri, 23 Aug 2024 23:18:23 -0400 Subject: [PATCH 2/6] minor fix --- Blocktest/Code/Scenes/GameScene.cs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Blocktest/Code/Scenes/GameScene.cs b/Blocktest/Code/Scenes/GameScene.cs index fc8ae64..45e93a3 100644 --- a/Blocktest/Code/Scenes/GameScene.cs +++ b/Blocktest/Code/Scenes/GameScene.cs @@ -41,12 +41,8 @@ public GameScene(BlocktestGame game, bool doConnect, IPEndPoint? ip) { _spriteBatch = new SpriteBatch(game.GraphicsDevice); _game = game; -<<<<<<< HEAD _cameraPosition = Vector2.Zero; - _camera = new Camera(_cameraPosition, new Vector2(512, 256), game.GraphicsDevice); -======= _camera = new Camera(Vector2.Zero, new Vector2(640, 360), game.GraphicsDevice); ->>>>>>> 502011249a7e6cbad7269a1d9f212d8211770372 _backgroundTilemapSprites = new RenderableTilemap(_worldState.Foreground, _camera); _foregroundTilemapSprites = new RenderableTilemap(_worldState.Background, _camera); From a4407ce20477b4e26bb24d52b63506081b162ef8 Mon Sep 17 00:00:00 2001 From: Cooper Trucks Date: Fri, 23 Aug 2024 23:20:01 -0400 Subject: [PATCH 3/6] minor changes? --- Blocktest/Code/Rendering/Renderable.cs | 4 ++-- Blocktest/Code/Scenes/GameScene.cs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Blocktest/Code/Rendering/Renderable.cs b/Blocktest/Code/Rendering/Renderable.cs index 232d81a..d6c066e 100644 --- a/Blocktest/Code/Rendering/Renderable.cs +++ b/Blocktest/Code/Rendering/Renderable.cs @@ -3,9 +3,9 @@ namespace Blocktest.Rendering; public enum Layer { Top = 0, - Player = 1, + Player = 3, Default = 2, - ForegroundBlocks = 3, + ForegroundBlocks = 1, BackgroundBlocks = 4 } diff --git a/Blocktest/Code/Scenes/GameScene.cs b/Blocktest/Code/Scenes/GameScene.cs index 45e93a3..a5f5bbf 100644 --- a/Blocktest/Code/Scenes/GameScene.cs +++ b/Blocktest/Code/Scenes/GameScene.cs @@ -42,7 +42,7 @@ public GameScene(BlocktestGame game, bool doConnect, IPEndPoint? ip) { _game = game; _cameraPosition = Vector2.Zero; - _camera = new Camera(Vector2.Zero, new Vector2(640, 360), game.GraphicsDevice); + _camera = new Camera(_cameraPosition, new Vector2(640, 360), game.GraphicsDevice); _backgroundTilemapSprites = new RenderableTilemap(_worldState.Foreground, _camera); _foregroundTilemapSprites = new RenderableTilemap(_worldState.Background, _camera); From 41dbe9ff643fe3fad4954745772f4dd2125f12ef Mon Sep 17 00:00:00 2001 From: Cooper Trucks Date: Wed, 28 Aug 2024 21:30:47 -0400 Subject: [PATCH 4/6] fix for camera not working with server --- Blocktest/Code/Networking/Client.cs | 6 ++++++ Blocktest/Code/Scenes/GameScene.cs | 27 ++++++++++++++++++--------- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/Blocktest/Code/Networking/Client.cs b/Blocktest/Code/Networking/Client.cs index 1f7ae50..ee34ad5 100644 --- a/Blocktest/Code/Networking/Client.cs +++ b/Blocktest/Code/Networking/Client.cs @@ -15,6 +15,7 @@ public sealed class Client : NetworkInterface { private readonly BlocktestGame _game; private readonly Dictionary _playerRenderables = new(); private bool _initialized; + public bool WorldDownloaded; public Client(WorldState worldState, Camera camera, BlocktestGame game) : base(worldState) { _camera = camera; @@ -76,6 +77,11 @@ protected override void HandlePackets(NetDataReader packetReader, int sourceId, }, Server); _initialized = true; + + if (!WorldDownloaded) + { + WorldDownloaded = true; // nyehhh i dont know what i'm doing with this + } break; default: Console.WriteLine("Bad packet!!!"); diff --git a/Blocktest/Code/Scenes/GameScene.cs b/Blocktest/Code/Scenes/GameScene.cs index a5f5bbf..54124a7 100644 --- a/Blocktest/Code/Scenes/GameScene.cs +++ b/Blocktest/Code/Scenes/GameScene.cs @@ -155,6 +155,7 @@ private void HandleInput() { int selfId = _networkingClient.Server?.RemoteId ?? 0; if (moveVector != Vector2.Zero) { //_camera.Position += moveVector; + Debug.WriteLine(selfId); MovePlayer movementPacket = new() { TickNum = _networkingClient.LocalTickBuffer.CurrTick, @@ -170,17 +171,25 @@ private void HandleInput() { // allows free camera movement with lctrl, returns to player Vector2 cameraMoveVector = Vector2.Zero; - if (currentKeyboardState.IsKeyDown(Keys.LeftControl)) { - if (_camera.RenderLocation.Contains(currentMouseState.Position)) { - cameraMoveVector.X = (currentMouseState.Position.X - _camera.RenderLocation.Center.X)/10; - cameraMoveVector.Y = -(currentMouseState.Position.Y - _camera.RenderLocation.Center.Y)/10; + if (_networkingClient.WorldDownloaded) + { + if (currentKeyboardState.IsKeyDown(Keys.LeftControl)) + { + if (_camera.RenderLocation.Contains(currentMouseState.Position)) + { + cameraMoveVector.X = (currentMouseState.Position.X - _camera.RenderLocation.Center.X) / 10; + cameraMoveVector.Y = -(currentMouseState.Position.Y - _camera.RenderLocation.Center.Y) / 10; + } + if (cameraMoveVector != Vector2.Zero) + { + _camera.Position += cameraMoveVector; + } } - if (cameraMoveVector != Vector2.Zero) { - _camera.Position += cameraMoveVector; + else + { + _camera.Position.X = _worldState.PlayerPositions[selfId].Position.X - _camera.RenderTarget.Width / 2; + _camera.Position.Y = _worldState.PlayerPositions[selfId].Position.Y - _camera.RenderTarget.Height / 2; } - } else { - _camera.Position.X = _worldState.PlayerPositions[selfId].Position.X - _camera.RenderTarget.Width / 2; - _camera.Position.Y = _worldState.PlayerPositions[selfId].Position.Y - _camera.RenderTarget.Height / 2; } _previousKeyboardState = currentKeyboardState; From e86c325986b95de7d456e4e6a26885e478a3ed3f Mon Sep 17 00:00:00 2001 From: Cooper Trucks Date: Wed, 28 Aug 2024 23:02:29 -0400 Subject: [PATCH 5/6] minor fix for debug mode --- Blocktest/Code/Scenes/GameScene.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Blocktest/Code/Scenes/GameScene.cs b/Blocktest/Code/Scenes/GameScene.cs index 436bfe9..7b922c0 100644 --- a/Blocktest/Code/Scenes/GameScene.cs +++ b/Blocktest/Code/Scenes/GameScene.cs @@ -171,7 +171,7 @@ private void HandleInput() { // allows free camera movement with lctrl, returns to player Vector2 cameraMoveVector = Vector2.Zero; - if (_networkingClient.WorldDownloaded) + if (!_connect || _networkingClient.WorldDownloaded) { if (currentKeyboardState.IsKeyDown(Keys.LeftControl)) { From 6c125aad3c2df3cfedab5322b59e9f7d870f67c9 Mon Sep 17 00:00:00 2001 From: Cooper Trucks Date: Wed, 28 Aug 2024 23:14:28 -0400 Subject: [PATCH 6/6] ACTUAL fix for camera not working with server --- Blocktest/Code/Scenes/GameScene.cs | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/Blocktest/Code/Scenes/GameScene.cs b/Blocktest/Code/Scenes/GameScene.cs index 7b922c0..259bdbc 100644 --- a/Blocktest/Code/Scenes/GameScene.cs +++ b/Blocktest/Code/Scenes/GameScene.cs @@ -75,7 +75,10 @@ public void Update(GameTime gameTime) { _networkingClient.Update(); } - HandleInput(); + if (!_connect || _networkingClient.WorldDownloaded) + { + HandleInput(); + } _networkingClient.LocalTickBuffer.IncrCurrTick(_worldState); } @@ -171,26 +174,23 @@ private void HandleInput() { // allows free camera movement with lctrl, returns to player Vector2 cameraMoveVector = Vector2.Zero; - if (!_connect || _networkingClient.WorldDownloaded) + if (currentKeyboardState.IsKeyDown(Keys.LeftControl)) { - if (currentKeyboardState.IsKeyDown(Keys.LeftControl)) + if (_camera.RenderLocation.Contains(currentMouseState.Position)) { - if (_camera.RenderLocation.Contains(currentMouseState.Position)) - { - cameraMoveVector.X = (currentMouseState.Position.X - _camera.RenderLocation.Center.X) / 10; - cameraMoveVector.Y = -(currentMouseState.Position.Y - _camera.RenderLocation.Center.Y) / 10; - } - if (cameraMoveVector != Vector2.Zero) - { - _camera.Position += cameraMoveVector; - } + cameraMoveVector.X = (currentMouseState.Position.X - _camera.RenderLocation.Center.X) / 10; + cameraMoveVector.Y = -(currentMouseState.Position.Y - _camera.RenderLocation.Center.Y) / 10; } - else + if (cameraMoveVector != Vector2.Zero) { - _camera.Position.X = _worldState.PlayerPositions[selfId].Position.X - _camera.RenderTarget.Width / 2; - _camera.Position.Y = _worldState.PlayerPositions[selfId].Position.Y - _camera.RenderTarget.Height / 2; + _camera.Position += cameraMoveVector; } } + else + { + _camera.Position.X = _worldState.PlayerPositions[selfId].Position.X - _camera.RenderTarget.Width / 2; + _camera.Position.Y = _worldState.PlayerPositions[selfId].Position.Y - _camera.RenderTarget.Height / 2; + } _previousKeyboardState = currentKeyboardState;