From 03610b157d620e86991a52722f402e11e6e08e26 Mon Sep 17 00:00:00 2001 From: MasterGordon Date: Wed, 9 Nov 2022 14:40:10 +0100 Subject: [PATCH] added cursor --- src/Mine2d.cs | 23 ++++++---------- src/core/data/World.cs | 8 ++++-- src/core/tiles/Tile.cs | 15 ++-------- src/core/tiles/TileRegistry.cs | 4 +-- src/engine/Renderer.cs | 13 +++++++++ src/frontend/Frontend.cs | 50 ++++++++++++++++++++++++++-------- src/state/GameState.cs | 3 ++ src/state/Player.cs | 4 ++- 8 files changed, 77 insertions(+), 43 deletions(-) diff --git a/src/Mine2d.cs b/src/Mine2d.cs index 43ab454..0464ee0 100644 --- a/src/Mine2d.cs +++ b/src/Mine2d.cs @@ -1,13 +1,12 @@ class Mine2d : Game { - private Context ctx; + private readonly Context ctx; public Mine2d(bool isHost) { var window = new Window("MultiPlayerGame" + (isHost ? " - host" : ""), 1200, 800); - if (isHost) - { - this.ctx = new Context( + this.ctx = isHost + ? new Context( isHost, new Backend(), new Frontend(), @@ -15,11 +14,8 @@ class Mine2d : Game new FrontendGameState(), new Renderer(window), window - ); - } - else - { - this.ctx = new Context( + ) + : new Context( isHost, new RemoteBackend(), new Frontend(), @@ -28,19 +24,18 @@ class Mine2d : Game new Renderer(window), window ); - } Bootstrapper.Bootstrap(); - ctx.Backend.Init(); - ctx.Frontend.Init(); + this.ctx.Backend.Init(); + this.ctx.Frontend.Init(); } protected override void draw() { - ctx.Frontend.Process(); + this.ctx.Frontend.Process(); } protected override void update(double dt) { - ctx.Backend.Process(dt); + this.ctx.Backend.Process(dt); } } diff --git a/src/core/data/World.cs b/src/core/data/World.cs index 0465f38..71d02e1 100644 --- a/src/core/data/World.cs +++ b/src/core/data/World.cs @@ -65,7 +65,11 @@ class World public int GetTileAt(int x, int y) { - var chunk = this.GetChunkAt(x, y); - return 0; + return this.GetChunkAt(x, y).GetTileAt(x, y); + } + + public bool HasTileAt(int x, int y) + { + return this.HasChunkAt(x, y) && this.GetChunkAt(x, y).hasTileAt(new Vector2(x, y)); } } diff --git a/src/core/tiles/Tile.cs b/src/core/tiles/Tile.cs index 6fed165..d49ef76 100644 --- a/src/core/tiles/Tile.cs +++ b/src/core/tiles/Tile.cs @@ -8,8 +8,8 @@ class Tile this.Name = name; var rl = Context.Get().ResourceLoader; - var res = rl.LoadToIntPtr("assets." + textureName + ".png"); - var sdlBuffer = SDL_RWFromMem(res.ptr, res.size); + var (ptr, size) = rl.LoadToIntPtr("assets." + textureName + ".png"); + var sdlBuffer = SDL_RWFromMem(ptr, size); var surface = IMG_Load_RW(sdlBuffer, 1); var texture = Context.Get().Renderer.CreateTextureFromSurface(surface); this.Texture = texture; @@ -34,15 +34,4 @@ class Tile Constants.TileSize * scale ); } - - public SDL_Rect GetCollisionRect(int x, int y) - { - return new SDL_Rect() - { - x = x, - y = y, - w = Constants.TileSize, - h = Constants.TileSize - }; - } } diff --git a/src/core/tiles/TileRegistry.cs b/src/core/tiles/TileRegistry.cs index 61341ec..4eb67f1 100644 --- a/src/core/tiles/TileRegistry.cs +++ b/src/core/tiles/TileRegistry.cs @@ -9,11 +9,11 @@ class TileRegistry public void RegisterTile() { - Tiles.Add(1, new Tile("stone", "stone")); + this.Tiles.Add(1, new Tile("stone", "stone")); } public Tile GetTile(int id) { - return Tiles[id]; + return this.Tiles[id]; } } diff --git a/src/engine/Renderer.cs b/src/engine/Renderer.cs index fa46b10..65c99b0 100644 --- a/src/engine/Renderer.cs +++ b/src/engine/Renderer.cs @@ -40,6 +40,19 @@ class Renderer SDL_RenderFillRect(renderer, ref rect); } + public void DrawOutline(int x, int y, int w, int h) + { + var rect = new SDL_Rect + { + x = x, + y = y, + w = w, + h = h + }; + + _ = SDL_RenderDrawRect(renderer, ref rect); + } + public void DrawLines(double[][] points) { SDL_Point[] sdlPoints = new SDL_Point[points.Length]; diff --git a/src/frontend/Frontend.cs b/src/frontend/Frontend.cs index 8997d03..493b615 100644 --- a/src/frontend/Frontend.cs +++ b/src/frontend/Frontend.cs @@ -1,15 +1,12 @@ class Frontend : IFrontend { - private string playerName = "Player"; - private bool fullscreen = false; - public void Init() { var ctx = Context.Get(); - this.playerName = Context.Get().IsHost ? "Host" : "Client"; + ctx.FrontendGameState.PlayerName = ctx.IsHost ? "Host" : "Client"; var guid = Guid.NewGuid(); ctx.FrontendGameState.PlayerGuid = guid; - var connectPacket = new ConnectPacket(playerName, guid); + var connectPacket = new ConnectPacket(ctx.FrontendGameState.PlayerName, guid); ctx.Backend.ProcessPacket(connectPacket); ctx.TileRegistry.RegisterTile(); var (width, height) = ctx.Window.GetSize(); @@ -39,23 +36,29 @@ class Frontend : IFrontend ctx.FrontendGameState.Camera.CenterOn(player.Position); } } + if (e.type == SDL_EventType.SDL_MOUSEMOTION) + { + var mousePos = new Vector2(e.motion.x, e.motion.y); + Console.WriteLine($"Mouse moved to {mousePos}"); + ctx.FrontendGameState.MousePosition = mousePos; + } if (e.type == SDL_EventType.SDL_KEYDOWN && e.key.repeat == 0) { var movementInput = ctx.FrontendGameState.MovementInput; if (e.key.keysym.scancode == SDL_Scancode.SDL_SCANCODE_F11) { - if (!fullscreen) + if (!ctx.FrontendGameState.Settings.Fullscreen) { - SDL_SetWindowFullscreen( + _ = SDL_SetWindowFullscreen( ctx.Window.GetRaw(), (uint)SDL_WindowFlags.SDL_WINDOW_FULLSCREEN_DESKTOP ); } else { - SDL_SetWindowFullscreen(ctx.Window.GetRaw(), 0); + _ = SDL_SetWindowFullscreen(ctx.Window.GetRaw(), 0); } - fullscreen = !fullscreen; + ctx.FrontendGameState.Settings.Fullscreen = !ctx.FrontendGameState.Settings.Fullscreen; } if (e.key.keysym.scancode == SDL_Scancode.SDL_SCANCODE_A) { @@ -107,11 +110,17 @@ class Frontend : IFrontend ) { if (e.key.repeat == 1) + { continue; + } + var movement = ctx.FrontendGameState.MovementInput; if (movement.Length() > 0) + { movement = Vector2.Normalize(movement); - ctx.Backend.ProcessPacket(new MovePacket(playerName, movement)); + } + + ctx.Backend.ProcessPacket(new MovePacket(ctx.FrontendGameState.PlayerName, movement)); } if (e.key.keysym.scancode == SDL_Scancode.SDL_SCANCODE_ESCAPE) { @@ -125,10 +134,15 @@ class Frontend : IFrontend new WorldRenderer().Render(); ctx.GameState.Players.ForEach(player => { - if (player.Name == playerName) + if (player.Name == ctx.FrontendGameState.PlayerName) + { ctx.Renderer.SetColor(0, 0, 255, 255); + } else + { ctx.Renderer.SetColor(255, 0, 0, 255); + } + ctx.Renderer.DrawRect( (player.Position.X - (int)camera.position.X) * scale, (player.Position.Y - (int)camera.position.Y) * scale - 32 * scale, @@ -136,6 +150,20 @@ class Frontend : IFrontend 32 * scale ); }); + var absoluteMousePos = ctx.FrontendGameState.MousePosition / ctx.FrontendGameState.Settings.GameScale + camera.position; + if (ctx.GameState.World.HasTileAt((int)absoluteMousePos.X, (int)absoluteMousePos.Y)) + { + var a = Constants.TileSize; + var tilePos = new Vector2(absoluteMousePos.X - absoluteMousePos.X % a, absoluteMousePos.Y - absoluteMousePos.Y % a); + ctx.Renderer.SetColor(255, 255, 255, 255); + ctx.Renderer.DrawOutline( + (int)tilePos.X * scale - (int)camera.position.X * scale, + (int)tilePos.Y * scale - (int)camera.position.Y * scale, + 16 * scale, + 16 * scale + ); + } + ctx.Renderer.Present(); } } diff --git a/src/state/GameState.cs b/src/state/GameState.cs index de86c15..4715814 100644 --- a/src/state/GameState.cs +++ b/src/state/GameState.cs @@ -6,7 +6,9 @@ class FrontendGameState public int WindowHeight; public Guid PlayerGuid; public Camera Camera = new Camera(); + public Vector2 MousePosition; public Settings Settings { get; set; } = new Settings(); + public string PlayerName { get; set; } = "Player"; } class Settings @@ -14,6 +16,7 @@ class Settings public int GameScale = 4; public int UIScale = 4; public bool ShowCollision = true; + public bool Fullscreen = false; } class GameState diff --git a/src/state/Player.cs b/src/state/Player.cs index 8a40db7..519a753 100644 --- a/src/state/Player.cs +++ b/src/state/Player.cs @@ -4,9 +4,11 @@ class Player public Vector2 Position; public Vector2 Movement; public Guid Guid; + public Vector2 Mining; + public int MiningCooldown; public Line GetBottomCollisionLine() { - return new Line(Position, Position + new Vector2(16, 0)); + return new Line(this.Position, this.Position + new Vector2(16, 0)); } }