Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 32 additions & 9 deletions Assets/PlayroomKit/PlayroomKit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class PlayroomKit
*/
private static Dictionary<string, object> MockDictionary = new();

public static readonly Dictionary<string, Player> Players = new();
private static readonly Dictionary<string, Player> Players = new();
Comment thread
momintlh marked this conversation as resolved.



Expand Down Expand Up @@ -1044,17 +1044,15 @@ public enum RpcMode
HOST
}

private static List<Action<string, string>> rpcRegisterCallbacks = new();
private static List<string> rpcRegisteredEvents = new();
private static Dictionary<string, Action<string, string>> rpcRegisterCallbacks = new();
private static List<string> rpcCalledEvents = new();

[DllImport("__Internal")]
private extern static void RpcRegisterInternal(string name, Action<string, string> rpcRegisterCallback, string onResponseReturn = null);

public static void RpcRegister(string name, Action<string, string> rpcRegisterCallback, string onResponseReturn = null)
{
rpcRegisterCallbacks.Add(rpcRegisterCallback);
rpcRegisteredEvents.Add(name);
rpcRegisterCallbacks.Add(name, rpcRegisterCallback);
RpcRegisterInternal(name, InvokeRpcRegisterCallBack, onResponseReturn);
}

Expand All @@ -1078,11 +1076,23 @@ private static void InvokeRpcRegisterCallBack(string dataJson, string senderJson
Debug.LogError(ex.Message);
}

for (int i = 0; i < Math.Min(rpcRegisteredEvents.Count, rpcCalledEvents.Count); i++)

Comment thread
momintlh marked this conversation as resolved.
List<string> updatedRpcCalledEvents = new();
// This state is required to update the called rpc events list, (Temp fix see RpcCall for more)
string nameJson = GetState<string>("rpcCalledEventName");

JSONArray jsonArray = JSON.Parse(nameJson).AsArray;
foreach (JSONNode node in jsonArray)
{
string item = node.Value;
updatedRpcCalledEvents.Add(item);
}

foreach (string name in updatedRpcCalledEvents)
{
if (rpcRegisteredEvents[i] == rpcCalledEvents[i])
if (rpcRegisterCallbacks.TryGetValue(name, out Action<string, string> callback))
{
rpcRegisterCallbacks[i].Invoke(dataJson, senderJson);
callback?.Invoke(dataJson, senderJson);
}
}

Expand Down Expand Up @@ -1111,6 +1121,19 @@ public static void RpcCall(string name, object data, RpcMode mode, Action callba
}
}


JSONArray jsonArray = new JSONArray();
foreach (string item in rpcCalledEvents)
{
jsonArray.Add(item);
Comment thread
SaadBazaz marked this conversation as resolved.
}
string jsonString = jsonArray.ToString();
/*
This is requrired to sync the rpc events between all players, without this players won't know which event has been called.
this is a temporary fix, RPC's need to be handled within JS for better control.
*/
SetState("rpcCalledEventName", jsonString, reliable: true);

RpcCallInternal(name, jsonData, mode, InvokeOnResponseCallback);
}

Expand All @@ -1125,7 +1148,7 @@ private static void InvokeOnResponseCallback()
{
var namesToRemove = new List<string>();

foreach (var name in rpcCalledEvents)
foreach (string name in rpcCalledEvents)
{
try
{
Expand Down
5 changes: 2 additions & 3 deletions Assets/Scenes/Game.unity
Original file line number Diff line number Diff line change
Expand Up @@ -433,9 +433,8 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
score: 0
scoreText1: {fileID: 396053685}
scoreText2: {fileID: 197975553}
scoreText: {fileID: 0}
scoreTextPlayer1: {fileID: 396053685}
scoreTextPlayer2: {fileID: 197975553}
--- !u!4 &330732770
Transform:
m_ObjectHideFlags: 0
Expand Down
92 changes: 61 additions & 31 deletions Assets/Scripts/GameManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,31 @@

public class GameManager : MonoBehaviour
{

[Header("Player holders")]
private static readonly List<PlayroomKit.Player> players = new();
private static readonly List<GameObject> playerGameObjects = new();

private static Dictionary<string, GameObject> PlayerDict = new();
Dictionary<string, float> moveData = new();


[Header("Score and Score UI")]
[SerializeField] private int score;
[SerializeField] private Text scoreText1;
[SerializeField] private Text scoreText2;
[SerializeField] private Text scoreTextPlayer1;
[SerializeField] private Text scoreTextPlayer2;

private Text selectedScoreText;

[SerializeField] private static bool playerJoined;
private static bool playerJoined;

[SerializeField] private Text scoreText;

bool isMoving;
void Awake()
{
// StartCoroutine(StartMatchmakingCoroutine());
Initialize();
}

private void Awake()
private void Initialize()
{
PlayroomKit.InsertCoin(new PlayroomKit.InitOptions()
{
Expand All @@ -44,11 +52,25 @@ private void Awake()
});
}

private IEnumerator StartMatchmakingCoroutine()
{
Initialize();

yield return new WaitUntil(() => PlayroomKit.GetPlayers().Count > 0);

PlayroomKit.StartMatchmaking();
}

void Start()
{
PlayroomKit.RpcRegister("ShootBullet", HandleScoreUpdate, "You shot a bullet!");
PlayroomKit.RpcRegister("Hello", Hello);
}

private void Hello(string data, string caller)
{
var player = PlayroomKit.GetPlayer(caller);
Debug.Log($"Caller: {caller}, Player Name: {player?.GetProfile().name}, Data: {data}");
}

void HandleScoreUpdate(string data, string caller)
Expand Down Expand Up @@ -89,25 +111,15 @@ private void Update()

players[index].SetState("pos", playerGameObjects[index].GetComponent<Transform>().position);

if (Input.GetKeyDown(KeyCode.Space))
{
Vector3 playerPosition = playerGameObjects[index].transform.position;
playerGameObjects[index].GetComponent<PlayerController>().ShootBullet(
playerPosition,
50f);
score += 50;
PlayroomKit.RpcCall("ShootBullet", score, () =>
{
Debug.Log("shooting bullet!");
});
}
ShootBullet(index);
SayHello();

if (Input.GetKeyDown(KeyCode.R) && PlayroomKit.IsHost())
{
PlayroomKit.ResetStates(null, () =>
{
var defscore = PlayroomKit.GetState<int>("score");
scoreText.text = "Score: " + defscore.ToString();
selectedScoreText.text = "Score: " + defscore.ToString();
});

}
Expand All @@ -129,11 +141,35 @@ private void Update()

}

private void ShootBullet(int pleyerIndex)
{
if (Input.GetKeyDown(KeyCode.Space))
{
Vector3 playerPosition = playerGameObjects[pleyerIndex].transform.position;
playerGameObjects[pleyerIndex].GetComponent<PlayerController>().ShootBullet(
playerPosition,
50f);
score += 50;
PlayroomKit.RpcCall("ShootBullet", score, () =>
{
Debug.Log("Shooting bullet");
});
}
}

public void AddPlayer(PlayroomKit.Player player)
private void SayHello()
{
Debug.Log("AddPlayer Invoked!");
if (Input.GetKeyDown(KeyCode.H))
{
PlayroomKit.RpcCall("Hello", -1, () =>
{
Debug.Log("saying helo");
});
}
}

public void AddPlayer(PlayroomKit.Player player)
{
GameObject playerObj = (GameObject)Instantiate(Resources.Load("Player"),
new Vector3(Random.Range(-4, 4), Random.Range(1, 5), 0), Quaternion.identity);

Expand All @@ -143,17 +179,11 @@ public void AddPlayer(PlayroomKit.Player player)
players.Add(player);
playerGameObjects.Add(playerObj);

scoreText = (players.Count == 1) ? scoreText1 : scoreText2;
playerObj.GetComponent<PlayerController>().scoreText = scoreText;
selectedScoreText = (players.Count == 1) ? scoreTextPlayer1 : scoreTextPlayer2;
playerObj.GetComponent<PlayerController>().scoreText = selectedScoreText;

playerJoined = true;
player.OnQuit(RemovePlayer);

}

public void SomeThingElseInvoked(PlayroomKit.Player player)
{
Debug.Log("SomeThingElseInvoked Invoked!!!");
}

[MonoPInvokeCallback(typeof(Action<string>))]
Expand All @@ -167,7 +197,7 @@ private static void RemovePlayer(string playerID)
}
else
{
Debug.LogWarning("player not in dict");
Debug.LogWarning("Player is not in dictionary");
}

}
Expand Down