Added moused pressed mining

This commit is contained in:
MasterGordon 2022-11-11 01:19:24 +01:00
parent f9fc871390
commit 9a07af8212
6 changed files with 75 additions and 44 deletions

View File

@ -68,4 +68,5 @@ dotnet_style_qualification_for_method = true:warning
dotnet_style_qualification_for_property = true:warning dotnet_style_qualification_for_property = true:warning
dotnet_style_qualification_for_event = true:warning dotnet_style_qualification_for_event = true:warning
csharp_style_unused_value_expression_statement_preference = false csharp_style_unused_value_expression_statement_preference = false
csharp_style_namespace_declarations=file_scoped:warning
dotnet_diagnostic.CA1805.severity = none dotnet_diagnostic.CA1805.severity = none

View File

@ -42,7 +42,9 @@ class Publisher
{ {
continue; continue;
} }
var del = Delegate.CreateDelegate( var del = method.GetParameters().Length == 0 ?
Delegate.CreateDelegate(typeof(Action), method) :
Delegate.CreateDelegate(
typeof(Action<>).MakeGenericTypeSafely(method.GetParameters()[0].ParameterType), typeof(Action<>).MakeGenericTypeSafely(method.GetParameters()[0].ParameterType),
method method
); );
@ -87,8 +89,15 @@ class Publisher
} }
foreach (var del in this.subscribers[type]) foreach (var del in this.subscribers[type])
{ {
del.DynamicInvoke(packet); if (del.Method.GetParameters().Length == 0)
{
del.DynamicInvoke();
}
else
{
del.DynamicInvoke(packet);
}
} }
} }
} }
} }

View File

