Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple independent camera movement #25

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ riderModule.iml
*.mgcb

#Visual Studio
.vs/
.vs/
/Blocktest/Content/Graphics/Player/placeholder-base.png
18 changes: 9 additions & 9 deletions Blocktest/Blocktest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
<ApplicationIcon>Icon.ico</ApplicationIcon>
</PropertyGroup>
<ItemGroup>
<None Remove="Icon.ico"/>
<None Remove="Icon.bmp"/>
<None Remove="Icon.ico" />
<None Remove="Icon.bmp" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Icon.ico"/>
<EmbeddedResource Include="Icon.bmp"/>
<EmbeddedResource Include="Icon.ico" />
<EmbeddedResource Include="Icon.bmp" />
</ItemGroup>
<PropertyGroup>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
Expand All @@ -26,17 +26,17 @@
<PackageReference Include="LiteNetLib" Version="1.2.0" />
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.1.303" />
<PackageReference Include="Myra" Version="1.5.5" />
<PackageReference Include="Nopipeline.Task" Version="2.2.*"/>
<PackageReference Include="Nopipeline.Task" Version="2.2.*" />
<PackageReference Include="MonoGame.Content.Builder.Task" Version="3.8.1.303" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Shared\Shared.csproj"/>
<ProjectReference Include="..\Shared\Shared.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Content\Fonts\"/>
<Folder Include="Content\Fonts\" />
</ItemGroup>
<Target Name="RestoreDotnetTools" BeforeTargets="Restore">
<Message Text="Restoring dotnet tools" Importance="High"/>
<Exec Command="dotnet tool restore"/>
<Message Text="Restoring dotnet tools" Importance="High" />
<Exec Command="dotnet tool restore" />
</Target>
</Project>
6 changes: 6 additions & 0 deletions Blocktest/Code/Networking/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public sealed class Client : NetworkInterface {
private readonly BlocktestGame _game;
private readonly Dictionary<int, Renderable> _playerRenderables = new();
private bool _initialized;
public bool WorldDownloaded;

public Client(WorldState worldState, Camera camera, BlocktestGame game) : base(worldState) {
_camera = camera;
Expand Down Expand Up @@ -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!!!");
Expand Down
4 changes: 2 additions & 2 deletions Blocktest/Code/Rendering/Renderable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ namespace Blocktest.Rendering;

public enum Layer {
Top = 0,
Player = 1,
Player = 3,
Default = 2,
ForegroundBlocks = 3,
ForegroundBlocks = 1,
BackgroundBlocks = 4
}

Expand Down
41 changes: 36 additions & 5 deletions Blocktest/Code/Scenes/GameScene.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics;
using System.Linq;
using System.Net;
using Blocktest.Block_System;
Expand Down Expand Up @@ -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;
Expand All @@ -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(640, 360), game.GraphicsDevice);
_cameraPosition = Vector2.Zero;
_camera = new Camera(_cameraPosition, new Vector2(640, 360), game.GraphicsDevice);

_backgroundTilemapSprites = new RenderableTilemap(_worldState.Foreground, _camera);
_foregroundTilemapSprites = new RenderableTilemap(_worldState.Background, _camera);
Expand Down Expand Up @@ -70,7 +75,10 @@ public void Update(GameTime gameTime) {
_networkingClient.Update();
}

HandleInput();
if (!_connect || _networkingClient.WorldDownloaded)
{
HandleInput();
}

_networkingClient.LocalTickBuffer.IncrCurrTick(_worldState);
}
Expand Down Expand Up @@ -147,20 +155,43 @@ private void HandleInput() {
moveVector.Y -= moveValue;
}

int selfId = _networkingClient.Server?.RemoteId ?? 0;
if (moveVector != Vector2.Zero) {
_camera.Position += moveVector;
//_camera.Position += moveVector;
Debug.WriteLine(selfId);

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) {
_networkingClient.SendPacket(movementPacket);
}
}

// 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 &&
Expand Down
7 changes: 6 additions & 1 deletion Shared/Code/Packets/MovePlayer.cs
Original file line number Diff line number Diff line change
@@ -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; }
Expand All @@ -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;
Expand Down