Skip to content

Commit

Permalink
Merge pull request #1 from aarijimam/refactor/kit-functions
Browse files Browse the repository at this point in the history
Refactor/kit functions
  • Loading branch information
aarijimam authored Oct 28, 2024
2 parents c8e71d8 + 6e8ffeb commit c774bc2
Show file tree
Hide file tree
Showing 20 changed files with 7,156 additions and 283 deletions.
8 changes: 8 additions & 0 deletions Assets/Packages.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private void Update()
{
if (playerJoined)
{
var myPlayer = PlayroomKit.MyPlayer();
var myPlayer = _playroomKit.MyPlayer();
var index = players.IndexOf(myPlayer);

playerGameObjects[index].GetComponent<PlayerController2d>().Move();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ private void Update()
{
if (playerJoined)
{
var myPlayer = PlayroomKit.MyPlayer();
var myPlayer = _playroomKit.MyPlayer();
var index = players.IndexOf(myPlayer);

playerGameObjects[index].GetComponent<IsometricPlayerController>().LookAround();
Expand Down
256 changes: 39 additions & 217 deletions Assets/PlayroomKit/PlayroomKit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ public PlayroomKit()
}
}

public PlayroomKit(IPlayroomBase playroomService)
public PlayroomKit(IPlayroomBase playroomService, IRPC rpc)
{
_playroomService = playroomService;
_rpc = rpc;
}

public void InsertCoin(InitOptions options = null, Action onLaunchCallBack = null,
Expand Down Expand Up @@ -177,254 +178,75 @@ public void OnDisconnect(Action callback)
_playroomService.OnDisconnect(callback);
}

// DI END


public class MatchMakingOptions
{
public int waitBeforeCreatingNewRoom = 5000;
}


private static List<Action<Player>> OnPlayerJoinCallbacks = new();


[MonoPInvokeCallback(typeof(Action<string>))]
private static void __OnPlayerJoinCallbackHandler(string id)
{
OnPlayerJoinWrapperCallback(id);
}


private static void OnPlayerJoinWrapperCallback(string id)
public bool IsStreamScreen()
{
var player = GetPlayer(id);
foreach (var callback in OnPlayerJoinCallbacks)
if (!isPlayRoomInitialized)
{
callback?.Invoke(player);
Debug.LogError("[Mock Mode] Playroom not initialized yet! Please call InsertCoin.");
return false;
}

return _playroomService.IsStreamScreen();
}

public static bool IsStreamScreen()
public void WaitForState(string stateKey, Action<string> onStateSetCallback = null)
{
if (IsRunningInBrowser())
{
return IsStreamScreenInternal();
}

if (isPlayRoomInitialized) return MockIsStreamScreen();
Debug.LogError("[Mock Mode] Playroom not initialized yet! Please call InsertCoin.");
return false;
_playroomService.WaitForState(stateKey, onStateSetCallback);
}


public static Player MyPlayer()
{
if (IsRunningInBrowser())
{
var id = MyPlayerInternal();
return GetPlayer(id);
}

if (isPlayRoomInitialized) return MockMyPlayer();
Debug.LogError("[Mock Mode] Playroom not initialized yet! Please call InsertCoin.");
return null;
}

public static Player Me()
public void WaitForPlayerState(string playerID, string stateKey, Action onStateSetCallback = null)
{
return IsRunningInBrowser() ? MyPlayer() : MockMe();
_playroomService.WaitForPlayerState(playerID, stateKey, onStateSetCallback);
}




[MonoPInvokeCallback(typeof(Action<string, string>))]
private static void InvokeCallback(string stateKey, string stateVal)
{
CallbackManager.InvokeCallback(stateKey, stateVal);
}

public static void WaitForState(string stateKey, Action<string> onStateSetCallback = null)
public void ResetStates(string[] keysToExclude = null, Action OnStatesReset = null)
{
if (IsRunningInBrowser())
{
CallbackManager.RegisterCallback(onStateSetCallback, stateKey);
WaitForStateInternal(stateKey, InvokeCallback);
}
else
{
MockWaitForState(stateKey, onStateSetCallback);
}
_playroomService.ResetStates(keysToExclude, OnStatesReset);
}


Action WaitForPlayerCallback = null;

public void WaitForPlayerState(string playerID, string stateKey, Action onStateSetCallback = null)
{
if (IsRunningInBrowser())
{
WaitForPlayerCallback = onStateSetCallback;
WaitForPlayerStateInternal(playerID, stateKey, OnStateSetCallback);
}
}

[MonoPInvokeCallback(typeof(Action))]
void OnStateSetCallback()

public void ResetPlayersStates(string[] keysToExclude = null, Action OnStatesReset = null)
{
WaitForPlayerCallback?.Invoke();
_playroomService.ResetPlayersStates(keysToExclude, OnStatesReset);
}


// Utils:
private static void SetStateHelper<T>(string key, Dictionary<string, T> values, bool reliable = false)

// Joystick
public void CreateJoyStick(JoystickOptions options)
{
var jsonObject = new JSONObject();

// Add key-value pairs to the JSON object
foreach (var kvp in values)
{
// Convert the value to double before adding to JSONNode
var value = Convert.ToDouble(kvp.Value);
jsonObject.Add(kvp.Key, value);
}

// Serialize the JSON object to a string
var jsonString = jsonObject.ToString();

// Output the JSON string
SetStateDictionary(key, jsonString, reliable);
_playroomService.CreateJoyStick(options);
}

private static Dictionary<string, T> ParseJsonToDictionary<T>(string jsonString)
public Dpad DpadJoystick()
{
var dictionary = new Dictionary<string, T>();
var jsonNode = JSON.Parse(jsonString);

foreach (var kvp in jsonNode.AsObject)
{
T value = default; // Initialize the value to default value of T

// Parse the JSONNode value to the desired type (T)
if (typeof(T) == typeof(float))
value = (T)(object)kvp.Value.AsFloat;
else if (typeof(T) == typeof(int))
value = (T)(object)kvp.Value.AsInt;
else if (typeof(T) == typeof(bool))
value = (T)(object)kvp.Value.AsBool;
else
Debug.LogError("Unsupported type: " + typeof(T).FullName);

dictionary.Add(kvp.Key, value);
}

return dictionary;
return _playroomService.DpadJoystick();
}


private static Action onstatesReset;
private static Action onplayersStatesReset;

public static void ResetStates(string[] keysToExclude = null, Action OnStatesReset = null)

public Player MyPlayer()
{
if (IsRunningInBrowser())
{
onstatesReset = OnStatesReset;
string keysJson = keysToExclude != null ? CreateJsonArray(keysToExclude).ToString() : null;
ResetStatesInternal(keysJson, InvokeResetCallBack);
}
else
if (!isPlayRoomInitialized)
{
MockResetStates(keysToExclude);
Debug.LogError("[Mock Mode] Playroom not initialized yet! Please call InsertCoin.");
return null;
}
return _playroomService.MyPlayer();
}

[MonoPInvokeCallback(typeof(Action))]
private static void InvokeResetCallBack()
{
onstatesReset?.Invoke();
}

[MonoPInvokeCallback(typeof(Action))]
private static void InvokePlayersResetCallBack()
{
onplayersStatesReset?.Invoke();
}


public static void ResetPlayersStates(string[] keysToExclude = null, Action OnStatesReset = null)
public Player Me()
{
if (IsRunningInBrowser())
{
onstatesReset = OnStatesReset;
string keysJson = keysToExclude != null ? CreateJsonArray(keysToExclude).ToString() : null;
ResetPlayersStatesInternal(keysJson, InvokePlayersResetCallBack);
}
else
if (!isPlayRoomInitialized)
{
MockResetPlayersStates(keysToExclude, OnStatesReset);
Debug.LogError("[Mock Mode] Playroom not initialized yet! Please call InsertCoin.");
return null;
}
}






private static void UnsubscribeOnQuit()
{
UnsubscribeOnQuitInternal();

return _playroomService.Me();
}



// Joystick
public static void CreateJoyStick(JoystickOptions options)
{
var jsonStr = ConvertJoystickOptionsToJson(options);
CreateJoystickInternal(jsonStr);
}

public static Dpad DpadJoystick()
private void UnsubscribeOnQuit()
{
var jsonString = DpadJoystickInternal();
Dpad myDpad = JsonUtility.FromJson<Dpad>(jsonString);
return myDpad;
}


public class JoystickOptions
{
public string type = "angular"; // default = angular, can be dpad

public ButtonOptions[] buttons;
public ZoneOptions zones = null;
}

[Serializable]
public class ButtonOptions
{
public string id = null;
public string label = "";
public string icon = null;
}

public class ZoneOptions
{
public ButtonOptions up = null;
public ButtonOptions down = null;
public ButtonOptions left = null;
public ButtonOptions right = null;
}


[Serializable]
public class Dpad
{
public string x;
public string y;
_playroomService.UnsubscribeOnQuit();
}

// DI END

}
}
Loading

0 comments on commit c774bc2

Please sign in to comment.