141 lines
3.8 KiB
C#
141 lines
3.8 KiB
C#
using DragonLi.Core;
|
|
using Mirror;
|
|
using Mirror.Discovery;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Unity.Mathematics;
|
|
using UnityEngine;
|
|
using UnityEngine.PlayerLoop;
|
|
|
|
public class MRNetworkManager : NetworkRoomManager
|
|
{
|
|
public static MRNetworkManager Ins { get; private set; }
|
|
public GameObject GameManagerPre;
|
|
public GameObject StoryManagerPre;
|
|
public GameObject StatisticManagerPre;
|
|
|
|
public NetworkDiscovery networkDiscovery;
|
|
public Dictionary<long, ServerResponse> discoveredServers = new Dictionary<long, ServerResponse>();
|
|
public int NeedFresh = 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();
|
|
if (roomSlots[roomSlots.Count - 1] != null)
|
|
{
|
|
StatisticManager.Ins.lastPositions.Add(roomSlots[roomSlots.Count - 1].transform.position);
|
|
StatisticManager.Ins.totalDistanceMoveds.Add(0f);
|
|
|
|
|
|
StatisticManager.Ins?.AddStatisticInfo(roomSlots[roomSlots.Count - 1].index);
|
|
|
|
}
|
|
//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);
|
|
|
|
Debug.Log("clientIndex" + clientIndex + "||" + newRoomGameObject);
|
|
}
|
|
|
|
public override void OnStartServer()
|
|
{
|
|
Debug.Log("服务器启动");
|
|
// 创建每端的GameManager
|
|
GameObject gameManager = Instantiate(GameManagerPre);
|
|
NetworkServer.Spawn(gameManager);
|
|
|
|
GameObject storyManager = Instantiate(StoryManagerPre);
|
|
NetworkServer.Spawn(storyManager);
|
|
|
|
GameObject statisticManager = Instantiate(StatisticManagerPre);
|
|
NetworkServer.Spawn(statisticManager);
|
|
}
|
|
|
|
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 = "192.168.1.221")
|
|
{
|
|
Debug.Log("加入房间" + roomIp);
|
|
networkAddress = roomIp;
|
|
StartClient();
|
|
}
|
|
|
|
// 创建房间
|
|
public void CreateRoom()
|
|
{
|
|
networkAddress = "localhost";
|
|
StartServer();
|
|
}
|
|
|
|
// 创建并加入房间
|
|
public void CreateAndJoinRoom()
|
|
{
|
|
StartHost();
|
|
}
|
|
|
|
public void OnDiscoveredServer(ServerResponse info)
|
|
{
|
|
Debug.Log("发现主机!");
|
|
// Note that you can check the versioning to decide if you can connect to the server or not using this method
|
|
discoveredServers[info.serverId] = info;
|
|
NeedFresh++;
|
|
}
|
|
}
|