@ -9,8 +9,8 @@ public struct MovePacket : IPacket
{ {
public readonly string Type => "move"; public readonly string Type => "move";
public readonly string PlayerName; public readonly string PlayerName { get; }
public readonly Vector2 Movement; public readonly Vector2 Movement { get; }
public MovePacket(string playerName, Vector2 movement) public MovePacket(string playerName, Vector2 movement)
{ {
@ -22,8 +22,8 @@ public struct MovePacket : IPacket
public readonly struct ConnectPacket : IPacket public readonly struct ConnectPacket : IPacket
{ {
public readonly string Type => "connect"; public readonly string Type => "connect";
public readonly string PlayerName; public readonly string PlayerName { get; }
public readonly Guid PlayerGuid; public readonly Guid PlayerGuid { get; }
public ConnectPacket(string playerName, Guid playerGuid) public ConnectPacket(string playerName, Guid playerGuid)
{ {
@ -32,10 +32,10 @@ public readonly struct ConnectPacket : IPacket
} }
} }
readonly struct TickPacket : IPacket public readonly struct TickPacket : IPacket
{ {
public readonly string Type => "tick"; public readonly string Type => "tick";
public readonly uint Tick; public readonly uint Tick { get; }
public TickPacket(uint tick) public TickPacket(uint tick)
{ {
@ -43,13 +43,26 @@ readonly struct TickPacket : IPacket
} }
} }
readonly struct SelfMovedPacket : IPacket public readonly struct SelfMovedPacket : IPacket
{ {
public readonly string Type => "selfMoved"; public readonly string Type => "selfMoved";
public readonly Vector2 Target; public readonly Vector2 Target { get; }
public SelfMovedPacket(Vector2 target) public SelfMovedPacket(Vector2 target)
{ {
this.Target = target; this.Target = target;
} }
} }
public readonly struct BreakPacket : IPacket
{
public readonly string Type => "break";
public readonly Guid PlayerGuid { get; }
public readonly Vector2 Target { get; }
public BreakPacket(Guid playerGuid, Vector2 target)
{
this.PlayerGuid = playerGuid;
this.Target = target;
}
}

View File

@ -1,30 +1,49 @@
using mine2d.backend.data; using mine2d.backend.data;
using mine2d.core.data;
using mine2d.engine.system.annotations; using mine2d.engine.system.annotations;
namespace mine2d.backend.interactor; namespace mine2d.backend.interactor;
[Interactor] [Interactor]
class Breaking public class Breaking
{ {
[Interaction(InteractorKind.Hybrid, "tick")] [Interaction(InteractorKind.Hybrid, "tick")]
public static void TickHybrid(TickPacket packet) public static void TickHybrid()
{ {
var ctx = Context.Get(); var ctx = Context.Get();
ctx.GameState.Players.ForEach(player => ctx.GameState.Players.ForEach(player =>
{ {
player.MiningCooldown = Math.Max(0, player.MiningCooldown - 1); player.MiningCooldown = Math.Max(0, player.MiningCooldown - 1);
if (player.Mining != Vector2.Zero && player.MiningCooldown == 0) if (player.Mining != Vector2.Zero && player.MiningCooldown == 0 && ctx.GameState.World.HasChunkAt(player.Mining))
{ {
var chunk = ctx.GameState.World.GetChunkAt(player.Mining); var chunk = ctx.GameState.World.GetChunkAt(player.Mining);
// chunk.SetTileAt(player.Mining, tile with { Hits = tile.Hits + 1 }); var tile = chunk.GetTileAt(player.Mining);
// var tile = chunk.GetTileAt(amp); var tileId = tile.Id;
// if (tile.Hits >= hardness) if (tileId != 0)
// { {
// chunk.SetTileAt(amp, STile.From(0)); player.MiningCooldown = 10;
// } var tileRegistry = ctx.TileRegistry;
var hardness = tileRegistry.GetTile(tileId).Hardness;
chunk.SetTileAt(player.Mining, tile with { Hits = tile.Hits + 1 });
if (tile.Hits >= hardness)
{
chunk.SetTileAt(player.Mining, STile.From(0));
}
}
} }
} }
); );
} }
}
[Interaction(InteractorKind.Server, "break")]
public static void BreakServer(BreakPacket packet)
{
var ctx = Context.Get();
var player = ctx.GameState.Players.Find(p => p.Guid == packet.PlayerGuid);
if (player == null)
{
return;
}
player.Mining = packet.Target;
}
}

View File

@ -7,10 +7,6 @@ public static class PacketUtils
public static string GetType(ValueType packet) public static string GetType(ValueType packet)
{ {
var t = packet.GetType(); var t = packet.GetType();
foreach (var pp in t.GetProperties())
{
Console.WriteLine(pp.Name);
}
var p = t.GetProperty(nameof(IPacket.Type)); var p = t.GetProperty(nameof(IPacket.Type));
if (p == null) if (p == null)
{ {

View File

@ -46,28 +46,21 @@ class Frontend : IFrontend
if (e.type == SDL_EventType.SDL_MOUSEMOTION) if (e.type == SDL_EventType.SDL_MOUSEMOTION)
{ {
var mousePos = new Vector2(e.motion.x, e.motion.y); var mousePos = new Vector2(e.motion.x, e.motion.y);
Console.WriteLine($"Mouse moved to {mousePos}");
ctx.FrontendGameState.MousePosition = mousePos; ctx.FrontendGameState.MousePosition = mousePos;
if (ctx.GameState.Players.Find(player => player.Guid == ctx.FrontendGameState.PlayerGuid).Mining != Vector2.Zero)
{
var amp = ctx.FrontendGameState.MousePosition / ctx.FrontendGameState.Settings.GameScale + ctx.FrontendGameState.Camera.Position;
ctx.Backend.ProcessPacket(new BreakPacket(ctx.FrontendGameState.PlayerGuid, amp));
}
} }
if (e.type == SDL_EventType.SDL_MOUSEBUTTONDOWN && e.button.button == SDL_BUTTON_LEFT) if (e.type == SDL_EventType.SDL_MOUSEBUTTONDOWN && e.button.button == SDL_BUTTON_LEFT)
{ {
var amp = ctx.FrontendGameState.MousePosition / ctx.FrontendGameState.Settings.GameScale + ctx.FrontendGameState.Camera.Position; var amp = ctx.FrontendGameState.MousePosition / ctx.FrontendGameState.Settings.GameScale + ctx.FrontendGameState.Camera.Position;
if (ctx.GameState.World.HasChunkAt(amp)) ctx.Backend.ProcessPacket(new BreakPacket(ctx.FrontendGameState.PlayerGuid, amp));
{ }
var chunk = ctx.GameState.World.GetChunkAt(amp); if (e.type == SDL_EventType.SDL_MOUSEBUTTONUP && e.button.button == SDL_BUTTON_LEFT)
var tile = chunk.GetTileAt(amp); {
var tileId = tile.Id; ctx.Backend.ProcessPacket(new BreakPacket(ctx.FrontendGameState.PlayerGuid, Vector2.Zero));
if (tile.Id != 0)
{
var tileRegistry = ctx.TileRegistry;
var hardness = tileRegistry.GetTile(tileId).Hardness;
chunk.SetTileAt(amp, tile with { Hits = tile.Hits + 1 });
if (tile.Hits >= hardness)
{
chunk.SetTileAt(amp, STile.From(0));
}
}
}
} }
if (e.type == SDL_EventType.SDL_KEYDOWN && e.key.repeat == 0) if (e.type == SDL_EventType.SDL_KEYDOWN && e.key.repeat == 0)
{ {
@ -193,4 +186,4 @@ class Frontend : IFrontend
ctx.Renderer.Present(); ctx.Renderer.Present();
} }
} }