Skip to content

Commit

Permalink
added dummy player movement communication
Browse files Browse the repository at this point in the history
  • Loading branch information
13on4rd committed Jan 15, 2025
1 parent 6345b1a commit 57322a3
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 19 deletions.
86 changes: 86 additions & 0 deletions Assets/Prefabs/Player.prefab

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

7 changes: 7 additions & 0 deletions Assets/Prefabs/Player.prefab.meta

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

31 changes: 18 additions & 13 deletions Assets/Scenes/Player/Player.unity

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

26 changes: 26 additions & 0 deletions Assets/Scripts/GameManager/DTOs/MultiplayerDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using UnityEngine;

public class MultiplayerDTO
{
public Vector3 position;
public AreaLocationDTO areaLocation;
public int character;

public MultiplayerDTO (Vector3 position, AreaLocationDTO areaLocation, int character)
{
this.position = position;
this.areaLocation = areaLocation;
this.character = character;
}

/// <summary>
/// This function converts a json string to a <c>MultiplayerDTO</c> object.
/// </summary>
/// <param name="jsonString">The json string to convert</param>
/// <returns>A <c>MultiplayerDTO</c> object containing the data</returns>
public static MultiplayerDTO CreateFromJSON(string jsonString)
{
return JsonUtility.FromJson<MultiplayerDTO>(jsonString);
}
}
11 changes: 11 additions & 0 deletions Assets/Scripts/GameManager/DTOs/MultiplayerDTO.cs.meta

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

32 changes: 26 additions & 6 deletions Assets/Scripts/GameManager/MultiplayerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@
using System;
using NativeWebSocket;
using System.Threading.Tasks;
using UnityEngine.UIElements;

public class MultiplayerManager : MonoBehaviour
{
private WebSocket websocket;
private bool connected = false;
private AreaLocationDTO areaLocationDTO;
[SerializeField] private GameObject prefab;
GameObject player;
private bool first = true;

#region singelton
public static MultiplayerManager Instance { get; private set; }
Expand All @@ -31,6 +36,11 @@ private void Awake()
}
#endregion

private void Start()
{
areaLocationDTO = new AreaLocationDTO(1,1);
}

void Update()
{
if (connected)
Expand Down Expand Up @@ -63,14 +73,23 @@ public async Task Initialize()

websocket.OnMessage += (bytes) =>
{
Debug.Log("Message!");
// getting the message as a string
var message = System.Text.Encoding.UTF8.GetString(bytes);
Debug.Log("message: " + message);
MultiplayerDTO data = MultiplayerDTO.CreateFromJSON(System.Text.Encoding.UTF8.GetString(bytes));
if (first)
{
first = false;
player = Instantiate(prefab, new Vector3(data.position.x, data.position.y - 1, 0), Quaternion.identity);
}
else
{
player.transform.position = new Vector3(data.position.x, data.position.y - 1, 0);
}

Debug.Log("message: " + System.Text.Encoding.UTF8.GetString(bytes));
};

// Keep sending messages at every 0.3s
InvokeRepeating(nameof(SendWebSocketMessage), 0.0f, 1.0f);
// Keep sending messages at every
InvokeRepeating(nameof(SendWebSocketMessage), 1.0f, 0.03f);

// waiting for messages
await websocket.Connect();
Expand All @@ -81,7 +100,8 @@ async void SendWebSocketMessage()
if (websocket.State == WebSocketState.Open)
{
// Sending plain text
await websocket.SendText("hello world!");
Debug.Log(transform.position.ToString());
await websocket.SendText(JsonUtility.ToJson(new MultiplayerDTO(transform.position, areaLocationDTO, 1)));
}
}

Expand Down

0 comments on commit 57322a3

Please sign in to comment.