147 lines
3.4 KiB
C#
147 lines
3.4 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using Mirror;
|
|
using Characters;
|
|
using System;
|
|
|
|
public class CustomNetworkManager : NetworkManager
|
|
{
|
|
public List<GameObject> players;
|
|
|
|
// public event Action RestartCallback;
|
|
|
|
public List<Pawn> alive;
|
|
public List<Pawn> dead;
|
|
|
|
public event Action OnSceneChanged;
|
|
public event Action OnLiveStateCallback;
|
|
public event Action OnPlayerSpawned;
|
|
|
|
private float _restartTime = 5f;
|
|
public int maxMonsters;
|
|
|
|
public override void OnServerSceneChanged(string sceneName)
|
|
{
|
|
// foreach (var player in players)
|
|
// {
|
|
// player.GetComponent<Pawn>().OnLiveState -= OnLiveState;
|
|
// }
|
|
|
|
base.OnServerSceneChanged(sceneName);
|
|
|
|
players.Clear();
|
|
alive.Clear();
|
|
dead.Clear();
|
|
|
|
foreach (NetworkConnectionToClient conn in NetworkServer.connections.Values)
|
|
{
|
|
NetworkServer.SetClientReady(conn);
|
|
NetworkClient.ready = true;
|
|
|
|
if (conn.identity != null)
|
|
{
|
|
this.OnServerAddPlayer(conn);
|
|
}
|
|
}
|
|
|
|
OnSceneChanged?.Invoke();
|
|
}
|
|
|
|
public void OnConnectedToServer()
|
|
{
|
|
print("OnConnectedToServer");
|
|
if (players.Count == maxConnections)
|
|
{
|
|
|
|
}
|
|
}
|
|
|
|
public override void OnServerAddPlayer(NetworkConnectionToClient conn)
|
|
{
|
|
base.OnServerAddPlayer(conn);
|
|
players.Add(conn.identity.gameObject);
|
|
|
|
Pawn player = conn.identity.GetComponent<Pawn>();
|
|
|
|
player.OnLiveState += OnLiveState;
|
|
print("OnPlayerSpawned?.Invoke");
|
|
OnPlayerSpawned?.Invoke();
|
|
}
|
|
|
|
public override void OnServerDisconnect(NetworkConnectionToClient conn)
|
|
{
|
|
players.Remove(conn.identity.gameObject);
|
|
Pawn player = conn.identity.GetComponent<Pawn>();
|
|
|
|
player.OnLiveState -= OnLiveState;
|
|
|
|
if (alive.Contains(player))
|
|
alive.Remove(player);
|
|
|
|
if (dead.Contains(player))
|
|
dead.Remove(player);
|
|
|
|
base.OnServerDisconnect(conn);
|
|
}
|
|
|
|
[Server]
|
|
public void CallLiveStateCallback()
|
|
{
|
|
OnLiveStateCallback?.Invoke();
|
|
}
|
|
|
|
[Server]
|
|
private void OnLiveState(Pawn player, bool state)
|
|
{
|
|
if (state)
|
|
{
|
|
if (!alive.Contains(player))
|
|
alive.Add(player);
|
|
|
|
if (dead.Contains(player))
|
|
dead.Remove(player);
|
|
}
|
|
else
|
|
{
|
|
if (alive.Contains(player))
|
|
alive.Remove(player);
|
|
|
|
if (!dead.Contains(player))
|
|
dead.Add(player);
|
|
}
|
|
|
|
CallLiveStateCallback();
|
|
}
|
|
|
|
[Server]
|
|
public void RestartScene()
|
|
{
|
|
StartCoroutine(RestartTimer());
|
|
}
|
|
|
|
string getLevelById(Int32 id)
|
|
{
|
|
switch (id)
|
|
{
|
|
case 0 :
|
|
return "Assets/Scenes/Space.unity";
|
|
case 1 :
|
|
return "Assets/Scenes/Procedural.unity";
|
|
default:
|
|
return "Assets/Scenes/Space.unity";
|
|
}
|
|
}
|
|
|
|
// ReSharper disable Unity.PerformanceAnalysis
|
|
IEnumerator RestartTimer()
|
|
{
|
|
yield return new WaitForSeconds(_restartTime);
|
|
|
|
print(PlayerPrefs.GetInt("Level"));
|
|
|
|
ServerChangeScene( getLevelById(PlayerPrefs.GetInt("Level")));
|
|
StopCoroutine(RestartTimer());
|
|
}
|
|
}
|