Skip to content

Commit

Permalink
feat: refactor Player class for improved network handling; update inp…
Browse files Browse the repository at this point in the history
…ut methods and streamline event processing
  • Loading branch information
MasterLaplace committed Nov 28, 2024
1 parent c28fee1 commit 5ec4e6a
Showing 1 changed file with 26 additions and 54 deletions.
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

0 comments on commit 5ec4e6a

Please sign in to comment.