added basic movement

This commit is contained in:
MasterGordon 2022-12-21 12:08:53 +01:00
parent 2068855979
commit 70bcef619e
9 changed files with 95 additions and 37 deletions

View File

@ -7,16 +7,16 @@ namespace Mine2d.game.backend.interactor;
[Interactor]
public class Move
{
// [Interaction(InteractorKind.Hybrid, PacketType.Move)]
// public static void MoveHybrid(MovePacket packet)
// {
// var ctx = Context.Get();
// var player = ctx.GameState.Players.Find(p => p.Name == packet.PlayerName);
// if (player != null)
// {
// player.Movement += packet.Movement * 2;
// }
// }
[Interaction(InteractorKind.Hybrid, PacketType.Jump)]
public static void MoveHybrid(JumpPacket packet)
{
var ctx = Context.Get();
var player = ctx.GameState.Players.Find(p => p.Id == packet.PlayerId);
if (player != null)
{
PlayerEntity.Jump(player);
}
}
// [Interaction(InteractorKind.Hybrid, PacketType.Tick)]
public static void TickHybrid()

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Mine2d.game.backend.network.packets;
public class JumpPacket : Packet
{
public override PacketType Type => PacketType.Jump;
public Guid PlayerId { get; init; }
}

View File

@ -10,5 +10,6 @@ public enum PacketType
Break,
PlayerMoved,
BlockBroken,
Place
Place,
Jump
}

View File

@ -6,4 +6,6 @@ public class Constants
public const int TileSize = 16;
public const int BreakDistance = 64;
public static Vector2 Gravity = new(0, 0.1f);
public const float JumpSpeed = 3f;
}

View File

