added scanning
This commit is contained in:
parent
f96e19b407
commit
f720ac17a9
|
|
@ -4,7 +4,10 @@
|
||||||
{
|
{
|
||||||
bool isHost = args.Contains("--host");
|
bool isHost = args.Contains("--host");
|
||||||
// bool isHost = true;
|
// bool isHost = true;
|
||||||
var game = new MultiPlayerGame(isHost);
|
// var game = new MultiPlayerGame(isHost);
|
||||||
game.Run();
|
// game.Run();
|
||||||
|
var p = new Publisher(isHost ? InteractorKind.Server : InteractorKind.Client);
|
||||||
|
p.Dump();
|
||||||
|
Console.WriteLine("Hello World!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@
|
||||||
<RootNamespace>multiplayer-game</RootNamespace>
|
<RootNamespace>multiplayer-game</RootNamespace>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<PublishTrimmed>true</PublishTrimmed>
|
<PublishTrimmed>true</PublishTrimmed>
|
||||||
|
<TrimmerDefaultAction>link</TrimmerDefaultAction>
|
||||||
<PublishReadyToRun>true</PublishReadyToRun>
|
<PublishReadyToRun>true</PublishReadyToRun>
|
||||||
<PublishSingleFile>true</PublishSingleFile>
|
<PublishSingleFile>true</PublishSingleFile>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
|
|
|
||||||
|
|
@ -6,15 +6,21 @@ using WatsonTcp;
|
||||||
class Backend : IBackend
|
class Backend : IBackend
|
||||||
{
|
{
|
||||||
private WatsonTcpServer server;
|
private WatsonTcpServer server;
|
||||||
|
private Queue<ValueType> pendingPackets = new Queue<ValueType>();
|
||||||
|
|
||||||
public void Process(double dt)
|
public void Process(double dt)
|
||||||
{
|
{
|
||||||
var ctx = Context.Get();
|
var ctx = Context.Get();
|
||||||
|
while (pendingPackets.Count > 0)
|
||||||
|
{
|
||||||
|
var packet = pendingPackets.Dequeue();
|
||||||
|
this.ProcessPacket(packet);
|
||||||
|
}
|
||||||
ctx.GameState.PlayerPositions.ForEach(player =>
|
ctx.GameState.PlayerPositions.ForEach(player =>
|
||||||
{
|
{
|
||||||
player.position += player.movement;
|
player.position += player.movement;
|
||||||
});
|
});
|
||||||
this.sendGameState();
|
// this.sendGameState();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ProcessPacket(ValueType packet)
|
public void ProcessPacket(ValueType packet)
|
||||||
|
|
@ -83,7 +89,7 @@ class Backend : IBackend
|
||||||
Console.WriteLine("Received packet: " + packet);
|
Console.WriteLine("Received packet: " + packet);
|
||||||
if (packet != null)
|
if (packet != null)
|
||||||
{
|
{
|
||||||
this.ProcessPacket(packet);
|
pendingPackets.Enqueue(packet);
|
||||||
}
|
}
|
||||||
Console.WriteLine(DateTime.Now - time);
|
Console.WriteLine(DateTime.Now - time);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,66 @@
|
||||||
|
class Publisher
|
||||||
|
{
|
||||||
|
private Dictionary<string, HashSet<Delegate>> subscribers =
|
||||||
|
new Dictionary<string, HashSet<Delegate>>();
|
||||||
|
private InteractorKind kind;
|
||||||
|
|
||||||
|
public Publisher(InteractorKind kind)
|
||||||
|
{
|
||||||
|
this.kind = kind;
|
||||||
|
this.scan();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void scan()
|
||||||
|
{
|
||||||
|
var assembly = this.GetType().Assembly;
|
||||||
|
var types = assembly.GetTypes();
|
||||||
|
foreach (var type in types)
|
||||||
|
{
|
||||||
|
var attrs = type.GetCustomAttributes(typeof(Interactor), false);
|
||||||
|
if (attrs.Length == 0)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
var methods = type.GetMethods();
|
||||||
|
foreach (var method in methods)
|
||||||
|
{
|
||||||
|
var attrs2 = method.GetCustomAttributes(typeof(Interaction), false);
|
||||||
|
if (attrs2.Length == 0)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
var attr = (Interaction)attrs2[0];
|
||||||
|
if (attr.Kind != this.kind && attr.Kind != InteractorKind.Hybrid)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
var del = Delegate.CreateDelegate(
|
||||||
|
typeof(Action<>).MakeGenericType(method.GetParameters()[0].ParameterType),
|
||||||
|
method
|
||||||
|
);
|
||||||
|
this.subscribe(attr.Type, del);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void subscribe(string type, Delegate callback)
|
||||||
|
{
|
||||||
|
if (!subscribers.ContainsKey(type))
|
||||||
|
{
|
||||||
|
subscribers[type] = new HashSet<Delegate>();
|
||||||
|
}
|
||||||
|
subscribers[type].Add(callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dump()
|
||||||
|
{
|
||||||
|
foreach (var pair in subscribers)
|
||||||
|
{
|
||||||
|
Console.WriteLine(pair.Key);
|
||||||
|
foreach (var del in pair.Value)
|
||||||
|
{
|
||||||
|
Console.WriteLine(del);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -8,11 +8,11 @@ class RemoteBackend : IBackend
|
||||||
|
|
||||||
public void Process(double dt)
|
public void Process(double dt)
|
||||||
{
|
{
|
||||||
// var ctx = Context.Get();
|
var ctx = Context.Get();
|
||||||
// ctx.GameState.PlayerPositions.ForEach(player =>
|
ctx.GameState.PlayerPositions.ForEach(player =>
|
||||||
// {
|
{
|
||||||
// player.position += player.movement;
|
player.position += player.movement;
|
||||||
// });
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ProcessPacket(ValueType packet)
|
public void ProcessPacket(ValueType packet)
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,23 @@
|
||||||
|
using System.Numerics;
|
||||||
|
|
||||||
|
[Interactor]
|
||||||
|
class Connect
|
||||||
|
{
|
||||||
|
[Interaction(InteractorKind.Server, "connect")]
|
||||||
|
public static void ConnectServer(ConnectPacket packet)
|
||||||
|
{
|
||||||
|
var ctx = Context.Get();
|
||||||
|
var player = ctx.GameState.PlayerPositions.Find(p => p.name == packet.playerName);
|
||||||
|
if (player == null)
|
||||||
|
{
|
||||||
|
ctx.GameState.PlayerPositions.Add(
|
||||||
|
new Player
|
||||||
|
{
|
||||||
|
name = packet.playerName,
|
||||||
|
position = new Vector2(0, 0),
|
||||||
|
movement = new Vector2(0, 0)
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,14 @@
|
||||||
|
[Interactor]
|
||||||
|
class Move
|
||||||
|
{
|
||||||
|
[Interaction(InteractorKind.Hybrid, "move")]
|
||||||
|
public static void MoveHybrid(MovePacket packet)
|
||||||
|
{
|
||||||
|
var ctx = Context.Get();
|
||||||
|
var player = ctx.GameState.PlayerPositions.Find(p => p.name == packet.playerName);
|
||||||
|
if (player != null)
|
||||||
|
{
|
||||||
|
player.movement = packet.movement * 4;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,33 +0,0 @@
|
||||||
[AttributeUsage(AttributeTargets.Method)]
|
|
||||||
class OnECSEvent : Attribute
|
|
||||||
{
|
|
||||||
public EventPriority Priority = EventPriority.Normal;
|
|
||||||
}
|
|
||||||
|
|
||||||
class OnTickEvent : OnECSEvent { }
|
|
||||||
|
|
||||||
struct TickEvent
|
|
||||||
{
|
|
||||||
public int Tick;
|
|
||||||
}
|
|
||||||
|
|
||||||
class OnKeyDownEvent : OnECSEvent
|
|
||||||
{
|
|
||||||
public SDL2.SDL.SDL_Scancode? Scancode;
|
|
||||||
}
|
|
||||||
|
|
||||||
class KeyEvent
|
|
||||||
{
|
|
||||||
public SDL2.SDL.SDL_Scancode Scancode;
|
|
||||||
public bool Canceled;
|
|
||||||
}
|
|
||||||
|
|
||||||
class OnKeyUpEvent : OnECSEvent
|
|
||||||
{
|
|
||||||
public SDL2.SDL.SDL_Scancode? Scancode;
|
|
||||||
}
|
|
||||||
|
|
||||||
class OnMouseButtonDownEvent : OnECSEvent
|
|
||||||
{
|
|
||||||
public SDL2.SDL.SDL_Scancode? Scancode;
|
|
||||||
}
|
|
||||||
|
|
@ -1,28 +0,0 @@
|
||||||
class ECSystemRegistry
|
|
||||||
{
|
|
||||||
private HashSet<object> hybridSystems;
|
|
||||||
private HashSet<object> clientSystems;
|
|
||||||
private HashSet<object> serverSystems;
|
|
||||||
|
|
||||||
public ECSystemRegistry()
|
|
||||||
{
|
|
||||||
hybridSystems = new HashSet<object>();
|
|
||||||
clientSystems = new HashSet<object>();
|
|
||||||
serverSystems = new HashSet<object>();
|
|
||||||
object obj = new object();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void RegisterSystem(object system, ECSystemKind kind)
|
|
||||||
{
|
|
||||||
this.GetSystems(kind).Add(system);
|
|
||||||
}
|
|
||||||
|
|
||||||
public HashSet<object> GetSystems(ECSystemKind kind) =>
|
|
||||||
kind switch
|
|
||||||
{
|
|
||||||
ECSystemKind.Hybrid => hybridSystems,
|
|
||||||
ECSystemKind.Client => clientSystems,
|
|
||||||
ECSystemKind.Server => serverSystems,
|
|
||||||
_ => throw new ArgumentException("Invalid ECSystemKind")
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
@ -0,0 +1,22 @@
|
||||||
|
enum InteractorKind
|
||||||
|
{
|
||||||
|
Client,
|
||||||
|
Server,
|
||||||
|
Hybrid
|
||||||
|
}
|
||||||
|
|
||||||
|
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
|
||||||
|
class Interactor : Attribute { }
|
||||||
|
|
||||||
|
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
|
||||||
|
class Interaction : Attribute
|
||||||
|
{
|
||||||
|
public string Type;
|
||||||
|
public InteractorKind Kind;
|
||||||
|
|
||||||
|
public Interaction(InteractorKind kind, string type)
|
||||||
|
{
|
||||||
|
this.Type = type;
|
||||||
|
this.Kind = kind;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,17 +0,0 @@
|
||||||
enum ECSystemKind
|
|
||||||
{
|
|
||||||
Client,
|
|
||||||
Server,
|
|
||||||
Hybrid
|
|
||||||
}
|
|
||||||
|
|
||||||
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
|
|
||||||
class ECSystem : Attribute
|
|
||||||
{
|
|
||||||
public ECSystemKind Kind;
|
|
||||||
|
|
||||||
public ECSystem(ECSystemKind kind)
|
|
||||||
{
|
|
||||||
this.Kind = kind;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -21,12 +21,6 @@ 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;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue