From 7da7db063f5f455af8eb7d82a7ae6d9acd2c3901 Mon Sep 17 00:00:00 2001 From: MasterLaplace Date: Fri, 22 Nov 2024 16:45:23 -0500 Subject: [PATCH] feat: add README.md for Flakkari4Unity package with usage examples and documentation --- Libraries/Flakkari4Unity/README.md | 106 +++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 Libraries/Flakkari4Unity/README.md diff --git a/Libraries/Flakkari4Unity/README.md b/Libraries/Flakkari4Unity/README.md new file mode 100644 index 0000000..b21e628 --- /dev/null +++ b/Libraries/Flakkari4Unity/README.md @@ -0,0 +1,106 @@ +# Flakkari4Unity + +Flakkari4Unity is a Unity package that provides a simple way to use Flakkari Protocol in Unity projects to communicate with Flakkari server. + +## Usage + +```csharp +using Flakkari4Unity; + +public class Example : MonoBehaviour +{ + private Flakkari4Unity flakkari; + [SerializeField] private string serverIP = "127.0.0.1"; + [SerializeField] private int serverPort = 54000; + [SerializeField] private string gameName = "SpaceWar"; + + private readonly Dictionary axisEvents = new(4); + + void Start() + { + NetworkClient networkClient = gameObject.AddComponent(); + networkClient.Create(serverIP, serverPort, gameName); + } + + public void FixedUpdate() + { + if (networkClient == null && networkClient.Enable) + return; + + List events = new(8); + Dictionary axisValues = new(4); + + Net_HandleMovement(networkClient, ref events, ref axisValues); + Net_HandleShooting(networkClient, ref events); + + if (events.Count > 0 || axisValues.Count > 0) + networkClient.Send(Flk_API.APIClient.ReqUserUpdates(events, axisValues)); + } + + private void Net_HandleMovement(NetworkClient networkClient, ref List events, ref Dictionary axisValues) + { + HandleNetworkInput("Fire2", CurrentProtocol.EventId.MOVE_FRONT, ref events); + HandleNetworkInput("Fire1", CurrentProtocol.EventId.MOVE_BACK, ref events); + + 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 events) + { + if (Input.GetButtonDown(inputName)) + events.Add(new CurrentProtocol.Event { id = eventId, state = CurrentProtocol.EventState.PRESSED }); + + else if (Input.GetButtonUp(inputName)) + events.Add(new CurrentProtocol.Event { id = eventId, state = CurrentProtocol.EventState.RELEASED }); + + } + + private void HandleMouseMovement(string axisName, CurrentProtocol.EventId negativeEventId, CurrentProtocol.EventId positiveEventId , ref Dictionary axisValues) + { + float axisValue = Input.GetAxis(axisName); + + 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; + } + } + + private void Net_HandleShooting(NetworkClient networkClient, ref List events) + { + if (Time.time - lastShotTime < cooldown) + return; + + HandleNetworkInput(KeyCode.Joystick1Button5, CurrentProtocol.EventId.SHOOT, ref events); + } +} +``` + +## License + +Flakkari4Unity is licensed under the MIT License. See the [LICENSE](LICENSE) file for more information. + +## Contact + +If you have any questions or feedback, please open an issue on the [GitHub repository](https://github.com/MasterLaplace/Flakkari/issues/new/choose).