ClosedRoomsManager.cs 1.17 KB
Newer Older
cann-alberto's avatar
cann-alberto committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using Mirror;
using UnityEngine;

public class ClosedRoomsManager : NetworkBehaviour
{   
    
    void Update()
    {
        if (isServer)
        {
            if (!PortManager.Instance.PortsToDestroy.IsEmpty)
            {
                while (PortManager.Instance.PortsToDestroy.TryDequeue(out int port))
                {
                    Debug.Log($"Try dequeue and destroy port: {port}");
                    DestroyRoom(port);
                }
            }            
        }        
    }
    
    public void DestroyRoom(int port)
    {
        if (!isServer) return;         
        
        Debug.Log("Searching the room");

        GameObject roomObject = PortManager.Instance.GetRoom(port);
        if (roomObject != null)
        {
            Debug.Log($"Destroying room on port {port}");

            // Free the port before destroying the object
            PortManager.Instance.FreePort(port);

            // destroy the GameObject 
            NetworkServer.Destroy(roomObject);
            Debug.Log($"Room on port {port} has been destroyed.");
        }
        else
        {
            Debug.LogWarning($"No room found on port {port}.");
        }
    }
}