Files
Loong/Assets/_Loong/Scripts/Net/MRNetworkManager.cs
2025-10-25 18:32:49 +08:00

139 lines
3.8 KiB
C#

using DragonLi.Core;
using Mirror;
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class MRNetworkManager : NetworkRoomManager
{
public static MRNetworkManager Ins { get; private set; }
public GameObject GameManagerPre;
public NetworkDiscovery networkDiscovery;
public Dictionary<long, ServerResponse> DiscoveredServers = new Dictionary<long, ServerResponse>();
public List<ServerResponse> DiscoveredServersList = new List<ServerResponse>();
[NonSerialized]
public int NeedFresh = 0;
[NonSerialized]
public long CreateTime = 0;
public override void Awake()
{
base.Awake();
Ins = this;
}
public override void OnServerConnect(NetworkConnectionToClient conn)
{
// // cannot join game in progress
// if (!Utils.IsSceneActive(RoomScene))
// {
// Debug.Log($"Not in Room scene...disconnecting {conn}");
// conn.Disconnect();
// return;
// }
base.OnServerConnect(conn);
OnRoomServerConnect(conn);
}
public override void CallOnClientEnterRoom()
{
OnRoomClientEnter();
//foreach (NetworkRoomPlayer player in roomSlots)
// if (player != null)
// {
// player.OnClientEnterRoom();
// }
}
public override void OnServerAddPlayer(NetworkConnectionToClient conn)
{
// increment the index before adding the player, so first player starts at 1
clientIndex++;
allPlayersReady = false;
//Debug.Log("NetworkRoomManager.OnServerAddPlayer playerPrefab: {roomPlayerPrefab.name}");
GameObject newRoomGameObject = OnRoomServerCreateRoomPlayer(conn);
if (newRoomGameObject == null)
newRoomGameObject = Instantiate(roomPlayerPrefab.gameObject, Vector3.zero, Quaternion.identity);
NetworkServer.AddPlayerForConnection(conn, newRoomGameObject);
}
public override void OnStartServer()
{
// 记录创建时间
CreateTime = (long)(DateTime.Now.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
Debug.Log("服务器启动 - 创建时间:" + CreateTime);
GameObject gameManager = Instantiate(GameManagerPre);
// gameManager.GetComponent<GameManager>().playerIndex;
NetworkServer.Spawn(gameManager);
}
public override void OnGUI()
{
// 暂时关闭,勿删
// if (!showRoomGUI)
// return;
// GUILayout.BeginArea(new Rect(Screen.width - 150f, 10f, 140f, 30f));
// GUILayout.EndArea();
// GUI.Box(new Rect(10f, 180f, 520f, 150f), "PLAYERS");
}
public override void OnStartHost()
{
Debug.Log("Host启动");
}
// 加入房间
public void JoinRoom(string roomIp, long roomCreateTime)
{
Debug.Log("请求加入房间 ->" + roomIp);
networkAddress = roomIp;
CreateTime = roomCreateTime;
StartClient();
}
// 创建房间
public void CreateRoom()
{
networkAddress = "localhost";
StartServer();
}
// 创建并加入房间
public void CreateAndJoinRoom()
{
StartHost();
}
public void OnDiscoveredServer(ServerResponse info)
{
Debug.Log("发现主机!");
if (info.gameName == "KOF")
{
DiscoveredServers[info.serverId] = info;
DiscoveredServersList = DiscoveredServers.Values.ToList();
// 按创建时间排序
DiscoveredServersList.Sort(SortByCreateTime);
NeedFresh++;
}
}
public static int SortByCreateTime(ServerResponse a, ServerResponse b)
{
if (a.createTime >= b.createTime)
{
return 1;
}
else
{
return -1;
}
}
}