gamemode stub
This commit is contained in:
7
Source/Game/GameMode/GameMode.cs
Normal file
7
Source/Game/GameMode/GameMode.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace Game
|
||||
{
|
||||
public abstract class GameMode
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
19
Source/Game/GameMode/GameModeManager.cs
Normal file
19
Source/Game/GameMode/GameModeManager.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using Cabrito;
|
||||
using FlaxEngine.Networking;
|
||||
|
||||
namespace Game
|
||||
{
|
||||
public static class GameModeManager
|
||||
{
|
||||
public static void Init()
|
||||
{
|
||||
NetworkManager.OnMessage += OnClientConnected;
|
||||
}
|
||||
|
||||
public static bool OnClientConnected(NetworkMessage message)
|
||||
{
|
||||
Console.Print("client connected");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using FlaxEditor;
|
||||
@@ -30,6 +31,14 @@ namespace Game
|
||||
|
||||
// NetworkMulticastAttribute: calls methods marked with this in all clients
|
||||
|
||||
public enum NetworkMessageType : byte
|
||||
{
|
||||
Handshake = 1,
|
||||
Message,
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static partial class NetworkManager
|
||||
{
|
||||
private static bool initialized = false;
|
||||
@@ -42,7 +51,8 @@ namespace Game
|
||||
private static ushort MTU = 1500;
|
||||
private static ushort MaximumClients = 32;
|
||||
|
||||
public static uint LocalPlayerClientId { get; private set; } = 0;
|
||||
public delegate bool OnMessageDecl(NetworkMessage message);
|
||||
public static OnMessageDecl OnMessage;
|
||||
|
||||
public static void Init()
|
||||
{
|
||||
@@ -68,6 +78,7 @@ namespace Game
|
||||
#endif
|
||||
|
||||
initialized = true;
|
||||
GameModeManager.Init(); // FIXME
|
||||
}
|
||||
|
||||
public static void Deinitialize()
|
||||
@@ -90,5 +101,44 @@ namespace Game
|
||||
client = null;
|
||||
}
|
||||
}
|
||||
|
||||
private static void OnNetworkMessage(NetworkEvent networkEvent)
|
||||
{
|
||||
byte messageTypeByte = networkEvent.Message.ReadByte();
|
||||
if (!Enum.IsDefined(typeof(NetworkMessageType), messageTypeByte))
|
||||
{
|
||||
Console.PrintError($"Unsupported message type received from client: {messageTypeByte}");
|
||||
return;
|
||||
}
|
||||
|
||||
NetworkMessageType messageType = (NetworkMessageType)messageTypeByte;
|
||||
|
||||
switch (messageType)
|
||||
{
|
||||
case NetworkMessageType.Handshake:
|
||||
{
|
||||
var message = networkEvent.Message.ReadString();
|
||||
Console.Print($"Received handshake from {networkEvent.Sender.ConnectionId}, msg: " + message);
|
||||
break;
|
||||
}
|
||||
case NetworkMessageType.Message:
|
||||
{
|
||||
if (OnMessage != null)
|
||||
{
|
||||
foreach (OnMessageDecl func in OnMessage.GetInvocationList()
|
||||
.Cast<OnMessageDecl>().ToArray())
|
||||
{
|
||||
bool ret = func.Invoke(networkEvent.Message);
|
||||
if (ret)
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
Console.PrintError($"Unsupported message type received from client: {messageTypeByte}");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using FlaxEngine;
|
||||
@@ -11,6 +12,8 @@ namespace Game
|
||||
{
|
||||
public static partial class NetworkManager
|
||||
{
|
||||
public static uint LocalPlayerClientId { get; private set; } = 0;
|
||||
|
||||
public static bool ConnectServer()
|
||||
{
|
||||
client = NetworkPeer.CreatePeer(new NetworkConfig
|
||||
@@ -38,14 +41,14 @@ namespace Game
|
||||
{
|
||||
using var _ = Utilities.ProfileScope("NetworkManager_OnClientUpdate");
|
||||
|
||||
while (client.PopEvent(out NetworkEvent eventData))
|
||||
while (client.PopEvent(out NetworkEvent networkEvent))
|
||||
{
|
||||
switch (eventData.EventType)
|
||||
switch (networkEvent.EventType)
|
||||
{
|
||||
case NetworkEventType.Connected:
|
||||
{
|
||||
LocalPlayerClientId = eventData.Sender.ConnectionId;
|
||||
Console.Print("Connected to server, ConnectionId: " + eventData.Sender.ConnectionId);
|
||||
LocalPlayerClientId = networkEvent.Sender.ConnectionId;
|
||||
Console.Print("Connected to server, ConnectionId: " + networkEvent.Sender.ConnectionId);
|
||||
break;
|
||||
}
|
||||
case NetworkEventType.Disconnected:
|
||||
@@ -62,21 +65,8 @@ namespace Game
|
||||
}
|
||||
case NetworkEventType.Message:
|
||||
{
|
||||
// Read the message contents
|
||||
var message = eventData.Message;
|
||||
var messageData = message.ReadString();
|
||||
|
||||
Console.Print($"Received message from Client({eventData.Sender.ConnectionId}): {messageData}");
|
||||
|
||||
// Send hello message to the client back
|
||||
{
|
||||
var sendmessage = client.BeginSendMessage();
|
||||
sendmessage.WriteString($"Hello, Server({eventData.Sender.ConnectionId})!");
|
||||
client.EndSendMessage(NetworkChannelType.Reliable, sendmessage);
|
||||
}
|
||||
|
||||
client.RecycleMessage(message);
|
||||
|
||||
OnNetworkMessage(networkEvent);
|
||||
client.RecycleMessage(networkEvent.Message);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
|
||||
@@ -81,43 +81,39 @@ namespace Game
|
||||
{
|
||||
using var _ = Utilities.ProfileScope("NetworkManager_OnServerUpdate");
|
||||
|
||||
while (server.PopEvent(out NetworkEvent eventData))
|
||||
while (server.PopEvent(out NetworkEvent networkEvent))
|
||||
{
|
||||
switch (eventData.EventType)
|
||||
switch (networkEvent.EventType)
|
||||
{
|
||||
case NetworkEventType.Connected:
|
||||
{
|
||||
Console.Print($"Client({eventData.Sender.ConnectionId}) connected!");
|
||||
Console.Print($"Client({networkEvent.Sender.ConnectionId}) connected!");
|
||||
|
||||
ConnectedClients.Add(eventData.Sender);
|
||||
ConnectedClients.Add(networkEvent.Sender);
|
||||
Console.Print("Connected clients: " + ConnectedClients.Count);
|
||||
|
||||
// Send hello message to the client back
|
||||
{
|
||||
var sendmessage = server.BeginSendMessage();
|
||||
sendmessage.WriteString($"Welcome, ({eventData.Sender.ConnectionId})");
|
||||
server.EndSendMessage(NetworkChannelType.Reliable, sendmessage, eventData.Sender);
|
||||
NetworkMessage sendmessage = server.BeginSendMessage();
|
||||
sendmessage.WriteByte((byte)NetworkMessageType.Message);
|
||||
//sendmessage.WriteString($"Welcome, ({networkEvent.Sender.ConnectionId})");
|
||||
server.EndSendMessage(NetworkChannelType.Reliable, sendmessage, networkEvent.Sender);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case NetworkEventType.Disconnected:
|
||||
case NetworkEventType.Timeout:
|
||||
{
|
||||
Console.Print($"Client({eventData.Sender.ConnectionId}) disconnected!");
|
||||
Console.Print($"Client({networkEvent.Sender.ConnectionId}) disconnected!");
|
||||
|
||||
ConnectedClients.Remove(eventData.Sender);
|
||||
ConnectedClients.Remove(networkEvent.Sender);
|
||||
Console.Print("Connected clients: " + ConnectedClients.Count);
|
||||
break;
|
||||
}
|
||||
case NetworkEventType.Message:
|
||||
{
|
||||
// Read the message contents
|
||||
var message = eventData.Message;
|
||||
var messageData = message.ReadString();
|
||||
|
||||
Console.Print($"Received message from Client({eventData.Sender.ConnectionId}): {messageData}");
|
||||
|
||||
server.RecycleMessage(message);
|
||||
OnNetworkMessage(networkEvent);
|
||||
server.RecycleMessage(networkEvent.Message);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
|
||||
@@ -29,7 +29,7 @@ public class GameTarget : GameProjectTarget
|
||||
|
||||
public override string GetOutputFilePath(BuildOptions options, TargetOutputType? outputType = null)
|
||||
{
|
||||
// For IDE builds only, these do not work during cooking
|
||||
// For IDE builds only, these do not work during cooking (fails to update cooked files in multiple ways)
|
||||
if (options.Configuration == TargetConfiguration.Development)
|
||||
options.OutputFolder = @"C:\dev\GoakeFlax\Output\WindowsDevelopment";
|
||||
else if (options.Configuration == TargetConfiguration.Release)
|
||||
|
||||
Reference in New Issue
Block a user