This commit is contained in:
MasterGordon 2022-10-26 22:25:49 +02:00
parent ceeeb070e9
commit f96e19b407
13 changed files with 98 additions and 53 deletions

View File

@ -4,7 +4,7 @@ class MultiPlayerGame : Game
public MultiPlayerGame(bool isHost) public MultiPlayerGame(bool isHost)
{ {
var window = new Window("MultiPlayerGame", 1200, 800); var window = new Window("MultiPlayerGame" + (isHost ? " - host" : ""), 1200, 800);
if (isHost) if (isHost)
{ {
this.ctx = new Context( this.ctx = new Context(

View File

@ -6,11 +6,9 @@ using WatsonTcp;
class Backend : IBackend class Backend : IBackend
{ {
private WatsonTcpServer server; private WatsonTcpServer server;
private int nextId = 0;
public void Process(double dt) public void Process(double dt)
{ {
nextId++;
var ctx = Context.Get(); var ctx = Context.Get();
ctx.GameState.PlayerPositions.ForEach(player => ctx.GameState.PlayerPositions.ForEach(player =>
{ {
@ -24,20 +22,25 @@ class Backend : IBackend
var gameState = Context.Get().GameState; var gameState = Context.Get().GameState;
if (packet is MovePacket movePacket) if (packet is MovePacket movePacket)
{ {
if (gameState.PlayerPositions.Find(player => player.name == movePacket.playerName) is Player player) if (
gameState.PlayerPositions.Find(player => player.name == movePacket.playerName)
is Player player
)
{ {
player.movement = movePacket.movement; player.movement = movePacket.movement * 4;
} }
} }
if (packet is ConnectPacket connectPacket) if (packet is ConnectPacket connectPacket)
{ {
Console.WriteLine($"Player {connectPacket.playerName} connected"); Console.WriteLine($"Player {connectPacket.playerName} connected");
gameState.PlayerPositions.Add(new Player gameState.PlayerPositions.Add(
new Player
{ {
name = connectPacket.playerName, name = connectPacket.playerName,
position = new Vector2(50, 50), position = new Vector2(50, 50),
movement = new Vector2(0, 0) movement = new Vector2(0, 0)
}); }
);
} }
this.sendGameState(); this.sendGameState();
} }
@ -74,6 +77,7 @@ class Backend : IBackend
private void messageReceived(object? sender, MessageReceivedEventArgs args) private void messageReceived(object? sender, MessageReceivedEventArgs args)
{ {
var time = DateTime.Now;
Console.WriteLine("Message Received: " + args.IpPort); Console.WriteLine("Message Received: " + args.IpPort);
var packet = Converter.ParsePacket(args.Data); var packet = Converter.ParsePacket(args.Data);
Console.WriteLine("Received packet: " + packet); Console.WriteLine("Received packet: " + packet);
@ -81,13 +85,16 @@ class Backend : IBackend
{ {
this.ProcessPacket(packet); this.ProcessPacket(packet);
} }
Console.WriteLine(DateTime.Now - time);
} }
private void sendGameState() private void sendGameState()
{ {
if (server == null) return; if (server == null)
return;
var clients = server.ListClients(); var clients = server.ListClients();
if (clients.Count() == 0) return; if (clients.Count() == 0)
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);

View File

@ -8,7 +8,11 @@ class RemoteBackend : IBackend
public void Process(double dt) public void Process(double dt)
{ {
// var ctx = Context.Get();
// ctx.GameState.PlayerPositions.ForEach(player =>
// {
// player.position += player.movement;
// });
} }
public void ProcessPacket(ValueType packet) public void ProcessPacket(ValueType packet)
@ -19,6 +23,11 @@ class RemoteBackend : IBackend
} }
public void Init() public void Init()
{
Task.Run(this.Run);
}
public void Run()
{ {
client = new WatsonTcpClient("127.0.0.1", 42069); client = new WatsonTcpClient("127.0.0.1", 42069);
client.Events.MessageReceived += (sender, args) => client.Events.MessageReceived += (sender, args) =>

View File

@ -1,6 +1,4 @@
enum Sound enum Sound { }
{
}
class AudioPlayer class AudioPlayer
{ {

View File

@ -17,7 +17,8 @@ class FontManager
public void RegisterFont(string name, string path, int fontSize) public void RegisterFont(string name, string path, int fontSize)
{ {
if (fonts.ContainsKey(name)) return; if (fonts.ContainsKey(name))
return;
var res = resourceLoader.LoadToIntPtr(path); var res = resourceLoader.LoadToIntPtr(path);
var sdlBuffer = SDL_RWFromConstMem(res.ptr, res.size); var sdlBuffer = SDL_RWFromConstMem(res.ptr, res.size);
var font = TTF_OpenFontRW(sdlBuffer, 1, fontSize); var font = TTF_OpenFontRW(sdlBuffer, 1, fontSize);

View File

@ -1,6 +1,6 @@
abstract class Game abstract class Game
{ {
public const int TPS = 64; public const int TPS = 128;
private Queue<int> fpsQueue = new Queue<int>(); private Queue<int> fpsQueue = new Queue<int>();
protected abstract void update(double dt); protected abstract void update(double dt);
protected abstract void draw(); protected abstract void draw();

View File

@ -9,7 +9,11 @@ class Renderer
public Renderer(Window window) public Renderer(Window window)
{ {
this.renderer = SDL_CreateRenderer(window.GetWindow(), -1, SDL_RendererFlags.SDL_RENDERER_ACCELERATED); this.renderer = SDL_CreateRenderer(
window.GetWindow(),
-1,
SDL_RendererFlags.SDL_RENDERER_ACCELERATED
);
} }
public void Clear() public void Clear()

View File

@ -15,14 +15,16 @@ class ResourceLoader
Console.WriteLine("Loading resource: " + resourceName); Console.WriteLine("Loading resource: " + resourceName);
return File.ReadAllText(ToPath(resourceName)); return File.ReadAllText(ToPath(resourceName));
#endif #endif
using var stream = this.GetType().Assembly.GetManifestResourceStream($"{this.assemblyName}.{resourceName}"); using var stream = this.GetType()
.Assembly.GetManifestResourceStream($"{this.assemblyName}.{resourceName}");
using var reader = new StreamReader(stream!); using var reader = new StreamReader(stream!);
return reader.ReadToEnd(); return reader.ReadToEnd();
} }
public byte[] LoadBytes(string resourceName) public byte[] LoadBytes(string resourceName)
{ {
using var stream = this.GetType().Assembly.GetManifestResourceStream($"{this.assemblyName}.{resourceName}"); using var stream = this.GetType()
.Assembly.GetManifestResourceStream($"{this.assemblyName}.{resourceName}");
using var memoryStream = new MemoryStream(); using var memoryStream = new MemoryStream();
stream!.CopyTo(memoryStream); stream!.CopyTo(memoryStream);
return memoryStream.ToArray(); return memoryStream.ToArray();

View File

@ -6,7 +6,14 @@ class Window
public Window(string title, int w, int h) public Window(string title, int w, int h)
{ {
window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, SDL_WindowFlags.SDL_WINDOW_VULKAN | SDL_WindowFlags.SDL_WINDOW_RESIZABLE); window = SDL_CreateWindow(
title,
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
w,
h,
SDL_WindowFlags.SDL_WINDOW_VULKAN | SDL_WindowFlags.SDL_WINDOW_RESIZABLE
);
} }
public Window(string title, int x, int y, int w, int h, SDL_WindowFlags flags) public Window(string title, int x, int y, int w, int h, SDL_WindowFlags flags)
@ -26,7 +33,8 @@ class Window
public (int width, int height) GetSize() public (int width, int height) GetSize()
{ {
int w, h; int w,
h;
SDL_GetWindowSize(this.window, out w, out h); SDL_GetWindowSize(this.window, out w, out h);
return (w, h); return (w, h);
} }

View File

@ -4,9 +4,7 @@ class OnECSEvent : Attribute
public EventPriority Priority = EventPriority.Normal; public EventPriority Priority = EventPriority.Normal;
} }
class OnTickEvent : OnECSEvent class OnTickEvent : OnECSEvent { }
{
}
struct TickEvent struct TickEvent
{ {

View File

@ -2,7 +2,10 @@ using static SDL2.SDL;
class Color class Color
{ {
int r, g, b, a; int r,
g,
b,
a;
public Color(int r, int g, int b, int a) public Color(int r, int g, int b, int a)
{ {

View File

@ -21,6 +21,12 @@ class Frontend : IFrontend
{ {
System.Environment.Exit(0); System.Environment.Exit(0);
} }
// Console.WriteLine("new key event");
// Console.WriteLine(e.key.repeat);
// Console.WriteLine(e.key.keysym.scancode);
// Console.WriteLine(e.key.keysym.sym);
// Console.WriteLine(e.key.keysym.unicode);
// Console.WriteLine(e.type);
if (e.type == SDL_EventType.SDL_KEYDOWN && e.key.repeat == 0) if (e.type == SDL_EventType.SDL_KEYDOWN && e.key.repeat == 0)
{ {
var movementInput = ctx.FrontendGameState.movementInput; var movementInput = ctx.FrontendGameState.movementInput;
@ -63,9 +69,18 @@ class Frontend : IFrontend
} }
ctx.FrontendGameState.movementInput = movementInput; ctx.FrontendGameState.movementInput = movementInput;
} }
if (e.key.keysym.scancode is if (
SDL_Scancode.SDL_SCANCODE_A or SDL_Scancode.SDL_SCANCODE_D or SDL_Scancode.SDL_SCANCODE_W or SDL_Scancode.SDL_SCANCODE_S) e.key.keysym.scancode
is SDL_Scancode.SDL_SCANCODE_A
or SDL_Scancode.SDL_SCANCODE_D
or SDL_Scancode.SDL_SCANCODE_W
or SDL_Scancode.SDL_SCANCODE_S
&& e.key.repeat == 0
&& e.type is SDL_EventType.SDL_KEYDOWN or SDL_EventType.SDL_KEYUP
)
{ {
if (e.key.repeat == 1)
continue;
var movement = ctx.FrontendGameState.movementInput; var movement = ctx.FrontendGameState.movementInput;
if (movement.Length() > 0) if (movement.Length() > 0)
movement = Vector2.Normalize(movement); movement = Vector2.Normalize(movement);