fixed visibilites

This commit is contained in:
MasterGordon 2022-11-13 16:31:43 +01:00
parent 378fb04015
commit 3945a492f7
40 changed files with 215 additions and 191 deletions

View File

@ -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;
}
}
}

View File

@ -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"),
};
}
}
}

View File

@ -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);
}
}
}

View File

@ -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!");
}
}
}

View File

@ -7,11 +7,11 @@ using WatsonTcp;
namespace mine2d.backend;
class Backend : IBackend
public class Backend : IBackend
{
private WatsonTcpServer server;
private Publisher publisher;
private Queue<ValueType> pendingPackets = new();
private readonly Queue<ValueType> 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);
}
}
}
}

View File

@ -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();
}
}

View File

@ -5,7 +5,7 @@ using mine2d.engine.system.annotations;
namespace mine2d.backend;
class Publisher
public class Publisher
{
private readonly Dictionary<string, HashSet<Delegate>> 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;

View File

@ -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;

View File

@ -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)
}
);
}
}
}
}

View File

@ -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);

View File

@ -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)));
}
}
}

View File

@ -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;
}
}
}

View File

@ -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);
}
public static Vector2 Gravity = new(0, 0.1f);
}

View File

@ -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);
}
}
}

View File

@ -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);
}
}
}

View File

@ -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);
}
}
}

View File

@ -1,6 +1,6 @@
namespace mine2d.core.data;
class World
public class World
{
public Dictionary<string, Chunk> Chunks { get; set; } = new Dictionary<string, Chunk>();
@ -79,4 +79,4 @@ class World
{
return this.HasChunkAt(x, y) && this.GetChunkAt(x, y).HasTileAt(new Vector2(x, y));
}
}
}

View File

@ -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);
}
}
}

View File

@ -1,11 +1,11 @@
namespace mine2d.core.tiles;
enum Tiles
public enum Tiles
{
Stone = 1,
}
class TileRegistry
public class TileRegistry
{
public Dictionary<int, Tile> Tiles { get; set; } = new();
@ -18,4 +18,4 @@ class TileRegistry
{
return this.Tiles[id];
}
}
}

View File

@ -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;
}
}
}

View File

@ -1,15 +1,15 @@
namespace mine2d.engine;
enum Sound { }
public enum Sound { }
class AudioPlayer
public class AudioPlayer
{
private Dictionary<Sound, byte[]> audioFiles = new();
private ResourceLoader resourceLoader = new();
private readonly Dictionary<Sound, byte[]> 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);
}
}
}

View File

@ -1,29 +1,34 @@
using Mine2d.engine;
namespace mine2d.engine;
class FontManager
public class FontManager
{
private Dictionary<string, IntPtr> fonts = new();
private ResourceLoader resourceLoader;
private readonly Dictionary<string, IntPtr> 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];
}
}
}

View File

@ -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<int> fpsQueue = new();
private readonly bool running = true;
private readonly Queue<int> 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();
}
}
}
}

View File

@ -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));
}
}
public static void ProcessStatus(int status)
{
if (status != 0)
{
throw new SDLException(SDL_GetError());
}
}
}

View File

@ -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];
}
}
}

View File

@ -0,0 +1,8 @@
namespace Mine2d.engine;
public class SDLException : Exception
{
public SDLException(string message) : base(message)
{
}
}

View File

@ -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;
}
}
}

View File

@ -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;
}
}
}

View File

@ -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,
}
}

View File

@ -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;
}
}
}

View File

@ -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;
}
}
}

View File

@ -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));
}
}
}

View File

@ -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)

View File

@ -1,7 +1,7 @@
namespace mine2d.frontend;
interface IFrontend
public interface IFrontend
{
public void Process();
public void Init();
}
}

View File

@ -1,6 +1,6 @@
namespace mine2d.frontend.renderer;
interface IRenderer
public interface IRenderer
{
public void Render();
}
}

View File

@ -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
}
}
}
}
}

View File

@ -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<string>();
Console.WriteLine("Packet type: " + packetType);
@ -27,7 +28,7 @@ class Converter
{
"move" => parsedRaw.ToObject<MovePacket>(),
"connect" => parsedRaw.ToObject<ConnectPacket>(),
_ => 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);
}
}
}

View File

@ -0,0 +1,8 @@
namespace Mine2d.network;
public class PacketException : Exception
{
public PacketException(string message) : base(message)
{
}
}

View File

@ -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<Player> Players { get; set; } = new List<Player>();
public World World { get; set; }
}
}

View File

@ -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));
}
}
}