This repository has been archived on 2023-09-13. You can view files and clone it, but cannot push or open issues or pull requests.
station_obscurum_unity/Assets/Scripts/Steam/SteamLobbyWithPlayer.cs

165 lines
4.8 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Steamworks;
using Steamworks.Data;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
public class SteamLobbyWithPlayer : MonoBehaviour
{
public static Lobby CurrentLobby;
public static bool UserInLobby;
public UnityEvent onLobbyCreated;
public UnityEvent onLobbyJoined;
public UnityEvent onLobbyLeave;
public Dictionary<SteamId, GameObject> inLobby = new();
public GameObject playerPrefab;
private void Start()
{
DontDestroyOnLoad(this);
SteamMatchmaking.OnLobbyCreated += OnLobbyCreatedCallBack;
SteamMatchmaking.OnLobbyEntered += OnLobbyEntered;
SteamMatchmaking.OnLobbyMemberJoined += OnLobbyMemberJoined;
// SteamMatchmaking.OnChatMessage += OnChatMessage;
SteamMatchmaking.OnLobbyMemberDisconnected += OnLobbyMemberDisconnected;
SteamMatchmaking.OnLobbyMemberLeave += OnLobbyMemberDisconnected;
SteamMatchmaking.OnLobbyGameCreated += OnLobbyGameCreated;
SteamFriends.OnGameLobbyJoinRequested += OnGameLobbyJoinRequest;
SteamMatchmaking.OnLobbyInvite += OnLobbyInvite;
CreateLobbyAsync();
}
private void OnLobbyInvite(Friend friend, Lobby lobby)
{
Debug.Log($"{friend.Name} invited you to his lobby.");
}
private void OnLobbyGameCreated(Lobby lobby, uint ip, ushort port, SteamId id)
{
}
private async void OnLobbyMemberJoined(Lobby lobby, Friend friend)
{
Debug.Log($"{friend.Name} joined the lobby");
GameObject obj = Instantiate(playerPrefab, Vector3.zero, Quaternion.identity);
inLobby.Add(friend.Id, obj);
}
private void OnLobbyMemberDisconnected(Lobby lobby, Friend friend)
{
Debug.Log($"{friend.Name} left the lobby");
Debug.Log($"New lobby owner is {CurrentLobby.Owner}");
if (inLobby.ContainsKey(friend.Id))
{
Destroy(inLobby[friend.Id]);
inLobby.Remove(friend.Id);
}
}
private async void OnGameLobbyJoinRequest(Lobby joinedLobby, SteamId id)
{
var joinedLobbySuccess = await joinedLobby.Join();
if (joinedLobbySuccess != RoomEnter.Success)
Debug.Log("failed to join lobby : " + joinedLobbySuccess);
else
CurrentLobby = joinedLobby;
}
private void OnLobbyCreatedCallBack(Result result, Lobby lobby)
{
if (result != Result.OK)
{
Debug.Log("lobby creation result not ok : " + result);
}
else
{
onLobbyCreated.Invoke();
Debug.Log("lobby creation result ok");
}
}
private async void OnLobbyEntered(Lobby lobby)
{
Debug.Log("Client joined the lobby");
UserInLobby = true;
foreach (var user in inLobby.Values) Destroy(user);
inLobby.Clear();
2023-06-01 18:11:37 +02:00
GameObject player = Instantiate(playerPrefab);
if(player == null)
Debug.Log("Player is null");
player.SetActive(true);
inLobby.Add(SteamClient.SteamId, player);
foreach (var friend in CurrentLobby.Members)
if (friend.Id != SteamClient.SteamId)
{
GameObject other_player = Instantiate(playerPrefab, Vector3.zero, Quaternion.identity);
inLobby.Add(friend.Id, other_player);
}
onLobbyJoined.Invoke();
}
public async void CreateLobbyAsync()
{
var result = await CreateLobby();
if (!result)
{
Debug.Log("Failed to create lobby");
}
else
{
Debug.Log("Lobby created");
}
}
public static async Task<bool> CreateLobby()
{
try
{
var createLobbyOutput = await SteamMatchmaking.CreateLobbyAsync();
if (!createLobbyOutput.HasValue)
{
Debug.Log("Lobby created but not correctly instantiated.");
return false;
}
CurrentLobby = createLobbyOutput.Value;
CurrentLobby.SetPublic();
//currentLobby.SetPrivate();
CurrentLobby.SetJoinable(true);
return true;
}
catch (Exception exception)
{
Debug.Log("Failed to create multiplayer lobby : " + exception);
return false;
}
}
public void LeaveLobby()
{
try
{
UserInLobby = false;
CurrentLobby.Leave();
onLobbyLeave.Invoke();
foreach (var user in inLobby.Values) Destroy(user);
inLobby.Clear();
}
catch
{
}
}
private void Update()
{
if(Input.GetKeyDown(KeyCode.Return)) {
print("OWNER?:"+CurrentLobby.Owner);
}
}
}