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] [Interactor]
public class Move public class Move
{ {
// [Interaction(InteractorKind.Hybrid, PacketType.Move)] [Interaction(InteractorKind.Hybrid, PacketType.Jump)]
// public static void MoveHybrid(MovePacket packet) public static void MoveHybrid(JumpPacket packet)
// { {
// var ctx = Context.Get(); var ctx = Context.Get();
// var player = ctx.GameState.Players.Find(p => p.Name == packet.PlayerName); var player = ctx.GameState.Players.Find(p => p.Id == packet.PlayerId);
// if (player != null) if (player != null)
// { {
// player.Movement += packet.Movement * 2; PlayerEntity.Jump(player);
// } }
// } }
// [Interaction(InteractorKind.Hybrid, PacketType.Tick)] // [Interaction(InteractorKind.Hybrid, PacketType.Tick)]
public static void TickHybrid() 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, Break,
PlayerMoved, PlayerMoved,
BlockBroken, BlockBroken,
Place Place,
Jump
} }

View File

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

View File

@ -26,15 +26,37 @@ public class PlayerEntity
var movement = player.PlayerMovementState; var movement = player.PlayerMovementState;
if (!movement.IsGrounded) if (!movement.IsGrounded)
{
movement.CurrentVelocity += Constants.Gravity; 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 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; Console.WriteLine(movement.IsGrounded);
Console.WriteLine(movement.CurrentVelocity);
player.Position += movement.CurrentMovement; 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) public static void Collide(Player player)
@ -47,11 +69,11 @@ public class PlayerEntity
var pL = player.Position + new Vector2(0, -8); var pL = player.Position + new Vector2(0, -8);
var pL2 = player.Position + new Vector2(0, -24); var pL2 = player.Position + new Vector2(0, -24);
hasCollision = hasCollision =
world.HasChunkAt(pL) && world.GetChunkAt(pL).HasSolidTileAt(pL) (world.HasChunkAt(pL) && world.GetChunkAt(pL).HasSolidTileAt(pL))
|| world.HasChunkAt(pL2) && world.GetChunkAt(pL2).HasSolidTileAt(pL2); || (world.HasChunkAt(pL2) && world.GetChunkAt(pL2).HasSolidTileAt(pL2));
if (hasCollision) if (hasCollision)
{ {
movement.CurrentVelocity = movement.CurrentVelocity with { X = 0 }; // movement.CurrentVelocity *= new Vector2(0.8f, 1);
player.Position += new Vector2(0.1f, 0); player.Position += new Vector2(0.1f, 0);
} }
} while (hasCollision); } while (hasCollision);
@ -60,11 +82,11 @@ public class PlayerEntity
var pR = player.Position + new Vector2(14, -8); var pR = player.Position + new Vector2(14, -8);
var pR2 = player.Position + new Vector2(14, -24); var pR2 = player.Position + new Vector2(14, -24);
hasCollision = hasCollision =
world.HasChunkAt(pR) && world.GetChunkAt(pR).HasSolidTileAt(pR) (world.HasChunkAt(pR) && world.GetChunkAt(pR).HasSolidTileAt(pR))
|| world.HasChunkAt(pR2) && world.GetChunkAt(pR2).HasSolidTileAt(pR2); || (world.HasChunkAt(pR2) && world.GetChunkAt(pR2).HasSolidTileAt(pR2));
if (hasCollision) if (hasCollision)
{ {
movement.CurrentVelocity = movement.CurrentVelocity with { X = 0 }; // movement.CurrentVelocity *= new Vector2(0.8f, 1);
player.Position += new Vector2(-0.1f, 0); player.Position += new Vector2(-0.1f, 0);
} }
} while (hasCollision); } while (hasCollision);
@ -73,11 +95,11 @@ public class PlayerEntity
var pL = player.Position + new Vector2(0, 0); var pL = player.Position + new Vector2(0, 0);
var pR = player.Position + new Vector2(14, 0); var pR = player.Position + new Vector2(14, 0);
hasCollision = hasCollision =
world.HasChunkAt(pL) && world.GetChunkAt(pL).HasSolidTileAt(pL) (world.HasChunkAt(pL) && world.GetChunkAt(pL).HasSolidTileAt(pL))
|| world.HasChunkAt(pR) && world.GetChunkAt(pR).HasSolidTileAt(pR); || (world.HasChunkAt(pR) && world.GetChunkAt(pR).HasSolidTileAt(pR));
if (hasCollision) if (hasCollision)
{ {
movement.CurrentVelocity = movement.CurrentVelocity with { Y = 0 }; // movement.CurrentVelocity *= new Vector2(1f, 0.8f);
player.Position += new Vector2(0, -0.01f); player.Position += new Vector2(0, -0.01f);
} }
} while (hasCollision); } while (hasCollision);
@ -86,11 +108,11 @@ public class PlayerEntity
var pL = player.Position + new Vector2(0, -28); var pL = player.Position + new Vector2(0, -28);
var pR = player.Position + new Vector2(14, -28); var pR = player.Position + new Vector2(14, -28);
hasCollision = hasCollision =
world.HasChunkAt(pL) && world.GetChunkAt(pL).HasSolidTileAt(pL) (world.HasChunkAt(pL) && world.GetChunkAt(pL).HasSolidTileAt(pL))
|| world.HasChunkAt(pR) && world.GetChunkAt(pR).HasSolidTileAt(pR); || (world.HasChunkAt(pR) && world.GetChunkAt(pR).HasSolidTileAt(pR));
if (hasCollision) if (hasCollision)
{ {
movement.CurrentVelocity = movement.CurrentVelocity with { Y = 0 }; movement.CurrentVelocity *= new Vector2(1f, 0.8f);
player.Position += new Vector2(0, 0.1f); player.Position += new Vector2(0, 0.1f);
} }
} while (hasCollision); } while (hasCollision);
@ -99,8 +121,8 @@ public class PlayerEntity
var groundCheckPosition = player.Position - new Vector2(0, -1f); var groundCheckPosition = player.Position - new Vector2(0, -1f);
var pL = groundCheckPosition + new Vector2(0, 0); var pL = groundCheckPosition + new Vector2(0, 0);
var pR = groundCheckPosition + new Vector2(14, 0); var pR = groundCheckPosition + new Vector2(14, 0);
movement.IsGrounded = world.HasChunkAt(pL) && world.GetChunkAt(pL).HasTileAt(pL) movement.IsGrounded = (world.HasChunkAt(pL) && world.GetChunkAt(pL).HasSolidTileAt(pL))
|| world.HasChunkAt(pR) && world.GetChunkAt(pR).HasTileAt(pR); || (world.HasChunkAt(pR) && world.GetChunkAt(pR).HasSolidTileAt(pR));
} }
} }
@ -108,7 +130,7 @@ public class PlayerEntity
{ {
var ctx = Context.Get(); var ctx = Context.Get();
const int ts = Constants.TileSize; 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( return ctx.GameState.Players.Any(
player => player =>
{ {

View File

@ -106,7 +106,5 @@ public class World
{ {
this.Cracks = new(this.Cracks.OrderBy(x => x.ResetTime)); 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)] [EventListener(EventType.MouseButtonDown)]
public static void OnMouseDown(SDL_Event e) public static void OnMouseDown(SDL_Event e)
{ {
Console.WriteLine("Mouse button down");
if (e.button.button != SDL_BUTTON_RIGHT) if (e.button.button != SDL_BUTTON_RIGHT)
{ {
return; return;

View File

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