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;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using FlaxEditor;
|
using FlaxEditor;
|
||||||
@@ -30,6 +31,14 @@ namespace Game
|
|||||||
|
|
||||||
// NetworkMulticastAttribute: calls methods marked with this in all clients
|
// NetworkMulticastAttribute: calls methods marked with this in all clients
|
||||||
|
|
||||||
|
public enum NetworkMessageType : byte
|
||||||
|
{
|
||||||
|
Handshake = 1,
|
||||||
|
Message,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public static partial class NetworkManager
|
public static partial class NetworkManager
|
||||||
{
|
{
|
||||||
private static bool initialized = false;
|
private static bool initialized = false;
|
||||||
@@ -42,7 +51,8 @@ namespace Game
|
|||||||
private static ushort MTU = 1500;
|
private static ushort MTU = 1500;
|
||||||
private static ushort MaximumClients = 32;
|
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()
|
public static void Init()
|
||||||
{
|
{
|
||||||
@@ -68,6 +78,7 @@ namespace Game
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
initialized = true;
|
initialized = true;
|
||||||
|
GameModeManager.Init(); // FIXME
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Deinitialize()
|
public static void Deinitialize()
|
||||||
@@ -90,5 +101,44 @@ namespace Game
|
|||||||
client = null;
|
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;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using FlaxEngine;
|
using FlaxEngine;
|
||||||
@@ -11,6 +12,8 @@ namespace Game
|
|||||||
{
|
{
|
||||||
public static partial class NetworkManager
|
public static partial class NetworkManager
|
||||||
{
|
{
|
||||||
|
public static uint LocalPlayerClientId { get; private set; } = 0;
|
||||||
|
|
||||||
public static bool ConnectServer()
|
public static bool ConnectServer()
|
||||||
{
|
{
|
||||||
client = NetworkPeer.CreatePeer(new NetworkConfig
|
client = NetworkPeer.CreatePeer(new NetworkConfig
|
||||||
@@ -38,14 +41,14 @@ namespace Game
|
|||||||
{
|
{
|
||||||
using var _ = Utilities.ProfileScope("NetworkManager_OnClientUpdate");
|
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:
|
case NetworkEventType.Connected:
|
||||||
{
|
{
|
||||||
LocalPlayerClientId = eventData.Sender.ConnectionId;
|
LocalPlayerClientId = networkEvent.Sender.ConnectionId;
|
||||||
Console.Print("Connected to server, ConnectionId: " + eventData.Sender.ConnectionId);
|
Console.Print("Connected to server, ConnectionId: " + networkEvent.Sender.ConnectionId);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case NetworkEventType.Disconnected:
|
case NetworkEventType.Disconnected:
|
||||||
@@ -62,21 +65,8 @@ namespace Game
|
|||||||
}
|
}
|
||||||
case NetworkEventType.Message:
|
case NetworkEventType.Message:
|
||||||
{
|
{
|
||||||
// Read the message contents
|
OnNetworkMessage(networkEvent);
|
||||||
var message = eventData.Message;
|
client.RecycleMessage(networkEvent.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);
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
|
|||||||
@@ -81,43 +81,39 @@ namespace Game
|
|||||||
{
|
{
|
||||||
using var _ = Utilities.ProfileScope("NetworkManager_OnServerUpdate");
|
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:
|
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);
|
Console.Print("Connected clients: " + ConnectedClients.Count);
|
||||||
|
|
||||||
// Send hello message to the client back
|
// Send hello message to the client back
|
||||||
{
|
{
|
||||||
var sendmessage = server.BeginSendMessage();
|
NetworkMessage sendmessage = server.BeginSendMessage();
|
||||||
sendmessage.WriteString($"Welcome, ({eventData.Sender.ConnectionId})");
|
sendmessage.WriteByte((byte)NetworkMessageType.Message);
|
||||||
server.EndSendMessage(NetworkChannelType.Reliable, sendmessage, eventData.Sender);
|
//sendmessage.WriteString($"Welcome, ({networkEvent.Sender.ConnectionId})");
|
||||||
|
server.EndSendMessage(NetworkChannelType.Reliable, sendmessage, networkEvent.Sender);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case NetworkEventType.Disconnected:
|
case NetworkEventType.Disconnected:
|
||||||
case NetworkEventType.Timeout:
|
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);
|
Console.Print("Connected clients: " + ConnectedClients.Count);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case NetworkEventType.Message:
|
case NetworkEventType.Message:
|
||||||
{
|
{
|
||||||
// Read the message contents
|
OnNetworkMessage(networkEvent);
|
||||||
var message = eventData.Message;
|
server.RecycleMessage(networkEvent.Message);
|
||||||
var messageData = message.ReadString();
|
|
||||||
|
|
||||||
Console.Print($"Received message from Client({eventData.Sender.ConnectionId}): {messageData}");
|
|
||||||
|
|
||||||
server.RecycleMessage(message);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ public class GameTarget : GameProjectTarget
|
|||||||
|
|
||||||
public override string GetOutputFilePath(BuildOptions options, TargetOutputType? outputType = null)
|
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)
|
if (options.Configuration == TargetConfiguration.Development)
|
||||||
options.OutputFolder = @"C:\dev\GoakeFlax\Output\WindowsDevelopment";
|
options.OutputFolder = @"C:\dev\GoakeFlax\Output\WindowsDevelopment";
|
||||||
else if (options.Configuration == TargetConfiguration.Release)
|
else if (options.Configuration == TargetConfiguration.Release)
|
||||||
|
|||||||
Reference in New Issue
Block a user