@ -24,17 +24,39 @@ public class PlayerEntity
var inputState = frontEndState.InputState;
var movement = player.PlayerMovementState;
if (!movement.IsGrounded)
{
movement.CurrentVelocity += Constants.Gravity;
}
else if(movement.CurrentVelocity.Y > 0)
{
movement.CurrentVelocity = movement.CurrentVelocity with
{
Y = movement.CurrentVelocity.Y * 0.5f
};
}
movement.CurrentVelocity = movement.CurrentVelocity with
{
X = inputState.GetAxis(InputAxis.Horizontal)
X = inputState.GetAxis(InputAxis.Horizontal) * movement.Speed.X
};
movement.CurrentMovement = movement.CurrentVelocity * movement.Speed * (float)context.GameState.DeltaTime;
player.Position += movement.CurrentMovement;
Console.WriteLine(movement.IsGrounded);
Console.WriteLine(movement.CurrentVelocity);
player.Position += movement.CurrentVelocity;
}
public static void Jump(Player player)
{
var movement = player.PlayerMovementState;
if (movement.IsGrounded)
{
movement.CurrentVelocity = movement.CurrentVelocity with
{
Y = -Constants.JumpSpeed
};
}
}
public static void Collide(Player player)
@ -47,11 +69,11 @@ public class PlayerEntity
var pL = player.Position + new Vector2(0, -8);
var pL2 = player.Position + new Vector2(0, -24);
hasCollision =
world.HasChunkAt(pL) && world.GetChunkAt(pL).HasSolidTileAt(pL)
|| world.HasChunkAt(pL2) && world.GetChunkAt(pL2).HasSolidTileAt(pL2);
(world.HasChunkAt(pL) && world.GetChunkAt(pL).HasSolidTileAt(pL))
|| (world.HasChunkAt(pL2) && world.GetChunkAt(pL2).HasSolidTileAt(pL2));
if (hasCollision)
{
movement.CurrentVelocity = movement.CurrentVelocity with { X = 0 };
// movement.CurrentVelocity *= new Vector2(0.8f, 1);
player.Position += new Vector2(0.1f, 0);
}
} while (hasCollision);
@ -60,11 +82,11 @@ public class PlayerEntity
var pR = player.Position + new Vector2(14, -8);
var pR2 = player.Position + new Vector2(14, -24);
hasCollision =
world.HasChunkAt(pR) && world.GetChunkAt(pR).HasSolidTileAt(pR)
|| world.HasChunkAt(pR2) && world.GetChunkAt(pR2).HasSolidTileAt(pR2);
(world.HasChunkAt(pR) && world.GetChunkAt(pR).HasSolidTileAt(pR))
|| (world.HasChunkAt(pR2) && world.GetChunkAt(pR2).HasSolidTileAt(pR2));
if (hasCollision)
{
movement.CurrentVelocity = movement.CurrentVelocity with { X = 0 };
// movement.CurrentVelocity *= new Vector2(0.8f, 1);
player.Position += new Vector2(-0.1f, 0);
}
} while (hasCollision);
@ -73,11 +95,11 @@ public class PlayerEntity
var pL = player.Position + new Vector2(0, 0);
var pR = player.Position + new Vector2(14, 0);
hasCollision =
world.HasChunkAt(pL) && world.GetChunkAt(pL).HasSolidTileAt(pL)
|| world.HasChunkAt(pR) && world.GetChunkAt(pR).HasSolidTileAt(pR);
(world.HasChunkAt(pL) && world.GetChunkAt(pL).HasSolidTileAt(pL))
|| (world.HasChunkAt(pR) && world.GetChunkAt(pR).HasSolidTileAt(pR));
if (hasCollision)
{
movement.CurrentVelocity = movement.CurrentVelocity with { Y = 0 };
// movement.CurrentVelocity *= new Vector2(1f, 0.8f);
player.Position += new Vector2(0, -0.01f);
}
} while (hasCollision);
@ -86,21 +108,21 @@ public class PlayerEntity
var pL = player.Position + new Vector2(0, -28);
var pR = player.Position + new Vector2(14, -28);
hasCollision =
world.HasChunkAt(pL) && world.GetChunkAt(pL).HasSolidTileAt(pL)
|| world.HasChunkAt(pR) && world.GetChunkAt(pR).HasSolidTileAt(pR);
(world.HasChunkAt(pL) && world.GetChunkAt(pL).HasSolidTileAt(pL))
|| (world.HasChunkAt(pR) && world.GetChunkAt(pR).HasSolidTileAt(pR));
if (hasCollision)
{
movement.CurrentVelocity = movement.CurrentVelocity with { Y = 0 };
movement.CurrentVelocity *= new Vector2(1f, 0.8f);
player.Position += new Vector2(0, 0.1f);
}
} while (hasCollision);
{
var groundCheckPosition = player.Position - new Vector2(0, -1f);
var pL = groundCheckPosition + new Vector2(0, 0);
var pR = groundCheckPosition + new Vector2(14, 0);
movement.IsGrounded = world.HasChunkAt(pL) && world.GetChunkAt(pL).HasTileAt(pL)
|| world.HasChunkAt(pR) && world.GetChunkAt(pR).HasTileAt(pR);
movement.IsGrounded = (world.HasChunkAt(pL) && world.GetChunkAt(pL).HasSolidTileAt(pL))
|| (world.HasChunkAt(pR) && world.GetChunkAt(pR).HasSolidTileAt(pR));
}
}
@ -108,7 +130,7 @@ public class PlayerEntity
{
var ctx = Context.Get();
const int ts = Constants.TileSize;
var tilePos = new Vector2(pos.X - pos.X % ts, pos.Y - pos.Y % ts);
var tilePos = new Vector2(pos.X - (pos.X % ts), pos.Y - (pos.Y % ts));
return ctx.GameState.Players.Any(
player =>
{

View File

@ -106,7 +106,5 @@ public class World
{
this.Cracks = new(this.Cracks.OrderBy(x => x.ResetTime));
}
Console.WriteLine(this.Cracks.Count);
Console.WriteLine(this.Cracks.ToString());
}
}

View File

@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Mine2d.engine.system;
using Mine2d.engine.system.annotations;
using Mine2d.game.backend.network.packets;
namespace Mine2d.game.frontend.events
{
public class PlayerMovementInput
{
[EventListener(EventType.KeyDown)]
public static void onKeyDown(SDL_Event e)
{
if(e.key.keysym.sym == SDL_Keycode.SDLK_SPACE) {
Context.Get().Backend.ProcessPacket(new JumpPacket {
PlayerId = Context.Get().FrontendGameState.PlayerGuid
});
}
}
}
}

View File

@ -9,7 +9,6 @@ public class PlayerPlaceInput
[EventListener(EventType.MouseButtonDown)]
public static void OnMouseDown(SDL_Event e)
{
Console.WriteLine("Mouse button down");
if (e.button.button != SDL_BUTTON_RIGHT)
{
return;

View File

@ -4,8 +4,8 @@ public class PlayerMovementState
{
public Vector2 Speed { get; set; } = new Vector2
{
X = 50f,
Y = 20f,
X = 0.65f,
Y = 0.5f,
};
public float Drag { get; set; } = 0.1f;
public bool IsGrounded { get; set; } = false;