fixed visibilites
This commit is contained in:
parent
378fb04015
commit
3945a492f7
|
|
@ -6,7 +6,7 @@ using mine2d.state;
|
||||||
|
|
||||||
namespace mine2d;
|
namespace mine2d;
|
||||||
|
|
||||||
class Context
|
public class Context
|
||||||
{
|
{
|
||||||
public bool IsHost { get; set; }
|
public bool IsHost { get; set; }
|
||||||
public IBackend Backend { get; set; }
|
public IBackend Backend { get; set; }
|
||||||
|
|
@ -38,16 +38,16 @@ class Context
|
||||||
this.Window = window;
|
this.Window = window;
|
||||||
this.TileRegistry = new TileRegistry();
|
this.TileRegistry = new TileRegistry();
|
||||||
this.ResourceLoader = new ResourceLoader();
|
this.ResourceLoader = new ResourceLoader();
|
||||||
Context.Instance = this;
|
Instance = this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Context Get()
|
public static Context Get()
|
||||||
{
|
{
|
||||||
if (Context.Instance == null)
|
if (Instance == null)
|
||||||
{
|
{
|
||||||
throw new Exception("Context not initialized");
|
throw new Exception("Context not initialized");
|
||||||
}
|
}
|
||||||
|
|
||||||
return Context.Instance;
|
return Instance;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
namespace mine2d;
|
namespace mine2d;
|
||||||
|
|
||||||
enum Control
|
public enum Control
|
||||||
{
|
{
|
||||||
Up,
|
Up,
|
||||||
Down,
|
Down,
|
||||||
|
|
@ -10,26 +10,19 @@ enum Control
|
||||||
Confirm,
|
Confirm,
|
||||||
}
|
}
|
||||||
|
|
||||||
static class ControlKeyExtension
|
public static class ControlKeyExtension
|
||||||
{
|
{
|
||||||
public static SDL_Keycode Key(this Control c)
|
public static SDL_Keycode Key(this Control c)
|
||||||
{
|
{
|
||||||
switch (c)
|
return c switch
|
||||||
{
|
{
|
||||||
case Control.Up:
|
Control.Up => SDL_Keycode.SDLK_w,
|
||||||
return SDL_Keycode.SDLK_w;
|
Control.Down => SDL_Keycode.SDLK_s,
|
||||||
case Control.Down:
|
Control.Left => SDL_Keycode.SDLK_a,
|
||||||
return SDL_Keycode.SDLK_s;
|
Control.Right => SDL_Keycode.SDLK_d,
|
||||||
case Control.Left:
|
Control.Stay => SDL_Keycode.SDLK_LCTRL,
|
||||||
return SDL_Keycode.SDLK_a;
|
Control.Confirm => SDL_Keycode.SDLK_SPACE,
|
||||||
case Control.Right:
|
_ => throw new ArgumentException("Invalid control"),
|
||||||
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");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -6,7 +6,7 @@ using mine2d.state;
|
||||||
|
|
||||||
namespace mine2d;
|
namespace mine2d;
|
||||||
|
|
||||||
class Mine2d : Game
|
public class Mine2d : Game
|
||||||
{
|
{
|
||||||
private readonly Context ctx;
|
private readonly Context ctx;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,12 @@
|
||||||
namespace mine2d;
|
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;
|
// bool isHost = true;
|
||||||
var game = new Mine2d(isHost);
|
var game = new Mine2d(isHost);
|
||||||
game.Run();
|
game.Run();
|
||||||
// var p = new Publisher(isHost ? InteractorKind.Server : InteractorKind.Client);
|
|
||||||
// p.Dump();
|
|
||||||
// Console.WriteLine("Hello World!");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -7,11 +7,11 @@ using WatsonTcp;
|
||||||
|
|
||||||
namespace mine2d.backend;
|
namespace mine2d.backend;
|
||||||
|
|
||||||
class Backend : IBackend
|
public class Backend : IBackend
|
||||||
{
|
{
|
||||||
private WatsonTcpServer server;
|
private WatsonTcpServer server;
|
||||||
private Publisher publisher;
|
private Publisher publisher;
|
||||||
private Queue<ValueType> pendingPackets = new();
|
private readonly Queue<ValueType> pendingPackets = new();
|
||||||
private uint tick;
|
private uint tick;
|
||||||
|
|
||||||
public void Process(double dt)
|
public void Process(double dt)
|
||||||
|
|
@ -77,10 +77,16 @@ class Backend : IBackend
|
||||||
private void SendGameState()
|
private void SendGameState()
|
||||||
{
|
{
|
||||||
if (this.server == null)
|
if (this.server == null)
|
||||||
|
{
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
var clients = this.server.ListClients().ToArray();
|
var clients = this.server.ListClients().ToArray();
|
||||||
if (!clients.Any())
|
if (!clients.Any())
|
||||||
|
{
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
var gameState = Context.Get().GameState;
|
var gameState = Context.Get().GameState;
|
||||||
var json = JsonConvert.SerializeObject(gameState);
|
var json = JsonConvert.SerializeObject(gameState);
|
||||||
var bytes = Encoding.UTF8.GetBytes(json);
|
var bytes = Encoding.UTF8.GetBytes(json);
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
namespace mine2d.backend;
|
namespace mine2d.backend;
|
||||||
|
|
||||||
interface IBackend
|
public interface IBackend
|
||||||
{
|
{
|
||||||
public void Process(double dt);
|
public void Process(double dt);
|
||||||
public void ProcessPacket(ValueType packet);
|
public void ProcessPacket(ValueType packet);
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ using mine2d.engine.system.annotations;
|
||||||
|
|
||||||
namespace mine2d.backend;
|
namespace mine2d.backend;
|
||||||
|
|
||||||
class Publisher
|
public class Publisher
|
||||||
{
|
{
|
||||||
private readonly Dictionary<string, HashSet<Delegate>> subscribers =
|
private readonly Dictionary<string, HashSet<Delegate>> subscribers =
|
||||||
new();
|
new();
|
||||||
|
|
@ -24,7 +24,7 @@ class Publisher
|
||||||
.GetTypesSafe();
|
.GetTypesSafe();
|
||||||
foreach (var type in types)
|
foreach (var type in types)
|
||||||
{
|
{
|
||||||
var attrs = type.GetCustomAttributes(typeof(Interactor), false);
|
var attrs = type.GetCustomAttributes(typeof(InteractorAttribute), false);
|
||||||
if (attrs.Length == 0)
|
if (attrs.Length == 0)
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
|
|
@ -32,12 +32,12 @@ class Publisher
|
||||||
var methods = type.GetMethods();
|
var methods = type.GetMethods();
|
||||||
foreach (var method in methods)
|
foreach (var method in methods)
|
||||||
{
|
{
|
||||||
var attrs2 = method.GetCustomAttributes(typeof(Interaction), false);
|
var attrs2 = method.GetCustomAttributes(typeof(InteractionAttribute), false);
|
||||||
if (attrs2.Length == 0)
|
if (attrs2.Length == 0)
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
var attr = (Interaction)attrs2[0];
|
var attr = (InteractionAttribute)attrs2[0];
|
||||||
if (attr.Kind != this.kind && this.kind != InteractorKind.Hybrid)
|
if (attr.Kind != this.kind && this.kind != InteractorKind.Hybrid)
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
|
|
|
||||||
|
|
@ -4,10 +4,10 @@ using mine2d.engine.system.annotations;
|
||||||
|
|
||||||
namespace mine2d.backend.interactor;
|
namespace mine2d.backend.interactor;
|
||||||
|
|
||||||
[Interactor]
|
[InteractorAttribute]
|
||||||
public class Breaking
|
public class Breaking
|
||||||
{
|
{
|
||||||
[Interaction(InteractorKind.Hybrid, "tick")]
|
[InteractionAttribute(InteractorKind.Hybrid, "tick")]
|
||||||
public static void TickHybrid()
|
public static void TickHybrid()
|
||||||
{
|
{
|
||||||
var ctx = Context.Get();
|
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)
|
public static void BreakServer(BreakPacket packet)
|
||||||
{
|
{
|
||||||
var ctx = Context.Get();
|
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)
|
if (player == null)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
|
|
|
||||||
|
|
@ -4,10 +4,10 @@ using mine2d.state;
|
||||||
|
|
||||||
namespace mine2d.backend.interactor;
|
namespace mine2d.backend.interactor;
|
||||||
|
|
||||||
[Interactor]
|
[InteractorAttribute]
|
||||||
class Connect
|
public class Connect
|
||||||
{
|
{
|
||||||
[Interaction(InteractorKind.Server, "connect")]
|
[InteractionAttribute(InteractorKind.Server, "connect")]
|
||||||
public static void ConnectServer(ConnectPacket packet)
|
public static void ConnectServer(ConnectPacket packet)
|
||||||
{
|
{
|
||||||
var ctx = Context.Get();
|
var ctx = Context.Get();
|
||||||
|
|
@ -18,7 +18,7 @@ class Connect
|
||||||
new Player
|
new Player
|
||||||
{
|
{
|
||||||
Name = packet.PlayerName,
|
Name = packet.PlayerName,
|
||||||
Guid = packet.PlayerGuid,
|
Id = packet.PlayerGuid,
|
||||||
Position = new Vector2(20, 16 * 16),
|
Position = new Vector2(20, 16 * 16),
|
||||||
Movement = new Vector2(0, 0)
|
Movement = new Vector2(0, 0)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,10 +4,10 @@ using mine2d.engine.system.annotations;
|
||||||
|
|
||||||
namespace mine2d.backend.interactor;
|
namespace mine2d.backend.interactor;
|
||||||
|
|
||||||
[Interactor]
|
[InteractorAttribute]
|
||||||
class Move
|
public class Move
|
||||||
{
|
{
|
||||||
[Interaction(InteractorKind.Hybrid, "move")]
|
[InteractionAttribute(InteractorKind.Hybrid, "move")]
|
||||||
public static void MoveHybrid(MovePacket packet)
|
public static void MoveHybrid(MovePacket packet)
|
||||||
{
|
{
|
||||||
var ctx = Context.Get();
|
var ctx = Context.Get();
|
||||||
|
|
@ -18,16 +18,16 @@ class Move
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[Interaction(InteractorKind.Hybrid, "tick")]
|
[InteractionAttribute(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(PlayerEntity.Move);
|
ctx.GameState.Players.ForEach(PlayerEntity.Move);
|
||||||
ctx.GameState.Players.ForEach(PlayerEntity.Collide);
|
ctx.GameState.Players.ForEach(PlayerEntity.Collide);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Interaction(InteractorKind.Client, "tick")]
|
[InteractionAttribute(InteractorKind.Client, "tick")]
|
||||||
public static void SelfMovedClient(TickPacket packet)
|
public static void SelfMovedClient()
|
||||||
{
|
{
|
||||||
var camera = Context.Get().FrontendGameState.Camera;
|
var camera = Context.Get().FrontendGameState.Camera;
|
||||||
camera.CenterOn(PlayerEntity.GetSelf().Position);
|
camera.CenterOn(PlayerEntity.GetSelf().Position);
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ using mine2d.core.world;
|
||||||
|
|
||||||
namespace mine2d.core;
|
namespace mine2d.core;
|
||||||
|
|
||||||
class Bootstrapper
|
public class Bootstrapper
|
||||||
{
|
{
|
||||||
public static void Bootstrap()
|
public static void Bootstrap()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
namespace mine2d.core;
|
namespace mine2d.core;
|
||||||
|
|
||||||
class Camera
|
public class Camera
|
||||||
{
|
{
|
||||||
public Vector2 Position;
|
public Vector2 Position { get; set; }
|
||||||
|
|
||||||
public Camera()
|
public Camera()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
namespace mine2d.core;
|
namespace mine2d.core;
|
||||||
|
|
||||||
class Constants
|
public class Constants
|
||||||
{
|
{
|
||||||
public const int ChunkSize = 32;
|
public const int ChunkSize = 32;
|
||||||
public const int TileSize = 16;
|
public const int TileSize = 16;
|
||||||
public static Vector2 Gravity = new Vector2(0, 0.1f);
|
public static Vector2 Gravity = new(0, 0.1f);
|
||||||
}
|
}
|
||||||
|
|
@ -2,18 +2,18 @@ using mine2d.state;
|
||||||
|
|
||||||
namespace mine2d.core;
|
namespace mine2d.core;
|
||||||
|
|
||||||
class PlayerEntity
|
public class PlayerEntity
|
||||||
{
|
{
|
||||||
public static bool IsSelf(Player p)
|
public static bool IsSelf(Player p)
|
||||||
{
|
{
|
||||||
return p.Guid == GetSelf().Guid;
|
return p.Id == GetSelf().Id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Player GetSelf()
|
public static Player GetSelf()
|
||||||
{
|
{
|
||||||
var ctx = Context.Get();
|
var ctx = Context.Get();
|
||||||
var player = ctx.GameState.Players.FirstOrDefault(
|
var player = ctx.GameState.Players.FirstOrDefault(
|
||||||
p => p.Guid == ctx.FrontendGameState.PlayerGuid
|
p => p.Id == ctx.FrontendGameState.PlayerGuid
|
||||||
);
|
);
|
||||||
return player;
|
return player;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
namespace mine2d.core.data;
|
namespace mine2d.core.data;
|
||||||
|
|
||||||
class Chunk
|
public class Chunk
|
||||||
{
|
{
|
||||||
public STile[,] Tiles { get; set; } = new STile[Constants.ChunkSize, Constants.ChunkSize];
|
public STile[,] Tiles { get; set; } = new STile[Constants.ChunkSize, Constants.ChunkSize];
|
||||||
public int X { get; set; }
|
public int X { get; set; }
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ using mine2d.core.tiles;
|
||||||
|
|
||||||
namespace mine2d.core.data;
|
namespace mine2d.core.data;
|
||||||
|
|
||||||
struct STile
|
public struct STile
|
||||||
{
|
{
|
||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
public int Hits { get; set; }
|
public int Hits { get; set; }
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
namespace mine2d.core.data;
|
namespace mine2d.core.data;
|
||||||
|
|
||||||
class World
|
public class World
|
||||||
{
|
{
|
||||||
public Dictionary<string, Chunk> Chunks { get; set; } = new Dictionary<string, Chunk>();
|
public Dictionary<string, Chunk> Chunks { get; set; } = new Dictionary<string, Chunk>();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ using mine2d.core.data;
|
||||||
|
|
||||||
namespace mine2d.core.tiles;
|
namespace mine2d.core.tiles;
|
||||||
|
|
||||||
class Tile
|
public class Tile
|
||||||
{
|
{
|
||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
public int Hardness { get; set; }
|
public int Hardness { get; set; }
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,11 @@
|
||||||
namespace mine2d.core.tiles;
|
namespace mine2d.core.tiles;
|
||||||
|
|
||||||
enum Tiles
|
public enum Tiles
|
||||||
{
|
{
|
||||||
Stone = 1,
|
Stone = 1,
|
||||||
}
|
}
|
||||||
|
|
||||||
class TileRegistry
|
public class TileRegistry
|
||||||
{
|
{
|
||||||
public Dictionary<int, Tile> Tiles { get; set; } = new();
|
public Dictionary<int, Tile> Tiles { get; set; } = new();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ using mine2d.core.data;
|
||||||
|
|
||||||
namespace mine2d.core.world;
|
namespace mine2d.core.world;
|
||||||
|
|
||||||
class ChunkGenerator
|
public class ChunkGenerator
|
||||||
{
|
{
|
||||||
public static Chunk CreateFilledChunk(int x, int y, STile fill)
|
public static Chunk CreateFilledChunk(int x, int y, STile fill)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,15 @@
|
||||||
namespace mine2d.engine;
|
namespace mine2d.engine;
|
||||||
|
|
||||||
enum Sound { }
|
public enum Sound { }
|
||||||
|
|
||||||
class AudioPlayer
|
public class AudioPlayer
|
||||||
{
|
{
|
||||||
private Dictionary<Sound, byte[]> audioFiles = new();
|
private readonly Dictionary<Sound, byte[]> audioFiles = new();
|
||||||
private ResourceLoader resourceLoader = new();
|
private readonly ResourceLoader resourceLoader = new();
|
||||||
|
|
||||||
public AudioPlayer()
|
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)
|
public void Register(Sound name, string path)
|
||||||
|
|
|
||||||
|
|
@ -1,29 +1,34 @@
|
||||||
|
using Mine2d.engine;
|
||||||
|
|
||||||
namespace mine2d.engine;
|
namespace mine2d.engine;
|
||||||
|
|
||||||
class FontManager
|
public class FontManager
|
||||||
{
|
{
|
||||||
private Dictionary<string, IntPtr> fonts = new();
|
private readonly Dictionary<string, IntPtr> fonts = new();
|
||||||
private ResourceLoader resourceLoader;
|
private readonly ResourceLoader resourceLoader;
|
||||||
|
|
||||||
public FontManager(ResourceLoader resourceLoader)
|
public FontManager(ResourceLoader resourceLoader)
|
||||||
{
|
{
|
||||||
this.resourceLoader = resourceLoader;
|
this.resourceLoader = resourceLoader;
|
||||||
if (TTF_Init() != 0)
|
if (TTF_Init() != 0)
|
||||||
{
|
{
|
||||||
throw new Exception("TTF_Init failed");
|
throw new SDLException(TTF_GetError());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RegisterFont(string name, string path, int fontSize)
|
public void RegisterFont(string name, string path, int fontSize)
|
||||||
{
|
{
|
||||||
if (this.fonts.ContainsKey(name))
|
if (this.fonts.ContainsKey(name))
|
||||||
|
{
|
||||||
return;
|
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);
|
var font = TTF_OpenFontRW(sdlBuffer, 1, fontSize);
|
||||||
if (font == IntPtr.Zero)
|
if (font == IntPtr.Zero)
|
||||||
{
|
{
|
||||||
throw new Exception("TTF_OpenFont failed");
|
throw new SDLException(TTF_GetError());
|
||||||
}
|
}
|
||||||
this.fonts.Add(name, font);
|
this.fonts.Add(name, font);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,11 @@
|
||||||
namespace mine2d.engine;
|
namespace mine2d.engine;
|
||||||
|
|
||||||
abstract class Game
|
public abstract class Game
|
||||||
{
|
{
|
||||||
public const int Tps = 128;
|
public const int Tps = 128;
|
||||||
|
|
||||||
private bool running = true;
|
private readonly bool running = true;
|
||||||
private Queue<int> fpsQueue = new();
|
private readonly Queue<int> fpsQueue = new();
|
||||||
protected abstract void Update(double dt);
|
protected abstract void Update(double dt);
|
||||||
protected abstract void Draw();
|
protected abstract void Draw();
|
||||||
|
|
||||||
|
|
@ -21,7 +21,10 @@ abstract class Game
|
||||||
var fps = (int)(1 / dt.TotalSeconds);
|
var fps = (int)(1 / dt.TotalSeconds);
|
||||||
this.fpsQueue.Enqueue(fps);
|
this.fpsQueue.Enqueue(fps);
|
||||||
while (this.fpsQueue.Count > fps)
|
while (this.fpsQueue.Count > fps)
|
||||||
|
{
|
||||||
this.fpsQueue.Dequeue();
|
this.fpsQueue.Dequeue();
|
||||||
|
}
|
||||||
|
|
||||||
while (tAcc >= TimeSpan.FromSeconds(1.0 / Tps))
|
while (tAcc >= TimeSpan.FromSeconds(1.0 / Tps))
|
||||||
{
|
{
|
||||||
this.Update(dt.TotalSeconds);
|
this.Update(dt.TotalSeconds);
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,9 @@
|
||||||
using mine2d.engine.utils;
|
using mine2d.engine.utils;
|
||||||
|
using Mine2d.engine;
|
||||||
|
|
||||||
namespace mine2d.engine;
|
namespace mine2d.engine;
|
||||||
|
|
||||||
class Renderer
|
public class Renderer
|
||||||
{
|
{
|
||||||
private readonly IntPtr renderer;
|
private readonly IntPtr renderer;
|
||||||
private IntPtr font;
|
private IntPtr font;
|
||||||
|
|
@ -15,12 +16,16 @@ class Renderer
|
||||||
-1,
|
-1,
|
||||||
SDL_RendererFlags.SDL_RENDERER_ACCELERATED
|
SDL_RendererFlags.SDL_RENDERER_ACCELERATED
|
||||||
);
|
);
|
||||||
|
if (this.renderer == IntPtr.Zero)
|
||||||
|
{
|
||||||
|
throw new SDLException(SDL_GetError());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Clear()
|
public void Clear()
|
||||||
{
|
{
|
||||||
SDL_SetRenderDrawColor(this.renderer, 0, 0, 0, 255);
|
ProcessStatus(SDL_SetRenderDrawColor(this.renderer, 0, 0, 0, 255));
|
||||||
SDL_RenderClear(this.renderer);
|
ProcessStatus(SDL_RenderClear(this.renderer));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Present()
|
public void Present()
|
||||||
|
|
@ -43,7 +48,7 @@ class Renderer
|
||||||
h = h
|
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)
|
public void DrawOutline(int x, int y, int w, int h)
|
||||||
|
|
@ -56,7 +61,7 @@ class Renderer
|
||||||
h = h
|
h = h
|
||||||
};
|
};
|
||||||
|
|
||||||
_ = SDL_RenderDrawRect(this.renderer, ref rect);
|
ProcessStatus(SDL_RenderDrawRect(this.renderer, ref rect));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DrawLines(double[][] points)
|
public void DrawLines(double[][] points)
|
||||||
|
|
@ -68,12 +73,12 @@ class Renderer
|
||||||
sdlPoints[i].y = (int)points[i][1];
|
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)
|
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)
|
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 surfaceMessage = TTF_RenderText_Solid(this.font, text, this.color);
|
||||||
var texture = SDL_CreateTextureFromSurface(this.renderer, surfaceMessage);
|
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
|
var rect = new SDL_Rect
|
||||||
{
|
{
|
||||||
|
|
@ -109,7 +114,7 @@ class Renderer
|
||||||
rect.y -= height / 2;
|
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_DestroyTexture(texture);
|
||||||
SDL_FreeSurface(surfaceMessage);
|
SDL_FreeSurface(surfaceMessage);
|
||||||
}
|
}
|
||||||
|
|
@ -133,7 +138,7 @@ class Renderer
|
||||||
w = w,
|
w = w,
|
||||||
h = h
|
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)
|
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,
|
w = srcWidth,
|
||||||
h = srcHeight,
|
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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -2,18 +2,18 @@ using System.Runtime.InteropServices;
|
||||||
|
|
||||||
namespace mine2d.engine;
|
namespace mine2d.engine;
|
||||||
|
|
||||||
class ResourceLoader
|
public class ResourceLoader
|
||||||
{
|
{
|
||||||
private string assemblyName;
|
private readonly string assemblyName;
|
||||||
|
|
||||||
public ResourceLoader()
|
public ResourceLoader()
|
||||||
{
|
{
|
||||||
this.assemblyName = this.GetType().Assembly.GetName().Name!;
|
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);
|
Console.WriteLine("Loading resource: " + resourceName);
|
||||||
return File.ReadAllText(ToPath(resourceName));
|
return File.ReadAllText(ToPath(resourceName));
|
||||||
#else
|
#else
|
||||||
|
|
@ -41,7 +41,7 @@ class ResourceLoader
|
||||||
return (ptr, bytes.Length);
|
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));
|
using var stream = new StreamWriter(ToPath(resourceName));
|
||||||
stream.Write(content);
|
stream.Write(content);
|
||||||
|
|
@ -50,6 +50,6 @@ class ResourceLoader
|
||||||
private static string ToPath(string resourceName)
|
private static string ToPath(string resourceName)
|
||||||
{
|
{
|
||||||
var s = resourceName.Split('.');
|
var s = resourceName.Split('.');
|
||||||
return String.Join('/', s[..^1]) + "." + s[^1];
|
return string.Join('/', s[..^1]) + "." + s[^1];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -0,0 +1,8 @@
|
||||||
|
namespace Mine2d.engine;
|
||||||
|
|
||||||
|
public class SDLException : Exception
|
||||||
|
{
|
||||||
|
public SDLException(string message) : base(message)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,9 +1,9 @@
|
||||||
namespace mine2d.engine;
|
namespace mine2d.engine;
|
||||||
|
|
||||||
struct Line
|
public struct Line
|
||||||
{
|
{
|
||||||
public Vector2 Start;
|
public Vector2 Start { get; set; }
|
||||||
public Vector2 End;
|
public Vector2 End { get; set; }
|
||||||
|
|
||||||
public Line(Vector2 start, Vector2 end)
|
public Line(Vector2 start, Vector2 end)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
namespace mine2d.engine;
|
namespace mine2d.engine;
|
||||||
|
|
||||||
class Window
|
public class Window
|
||||||
{
|
{
|
||||||
IntPtr window;
|
private readonly IntPtr window;
|
||||||
|
|
||||||
public Window(string title, int w, int h)
|
public Window(string title, int w, int h)
|
||||||
{
|
{
|
||||||
|
|
@ -33,9 +33,7 @@ class Window
|
||||||
|
|
||||||
public (int width, int height) GetSize()
|
public (int width, int height) GetSize()
|
||||||
{
|
{
|
||||||
int w,
|
SDL_GetWindowSize(this.window, out var w, out var h);
|
||||||
h;
|
|
||||||
SDL_GetWindowSize(this.window, out w, out h);
|
|
||||||
return (w, h);
|
return (w, h);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
namespace mine2d.engine.system;
|
namespace mine2d.engine.system;
|
||||||
|
|
||||||
enum EventPriority
|
public enum EventPriority
|
||||||
{
|
{
|
||||||
Lowest = 0,
|
Lowest = 0,
|
||||||
Low = 1,
|
Low = 1,
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
namespace mine2d.engine.system.annotations;
|
namespace mine2d.engine.system.annotations;
|
||||||
|
|
||||||
enum InteractorKind
|
public enum InteractorKind
|
||||||
{
|
{
|
||||||
Client,
|
Client,
|
||||||
Server,
|
Server,
|
||||||
|
|
@ -8,15 +8,15 @@ enum InteractorKind
|
||||||
}
|
}
|
||||||
|
|
||||||
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
|
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
|
||||||
class Interactor : Attribute { }
|
public class InteractorAttribute : Attribute { }
|
||||||
|
|
||||||
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
|
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
|
||||||
class Interaction : Attribute
|
public class InteractionAttribute : Attribute
|
||||||
{
|
{
|
||||||
public string Type;
|
public string Type { get; set; }
|
||||||
public InteractorKind Kind;
|
public InteractorKind Kind { get; set; }
|
||||||
|
|
||||||
public Interaction(InteractorKind kind, string type)
|
public InteractionAttribute(InteractorKind kind, string type)
|
||||||
{
|
{
|
||||||
this.Type = type;
|
this.Type = type;
|
||||||
this.Kind = kind;
|
this.Kind = kind;
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
namespace mine2d.engine.utils;
|
namespace mine2d.engine.utils;
|
||||||
|
|
||||||
class Color
|
public class Color
|
||||||
{
|
{
|
||||||
public int R,
|
public int R,
|
||||||
G,
|
G,
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
namespace mine2d.engine.utils;
|
namespace mine2d.engine.utils;
|
||||||
|
|
||||||
class Point
|
public class Point
|
||||||
{
|
{
|
||||||
public static double Distance(double x1, double y1, double x2, double y2)
|
public static double Distance(double x1, double y1, double x2, double y2)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,10 @@
|
||||||
using mine2d.backend.data;
|
using mine2d.backend.data;
|
||||||
using mine2d.core;
|
using mine2d.core;
|
||||||
using mine2d.core.data;
|
|
||||||
using mine2d.frontend.renderer;
|
using mine2d.frontend.renderer;
|
||||||
|
|
||||||
namespace mine2d.frontend;
|
namespace mine2d.frontend;
|
||||||
|
|
||||||
class Frontend : IFrontend
|
public class Frontend : IFrontend
|
||||||
{
|
{
|
||||||
public void Init()
|
public void Init()
|
||||||
{
|
{
|
||||||
|
|
@ -38,7 +37,7 @@ class Frontend : IFrontend
|
||||||
ctx.FrontendGameState.WindowHeight = e.window.data2;
|
ctx.FrontendGameState.WindowHeight = e.window.data2;
|
||||||
Console.WriteLine($"Window resized to {e.window.data1}x{e.window.data2}");
|
Console.WriteLine($"Window resized to {e.window.data1}x{e.window.data2}");
|
||||||
var player = ctx.GameState.Players.Find(
|
var player = ctx.GameState.Players.Find(
|
||||||
p => p.Guid == ctx.FrontendGameState.PlayerGuid
|
p => p.Id == ctx.FrontendGameState.PlayerGuid
|
||||||
);
|
);
|
||||||
ctx.FrontendGameState.Camera.CenterOn(player.Position);
|
ctx.FrontendGameState.Camera.CenterOn(player.Position);
|
||||||
}
|
}
|
||||||
|
|
@ -47,7 +46,7 @@ class Frontend : IFrontend
|
||||||
{
|
{
|
||||||
var mousePos = new Vector2(e.motion.x, e.motion.y);
|
var mousePos = new Vector2(e.motion.x, e.motion.y);
|
||||||
ctx.FrontendGameState.MousePosition = mousePos;
|
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;
|
var amp = ctx.FrontendGameState.MousePosition / ctx.FrontendGameState.Settings.GameScale + ctx.FrontendGameState.Camera.Position;
|
||||||
ctx.Backend.ProcessPacket(new BreakPacket(ctx.FrontendGameState.PlayerGuid, amp));
|
ctx.Backend.ProcessPacket(new BreakPacket(ctx.FrontendGameState.PlayerGuid, amp));
|
||||||
|
|
@ -66,18 +65,11 @@ class Frontend : IFrontend
|
||||||
{
|
{
|
||||||
var movementInput = ctx.FrontendGameState.MovementInput;
|
var movementInput = ctx.FrontendGameState.MovementInput;
|
||||||
if (e.key.keysym.scancode == SDL_Scancode.SDL_SCANCODE_F11)
|
if (e.key.keysym.scancode == SDL_Scancode.SDL_SCANCODE_F11)
|
||||||
{
|
|
||||||
if (!ctx.FrontendGameState.Settings.Fullscreen)
|
|
||||||
{
|
{
|
||||||
_ = SDL_SetWindowFullscreen(
|
_ = SDL_SetWindowFullscreen(
|
||||||
ctx.Window.GetRaw(),
|
ctx.Window.GetRaw(),
|
||||||
(uint)SDL_WindowFlags.SDL_WINDOW_FULLSCREEN_DESKTOP
|
ctx.FrontendGameState.Settings.Fullscreen ? 0 : (uint)SDL_WindowFlags.SDL_WINDOW_FULLSCREEN_DESKTOP
|
||||||
);
|
);
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
_ = SDL_SetWindowFullscreen(ctx.Window.GetRaw(), 0);
|
|
||||||
}
|
|
||||||
ctx.FrontendGameState.Settings.Fullscreen = !ctx.FrontendGameState.Settings.Fullscreen;
|
ctx.FrontendGameState.Settings.Fullscreen = !ctx.FrontendGameState.Settings.Fullscreen;
|
||||||
}
|
}
|
||||||
if (e.key.keysym.scancode == SDL_Scancode.SDL_SCANCODE_A)
|
if (e.key.keysym.scancode == SDL_Scancode.SDL_SCANCODE_A)
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
namespace mine2d.frontend;
|
namespace mine2d.frontend;
|
||||||
|
|
||||||
interface IFrontend
|
public interface IFrontend
|
||||||
{
|
{
|
||||||
public void Process();
|
public void Process();
|
||||||
public void Init();
|
public void Init();
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
namespace mine2d.frontend.renderer;
|
namespace mine2d.frontend.renderer;
|
||||||
|
|
||||||
interface IRenderer
|
public interface IRenderer
|
||||||
{
|
{
|
||||||
public void Render();
|
public void Render();
|
||||||
}
|
}
|
||||||
|
|
@ -2,7 +2,7 @@ using mine2d.core;
|
||||||
|
|
||||||
namespace mine2d.frontend.renderer;
|
namespace mine2d.frontend.renderer;
|
||||||
|
|
||||||
class WorldRenderer : IRenderer
|
public class WorldRenderer : IRenderer
|
||||||
{
|
{
|
||||||
public void Render()
|
public void Render()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,12 @@
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using mine2d.backend.data;
|
using mine2d.backend.data;
|
||||||
|
using Mine2d.network;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using Newtonsoft.Json.Linq;
|
using Newtonsoft.Json.Linq;
|
||||||
|
|
||||||
namespace mine2d.network;
|
namespace mine2d.network;
|
||||||
|
|
||||||
class Converter
|
public class Converter
|
||||||
{
|
{
|
||||||
public static ValueType ParsePacket(byte[] bytes)
|
public static ValueType ParsePacket(byte[] bytes)
|
||||||
{
|
{
|
||||||
|
|
@ -19,7 +20,7 @@ class Converter
|
||||||
var type = parsedRaw.GetValue("type");
|
var type = parsedRaw.GetValue("type");
|
||||||
if (type == null)
|
if (type == null)
|
||||||
{
|
{
|
||||||
throw new Exception("Packet has no type");
|
throw new PacketException("Packet has no type");
|
||||||
}
|
}
|
||||||
var packetType = type.Value<string>();
|
var packetType = type.Value<string>();
|
||||||
Console.WriteLine("Packet type: " + packetType);
|
Console.WriteLine("Packet type: " + packetType);
|
||||||
|
|
@ -27,7 +28,7 @@ class Converter
|
||||||
{
|
{
|
||||||
"move" => parsedRaw.ToObject<MovePacket>(),
|
"move" => parsedRaw.ToObject<MovePacket>(),
|
||||||
"connect" => parsedRaw.ToObject<ConnectPacket>(),
|
"connect" => parsedRaw.ToObject<ConnectPacket>(),
|
||||||
_ => throw new Exception("Unknown packet type")
|
_ => throw new PacketException("Unknown packet type")
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,8 @@
|
||||||
|
namespace Mine2d.network;
|
||||||
|
|
||||||
|
public class PacketException : Exception
|
||||||
|
{
|
||||||
|
public PacketException(string message) : base(message)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -3,28 +3,28 @@ using mine2d.core.data;
|
||||||
|
|
||||||
namespace mine2d.state;
|
namespace mine2d.state;
|
||||||
|
|
||||||
class FrontendGameState
|
public class FrontendGameState
|
||||||
{
|
{
|
||||||
public Vector2 MovementInput;
|
public Vector2 MovementInput { get; set; }
|
||||||
public Vector2 CameraPosition;
|
public Vector2 CameraPosition { get; set; }
|
||||||
public int WindowWidth;
|
public int WindowWidth { get; set; }
|
||||||
public int WindowHeight;
|
public int WindowHeight { get; set; }
|
||||||
public Guid PlayerGuid;
|
public Guid PlayerGuid { get; set; }
|
||||||
public Camera Camera = new Camera();
|
public Camera Camera { get; set; } = new();
|
||||||
public Vector2 MousePosition;
|
public Vector2 MousePosition { get; set; }
|
||||||
public Settings Settings { get; set; } = new Settings();
|
public Settings Settings { get; set; } = new Settings();
|
||||||
public string PlayerName { get; set; } = "Player";
|
public string PlayerName { get; set; } = "Player";
|
||||||
}
|
}
|
||||||
|
|
||||||
class Settings
|
public class Settings
|
||||||
{
|
{
|
||||||
public int GameScale = 4;
|
public int GameScale { get; set; } = 4;
|
||||||
public int UiScale = 4;
|
public int UiScale { get; set; } = 4;
|
||||||
public bool ShowCollision = true;
|
public bool ShowCollision { get; set; } = true;
|
||||||
public bool Fullscreen = false;
|
public bool Fullscreen { get; set; } = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
class GameState
|
public class GameState
|
||||||
{
|
{
|
||||||
public List<Player> Players { get; set; } = new List<Player>();
|
public List<Player> Players { get; set; } = new List<Player>();
|
||||||
public World World { get; set; }
|
public World World { get; set; }
|
||||||
|
|
|
||||||
|
|
@ -2,14 +2,14 @@ using mine2d.engine;
|
||||||
|
|
||||||
namespace mine2d.state;
|
namespace mine2d.state;
|
||||||
|
|
||||||
class Player
|
public class Player
|
||||||
{
|
{
|
||||||
public string Name;
|
public string Name { get; set; }
|
||||||
public Vector2 Position;
|
public Vector2 Position { get; set; }
|
||||||
public Vector2 Movement;
|
public Vector2 Movement { get; set; }
|
||||||
public Guid Guid;
|
public Guid Id { get; set; }
|
||||||
public Vector2 Mining;
|
public Vector2 Mining { get; set; }
|
||||||
public int MiningCooldown;
|
public int MiningCooldown { get; set; }
|
||||||
|
|
||||||
public Line GetBottomCollisionLine()
|
public Line GetBottomCollisionLine()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue