Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: RPC events sync. #67

Merged
merged 7 commits into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
12 changes: 12 additions & 0 deletions Assets/PlayroomKit/PlayroomKit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,15 @@ private static void InvokeRpcRegisterCallBack(string dataJson, string senderJson
Debug.LogError(ex.Message);
}


momintlh marked this conversation as resolved.
Show resolved Hide resolved
string name = GetState<string>("rpcCalledEventName");

if (!rpcCalledEvents.Contains(name))
{
rpcCalledEvents.Add(name);
}


for (int i = 0; i < Math.Min(rpcRegisteredEvents.Count, rpcCalledEvents.Count); i++)
{
if (rpcRegisteredEvents[i] == rpcCalledEvents[i])
Expand All @@ -1096,6 +1105,9 @@ private static void InvokeRpcRegisterCallBack(string dataJson, string senderJson
public static void RpcCall(string name, object data, RpcMode mode, Action callbackOnResponse)
{

SetState("rpcCalledEventName", name, reliable: true);


string jsonData = ConvertToJson(data);

if (OnResponseCallbacks.ContainsKey(name))
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
89 changes: 58 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;

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

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

bool isMoving;

private void Awake()
void Awake()
{
// StartCoroutine(StartMatchmakingCoroutine());
Initialize();
}

private void Initialize()
{
PlayroomKit.InsertCoin(new PlayroomKit.InitOptions()
{
Expand All @@ -44,11 +52,24 @@ 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 arg1, string arg2)
{
print("helo");
}

void HandleScoreUpdate(string data, string caller)
Expand Down Expand Up @@ -89,25 +110,14 @@ 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);

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 +139,34 @@ 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 +176,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 +194,7 @@ private static void RemovePlayer(string playerID)
}
else
{
Debug.LogWarning("player not in dict");
Debug.LogWarning("Player is not in dictionary");
}

}
Expand Down
Loading