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

Fixes! #30

Merged
merged 3 commits into from
Jan 19, 2024
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
6 changes: 3 additions & 3 deletions Assets/PlayroomKit/PlayroomKit.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using UnityEngine;
using System.Collections.Generic;
using System.Globalization;
using System.Runtime.InteropServices;
using AOT;
using System;
Expand Down Expand Up @@ -57,7 +58,6 @@ public static void InsertCoin(Action callback, InitOptions options = null)
InsertCoinCallback = callback;
string optionsJson = null;
if (options != null) { optionsJson = SerializeInitOptions(options); }
Debug.Log("C# " + optionsJson);
InsertCoinInternal(InvokeInsertCoin, optionsJson, __OnPlayerJoinCallbackHandler, __OnQuitInternalHandler);
}
else
Expand Down Expand Up @@ -336,7 +336,7 @@ public static void SetState(string key, float value, bool reliable = false)
{
if (IsRunningInBrowser())
{
var floatAsString = value.ToString();
var floatAsString = value.ToString(CultureInfo.InvariantCulture);
SetStateFloatInternal(key, floatAsString, reliable);
}
else
Expand Down Expand Up @@ -929,7 +929,7 @@ public void SetState(string key, float value, bool reliable = false)
{
if (IsRunningInBrowser())
{
SetPlayerStateFloatByPlayerId(id, key, value.ToString(), reliable);
SetPlayerStateFloatByPlayerId(id, key, value.ToString(CultureInfo.InvariantCulture), reliable);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this done everywhere where ToString is used? If yes, has it been tested to not have any negative effects?

}
else
{
Expand Down
8 changes: 1 addition & 7 deletions Assets/Plugins/PlayroomPlugin.jslib
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mergeInto(LibraryManager.library, {
// onLaunchCallback
) {
function embedScript(src) {
script.crossOrigin = 'anonymous';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tested this? does this cause any issues?

Copy link
Collaborator

@SaadBazaz SaadBazaz Jan 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requested and tested by a user here #25.

Also tested locally. Does not break the SDK in any way, however, the security of it is debatable. We shouldn't allow all origins I suppose. @momintlh

Will keep this under watch.

return new Promise((resolve, reject) => {
var script = document.createElement("script");
script.src = src;
Expand All @@ -23,7 +24,6 @@ mergeInto(LibraryManager.library, {
}

var options = optionsJson ? JSON.parse(UTF8ToString(optionsJson)) : {};
// console.log(options)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove, don't comment.
Keeps the code neat and clean.


Promise.all([
embedScript("https://unpkg.com/[email protected]/umd/react.development.js"),
Expand Down Expand Up @@ -713,23 +713,17 @@ mergeInto(LibraryManager.library, {
}

const players = window._multiplayer.getPlayers();
console.log(players)

if (typeof players !== "object" || players === null) {
console.error('The "players" variable is not an object:', players);
return null;
}
const playerState = players[UTF8ToString(playerID)];
console.log(playerState)

if (!playerState) {
console.error("Player with ID", UTF8ToString(playerID), "not found.");
return null;
}

const p = playerState.kick()
console.log(p)

playerState.kick().then(() => {
dynCall('v', onKickCallBack, [])
}).catch((error) => {
Expand Down
12 changes: 6 additions & 6 deletions Assets/Scenes/Game.unity
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ GameObject:
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 0
m_IsActive: 1
--- !u!224 &77751265
RectTransform:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -207,9 +207,9 @@ MonoBehaviour:
m_OnClick:
m_PersistentCalls:
m_Calls:
- m_Target: {fileID: 330732771}
m_TargetAssemblyTypeName: TestManager, Assembly-CSharp
m_MethodName: SetStateTest
- m_Target: {fileID: 330732769}
m_TargetAssemblyTypeName: GameManager, Assembly-CSharp
m_MethodName: Kick
m_Mode: 1
m_Arguments:
m_ObjectArgument: {fileID: 0}
Expand Down Expand Up @@ -454,7 +454,7 @@ MonoBehaviour:
m_HorizontalOverflow: 0
m_VerticalOverflow: 0
m_LineSpacing: 1
m_Text: SetState
m_Text: Kick
--- !u!222 &360417501
CanvasRenderer:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -615,7 +615,7 @@ GameObject:
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
m_IsActive: 0
--- !u!114 &1036353973
MonoBehaviour:
m_ObjectHideFlags: 0
Expand Down
69 changes: 19 additions & 50 deletions Assets/Scripts/GameManager.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using AOT;
using Playroom;
using UnityEngine;
Expand All @@ -16,63 +17,48 @@ public class GameManager : MonoBehaviour
[SerializeField] private int score = 0;
[SerializeField] private static bool playerJoined;


private void Awake()
{
#if !UNITY_EDITOR && UNITY_WEBGL
WebGLInput.captureAllKeyboardInput = false;

PlayroomKit.InsertCoin(() =>
{
PlayroomKit.OnPlayerJoin(AddPlayer);
PlayroomKit.SetState("score", score);
//PlayroomKit.SetState("score", score);
//PlayroomKit.SetState("Started", true);
WebGLInput.captureAllKeyboardInput = true;
});
#else
#endif
}

private void Start()
{

[MonoPInvokeCallback(typeof(Action))]
static void WaitForSate()
{
Debug.Log("Waiting for state callback");
}


private void Update()
{
if (playerJoined)
{
var myPlayer = PlayroomKit.MyPlayer();
var index = players.IndexOf(myPlayer);

//Debug.Log("Started" + PlayroomKit.GetState<bool>("Started"));

/*var myDpad = PlayroomKit.DpadJoystick();


if (myDpad.y == "up")
{
playerGameObjects[index].GetComponent<PlayerController>().Jump();
}
if (myDpad.x == "left")
{
playerGameObjects[index].GetComponent<PlayerController>().dirX = -1;
}
else if (myDpad.x == "right")
{
playerGameObjects[index].GetComponent<PlayerController>().dirX = 1;
}
else
{
playerGameObjects[index].GetComponent<PlayerController>().dirX = 0;
}*/
playerGameObjects[index].GetComponent<PlayerController>().Move();

if (Input.GetKeyDown(KeyCode.L))
{
var code = PlayroomKit.GetRoomCode();
Debug.Log("Room Code" + code);
}


players[index].SetState("posX", playerGameObjects[index].GetComponent<Transform>().position.x);
players[index].SetState("posY", playerGameObjects[index].GetComponent<Transform>().position.y);

}



for (var i = 0; i < players.Count; i++)
{

Expand All @@ -91,7 +77,7 @@ private void Update()
if (playerGameObjects[i].GetComponent<Transform>().position.x >= 0f)
{
score += 10;
PlayroomKit.SetState("score", score);
//PlayroomKit.SetState("score", score);
}
}
else
Expand All @@ -102,33 +88,16 @@ private void Update()
}
}


public static 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);


// creates joystick
/*PlayroomKit.CreateJoyStick(new PlayroomKit.JoystickOptions()
{
type = "dpad",
buttons = new []{new PlayroomKit.ButtonOptions()
{
id = "jump",
label = "jump"
}},
zones = new PlayroomKit.ZoneOptions() {right = new PlayroomKit.ButtonOptions()
{
id = "right",
label = "right"
}}

});
*/

playerObj.GetComponent<SpriteRenderer>().color = player.GetProfile().color;
Debug.Log(player.GetProfile().name + " Joined the game!" + "id: " + player.id);


PlayerDict.Add(player.id, playerObj);
players.Add(player);
playerGameObjects.Add(playerObj);
Expand Down