|
| 1 | +# Flakkari4Unity |
| 2 | + |
| 3 | +Flakkari4Unity is a Unity package that provides a simple way to use Flakkari Protocol in Unity projects to communicate with Flakkari server. |
| 4 | + |
| 5 | +## Usage |
| 6 | + |
| 7 | +```csharp |
| 8 | +using Flakkari4Unity; |
| 9 | + |
| 10 | +public class Example : MonoBehaviour |
| 11 | +{ |
| 12 | + private Flakkari4Unity flakkari; |
| 13 | + [SerializeField] private string serverIP = "127.0.0.1"; |
| 14 | + [SerializeField] private int serverPort = 54000; |
| 15 | + [SerializeField] private string gameName = "SpaceWar"; |
| 16 | + |
| 17 | + private readonly Dictionary<CurrentProtocol.EventId, CurrentProtocol.EventState> axisEvents = new(4); |
| 18 | + |
| 19 | + void Start() |
| 20 | + { |
| 21 | + NetworkClient networkClient = gameObject.AddComponent<NetworkClient>(); |
| 22 | + networkClient.Create(serverIP, serverPort, gameName); |
| 23 | + } |
| 24 | + |
| 25 | + public void FixedUpdate() |
| 26 | + { |
| 27 | + if (networkClient == null && networkClient.Enable) |
| 28 | + return; |
| 29 | + |
| 30 | + List<CurrentProtocol.Event> events = new(8); |
| 31 | + Dictionary<CurrentProtocol.EventId, float> axisValues = new(4); |
| 32 | + |
| 33 | + Net_HandleMovement(networkClient, ref events, ref axisValues); |
| 34 | + Net_HandleShooting(networkClient, ref events); |
| 35 | + |
| 36 | + if (events.Count > 0 || axisValues.Count > 0) |
| 37 | + networkClient.Send(Flk_API.APIClient.ReqUserUpdates(events, axisValues)); |
| 38 | + } |
| 39 | + |
| 40 | + private void Net_HandleMovement(NetworkClient networkClient, ref List<CurrentProtocol.Event> events, ref Dictionary<CurrentProtocol.EventId, float> axisValues) |
| 41 | + { |
| 42 | + HandleNetworkInput("Fire2", CurrentProtocol.EventId.MOVE_FRONT, ref events); |
| 43 | + HandleNetworkInput("Fire1", CurrentProtocol.EventId.MOVE_BACK, ref events); |
| 44 | + |
| 45 | + HandleMouseMovement("Horizontal", CurrentProtocol.EventId.LOOK_LEFT, CurrentProtocol.EventId.LOOK_RIGHT, ref axisValues); |
| 46 | + HandleMouseMovement("Vertical", CurrentProtocol.EventId.LOOK_DOWN, CurrentProtocol.EventId.LOOK_UP, ref axisValues); |
| 47 | + } |
| 48 | + |
| 49 | + private void HandleNetworkInput(string inputName, CurrentProtocol.EventId eventId, ref List<CurrentProtocol.Event> events) |
| 50 | + { |
| 51 | + if (Input.GetButtonDown(inputName)) |
| 52 | + events.Add(new CurrentProtocol.Event { id = eventId, state = CurrentProtocol.EventState.PRESSED }); |
| 53 | + |
| 54 | + else if (Input.GetButtonUp(inputName)) |
| 55 | + events.Add(new CurrentProtocol.Event { id = eventId, state = CurrentProtocol.EventState.RELEASED }); |
| 56 | + |
| 57 | + } |
| 58 | + |
| 59 | + private void HandleMouseMovement(string axisName, CurrentProtocol.EventId negativeEventId, CurrentProtocol.EventId positiveEventId , ref Dictionary<CurrentProtocol.EventId, float> axisValues) |
| 60 | + { |
| 61 | + float axisValue = Input.GetAxis(axisName); |
| 62 | + |
| 63 | + if (axisValue < 0 && axisEvents[negativeEventId] != CurrentProtocol.EventState.PRESSED) |
| 64 | + { |
| 65 | + axisEvents[negativeEventId] = CurrentProtocol.EventState.PRESSED; |
| 66 | + axisEvents[positiveEventId] = CurrentProtocol.EventState.RELEASED; |
| 67 | + axisValues[negativeEventId] = axisValue; |
| 68 | + axisValues[positiveEventId] = 0; |
| 69 | + } |
| 70 | + else if (axisValue > 0 && axisEvents[positiveEventId] != CurrentProtocol.EventState.PRESSED) |
| 71 | + { |
| 72 | + axisEvents[positiveEventId] = CurrentProtocol.EventState.PRESSED; |
| 73 | + axisEvents[negativeEventId] = CurrentProtocol.EventState.RELEASED; |
| 74 | + axisValues[positiveEventId] = axisValue; |
| 75 | + axisValues[negativeEventId] = 0; |
| 76 | + } |
| 77 | + |
| 78 | + if (axisValue == 0 && axisEvents[negativeEventId] == CurrentProtocol.EventState.PRESSED) |
| 79 | + { |
| 80 | + axisEvents[negativeEventId] = CurrentProtocol.EventState.RELEASED; |
| 81 | + axisValues[negativeEventId] = 0; |
| 82 | + } |
| 83 | + if (axisValue == 0 && axisEvents[positiveEventId] == CurrentProtocol.EventState.PRESSED) |
| 84 | + { |
| 85 | + axisEvents[positiveEventId] = CurrentProtocol.EventState.RELEASED; |
| 86 | + axisValues[positiveEventId] = 0; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + private void Net_HandleShooting(NetworkClient networkClient, ref List<CurrentProtocol.Event> events) |
| 91 | + { |
| 92 | + if (Time.time - lastShotTime < cooldown) |
| 93 | + return; |
| 94 | + |
| 95 | + HandleNetworkInput(KeyCode.Joystick1Button5, CurrentProtocol.EventId.SHOOT, ref events); |
| 96 | + } |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +## License |
| 101 | + |
| 102 | +Flakkari4Unity is licensed under the MIT License. See the [LICENSE](LICENSE) file for more information. |
| 103 | + |
| 104 | +## Contact |
| 105 | + |
| 106 | +If you have any questions or feedback, please open an issue on the [GitHub repository](https://github.com/MasterLaplace/Flakkari/issues/new/choose). |
0 commit comments