Skip to content

Commit

Permalink
Merge pull request #58 from MasterLaplace/55-feature-finalize-engine-…
Browse files Browse the repository at this point in the history
…implementation

Finalize engine implementation
  • Loading branch information
MasterLaplace authored Nov 29, 2024
2 parents d7bf0b0 + 086cfca commit a910c2e
Show file tree
Hide file tree
Showing 23 changed files with 132 additions and 165 deletions.
10 changes: 3 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ install(FILES ${CMAKE_BINARY_DIR}/config.h DESTINATION include/Flakkari)
install(FILES ${HEADER_LIB_LOGGER} DESTINATION include/Flakkari/Logger)
install(FILES ${HEADER_LIB_NETWORK} DESTINATION include/Flakkari/Network)
install(FILES ${HEADER_LIB_PROTOCOL} DESTINATION include/Flakkari/Protocol)
install(DIRECTORY ${FLAKKARI_GAME_DIR} DESTINATION ${CMAKE_INSTALL_PREFIX})

# Packaging:
set(CPACK_PACKAGE_NAME "flakkari")
Expand Down Expand Up @@ -226,16 +227,11 @@ if (WIN32)
set(CPACK_NSIS_MUI_UNIICON "${CMAKE_SOURCE_DIR}/docs/Images/icon.ico")
set(CPACK_NSIS_EXECUTABLES_DIRECTORY "bin")
set(CPACK_PACKAGE_FILE_NAME "flakkari-win64")
set(CPACK_NSIS_CREATE_ICONS_EXTRA "CreateShortCut '$SMPROGRAMS\\\\$STARTMENU_FOLDER\\\\Flakkari.lnk' '$INSTDIR\\\\bin\\\\flakkari.exe'")
elseif (APPLE)
set(CPACK_GENERATOR "DragNDrop")
set(CPACK_DMG_FORMAT "UDBZ")
set(CPACK_DMG_DS_STORE "${CMAKE_SOURCE_DIR}/.DS_Store")
set(CPACK_DMG_BACKGROUND_IMAGE "${CMAKE_SOURCE_DIR}/background.png")
set(CPACK_DMG_DS_STORE_SETUP_SCRIPT "${CMAKE_SOURCE_DIR}/DS_Store.sh")
set(CPACK_DMG_DS_STORE_SETUP_SCRIPT_PERMISSIONS "0775")
set(CPACK_DMG_DS_STORE_SETUP_SCRIPT_EXECUTABLE ON)
set(CPACK_DMG_DS_STORE_SETUP_SCRIPT_NAME "DS_Store.sh")
set(CPACK_DMG_DS_STORE_SETUP_SCRIPT_DESTINATION ".background")
set(CPACK_DMG_DS_STORE "")
set(CPACK_PACKAGE_FILE_NAME "flakkari-macos")
else()
set(CPACK_GENERATOR "DEB;RPM")
Expand Down
2 changes: 1 addition & 1 deletion Flakkari/Engine/Math/Vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ template <typename Type> struct Vector {
*/
void rotate(const Vector<Type> &axis, float angleDegrees)
{
double _pi = 3.14159265358979323846;
const double _pi = 3.14159265358979323846;
double angleRadians = (angleDegrees * _pi / 180) / 2.0;
double sinHalfAngle = sin(angleRadians);
auto normalizedAxis = axis.normalized();
Expand Down
2 changes: 1 addition & 1 deletion Flakkari/Server/Game/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void Game::loadSystems(Engine::ECS::Registry &registry, const std::string &scene
registry.add_system(
[this](Engine::ECS::Registry &r) { Engine::ECS::Systems::_3D::apply_movable(r, _deltaTime); });

if (sysName == "spawn_enemy")
else if (sysName == "spawn_enemy")
registry.add_system([this, sceneName](Engine::ECS::Registry &r) {
std::string templateName;
Engine::ECS::Entity entity;
Expand Down
80 changes: 26 additions & 54 deletions Libraries/Flakkari4Unity/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,94 +5,66 @@ Flakkari4Unity is a Unity package that provides a simple way to use Flakkari Pro
## Usage

```csharp
using Flakkari4Unity;
using Flk_API = Flakkari4Unity.API;
using CurrentProtocol = Flakkari4Unity.Protocol.V1;

public class Example : MonoBehaviour
public class Player : Flakkari4Unity.ECS.Entity
{
private Flakkari4Unity flakkari;
[SerializeField] private string serverIP = "127.0.0.1";
[SerializeField] private int serverPort = 54000;
[SerializeField] private string gameName = "SpaceWar";

[HideInInspector] private NetworkClient networkClient = null;
private readonly Dictionary<CurrentProtocol.EventId, CurrentProtocol.EventState> axisEvents = new(4);

void Start()
public override void Start()
{
NetworkClient networkClient = gameObject.AddComponent<NetworkClient>();
networkClient.Create(serverIP, serverPort, gameName);
networkClient = gameObject.AddComponent<NetworkClient>();
networkClient.Create("127.0.0.1", 54000, "GameName");
}

public void FixedUpdate()
public override void Update()
{
if (networkClient == null && networkClient.Enable)
return;

List<CurrentProtocol.Event> events = new(8);
Dictionary<CurrentProtocol.EventId, float> axisValues = new(4);

Net_HandleMovement(networkClient, ref events, ref axisValues);
Net_HandleShooting(networkClient, ref events);
Net_HandleMovement(ref events, ref axisValues);

if (events.Count > 0 || axisValues.Count > 0)
networkClient.Send(Flk_API.APIClient.ReqUserUpdates(events, axisValues));
}

private void Net_HandleMovement(NetworkClient networkClient, ref List<CurrentProtocol.Event> events, ref Dictionary<CurrentProtocol.EventId, float> axisValues)
private void Net_HandleMovement(ref List<CurrentProtocol.Event> events, ref Dictionary<CurrentProtocol.EventId, float> axisValues)
{
HandleNetworkInput("Fire2", CurrentProtocol.EventId.MOVE_FRONT, ref events);
HandleNetworkInput("Fire1", CurrentProtocol.EventId.MOVE_BACK, ref events);
HandleNetworkInput(KeyCode.w, CurrentProtocol.EventId.MOVE_FRONT, ref events);
HandleNetworkInput(KeyCode.s, CurrentProtocol.EventId.MOVE_BACK, ref events);

HandleMouseMovement("Mouse X", CurrentProtocol.EventId.LOOK_LEFT, CurrentProtocol.EventId.LOOK_RIGHT, ref axisValues);
HandleMouseMovement("Mouse Y", CurrentProtocol.EventId.LOOK_DOWN, CurrentProtocol.EventId.LOOK_UP, ref axisValues);
HandleMouseMovement("Horizontal", CurrentProtocol.EventId.LOOK_LEFT, CurrentProtocol.EventId.LOOK_RIGHT, ref axisValues);
HandleMouseMovement("Vertical", CurrentProtocol.EventId.LOOK_DOWN, CurrentProtocol.EventId.LOOK_UP, ref axisValues);
}

private void HandleNetworkInput(string inputName, CurrentProtocol.EventId eventId, ref List<CurrentProtocol.Event> events)
private void HandleNetworkInput(KeyCode keyCode, CurrentProtocol.EventId eventId, ref List<CurrentProtocol.Event> events)
{
if (Input.GetButtonDown(inputName))
if (Input.GetKeyDown(keyCode))
events.Add(new CurrentProtocol.Event { id = eventId, state = CurrentProtocol.EventState.PRESSED });

else if (Input.GetButtonUp(inputName))
else if (Input.GetKeyUp(keyCode))
events.Add(new CurrentProtocol.Event { id = eventId, state = CurrentProtocol.EventState.RELEASED });

}

private void HandleMouseMovement(string axisName, CurrentProtocol.EventId negativeEventId, CurrentProtocol.EventId positiveEventId , ref Dictionary<CurrentProtocol.EventId, float> axisValues)
private void HandleNetworkMouseInput(int keyCode, CurrentProtocol.EventId eventId, ref List<CurrentProtocol.Event> events)
{
float axisValue = Input.GetAxis(axisName);
if (Input.GetMouseButtonDown(keyCode))
events.Add(new CurrentProtocol.Event { id = eventId, state = CurrentProtocol.EventState.PRESSED });

if (axisValue < 0 && axisEvents[negativeEventId] != CurrentProtocol.EventState.PRESSED)
{
axisEvents[negativeEventId] = CurrentProtocol.EventState.PRESSED;
axisEvents[positiveEventId] = CurrentProtocol.EventState.RELEASED;
axisValues[negativeEventId] = axisValue;
axisValues[positiveEventId] = 0;
}
else if (axisValue > 0 && axisEvents[positiveEventId] != CurrentProtocol.EventState.PRESSED)
{
axisEvents[positiveEventId] = CurrentProtocol.EventState.PRESSED;
axisEvents[negativeEventId] = CurrentProtocol.EventState.RELEASED;
axisValues[positiveEventId] = axisValue;
axisValues[negativeEventId] = 0;
}

if (axisValue == 0 && axisEvents[negativeEventId] == CurrentProtocol.EventState.PRESSED)
{
axisEvents[negativeEventId] = CurrentProtocol.EventState.RELEASED;
axisValues[negativeEventId] = 0;
}
if (axisValue == 0 && axisEvents[positiveEventId] == CurrentProtocol.EventState.PRESSED)
{
axisEvents[positiveEventId] = CurrentProtocol.EventState.RELEASED;
axisValues[positiveEventId] = 0;
}
else if (Input.GetMouseButtonUp(keyCode))
events.Add(new CurrentProtocol.Event { id = eventId, state = CurrentProtocol.EventState.RELEASED });
}

private void Net_HandleShooting(NetworkClient networkClient, ref List<CurrentProtocol.Event> events)
private void HandleMouseMovement(string axisName, CurrentProtocol.EventId negativeEventId, CurrentProtocol.EventId positiveEventId, ref Dictionary<CurrentProtocol.EventId, float> axisValues)
{
if (Time.time - lastShotTime < cooldown)
return;
float axisValue = Input.GetAxis(axisName);

HandleNetworkInput(KeyCode.Joystick1Button5, CurrentProtocol.EventId.SHOOT, ref events);
if (axisValue != 0)
axisValues[positiveEventId] = axisValue;
}
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,35 @@ public static byte[] ReqConnect(string gameName)
);
}

public static void ResConnect(byte[] payload, ref Synchronizer synchronizer, out ulong entityId)
/// <summary>
/// Processes the RES_CONNECT message and extracts the entity ID and template name.
/// </summary>
/// <param name="payload">The byte array containing the payload of the RES_CONNECT message.</param>
/// <param name="entityId">The extracted entity ID from the payload.</param>
/// <param name="templateName">The extracted template name from the payload.</param>
/// <returns>The remaining payload after extracting the entity ID and template name.</returns>
public static byte[] ResConnect(byte[] payload, out ulong entityId, out string templateName)
{
entityId = BitConverter.ToUInt64(payload, 0);
int index = sizeof(ulong);

ulong length = BitConverter.ToUInt64(payload, index);
index += sizeof(ulong);
string templateName = System.Text.Encoding.UTF8.GetString(payload, index, (int)length);
uint length = BitConverter.ToUInt32(payload, index);
index += sizeof(uint);
templateName = System.Text.Encoding.UTF8.GetString(payload, index, (int)length);
index += (int)length;
return payload.Skip(index).ToArray();
}

GameObject player = Instantiate(Resources.Load<GameObject>(templateName));
player.name = templateName + "_" + entityId;
synchronizer.AddEntity(entityId, player.GetComponent<ECS.Entity>(), payload.Skip(index).ToArray());
/// <summary>
/// Creates a REQ_DISCONNECT message to disconnect from the server.
/// </summary>
/// <returns>A byte array representing the serialized REQ_DISCONNECT message.</returns>
public static byte[] ReqDisconnect()
{
return CurrentProtocol.Packet.Serialize(
CurrentProtocol.Priority.HIGH,
CurrentProtocol.CommandId.REQ_DISCONNECT
);
}

/// <summary>
Expand All @@ -48,25 +64,23 @@ public static void ResConnect(byte[] payload, ref Synchronizer synchronizer, out
/// </remarks>
public static byte[] ReqKeepAlive()
{
// Debug.Log("REQ_HEARTBEAT message sent to the server.");
return CurrentProtocol.Packet.Serialize(
CurrentProtocol.Priority.LOW,
CurrentProtocol.CommandId.REQ_HEARTBEAT
);
}

public static void ReqEntitySpawn(byte[] payload, ref Synchronizer synchronizer)
public static byte[] ReqEntitySpawn(byte[] payload, out ulong entityId, out string templateName)
{
ulong entityId = BitConverter.ToUInt64(payload, 0);
entityId = BitConverter.ToUInt64(payload, 0);
int index = sizeof(ulong);

ulong length = BitConverter.ToUInt64(payload, index);
index += sizeof(ulong);
string templateName = System.Text.Encoding.UTF8.GetString(payload, index, (int)length);
uint length = BitConverter.ToUInt32(payload, index);
index += sizeof(uint);
templateName = System.Text.Encoding.UTF8.GetString(payload, index, (int)length);
index += (int)length;

GameObject entity = Instantiate(Resources.Load<GameObject>(templateName));
entity.name = templateName + "_" + entityId;
synchronizer.AddEntity(entityId, entity.GetComponent<ECS.Entity>(), payload.Skip(index).ToArray());
return payload.Skip(index).ToArray();
}

public static void ReqEntityUpdate(byte[] payload, ref Synchronizer synchronizer)
Expand Down Expand Up @@ -125,21 +139,6 @@ public static byte[] ReqUserUpdates(List<CurrentProtocol.Event> events, Dictiona
);
}

public static byte[] ReqUserUpdate(CurrentProtocol.EventId id, CurrentProtocol.EventState state)
{
CurrentProtocol.Event _event = new()
{
id = id,
state = state
};

return CurrentProtocol.Packet.Serialize(
CurrentProtocol.Priority.HIGH,
CurrentProtocol.CommandId.REQ_USER_UPDATE,
CurrentProtocol.Event.Serialize(_event)
);
}

/// <summary>
/// Processes the received data and extracts the command ID, sequence number, and message.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ public class Child : MonoBehaviour

internal void Deserialize(byte[] data, ref int i)
{
ulong length = BitConverter.ToUInt64(data, i);
i += sizeof(ulong);
uint length = BitConverter.ToUInt32(data, i);
i += sizeof(uint);
_name = System.Text.Encoding.UTF8.GetString(data, i, (int)length);
i += (int)length;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ public class Evolve : MonoBehaviour

internal void Deserialize(byte[] data, ref int i)
{
ulong length = BitConverter.ToUInt64(data, i);
i += sizeof(ulong);
uint length = BitConverter.ToUInt32(data, i);
i += sizeof(uint);
_name = System.Text.Encoding.UTF8.GetString(data, i, (int)length);
i += (int)length;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ internal void Deserialize(byte[] data, ref int i)
{
level = BitConverter.ToUInt64(data, i);
i += sizeof(ulong);
ulong length = BitConverter.ToUInt64(data, i);
i += sizeof(ulong);
uint length = BitConverter.ToUInt32(data, i);
i += sizeof(uint);
currentWeapon = System.Text.Encoding.UTF8.GetString(data, i, (int)length);
i += (int)length;
currentExp = BitConverter.ToUInt64(data, i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ public class Tag : MonoBehaviour

internal void Deserialize(byte[] data, ref int i)
{
ulong length = BitConverter.ToUInt64(data, i);
i += sizeof(ulong);
uint length = BitConverter.ToUInt32(data, i);
i += sizeof(uint);
name = System.Text.Encoding.UTF8.GetString(data, i, (int)length);
i += (int)length;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ public class Template : MonoBehaviour
{
internal void Deserialize(byte[] data, ref int i)
{
ulong length = BitConverter.ToUInt64(data, i);
i += sizeof(ulong);
uint length = BitConverter.ToUInt32(data, i);
i += sizeof(uint);
name = System.Text.Encoding.UTF8.GetString(data, i, (int)length);
i += (int)length;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Unity.VisualScripting;
using UnityEngine;

using CurrentProtocol = Flakkari4Unity.Protocol.V1;
Expand Down Expand Up @@ -100,14 +101,20 @@ public void CreateComponents(byte[] data)

case CurrentProtocol.ComponentId.BOXCOLLIDER:
i++;
var boxCollider = gameObject.AddComponent<Components._3D.BoxCollider>();
boxCollider.Deserialize(data, ref i);
var boxCollider = gameObject.AddComponent<BoxCollider>();
boxCollider.center = new Vector3(BitConverter.ToSingle(data, i), BitConverter.ToSingle(data, i + sizeof(float)), BitConverter.ToSingle(data, i + sizeof(float) * 2));
i += sizeof(float) * 3;
boxCollider.size = new Vector3(BitConverter.ToSingle(data, i), BitConverter.ToSingle(data, i + sizeof(float)), BitConverter.ToSingle(data, i + sizeof(float) * 2));
i += sizeof(float) * 3;
break;

case CurrentProtocol.ComponentId.SPHERECOLLIDER:
i++;
var sphereCollider = gameObject.AddComponent<Components._3D.SphereCollider>();
sphereCollider.Deserialize(data, ref i);
var sphereCollider = gameObject.AddComponent<SphereCollider>();
sphereCollider.center = new Vector3(BitConverter.ToSingle(data, i), BitConverter.ToSingle(data, i + sizeof(float)), BitConverter.ToSingle(data, i + sizeof(float) * 2));
i += sizeof(float) * 3;
sphereCollider.radius = BitConverter.ToSingle(data, i);
i += sizeof(float);
break;

case CurrentProtocol.ComponentId.RIGIDBODY_3D:
Expand Down Expand Up @@ -204,14 +211,20 @@ public void UpdateComponents(byte[] data)

case CurrentProtocol.ComponentId.BOXCOLLIDER:
i++;
var boxCollider = gameObject.GetComponent<Components._3D.BoxCollider>();
boxCollider.Deserialize(data, ref i);
var boxCollider = gameObject.GetComponent<BoxCollider>();
boxCollider.center = new Vector3(BitConverter.ToSingle(data, i), BitConverter.ToSingle(data, i + sizeof(float)), BitConverter.ToSingle(data, i + sizeof(float) * 2));
i += sizeof(float) * 3;
boxCollider.size = new Vector3(BitConverter.ToSingle(data, i), BitConverter.ToSingle(data, i + sizeof(float)), BitConverter.ToSingle(data, i + sizeof(float) * 2));
i += sizeof(float) * 3;
break;

case CurrentProtocol.ComponentId.SPHERECOLLIDER:
i++;
var sphereCollider = gameObject.GetComponent<Components._3D.SphereCollider>();
sphereCollider.Deserialize(data, ref i);
var sphereCollider = gameObject.GetComponent<SphereCollider>();
sphereCollider.center = new Vector3(BitConverter.ToSingle(data, i), BitConverter.ToSingle(data, i + sizeof(float)), BitConverter.ToSingle(data, i + sizeof(float) * 2));
i += sizeof(float) * 3;
sphereCollider.radius = BitConverter.ToSingle(data, i);
i += sizeof(float);
break;

case CurrentProtocol.ComponentId.RIGIDBODY_3D:
Expand Down
Loading

0 comments on commit a910c2e

Please sign in to comment.