added debug console
This commit is contained in:
parent
b1dbad6dd4
commit
072c4de56c
|
|
@ -82,5 +82,5 @@ resharper_web_config_module_not_resolved_highlighting = warning
|
|||
resharper_web_config_type_not_resolved_highlighting = warning
|
||||
resharper_web_config_wrong_module_highlighting = warning
|
||||
|
||||
# ignore RCS1102
|
||||
dotnet_diagnostic.RCS1102.severity = none
|
||||
dotnet_diagnostic.RCS1102.severity = none
|
||||
csharp_style_namespace_declarations = file_scoped:warning
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@
|
|||
<TargetFramework>net7.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>disable</Nullable>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
|
||||
|
|
|
|||
Binary file not shown.
|
After Width: | Height: | Size: 243 B |
Binary file not shown.
|
After Width: | Height: | Size: 390 B |
Binary file not shown.
Binary file not shown.
|
After Width: | Height: | Size: 201 B |
|
|
@ -11,11 +11,11 @@ internal struct EventAction
|
|||
public Delegate Del;
|
||||
}
|
||||
|
||||
public class EventPublisher
|
||||
public class eventPublisher
|
||||
{
|
||||
private readonly Dictionary<EventType, List<EventAction>> eventListeners = new();
|
||||
|
||||
public EventPublisher()
|
||||
public eventPublisher()
|
||||
{
|
||||
this.Scan();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,44 +4,26 @@ namespace Mine2d.engine;
|
|||
|
||||
public class EventService
|
||||
{
|
||||
private static readonly EventPublisher EventPublisher = new();
|
||||
private static readonly eventPublisher EventPublisher = new();
|
||||
|
||||
public static void PollEvents()
|
||||
{
|
||||
while (SDL_PollEvent(out var e) != 0)
|
||||
{
|
||||
if (e.type == SDL_EventType.SDL_QUIT)
|
||||
var eventType = e.type switch
|
||||
{
|
||||
EventPublisher.Publish(EventType.Quit, e);
|
||||
}
|
||||
if (e.type == SDL_EventType.SDL_WINDOWEVENT && e.window.windowEvent == SDL_WindowEventID.SDL_WINDOWEVENT_RESIZED)
|
||||
{
|
||||
EventPublisher.Publish(EventType.WindowResize, e);
|
||||
}
|
||||
if (e.type == SDL_EventType.SDL_MOUSEMOTION)
|
||||
{
|
||||
EventPublisher.Publish(EventType.MouseMotion, e);
|
||||
}
|
||||
if (e.type == SDL_EventType.SDL_MOUSEBUTTONDOWN)
|
||||
{
|
||||
EventPublisher.Publish(EventType.MouseButtonDown, e);
|
||||
}
|
||||
if (e.type == SDL_EventType.SDL_MOUSEBUTTONUP)
|
||||
{
|
||||
EventPublisher.Publish(EventType.MouseButtonUp, e);
|
||||
}
|
||||
if (e.type == SDL_EventType.SDL_KEYDOWN)
|
||||
{
|
||||
EventPublisher.Publish(EventType.KeyDown, e);
|
||||
}
|
||||
if (e.type == SDL_EventType.SDL_KEYUP)
|
||||
{
|
||||
EventPublisher.Publish(EventType.KeyUp, e);
|
||||
}
|
||||
if (e.type == SDL_EventType.SDL_MOUSEWHEEL)
|
||||
{
|
||||
EventPublisher.Publish(EventType.MouseWheel, e);
|
||||
}
|
||||
SDL_EventType.SDL_QUIT => EventType.Quit,
|
||||
SDL_EventType.SDL_WINDOWEVENT when e.window.windowEvent == SDL_WindowEventID.SDL_WINDOWEVENT_RESIZED => EventType.WindowResize,
|
||||
SDL_EventType.SDL_MOUSEMOTION => EventType.MouseMotion,
|
||||
SDL_EventType.SDL_MOUSEBUTTONDOWN => EventType.MouseButtonDown,
|
||||
SDL_EventType.SDL_MOUSEBUTTONUP => EventType.MouseButtonUp,
|
||||
SDL_EventType.SDL_KEYDOWN => EventType.KeyDown,
|
||||
SDL_EventType.SDL_KEYUP => EventType.KeyUp,
|
||||
SDL_EventType.SDL_MOUSEWHEEL => EventType.MouseWheel,
|
||||
SDL_EventType.SDL_TEXTINPUT => EventType.TextInput,
|
||||
_ => EventType.Unhandled
|
||||
};
|
||||
EventPublisher.Publish(eventType, e);
|
||||
}
|
||||
EventPublisher.Publish(EventType.Tick, new SDL_Event());
|
||||
}
|
||||
|
|
|
|||
|
|
@ -94,7 +94,7 @@ public class Renderer
|
|||
|
||||
public (IntPtr texture, int width, int height, IntPtr surfaceMessage) CreateTextTexture(string text)
|
||||
{
|
||||
var surfaceMessage = TTF_RenderText_Solid(this.font, text, this.color);
|
||||
var surfaceMessage = TTF_RenderText_Solid_Wrapped(this.font, text, this.color, 1000);
|
||||
var texture = SDL_CreateTextureFromSurface(this.renderer, surfaceMessage);
|
||||
ProcessStatus(SDL_QueryTexture(texture, out _, out _, out var width, out var height));
|
||||
return (texture, width, height, surfaceMessage);
|
||||
|
|
|
|||
|
|
@ -11,4 +11,6 @@ public enum EventType
|
|||
WindowResize,
|
||||
MouseWheel,
|
||||
Tick,
|
||||
TextInput,
|
||||
Unhandled
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,52 @@
|
|||
using Mine2d.engine.extensions;
|
||||
using Mine2d.engine.system.annotations;
|
||||
using Mine2d.game.backend.network.packets;
|
||||
using Mine2d.game.core;
|
||||
|
||||
namespace Mine2d.game.backend.interactor;
|
||||
|
||||
[Interactor]
|
||||
public class DebugCommandHandler
|
||||
{
|
||||
[Interaction(InteractorKind.Hybrid, PacketType.DebugCommand)]
|
||||
public static void HandleUnknownDebugCommand(DebugCommandPacket packet)
|
||||
{
|
||||
if (packet.Command == DebugCommand.Unknown)
|
||||
{
|
||||
Debugger.Print("Unknown debug command '" + packet.RawCommand + "'");
|
||||
Debugger.Print("Available commands:");
|
||||
var cmds = string.Join(", ", Enum.GetValues<DebugCommand>().Where(command => command != DebugCommand.Unknown).Select(command => command.ToString().ToLower()));
|
||||
Debugger.Print(cmds);
|
||||
}
|
||||
}
|
||||
|
||||
[Interaction(InteractorKind.Hybrid, PacketType.DebugCommand)]
|
||||
public static void HandleHelpDebugCommand(DebugCommandPacket packet)
|
||||
{
|
||||
if (packet.Command == DebugCommand.Help)
|
||||
{
|
||||
var enumType = typeof(DebugCommand);
|
||||
Enum.GetValues<DebugCommand>()
|
||||
.Where(command => command != DebugCommand.Unknown)
|
||||
.ForEach(command =>
|
||||
{
|
||||
var memberInfo = enumType.GetMember(command.ToString());
|
||||
var enumValueMemberInfo = memberInfo[0];
|
||||
var valueAttributes = enumValueMemberInfo.GetCustomAttributes(typeof(DebugCommandAttribute), false);
|
||||
var attribute = (DebugCommandAttribute)valueAttributes[0];
|
||||
Debugger.Print(command.ToString().ToLower() + attribute.Usage + " - " + attribute.Desc);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
[Interaction(InteractorKind.Hybrid, PacketType.DebugCommand)]
|
||||
public static void HandleNoClipDebugCommand(DebugCommandPacket packet)
|
||||
{
|
||||
if (packet.Command == DebugCommand.NoClip)
|
||||
{
|
||||
var ds = Context.Get().FrontendGameState.DebugState;
|
||||
ds.NoClip = !ds.NoClip;
|
||||
Debugger.Print("noclip: " + ds.NoClip);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -18,34 +18,33 @@ public class Move
|
|||
}
|
||||
}
|
||||
|
||||
// [Interaction(InteractorKind.Hybrid, PacketType.Tick)]
|
||||
public static void TickHybrid()
|
||||
{
|
||||
var ctx = Context.Get();
|
||||
var fromPositions = new Dictionary<Guid, Vector2>();
|
||||
foreach (var player in ctx.GameState.Players)
|
||||
{
|
||||
fromPositions.Add(player.Id, player.Position);
|
||||
}
|
||||
ctx.GameState.Players.ForEach(PlayerEntity.Move);
|
||||
ctx.GameState.Players.ForEach(PlayerEntity.Collide);
|
||||
foreach (var player in ctx.GameState.Players)
|
||||
{
|
||||
if (player.Position != fromPositions[player.Id])
|
||||
{
|
||||
ctx.Backend.ProcessPacket(new PlayerMovedPacket
|
||||
{
|
||||
PlayerGuid = player.Id,
|
||||
Target = player.Position
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Interaction(InteractorKind.Client, PacketType.Tick)]
|
||||
public static void SelfMovedClient()
|
||||
{
|
||||
var camera = Context.Get().FrontendGameState.Camera;
|
||||
camera.CenterOn(PlayerEntity.GetSelf().Position);
|
||||
}
|
||||
|
||||
[Interaction(InteractorKind.Hybrid, PacketType.Tick)]
|
||||
public static void OnHybridTick()
|
||||
{
|
||||
var context = Context.Get();
|
||||
var gameState = context.GameState;
|
||||
|
||||
gameState.Players.ForEach(player =>
|
||||
{
|
||||
var position = player.Position;
|
||||
PlayerEntity.Move(player);
|
||||
PlayerEntity.Collide(player);
|
||||
|
||||
if (position != player.Position)
|
||||
{
|
||||
context.Backend.ProcessPacket(new PlayerMovedPacket
|
||||
{
|
||||
PlayerGuid = player.Id,
|
||||
Target = player.Position
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ using Mine2d.game.backend.network.packets;
|
|||
using Mine2d.game.core;
|
||||
using Mine2d.game.core.data;
|
||||
using Mine2d.game.core.tiles;
|
||||
using Mine2d.game.frontend.inventory;
|
||||
using Mine2d.game.state;
|
||||
|
||||
namespace Mine2d.game.backend.interactor;
|
||||
|
|
|
|||
|
|
@ -1,37 +0,0 @@
|
|||
using Mine2d.engine.system.annotations;
|
||||
using Mine2d.game.backend.network.packets;
|
||||
using Mine2d.game.core;
|
||||
|
||||
namespace Mine2d.game.backend.interactor;
|
||||
|
||||
[Interactor]
|
||||
public class PlayerMovement
|
||||
{
|
||||
[Interaction(InteractorKind.Client, PacketType.Tick)]
|
||||
public static void OnTick()
|
||||
{
|
||||
}
|
||||
|
||||
[Interaction(InteractorKind.Hybrid, PacketType.Tick)]
|
||||
public static void OnHybridTick()
|
||||
{
|
||||
var context = Context.Get();
|
||||
var gameState = context.GameState;
|
||||
|
||||
gameState.Players.ForEach(player =>
|
||||
{
|
||||
var position = player.Position;
|
||||
PlayerEntity.Move(player);
|
||||
PlayerEntity.Collide(player);
|
||||
|
||||
if (position != player.Position)
|
||||
{
|
||||
context.Backend.ProcessPacket(new PlayerMovedPacket
|
||||
{
|
||||
PlayerGuid = player.Id,
|
||||
Target = player.Position
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Mine2d.game.backend.network.packets;
|
||||
|
||||
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
|
||||
public class DebugCommandAttribute : Attribute
|
||||
{
|
||||
public string Usage { get; set; }
|
||||
public string Desc { get; set; }
|
||||
|
||||
public DebugCommandAttribute(string usage, string desc)
|
||||
{
|
||||
this.Usage = usage;
|
||||
this.Desc = desc;
|
||||
}
|
||||
}
|
||||
|
||||
public enum DebugCommand {
|
||||
[DebugCommand("", "Disables gravity and collision")]
|
||||
NoClip,
|
||||
|
||||
[DebugCommand(" <item> <amount>", "Gives you an item")]
|
||||
Give,
|
||||
|
||||
[DebugCommand("", "Lists all commands")]
|
||||
Help,
|
||||
|
||||
Unknown
|
||||
}
|
||||
|
||||
public class DebugCommandPacket : Packet
|
||||
{
|
||||
public override PacketType Type => PacketType.DebugCommand;
|
||||
|
||||
public DebugCommand Command { get; set; }
|
||||
public string RawCommand { get; set; }
|
||||
public string[] Args { get; set; }
|
||||
}
|
||||
|
|
@ -11,5 +11,6 @@ public enum PacketType
|
|||
PlayerMoved,
|
||||
BlockBroken,
|
||||
Place,
|
||||
Jump
|
||||
Jump,
|
||||
DebugCommand
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ public class Constants
|
|||
public const int ChunkSize = 32;
|
||||
public const int TileSize = 16;
|
||||
public const int BreakDistance = 64;
|
||||
public static Vector2 Gravity = new(0, 0.1f);
|
||||
public static Vector2 Gravity = new(0, 0.06f);
|
||||
|
||||
public const float JumpSpeed = 3f;
|
||||
public const float JumpSpeed = 2.2f;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,14 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Mine2d.game.core;
|
||||
|
||||
public static class Debugger
|
||||
{
|
||||
public static void Print(string message)
|
||||
{
|
||||
Context.Get().FrontendGameState.DebugState.Messages.Enqueue(message);
|
||||
}
|
||||
}
|
||||
|
|
@ -21,7 +21,16 @@ public class PlayerEntity
|
|||
{
|
||||
var context = Context.Get();
|
||||
var frontEndState = context.FrontendGameState;
|
||||
var ds = frontEndState.DebugState;
|
||||
var inputState = frontEndState.InputState;
|
||||
if (ds.NoClip)
|
||||
{
|
||||
player.Position += new Vector2(
|
||||
inputState.GetAxis(InputAxis.Horizontal) * 2,
|
||||
inputState.GetAxis(InputAxis.Vertical) * 2
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
var movement = player.PlayerMovementState;
|
||||
|
||||
|
|
@ -29,7 +38,7 @@ public class PlayerEntity
|
|||
{
|
||||
movement.CurrentVelocity += Constants.Gravity;
|
||||
}
|
||||
else if(movement.CurrentVelocity.Y > 0)
|
||||
else if (movement.CurrentVelocity.Y > 0)
|
||||
{
|
||||
movement.CurrentVelocity = movement.CurrentVelocity with
|
||||
{
|
||||
|
|
@ -59,6 +68,7 @@ public class PlayerEntity
|
|||
|
||||
public static void Collide(Player player)
|
||||
{
|
||||
if (Context.Get().FrontendGameState.DebugState.NoClip) return;
|
||||
var movement = player.PlayerMovementState;
|
||||
var world = Context.Get().GameState.World;
|
||||
bool hasCollision;
|
||||
|
|
|
|||
|
|
@ -35,17 +35,17 @@ public class TileRegistry
|
|||
this.Tiles.Add(4, new OreTile("ore3", new[] { "stone", "ore3" }, 8));
|
||||
this.Tiles.Add(5, new OreTile("ore4", new[] { "stone", "ore4" }, 10));
|
||||
this.Tiles.Add(6, new Workbench("workbench", "workbench", 10));
|
||||
this.Tiles.Add(7, new OreTile("iron-ore", new [] {"stone", "iron-ore"}, 6, ItemId.RawIron));
|
||||
this.Tiles.Add(8, new OreTile("copper-ore", new [] {"stone", "copper-ore"}, 6, ItemId.RawCopper));
|
||||
this.Tiles.Add(9, new OreTile("tin-ore", new [] {"stone", "tin-ore"}, 6, ItemId.RawTin));
|
||||
this.Tiles.Add(10, new OreTile("silver-ore", new [] {"stone", "silver-ore"}, 7, ItemId.RawSilver));
|
||||
this.Tiles.Add(11, new OreTile("gold-ore", new [] {"stone", "gold-ore"}, 8, ItemId.RawGold));
|
||||
this.Tiles.Add(12, new OreTile("lead-ore", new [] {"stone", "lead-ore"}, 9, ItemId.RawLead));
|
||||
this.Tiles.Add(13, new OreTile("platinum-ore", new [] {"stone", "platinum-ore"}, 10, ItemId.RawPlatinum));
|
||||
this.Tiles.Add(14, new OreTile("cobalt-ore", new [] {"stone", "cobalt-ore"}, 11, ItemId.RawCobalt));
|
||||
this.Tiles.Add(15, new OreTile("tungsten-ore", new [] {"stone", "tungsten-ore"}, 15, ItemId.RawTungsten));
|
||||
this.Tiles.Add(16, new OreTile("uranium-ore", new [] {"stone", "uranium-ore"}, 15, ItemId.RawUranium));
|
||||
this.Tiles.Add(17, new OreTile("diamond-ore", new [] {"stone", "diamond-ore"}, 10, ItemId.Diamond));
|
||||
this.Tiles.Add(7, new OreTile("iron-ore", new[] { "stone", "iron-ore" }, 6, ItemId.RawIron));
|
||||
this.Tiles.Add(8, new OreTile("copper-ore", new[] { "stone", "copper-ore" }, 6, ItemId.RawCopper));
|
||||
this.Tiles.Add(9, new OreTile("tin-ore", new[] { "stone", "tin-ore" }, 6, ItemId.RawTin));
|
||||
this.Tiles.Add(10, new OreTile("silver-ore", new[] { "stone", "silver-ore" }, 7, ItemId.RawSilver));
|
||||
this.Tiles.Add(11, new OreTile("gold-ore", new[] { "stone", "gold-ore" }, 8, ItemId.RawGold));
|
||||
this.Tiles.Add(12, new OreTile("lead-ore", new[] { "stone", "lead-ore" }, 9, ItemId.RawLead));
|
||||
this.Tiles.Add(13, new OreTile("platinum-ore", new[] { "stone", "platinum-ore" }, 10, ItemId.RawPlatinum));
|
||||
this.Tiles.Add(14, new OreTile("cobalt-ore", new[] { "stone", "cobalt-ore" }, 11, ItemId.RawCobalt));
|
||||
this.Tiles.Add(15, new OreTile("tungsten-ore", new[] { "stone", "tungsten-ore" }, 15, ItemId.RawTungsten));
|
||||
this.Tiles.Add(16, new OreTile("uranium-ore", new[] { "stone", "uranium-ore" }, 15, ItemId.RawUranium));
|
||||
this.Tiles.Add(17, new OreTile("diamond-ore", new[] { "stone", "diamond-ore" }, 10, ItemId.Diamond));
|
||||
}
|
||||
|
||||
public Tile GetTile(int id)
|
||||
|
|
|
|||
|
|
@ -1,7 +1,3 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Mine2d.game.core.tiles;
|
||||
|
||||
namespace Mine2d.game.core.world;
|
||||
|
|
@ -111,4 +107,4 @@ public class WorldGenerator
|
|||
}
|
||||
return Tiles.Stone;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,27 +1,119 @@
|
|||
using Mine2d.engine.system;
|
||||
using Mine2d.engine.system.annotations;
|
||||
using Mine2d.game.backend.network.packets;
|
||||
using Mine2d.game.core;
|
||||
using Mine2d.game.core.data;
|
||||
using Mine2d.game.core.data.entities;
|
||||
using Mine2d.game.frontend.inventory;
|
||||
|
||||
namespace Mine2d.game.frontend.events;
|
||||
|
||||
public class DebugInput
|
||||
{
|
||||
[EventListener(EventType.KeyDown)]
|
||||
[EventListener(EventType.KeyDown, Priority = EventPriority.Important)]
|
||||
public static void OnKeyDown(SDL_Event e)
|
||||
{
|
||||
if (e.key.keysym.scancode == SDL_Scancode.SDL_SCANCODE_K)
|
||||
if (e.key.keysym.scancode == SDL_Scancode.SDL_SCANCODE_F10)
|
||||
{
|
||||
var gameState = Context.Get().GameState;
|
||||
var player = PlayerEntity.GetSelf();
|
||||
var chunk = gameState.World.GetChunkAt(player.Position);
|
||||
var item = new ItemEntity
|
||||
if (Context.Get().FrontendGameState.OpenInventory == InventoryKind.None)
|
||||
{
|
||||
Position = player.Position,
|
||||
ItemId = ItemId.Stone
|
||||
};
|
||||
chunk.AddEntity(item);
|
||||
Context.Get().FrontendGameState.OpenInventory = InventoryKind.DebugConsole;
|
||||
}
|
||||
else
|
||||
{
|
||||
Context.Get().FrontendGameState.OpenInventory = InventoryKind.None;
|
||||
}
|
||||
}
|
||||
if (e.key.keysym.scancode == SDL_Scancode.SDL_SCANCODE_BACKSPACE)
|
||||
{
|
||||
var frontendGameState = Context.Get().FrontendGameState;
|
||||
if (frontendGameState.OpenInventory == InventoryKind.DebugConsole)
|
||||
{
|
||||
if (frontendGameState.DebugState.ConsoleInput.Length > 0)
|
||||
{
|
||||
frontendGameState.DebugState.ConsoleInput = frontendGameState.DebugState.ConsoleInput[0..^1];
|
||||
}
|
||||
}
|
||||
}
|
||||
if (e.key.keysym.scancode == SDL_Scancode.SDL_SCANCODE_RETURN)
|
||||
{
|
||||
var frontendGameState = Context.Get().FrontendGameState;
|
||||
if (frontendGameState.OpenInventory == InventoryKind.DebugConsole)
|
||||
{
|
||||
frontendGameState.DebugState.Messages.Enqueue("> " + frontendGameState.DebugState.ConsoleInput);
|
||||
var split = frontendGameState.DebugState.ConsoleInput.Split(' ');
|
||||
var Command = split[0].ToLower() switch
|
||||
{
|
||||
"give" => DebugCommand.Give,
|
||||
"help" => DebugCommand.Help,
|
||||
"noclip" => DebugCommand.NoClip,
|
||||
_ => DebugCommand.Unknown
|
||||
};
|
||||
var Args = split.Length > 1 ? split[1..] : Array.Empty<string>();
|
||||
|
||||
Context.Get().Backend.ProcessPacket(new DebugCommandPacket
|
||||
{
|
||||
Command = Command,
|
||||
Args = Args,
|
||||
RawCommand = split[0]
|
||||
});
|
||||
frontendGameState.DebugState.ConsoleHistory.Add(frontendGameState.DebugState.ConsoleInput);
|
||||
frontendGameState.DebugState.ConsoleHistoryIndex = frontendGameState.DebugState.ConsoleHistory.Count;
|
||||
frontendGameState.DebugState.ConsoleInput = "";
|
||||
}
|
||||
}
|
||||
if (e.key.keysym.scancode == SDL_Scancode.SDL_SCANCODE_UP)
|
||||
{
|
||||
var frontendGameState = Context.Get().FrontendGameState;
|
||||
if (frontendGameState.OpenInventory == InventoryKind.DebugConsole)
|
||||
{
|
||||
if (frontendGameState.DebugState.ConsoleHistoryIndex > 0)
|
||||
{
|
||||
frontendGameState.DebugState.ConsoleHistoryIndex--;
|
||||
if (frontendGameState.DebugState.ConsoleHistoryIndex < frontendGameState.DebugState.ConsoleHistory.Count)
|
||||
frontendGameState.DebugState.ConsoleInput = frontendGameState.DebugState.ConsoleHistory[frontendGameState.DebugState.ConsoleHistoryIndex];
|
||||
}
|
||||
}
|
||||
}
|
||||
if (e.key.keysym.scancode == SDL_Scancode.SDL_SCANCODE_DOWN)
|
||||
{
|
||||
var frontendGameState = Context.Get().FrontendGameState;
|
||||
if (frontendGameState.OpenInventory == InventoryKind.DebugConsole)
|
||||
{
|
||||
if (frontendGameState.DebugState.ConsoleHistoryIndex < frontendGameState.DebugState.ConsoleHistory.Count)
|
||||
{
|
||||
frontendGameState.DebugState.ConsoleHistoryIndex++;
|
||||
if (frontendGameState.DebugState.ConsoleHistoryIndex < frontendGameState.DebugState.ConsoleHistory.Count)
|
||||
frontendGameState.DebugState.ConsoleInput = frontendGameState.DebugState.ConsoleHistory[frontendGameState.DebugState.ConsoleHistoryIndex];
|
||||
else if (frontendGameState.DebugState.ConsoleHistoryIndex == frontendGameState.DebugState.ConsoleHistory.Count && frontendGameState.DebugState.ConsoleHistory[frontendGameState.DebugState.ConsoleHistoryIndex - 1] == frontendGameState.DebugState.ConsoleInput)
|
||||
frontendGameState.DebugState.ConsoleInput = "";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[EventListener(EventType.TextInput)]
|
||||
public static void OnTextInput(SDL_Event e)
|
||||
{
|
||||
var frontendGameState = Context.Get().FrontendGameState;
|
||||
if (frontendGameState.OpenInventory == InventoryKind.DebugConsole)
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
var bytes = e.text.text;
|
||||
var text = System.Text.Encoding.UTF8.GetString(bytes, 32);
|
||||
frontendGameState.DebugState.ConsoleInput += text[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[EventListener(EventType.Tick)]
|
||||
public static void OnTick()
|
||||
{
|
||||
var frontendGameState = Context.Get().FrontendGameState;
|
||||
while (frontendGameState.DebugState.Messages.Count > 30)
|
||||
{
|
||||
var _ = frontendGameState.DebugState.Messages.Dequeue();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
using Mine2d.engine;
|
||||
using Mine2d.engine.system;
|
||||
using Mine2d.engine.system.annotations;
|
||||
using Mine2d.game.frontend.inventory;
|
||||
using Mine2d.game.state;
|
||||
|
||||
namespace Mine2d.game.frontend.events;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,29 @@
|
|||
namespace Mine2d.game.frontend.inventory;
|
||||
|
||||
public class DebugConsoleInventory : Inventory
|
||||
{
|
||||
public override void Render()
|
||||
{
|
||||
var ctx = Context.Get();
|
||||
const int uiScale = 1;
|
||||
var windowHeight = ctx.FrontendGameState.WindowHeight;
|
||||
var history = ctx.FrontendGameState.DebugState.Messages;
|
||||
var consoleInput = "> " + ctx.FrontendGameState.DebugState.ConsoleInput;
|
||||
(nint texture, int width, int height, nint surfaceMessage) historyTex = default;
|
||||
if (history.Count > 0)
|
||||
{
|
||||
historyTex = ctx.Renderer.CreateTextTexture(string.Join("\n", history));
|
||||
}
|
||||
var inputTex = ctx.Renderer.CreateTextTexture(consoleInput);
|
||||
if (history.Count > 0)
|
||||
{
|
||||
ctx.Renderer.DrawTexture(historyTex.texture, 0, windowHeight - (inputTex.height * uiScale) - (historyTex.height * uiScale), historyTex.width * uiScale, historyTex.height * uiScale);
|
||||
}
|
||||
ctx.Renderer.DrawTexture(inputTex.texture, 0, windowHeight - (inputTex.height * uiScale), inputTex.width * uiScale, inputTex.height * uiScale);
|
||||
|
||||
SDL_DestroyTexture(inputTex.texture);
|
||||
SDL_DestroyTexture(historyTex.texture);
|
||||
SDL_FreeSurface(historyTex.surfaceMessage);
|
||||
SDL_FreeSurface(inputTex.surfaceMessage);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +1,12 @@
|
|||
namespace Mine2d.game.frontend.inventory;
|
||||
|
||||
public enum InventoryKind {
|
||||
None,
|
||||
Player,
|
||||
Workbench,
|
||||
DebugConsole
|
||||
}
|
||||
|
||||
public static class InventoryConstants
|
||||
{
|
||||
public static readonly int ExtraSlotsWidth = 21;
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ public class InventoryRegistry
|
|||
{
|
||||
this.inventoryRenderers.Add(InventoryKind.Player, new PlayerInventoryRenderer());
|
||||
this.inventoryRenderers.Add(InventoryKind.Workbench, new WorkbenchInventory());
|
||||
this.inventoryRenderers.Add(InventoryKind.DebugConsole, new DebugConsoleInventory());
|
||||
}
|
||||
|
||||
public Inventory GetInventory(InventoryKind inventory)
|
||||
|
|
|
|||
|
|
@ -1,40 +1,39 @@
|
|||
using Mine2d.engine;
|
||||
using Mine2d.game.core;
|
||||
using Mine2d.game.state;
|
||||
using Mine2d.game.frontend.inventory;
|
||||
|
||||
namespace Mine2d.game.frontend.renderer
|
||||
namespace Mine2d.game.frontend.renderer;
|
||||
|
||||
public class InventoryRenderer : IRenderer
|
||||
{
|
||||
public class InventoryRenderer : IRenderer
|
||||
public void Render()
|
||||
{
|
||||
public void Render()
|
||||
var ctx = Context.Get();
|
||||
var inventory = ctx.FrontendGameState.OpenInventory;
|
||||
if (inventory == InventoryKind.None) return;
|
||||
ctx.Renderer.SetColor(0, 0, 0, 200);
|
||||
ctx.Renderer.SetDrawBlendMode(SDL_BlendMode.SDL_BLENDMODE_BLEND);
|
||||
ctx.Renderer.DrawRect(0, 0, ctx.FrontendGameState.WindowWidth, ctx.FrontendGameState.WindowHeight);
|
||||
var inventoryRenderer = ctx.InventoryRegistry.GetInventory(inventory);
|
||||
inventoryRenderer.Render();
|
||||
var player = PlayerEntity.GetSelf();
|
||||
if (player.Inventory.Cursor != null)
|
||||
{
|
||||
var ctx = Context.Get();
|
||||
var inventory = ctx.FrontendGameState.OpenInventory;
|
||||
if (inventory == InventoryKind.None) return;
|
||||
ctx.Renderer.SetColor(0, 0, 0, 200);
|
||||
ctx.Renderer.SetDrawBlendMode(SDL_BlendMode.SDL_BLENDMODE_BLEND);
|
||||
ctx.Renderer.DrawRect(0, 0, ctx.FrontendGameState.WindowWidth, ctx.FrontendGameState.WindowHeight);
|
||||
var inventoryRenderer = ctx.InventoryRegistry.GetInventory(inventory);
|
||||
inventoryRenderer.Render();
|
||||
var player = PlayerEntity.GetSelf();
|
||||
if (player.Inventory.Cursor != null)
|
||||
{
|
||||
var cursorPosition = ctx.FrontendGameState.CursorPosition;
|
||||
var itemTexture = player.Inventory.Cursor.GetTexture();
|
||||
var uiScale = ctx.FrontendGameState.Settings.UiScale;
|
||||
ctx.Renderer.DrawTexture(
|
||||
itemTexture,
|
||||
(int)(cursorPosition.X + 2),
|
||||
(int)(cursorPosition.Y + 2),
|
||||
16 * uiScale,
|
||||
16 * uiScale
|
||||
);
|
||||
ctx.Renderer.DrawText(
|
||||
"" + player.Inventory.Cursor.Count,
|
||||
(int)(cursorPosition.X + 2),
|
||||
(int)(cursorPosition.Y + (12 * uiScale))
|
||||
);
|
||||
}
|
||||
var cursorPosition = ctx.FrontendGameState.CursorPosition;
|
||||
var itemTexture = player.Inventory.Cursor.GetTexture();
|
||||
var uiScale = ctx.FrontendGameState.Settings.UiScale;
|
||||
ctx.Renderer.DrawTexture(
|
||||
itemTexture,
|
||||
(int)(cursorPosition.X + 2),
|
||||
(int)(cursorPosition.Y + 2),
|
||||
16 * uiScale,
|
||||
16 * uiScale
|
||||
);
|
||||
ctx.Renderer.DrawText(
|
||||
"" + player.Inventory.Cursor.Count,
|
||||
(int)(cursorPosition.X + 2),
|
||||
(int)(cursorPosition.Y + (12 * uiScale))
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,12 +1,8 @@
|
|||
using Mine2d.game.core;
|
||||
using Mine2d.game.frontend.inventory;
|
||||
|
||||
namespace Mine2d.game.state;
|
||||
|
||||
public enum InventoryKind {
|
||||
None,
|
||||
Player,
|
||||
Workbench
|
||||
}
|
||||
|
||||
public class FrontendGameState
|
||||
{
|
||||
|
|
@ -23,6 +19,15 @@ public class FrontendGameState
|
|||
public string Tooltip { get; set; } = "Test";
|
||||
public InventoryKind OpenInventory { get; set; } = InventoryKind.None;
|
||||
public InputState InputState { get; set; } = new();
|
||||
public DebugState DebugState { get; set; } = new();
|
||||
}
|
||||
|
||||
public class DebugState {
|
||||
public Queue<string> Messages { get; set; } = new();
|
||||
public string ConsoleInput { get; set; } = "";
|
||||
public List<string> ConsoleHistory { get; set; } = new();
|
||||
public int ConsoleHistoryIndex { get; set; } = 0;
|
||||
public bool NoClip { get; set; } = false;
|
||||
}
|
||||
|
||||
public class Settings
|
||||
|
|
|
|||
Loading…
Reference in New Issue