diff --git a/Mine2d/Context.cs b/Mine2d/Context.cs index 375f415..e1c7bf8 100644 --- a/Mine2d/Context.cs +++ b/Mine2d/Context.cs @@ -6,7 +6,7 @@ using mine2d.state; namespace mine2d; -class Context +public class Context { public bool IsHost { get; set; } public IBackend Backend { get; set; } @@ -38,16 +38,16 @@ class Context this.Window = window; this.TileRegistry = new TileRegistry(); this.ResourceLoader = new ResourceLoader(); - Context.Instance = this; + Instance = this; } public static Context Get() { - if (Context.Instance == null) + if (Instance == null) { throw new Exception("Context not initialized"); } - return Context.Instance; + return Instance; } -} \ No newline at end of file +} diff --git a/Mine2d/Controls.cs b/Mine2d/Controls.cs index cf93aaa..1f05756 100644 --- a/Mine2d/Controls.cs +++ b/Mine2d/Controls.cs @@ -1,6 +1,6 @@ namespace mine2d; -enum Control +public enum Control { Up, Down, @@ -10,26 +10,19 @@ enum Control Confirm, } -static class ControlKeyExtension +public static class ControlKeyExtension { public static SDL_Keycode Key(this Control c) { - switch (c) + return c switch { - case Control.Up: - return SDL_Keycode.SDLK_w; - case Control.Down: - return SDL_Keycode.SDLK_s; - case Control.Left: - return SDL_Keycode.SDLK_a; - case Control.Right: - return SDL_Keycode.SDLK_d; - case Control.Stay: - return SDL_Keycode.SDLK_LCTRL; - case Control.Confirm: - return SDL_Keycode.SDLK_SPACE; - default: - throw new ArgumentException("Invalid control"); - } + Control.Up => SDL_Keycode.SDLK_w, + Control.Down => SDL_Keycode.SDLK_s, + Control.Left => SDL_Keycode.SDLK_a, + Control.Right => SDL_Keycode.SDLK_d, + Control.Stay => SDL_Keycode.SDLK_LCTRL, + Control.Confirm => SDL_Keycode.SDLK_SPACE, + _ => throw new ArgumentException("Invalid control"), + }; } -} \ No newline at end of file +} diff --git a/Mine2d/Mine2d.cs b/Mine2d/Mine2d.cs index 2e8ad2c..3a9af89 100644 --- a/Mine2d/Mine2d.cs +++ b/Mine2d/Mine2d.cs @@ -6,7 +6,7 @@ using mine2d.state; namespace mine2d; -class Mine2d : Game +public class Mine2d : Game { private readonly Context ctx; @@ -46,4 +46,4 @@ class Mine2d : Game { this.ctx.Backend.Process(dt); } -} \ No newline at end of file +} diff --git a/Mine2d/Program.cs b/Mine2d/Program.cs index 03d9fc4..b6399bc 100644 --- a/Mine2d/Program.cs +++ b/Mine2d/Program.cs @@ -1,15 +1,12 @@ namespace mine2d; -class Program +public class Program { - static void Main(string[] args) + public static void Main(string[] args) { - bool isHost = args.Contains("--host"); + var isHost = args.Contains("--host"); // bool isHost = true; var game = new Mine2d(isHost); game.Run(); - // var p = new Publisher(isHost ? InteractorKind.Server : InteractorKind.Client); - // p.Dump(); - // Console.WriteLine("Hello World!"); } -} \ No newline at end of file +} diff --git a/Mine2d/backend/Backend.cs b/Mine2d/backend/Backend.cs index 60fcea3..1344010 100644 --- a/Mine2d/backend/Backend.cs +++ b/Mine2d/backend/Backend.cs @@ -7,11 +7,11 @@ using WatsonTcp; namespace mine2d.backend; -class Backend : IBackend +public class Backend : IBackend { private WatsonTcpServer server; private Publisher publisher; - private Queue pendingPackets = new(); + private readonly Queue pendingPackets = new(); private uint tick; public void Process(double dt) @@ -77,10 +77,16 @@ class Backend : IBackend private void SendGameState() { if (this.server == null) + { return; + } + var clients = this.server.ListClients().ToArray(); if (!clients.Any()) + { return; + } + var gameState = Context.Get().GameState; var json = JsonConvert.SerializeObject(gameState); var bytes = Encoding.UTF8.GetBytes(json); @@ -89,4 +95,4 @@ class Backend : IBackend this.server.Send(client, bytes); } } -} \ No newline at end of file +} diff --git a/Mine2d/backend/IBackend.cs b/Mine2d/backend/IBackend.cs index ab85e6d..5886456 100644 --- a/Mine2d/backend/IBackend.cs +++ b/Mine2d/backend/IBackend.cs @@ -1,8 +1,8 @@ namespace mine2d.backend; -interface IBackend +public interface IBackend { public void Process(double dt); public void ProcessPacket(ValueType packet); public void Init(); -} \ No newline at end of file +} diff --git a/Mine2d/backend/Publisher.cs b/Mine2d/backend/Publisher.cs index 0407e96..beb520a 100644 --- a/Mine2d/backend/Publisher.cs +++ b/Mine2d/backend/Publisher.cs @@ -5,7 +5,7 @@ using mine2d.engine.system.annotations; namespace mine2d.backend; -class Publisher +public class Publisher { private readonly Dictionary> subscribers = new(); @@ -24,7 +24,7 @@ class Publisher .GetTypesSafe(); foreach (var type in types) { - var attrs = type.GetCustomAttributes(typeof(Interactor), false); + var attrs = type.GetCustomAttributes(typeof(InteractorAttribute), false); if (attrs.Length == 0) { continue; @@ -32,12 +32,12 @@ class Publisher var methods = type.GetMethods(); foreach (var method in methods) { - var attrs2 = method.GetCustomAttributes(typeof(Interaction), false); + var attrs2 = method.GetCustomAttributes(typeof(InteractionAttribute), false); if (attrs2.Length == 0) { continue; } - var attr = (Interaction)attrs2[0]; + var attr = (InteractionAttribute)attrs2[0]; if (attr.Kind != this.kind && this.kind != InteractorKind.Hybrid) { continue; diff --git a/Mine2d/backend/interactor/Breaking.cs b/Mine2d/backend/interactor/Breaking.cs index 5c44f77..c52c24d 100644 --- a/Mine2d/backend/interactor/Breaking.cs +++ b/Mine2d/backend/interactor/Breaking.cs @@ -4,10 +4,10 @@ using mine2d.engine.system.annotations; namespace mine2d.backend.interactor; -[Interactor] +[InteractorAttribute] public class Breaking { - [Interaction(InteractorKind.Hybrid, "tick")] + [InteractionAttribute(InteractorKind.Hybrid, "tick")] public static void TickHybrid() { var ctx = Context.Get(); @@ -35,11 +35,11 @@ public class Breaking ); } - [Interaction(InteractorKind.Server, "break")] + [InteractionAttribute(InteractorKind.Server, "break")] public static void BreakServer(BreakPacket packet) { var ctx = Context.Get(); - var player = ctx.GameState.Players.Find(p => p.Guid == packet.PlayerGuid); + var player = ctx.GameState.Players.Find(p => p.Id == packet.PlayerGuid); if (player == null) { return; diff --git a/Mine2d/backend/interactor/Connect.cs b/Mine2d/backend/interactor/Connect.cs index ed8ed22..636dc39 100644 --- a/Mine2d/backend/interactor/Connect.cs +++ b/Mine2d/backend/interactor/Connect.cs @@ -4,10 +4,10 @@ using mine2d.state; namespace mine2d.backend.interactor; -[Interactor] -class Connect +[InteractorAttribute] +public class Connect { - [Interaction(InteractorKind.Server, "connect")] + [InteractionAttribute(InteractorKind.Server, "connect")] public static void ConnectServer(ConnectPacket packet) { var ctx = Context.Get(); @@ -18,11 +18,11 @@ class Connect new Player { Name = packet.PlayerName, - Guid = packet.PlayerGuid, + Id = packet.PlayerGuid, Position = new Vector2(20, 16 * 16), Movement = new Vector2(0, 0) } ); } } -} \ No newline at end of file +} diff --git a/Mine2d/backend/interactor/Move.cs b/Mine2d/backend/interactor/Move.cs index f77ad1b..61adebb 100644 --- a/Mine2d/backend/interactor/Move.cs +++ b/Mine2d/backend/interactor/Move.cs @@ -4,10 +4,10 @@ using mine2d.engine.system.annotations; namespace mine2d.backend.interactor; -[Interactor] -class Move +[InteractorAttribute] +public class Move { - [Interaction(InteractorKind.Hybrid, "move")] + [InteractionAttribute(InteractorKind.Hybrid, "move")] public static void MoveHybrid(MovePacket packet) { var ctx = Context.Get(); @@ -18,16 +18,16 @@ class Move } } - [Interaction(InteractorKind.Hybrid, "tick")] - public static void TickHybrid(TickPacket packet) + [InteractionAttribute(InteractorKind.Hybrid, "tick")] + public static void TickHybrid() { var ctx = Context.Get(); ctx.GameState.Players.ForEach(PlayerEntity.Move); ctx.GameState.Players.ForEach(PlayerEntity.Collide); } - [Interaction(InteractorKind.Client, "tick")] - public static void SelfMovedClient(TickPacket packet) + [InteractionAttribute(InteractorKind.Client, "tick")] + public static void SelfMovedClient() { var camera = Context.Get().FrontendGameState.Camera; camera.CenterOn(PlayerEntity.GetSelf().Position); diff --git a/Mine2d/core/Bootstrapper.cs b/Mine2d/core/Bootstrapper.cs index b733e37..9564888 100644 --- a/Mine2d/core/Bootstrapper.cs +++ b/Mine2d/core/Bootstrapper.cs @@ -4,7 +4,7 @@ using mine2d.core.world; namespace mine2d.core; -class Bootstrapper +public class Bootstrapper { public static void Bootstrap() { @@ -14,4 +14,4 @@ class Bootstrapper ctx.GameState.World.AddChunk(ChunkGenerator.CreateFilledChunk(1, 1, STile.From(Tiles.Stone))); ctx.GameState.World.AddChunk(ChunkGenerator.CreateFilledChunk(1, 0, STile.From(Tiles.Stone))); } -} \ No newline at end of file +} diff --git a/Mine2d/core/Camera.cs b/Mine2d/core/Camera.cs index 1a4ff24..da92c8f 100644 --- a/Mine2d/core/Camera.cs +++ b/Mine2d/core/Camera.cs @@ -1,8 +1,8 @@ namespace mine2d.core; -class Camera +public class Camera { - public Vector2 Position; + public Vector2 Position { get; set; } public Camera() { @@ -17,4 +17,4 @@ class Camera var windowHeight = ctx.FrontendGameState.WindowHeight; this.Position = target - (new Vector2(windowWidth, windowHeight) / 2) / scale; } -} \ No newline at end of file +} diff --git a/Mine2d/core/Constants.cs b/Mine2d/core/Constants.cs index 758f520..d0fc1e1 100644 --- a/Mine2d/core/Constants.cs +++ b/Mine2d/core/Constants.cs @@ -1,8 +1,8 @@ namespace mine2d.core; -class Constants +public class Constants { public const int ChunkSize = 32; public const int TileSize = 16; - public static Vector2 Gravity = new Vector2(0, 0.1f); -} \ No newline at end of file + public static Vector2 Gravity = new(0, 0.1f); +} diff --git a/Mine2d/core/PlayerEntity.cs b/Mine2d/core/PlayerEntity.cs index 5014c84..1030881 100644 --- a/Mine2d/core/PlayerEntity.cs +++ b/Mine2d/core/PlayerEntity.cs @@ -2,18 +2,18 @@ using mine2d.state; namespace mine2d.core; -class PlayerEntity +public class PlayerEntity { public static bool IsSelf(Player p) { - return p.Guid == GetSelf().Guid; + return p.Id == GetSelf().Id; } public static Player GetSelf() { var ctx = Context.Get(); var player = ctx.GameState.Players.FirstOrDefault( - p => p.Guid == ctx.FrontendGameState.PlayerGuid + p => p.Id == ctx.FrontendGameState.PlayerGuid ); return player; } @@ -77,4 +77,4 @@ class PlayerEntity } } while (hasCollision); } -} \ No newline at end of file +} diff --git a/Mine2d/core/data/Chunk.cs b/Mine2d/core/data/Chunk.cs index 8b7fd85..ecbfe45 100644 --- a/Mine2d/core/data/Chunk.cs +++ b/Mine2d/core/data/Chunk.cs @@ -1,6 +1,6 @@ namespace mine2d.core.data; -class Chunk +public class Chunk { public STile[,] Tiles { get; set; } = new STile[Constants.ChunkSize, Constants.ChunkSize]; public int X { get; set; } @@ -76,4 +76,4 @@ class Chunk return pos - new Vector2(this.X * Constants.ChunkSize * Constants.TileSize, this.Y * Constants.ChunkSize * Constants.TileSize); } -} \ No newline at end of file +} diff --git a/Mine2d/core/data/STile.cs b/Mine2d/core/data/STile.cs index 9e87155..ed16077 100644 --- a/Mine2d/core/data/STile.cs +++ b/Mine2d/core/data/STile.cs @@ -2,7 +2,7 @@ using mine2d.core.tiles; namespace mine2d.core.data; -struct STile +public struct STile { public int Id { get; set; } public int Hits { get; set; } @@ -19,4 +19,4 @@ struct STile { return From((int)id); } -} \ No newline at end of file +} diff --git a/Mine2d/core/data/World.cs b/Mine2d/core/data/World.cs index f0035f9..9c5da70 100644 --- a/Mine2d/core/data/World.cs +++ b/Mine2d/core/data/World.cs @@ -1,6 +1,6 @@ namespace mine2d.core.data; -class World +public class World { public Dictionary Chunks { get; set; } = new Dictionary(); @@ -79,4 +79,4 @@ class World { return this.HasChunkAt(x, y) && this.GetChunkAt(x, y).HasTileAt(new Vector2(x, y)); } -} \ No newline at end of file +} diff --git a/Mine2d/core/tiles/Tile.cs b/Mine2d/core/tiles/Tile.cs index d12fc31..ae3a730 100644 --- a/Mine2d/core/tiles/Tile.cs +++ b/Mine2d/core/tiles/Tile.cs @@ -2,7 +2,7 @@ using mine2d.core.data; namespace mine2d.core.tiles; -class Tile +public class Tile { public string Name { get; set; } public int Hardness { get; set; } @@ -68,4 +68,4 @@ class Tile breakingTexture = Context.Get().Renderer.CreateTextureFromSurface(surface); SDL_FreeSurface(surface); } -} \ No newline at end of file +} diff --git a/Mine2d/core/tiles/TileRegistry.cs b/Mine2d/core/tiles/TileRegistry.cs index bfacc08..448e56b 100644 --- a/Mine2d/core/tiles/TileRegistry.cs +++ b/Mine2d/core/tiles/TileRegistry.cs @@ -1,11 +1,11 @@ namespace mine2d.core.tiles; -enum Tiles +public enum Tiles { Stone = 1, } -class TileRegistry +public class TileRegistry { public Dictionary Tiles { get; set; } = new(); @@ -18,4 +18,4 @@ class TileRegistry { return this.Tiles[id]; } -} \ No newline at end of file +} diff --git a/Mine2d/core/world/ChunkGenerator.cs b/Mine2d/core/world/ChunkGenerator.cs index eb5bd75..ef78bd4 100644 --- a/Mine2d/core/world/ChunkGenerator.cs +++ b/Mine2d/core/world/ChunkGenerator.cs @@ -2,7 +2,7 @@ using mine2d.core.data; namespace mine2d.core.world; -class ChunkGenerator +public class ChunkGenerator { public static Chunk CreateFilledChunk(int x, int y, STile fill) { @@ -16,4 +16,4 @@ class ChunkGenerator } return chunk; } -} \ No newline at end of file +} diff --git a/Mine2d/engine/AudioPlayer.cs b/Mine2d/engine/AudioPlayer.cs index 9999ada..8ebe665 100644 --- a/Mine2d/engine/AudioPlayer.cs +++ b/Mine2d/engine/AudioPlayer.cs @@ -1,15 +1,15 @@ namespace mine2d.engine; -enum Sound { } +public enum Sound { } -class AudioPlayer +public class AudioPlayer { - private Dictionary audioFiles = new(); - private ResourceLoader resourceLoader = new(); + private readonly Dictionary audioFiles = new(); + private readonly ResourceLoader resourceLoader = new(); public AudioPlayer() { - SDL2.SDL_mixer.Mix_OpenAudio(44100, SDL2.SDL_mixer.MIX_DEFAULT_FORMAT, 2, 2048); + _ = SDL2.SDL_mixer.Mix_OpenAudio(44100, SDL2.SDL_mixer.MIX_DEFAULT_FORMAT, 2, 2048); } public void Register(Sound name, string path) @@ -24,4 +24,4 @@ class AudioPlayer var sound = SDL2.SDL_mixer.Mix_QuickLoad_WAV(buffer); SDL2.SDL_mixer.Mix_PlayChannel((int)name, sound, 0); } -} \ No newline at end of file +} diff --git a/Mine2d/engine/FontManager.cs b/Mine2d/engine/FontManager.cs index f5075a2..b9082ae 100644 --- a/Mine2d/engine/FontManager.cs +++ b/Mine2d/engine/FontManager.cs @@ -1,29 +1,34 @@ +using Mine2d.engine; + namespace mine2d.engine; -class FontManager +public class FontManager { - private Dictionary fonts = new(); - private ResourceLoader resourceLoader; + private readonly Dictionary fonts = new(); + private readonly ResourceLoader resourceLoader; public FontManager(ResourceLoader resourceLoader) { this.resourceLoader = resourceLoader; if (TTF_Init() != 0) { - throw new Exception("TTF_Init failed"); + throw new SDLException(TTF_GetError()); } } public void RegisterFont(string name, string path, int fontSize) { if (this.fonts.ContainsKey(name)) + { return; - var res = this.resourceLoader.LoadToIntPtr(path); - var sdlBuffer = SDL_RWFromConstMem(res.ptr, res.size); + } + + var (ptr, size) = this.resourceLoader.LoadToIntPtr(path); + var sdlBuffer = SDL_RWFromConstMem(ptr, size); var font = TTF_OpenFontRW(sdlBuffer, 1, fontSize); if (font == IntPtr.Zero) { - throw new Exception("TTF_OpenFont failed"); + throw new SDLException(TTF_GetError()); } this.fonts.Add(name, font); } @@ -32,4 +37,4 @@ class FontManager { return this.fonts[name]; } -} \ No newline at end of file +} diff --git a/Mine2d/engine/Game.cs b/Mine2d/engine/Game.cs index 878c618..89c7579 100644 --- a/Mine2d/engine/Game.cs +++ b/Mine2d/engine/Game.cs @@ -1,11 +1,11 @@ namespace mine2d.engine; -abstract class Game +public abstract class Game { public const int Tps = 128; - private bool running = true; - private Queue fpsQueue = new(); + private readonly bool running = true; + private readonly Queue fpsQueue = new(); protected abstract void Update(double dt); protected abstract void Draw(); @@ -21,7 +21,10 @@ abstract class Game var fps = (int)(1 / dt.TotalSeconds); this.fpsQueue.Enqueue(fps); while (this.fpsQueue.Count > fps) + { this.fpsQueue.Dequeue(); + } + while (tAcc >= TimeSpan.FromSeconds(1.0 / Tps)) { this.Update(dt.TotalSeconds); @@ -31,4 +34,4 @@ abstract class Game this.Draw(); } } -} \ No newline at end of file +} diff --git a/Mine2d/engine/Renderer.cs b/Mine2d/engine/Renderer.cs index 7da56a5..3c259fe 100644 --- a/Mine2d/engine/Renderer.cs +++ b/Mine2d/engine/Renderer.cs @@ -1,8 +1,9 @@ using mine2d.engine.utils; +using Mine2d.engine; namespace mine2d.engine; -class Renderer +public class Renderer { private readonly IntPtr renderer; private IntPtr font; @@ -15,12 +16,16 @@ class Renderer -1, SDL_RendererFlags.SDL_RENDERER_ACCELERATED ); + if (this.renderer == IntPtr.Zero) + { + throw new SDLException(SDL_GetError()); + } } public void Clear() { - SDL_SetRenderDrawColor(this.renderer, 0, 0, 0, 255); - SDL_RenderClear(this.renderer); + ProcessStatus(SDL_SetRenderDrawColor(this.renderer, 0, 0, 0, 255)); + ProcessStatus(SDL_RenderClear(this.renderer)); } public void Present() @@ -43,7 +48,7 @@ class Renderer h = h }; - SDL_RenderFillRect(this.renderer, ref rect); + ProcessStatus(SDL_RenderFillRect(this.renderer, ref rect)); } public void DrawOutline(int x, int y, int w, int h) @@ -56,7 +61,7 @@ class Renderer h = h }; - _ = SDL_RenderDrawRect(this.renderer, ref rect); + ProcessStatus(SDL_RenderDrawRect(this.renderer, ref rect)); } public void DrawLines(double[][] points) @@ -68,12 +73,12 @@ class Renderer sdlPoints[i].y = (int)points[i][1]; } - SDL_RenderDrawLines(this.renderer, sdlPoints, points.Length); + ProcessStatus(SDL_RenderDrawLines(this.renderer, sdlPoints, points.Length)); } public void SetColor(int r, int g, int b, int a = 255) { - SDL_SetRenderDrawColor(this.renderer, (byte)r, (byte)g, (byte)b, (byte)a); + ProcessStatus(SDL_SetRenderDrawColor(this.renderer, (byte)r, (byte)g, (byte)b, (byte)a)); } public void SetFont(IntPtr font, SDL_Color color) @@ -93,7 +98,7 @@ class Renderer var surfaceMessage = TTF_RenderText_Solid(this.font, text, this.color); var texture = SDL_CreateTextureFromSurface(this.renderer, surfaceMessage); - SDL_QueryTexture(texture, out _, out _, out var width, out var height); + ProcessStatus(SDL_QueryTexture(texture, out _, out _, out var width, out var height)); var rect = new SDL_Rect { @@ -109,7 +114,7 @@ class Renderer rect.y -= height / 2; } - SDL_RenderCopy(this.renderer, texture, IntPtr.Zero, ref rect); + ProcessStatus(SDL_RenderCopy(this.renderer, texture, IntPtr.Zero, ref rect)); SDL_DestroyTexture(texture); SDL_FreeSurface(surfaceMessage); } @@ -133,7 +138,7 @@ class Renderer w = w, h = h }; - _ = SDL_RenderCopy(this.renderer, texture, IntPtr.Zero, ref rect); + ProcessStatus(SDL_RenderCopy(this.renderer, texture, IntPtr.Zero, ref rect)); } public void DrawTexture(IntPtr texture, int x, int y, int w, int h, int offsetIndex, int srcWidth, int srcHeight) @@ -152,6 +157,14 @@ class Renderer w = srcWidth, h = srcHeight, }; - _ = SDL_RenderCopy(this.renderer, texture, ref srcRect, ref rect); + ProcessStatus(SDL_RenderCopy(this.renderer, texture, ref srcRect, ref rect)); } -} \ No newline at end of file + + public static void ProcessStatus(int status) + { + if (status != 0) + { + throw new SDLException(SDL_GetError()); + } + } +} diff --git a/Mine2d/engine/ResourceLoader.cs b/Mine2d/engine/ResourceLoader.cs index 20647bb..c936dba 100644 --- a/Mine2d/engine/ResourceLoader.cs +++ b/Mine2d/engine/ResourceLoader.cs @@ -2,18 +2,18 @@ using System.Runtime.InteropServices; namespace mine2d.engine; -class ResourceLoader +public class ResourceLoader { - private string assemblyName; + private readonly string assemblyName; public ResourceLoader() { this.assemblyName = this.GetType().Assembly.GetName().Name!; } - public string LoadString(string resourceName) + public static string LoadString(string resourceName) { -#if (DEBUG) +#if DEBUG Console.WriteLine("Loading resource: " + resourceName); return File.ReadAllText(ToPath(resourceName)); #else @@ -41,7 +41,7 @@ class ResourceLoader return (ptr, bytes.Length); } - public void SaveString(string resourceName, string content) + public static void SaveString(string resourceName, string content) { using var stream = new StreamWriter(ToPath(resourceName)); stream.Write(content); @@ -50,6 +50,6 @@ class ResourceLoader private static string ToPath(string resourceName) { var s = resourceName.Split('.'); - return String.Join('/', s[..^1]) + "." + s[^1]; + return string.Join('/', s[..^1]) + "." + s[^1]; } -} \ No newline at end of file +} diff --git a/Mine2d/engine/SDLException.cs b/Mine2d/engine/SDLException.cs new file mode 100644 index 0000000..02a4dd5 --- /dev/null +++ b/Mine2d/engine/SDLException.cs @@ -0,0 +1,8 @@ +namespace Mine2d.engine; + +public class SDLException : Exception +{ + public SDLException(string message) : base(message) + { + } +} diff --git a/Mine2d/engine/Shapes.cs b/Mine2d/engine/Shapes.cs index 79d6a2a..6af8e56 100644 --- a/Mine2d/engine/Shapes.cs +++ b/Mine2d/engine/Shapes.cs @@ -1,13 +1,13 @@ namespace mine2d.engine; -struct Line +public struct Line { - public Vector2 Start; - public Vector2 End; + public Vector2 Start { get; set; } + public Vector2 End { get; set; } public Line(Vector2 start, Vector2 end) { this.Start = start; this.End = end; } -} \ No newline at end of file +} diff --git a/Mine2d/engine/Window.cs b/Mine2d/engine/Window.cs index bf04f0b..1cebe81 100644 --- a/Mine2d/engine/Window.cs +++ b/Mine2d/engine/Window.cs @@ -1,8 +1,8 @@ namespace mine2d.engine; -class Window +public class Window { - IntPtr window; + private readonly IntPtr window; public Window(string title, int w, int h) { @@ -33,9 +33,7 @@ class Window public (int width, int height) GetSize() { - int w, - h; - SDL_GetWindowSize(this.window, out w, out h); + SDL_GetWindowSize(this.window, out var w, out var h); return (w, h); } @@ -43,4 +41,4 @@ class Window { return this.window; } -} \ No newline at end of file +} diff --git a/Mine2d/engine/system/EventPriority.cs b/Mine2d/engine/system/EventPriority.cs index 200c9d9..8ca89ce 100644 --- a/Mine2d/engine/system/EventPriority.cs +++ b/Mine2d/engine/system/EventPriority.cs @@ -1,6 +1,6 @@ namespace mine2d.engine.system; -enum EventPriority +public enum EventPriority { Lowest = 0, Low = 1, @@ -8,4 +8,4 @@ enum EventPriority High = 3, Highest = 4, Important = 5, -} \ No newline at end of file +} diff --git a/Mine2d/engine/system/annotations/Interactor.cs b/Mine2d/engine/system/annotations/Interactor.cs index c790fea..acfda97 100644 --- a/Mine2d/engine/system/annotations/Interactor.cs +++ b/Mine2d/engine/system/annotations/Interactor.cs @@ -1,6 +1,6 @@ namespace mine2d.engine.system.annotations; -enum InteractorKind +public enum InteractorKind { Client, Server, @@ -8,17 +8,17 @@ enum InteractorKind } [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)] -class Interactor : Attribute { } +public class InteractorAttribute : Attribute { } [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] -class Interaction : Attribute +public class InteractionAttribute : Attribute { - public string Type; - public InteractorKind Kind; + public string Type { get; set; } + public InteractorKind Kind { get; set; } - public Interaction(InteractorKind kind, string type) + public InteractionAttribute(InteractorKind kind, string type) { this.Type = type; this.Kind = kind; } -} \ No newline at end of file +} diff --git a/Mine2d/engine/utils/Color.cs b/Mine2d/engine/utils/Color.cs index bedd031..22a5e89 100644 --- a/Mine2d/engine/utils/Color.cs +++ b/Mine2d/engine/utils/Color.cs @@ -1,6 +1,6 @@ namespace mine2d.engine.utils; -class Color +public class Color { public int R, G, @@ -32,4 +32,4 @@ class Color color.a = (byte)this.A; return color; } -} \ No newline at end of file +} diff --git a/Mine2d/engine/utils/Point.cs b/Mine2d/engine/utils/Point.cs index ae36590..50c3261 100644 --- a/Mine2d/engine/utils/Point.cs +++ b/Mine2d/engine/utils/Point.cs @@ -1,9 +1,9 @@ namespace mine2d.engine.utils; -class Point +public class Point { public static double Distance(double x1, double y1, double x2, double y2) { return Math.Sqrt(Math.Pow(x2 - x1, 2) + Math.Pow(y2 - y1, 2)); } -} \ No newline at end of file +} diff --git a/Mine2d/frontend/Frontend.cs b/Mine2d/frontend/Frontend.cs index bb5298a..2d2b07e 100644 --- a/Mine2d/frontend/Frontend.cs +++ b/Mine2d/frontend/Frontend.cs @@ -1,11 +1,10 @@ using mine2d.backend.data; using mine2d.core; -using mine2d.core.data; using mine2d.frontend.renderer; namespace mine2d.frontend; -class Frontend : IFrontend +public class Frontend : IFrontend { public void Init() { @@ -38,7 +37,7 @@ class Frontend : IFrontend ctx.FrontendGameState.WindowHeight = e.window.data2; Console.WriteLine($"Window resized to {e.window.data1}x{e.window.data2}"); var player = ctx.GameState.Players.Find( - p => p.Guid == ctx.FrontendGameState.PlayerGuid + p => p.Id == ctx.FrontendGameState.PlayerGuid ); ctx.FrontendGameState.Camera.CenterOn(player.Position); } @@ -47,7 +46,7 @@ class Frontend : IFrontend { var mousePos = new Vector2(e.motion.x, e.motion.y); ctx.FrontendGameState.MousePosition = mousePos; - if (ctx.GameState.Players.Find(player => player.Guid == ctx.FrontendGameState.PlayerGuid)?.Mining != Vector2.Zero) + if (ctx.GameState.Players.Find(player => player.Id == 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)); @@ -67,17 +66,10 @@ class Frontend : IFrontend var movementInput = ctx.FrontendGameState.MovementInput; if (e.key.keysym.scancode == SDL_Scancode.SDL_SCANCODE_F11) { - if (!ctx.FrontendGameState.Settings.Fullscreen) - { - _ = SDL_SetWindowFullscreen( - ctx.Window.GetRaw(), - (uint)SDL_WindowFlags.SDL_WINDOW_FULLSCREEN_DESKTOP - ); - } - else - { - _ = SDL_SetWindowFullscreen(ctx.Window.GetRaw(), 0); - } + _ = SDL_SetWindowFullscreen( + ctx.Window.GetRaw(), + ctx.FrontendGameState.Settings.Fullscreen ? 0 : (uint)SDL_WindowFlags.SDL_WINDOW_FULLSCREEN_DESKTOP + ); ctx.FrontendGameState.Settings.Fullscreen = !ctx.FrontendGameState.Settings.Fullscreen; } if (e.key.keysym.scancode == SDL_Scancode.SDL_SCANCODE_A) diff --git a/Mine2d/frontend/IFrontend.cs b/Mine2d/frontend/IFrontend.cs index fca0a75..789748f 100644 --- a/Mine2d/frontend/IFrontend.cs +++ b/Mine2d/frontend/IFrontend.cs @@ -1,7 +1,7 @@ namespace mine2d.frontend; -interface IFrontend +public interface IFrontend { public void Process(); public void Init(); -} \ No newline at end of file +} diff --git a/Mine2d/frontend/renderer/IRenderer.cs b/Mine2d/frontend/renderer/IRenderer.cs index af1c3cb..ed3da4c 100644 --- a/Mine2d/frontend/renderer/IRenderer.cs +++ b/Mine2d/frontend/renderer/IRenderer.cs @@ -1,6 +1,6 @@ namespace mine2d.frontend.renderer; -interface IRenderer +public interface IRenderer { public void Render(); -} \ No newline at end of file +} diff --git a/Mine2d/frontend/renderer/WorldRenderer.cs b/Mine2d/frontend/renderer/WorldRenderer.cs index 42990cc..49bec66 100644 --- a/Mine2d/frontend/renderer/WorldRenderer.cs +++ b/Mine2d/frontend/renderer/WorldRenderer.cs @@ -2,7 +2,7 @@ using mine2d.core; namespace mine2d.frontend.renderer; -class WorldRenderer : IRenderer +public class WorldRenderer : IRenderer { public void Render() { @@ -29,4 +29,4 @@ class WorldRenderer : IRenderer } } } -} \ No newline at end of file +} diff --git a/Mine2d/network/Converter.cs b/Mine2d/network/Converter.cs index b14a42f..2a201de 100644 --- a/Mine2d/network/Converter.cs +++ b/Mine2d/network/Converter.cs @@ -1,11 +1,12 @@ using System.Text; using mine2d.backend.data; +using Mine2d.network; using Newtonsoft.Json; using Newtonsoft.Json.Linq; namespace mine2d.network; -class Converter +public class Converter { public static ValueType ParsePacket(byte[] bytes) { @@ -19,7 +20,7 @@ class Converter var type = parsedRaw.GetValue("type"); if (type == null) { - throw new Exception("Packet has no type"); + throw new PacketException("Packet has no type"); } var packetType = type.Value(); Console.WriteLine("Packet type: " + packetType); @@ -27,7 +28,7 @@ class Converter { "move" => parsedRaw.ToObject(), "connect" => parsedRaw.ToObject(), - _ => throw new Exception("Unknown packet type") + _ => throw new PacketException("Unknown packet type") }; } @@ -36,4 +37,4 @@ class Converter var jsonString = JsonConvert.SerializeObject(packet); return Encoding.UTF8.GetBytes(jsonString); } -} \ No newline at end of file +} diff --git a/Mine2d/network/PacketException.cs b/Mine2d/network/PacketException.cs new file mode 100644 index 0000000..938cf99 --- /dev/null +++ b/Mine2d/network/PacketException.cs @@ -0,0 +1,8 @@ +namespace Mine2d.network; + +public class PacketException : Exception +{ + public PacketException(string message) : base(message) + { + } +} diff --git a/Mine2d/state/GameState.cs b/Mine2d/state/GameState.cs index a00dedd..d7e0a9a 100644 --- a/Mine2d/state/GameState.cs +++ b/Mine2d/state/GameState.cs @@ -3,29 +3,29 @@ using mine2d.core.data; namespace mine2d.state; -class FrontendGameState +public class FrontendGameState { - public Vector2 MovementInput; - public Vector2 CameraPosition; - public int WindowWidth; - public int WindowHeight; - public Guid PlayerGuid; - public Camera Camera = new Camera(); - public Vector2 MousePosition; + public Vector2 MovementInput { get; set; } + public Vector2 CameraPosition { get; set; } + public int WindowWidth { get; set; } + public int WindowHeight { get; set; } + public Guid PlayerGuid { get; set; } + public Camera Camera { get; set; } = new(); + public Vector2 MousePosition { get; set; } public Settings Settings { get; set; } = new Settings(); public string PlayerName { get; set; } = "Player"; } -class Settings +public class Settings { - public int GameScale = 4; - public int UiScale = 4; - public bool ShowCollision = true; - public bool Fullscreen = false; + public int GameScale { get; set; } = 4; + public int UiScale { get; set; } = 4; + public bool ShowCollision { get; set; } = true; + public bool Fullscreen { get; set; } = false; } -class GameState +public class GameState { public List Players { get; set; } = new List(); public World World { get; set; } -} \ No newline at end of file +} diff --git a/Mine2d/state/Player.cs b/Mine2d/state/Player.cs index 0292ce6..7a3cb81 100644 --- a/Mine2d/state/Player.cs +++ b/Mine2d/state/Player.cs @@ -2,17 +2,17 @@ using mine2d.engine; namespace mine2d.state; -class Player +public class Player { - public string Name; - public Vector2 Position; - public Vector2 Movement; - public Guid Guid; - public Vector2 Mining; - public int MiningCooldown; + public string Name { get; set; } + public Vector2 Position { get; set; } + public Vector2 Movement { get; set; } + public Guid Id { get; set; } + public Vector2 Mining { get; set; } + public int MiningCooldown { get; set; } public Line GetBottomCollisionLine() { return new Line(this.Position, this.Position + new Vector2(16, 0)); } -} \ No newline at end of file +}