154 lines
3.8 KiB
C#
154 lines
3.8 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using Mirror;
|
|
using System;
|
|
using Characters;
|
|
|
|
public enum GameSounds : byte
|
|
{
|
|
FirstBlood = 0,
|
|
StartSound,
|
|
PlayerDied,
|
|
}
|
|
|
|
public class NetworkGameManager : NetworkBehaviour
|
|
{
|
|
public static NetworkGameManager singleton { get; private set; }
|
|
private CustomNetworkManager networkManager;
|
|
|
|
private int _level;
|
|
|
|
public AudioSource AudioSource;
|
|
public GameObject enemyPrefab;
|
|
public List<GameObject> enemyList = new();
|
|
|
|
public GameObject winner;
|
|
public bool gameStart = false;
|
|
public event Action RestartCallback;
|
|
|
|
private AudioClip _firstBloodSound, _startSound, _playerDiedSound;
|
|
|
|
public AudioClip GetClipByType(GameSounds type)
|
|
{
|
|
switch (type)
|
|
{
|
|
case GameSounds.FirstBlood:
|
|
return _firstBloodSound;
|
|
|
|
case GameSounds.StartSound:
|
|
return _startSound;
|
|
|
|
case GameSounds.PlayerDied:
|
|
return _playerDiedSound;
|
|
|
|
default:
|
|
return _startSound;
|
|
}
|
|
}
|
|
|
|
|
|
void Awake()
|
|
{
|
|
if (singleton != null && singleton != this)
|
|
{
|
|
Destroy(this);
|
|
}
|
|
else
|
|
{
|
|
singleton = this;
|
|
}
|
|
}
|
|
|
|
void Start()
|
|
{
|
|
_level = PlayerPrefs.GetInt("Level");
|
|
|
|
_startSound = Resources.Load<AudioClip>("Audio/Dota2/the_battle_begins");
|
|
_firstBloodSound = Resources.Load<AudioClip>("Audio/Dota2/first_blood");
|
|
_playerDiedSound = Resources.Load<AudioClip>("Audio/CharacterSounds/ya_maslinu_poymal");
|
|
|
|
networkManager = NetworkManager.singleton.GetComponent<CustomNetworkManager>();
|
|
|
|
networkManager.OnLiveStateCallback += OnLiveStateCallback;
|
|
|
|
// foreach (var playerObject in networkManager.players)
|
|
// {
|
|
// var player = playerObject.GetComponent<Pawn>();
|
|
// player.OnLiveState += OnLiveState;
|
|
// }
|
|
}
|
|
|
|
private int _bufPlayerCount = 0;
|
|
|
|
[Server]
|
|
void OnLiveStateCallback()
|
|
{
|
|
if (networkManager.alive.Count == networkManager.players.Count - 1)
|
|
{
|
|
RpcPlaySound(GameSounds.FirstBlood);
|
|
_bufPlayerCount = networkManager.alive.Count;
|
|
}
|
|
else if (networkManager.alive.Count < _bufPlayerCount)
|
|
{
|
|
RpcPlaySound(GameSounds.PlayerDied);
|
|
_bufPlayerCount = networkManager.alive.Count;
|
|
//PROVERIT!
|
|
}
|
|
|
|
if (networkManager.dead.Count == networkManager.players.Count)
|
|
{
|
|
CmdRestartScene();
|
|
}
|
|
|
|
if (networkManager.alive.Count == 1 && networkManager.players.Count > 1)
|
|
{
|
|
SetWinner(networkManager.alive[0].gameObject);
|
|
}
|
|
}
|
|
|
|
[ClientRpc]
|
|
public void RpcPlaySound(GameSounds type)
|
|
{
|
|
AudioSource.PlayOneShot(GetClipByType(type));
|
|
}
|
|
|
|
|
|
[ClientRpc]
|
|
public void SetWinner(GameObject gameObject)
|
|
{
|
|
winner = gameObject;
|
|
CmdRestartScene();
|
|
}
|
|
|
|
|
|
[ClientRpc]
|
|
void RpcResetScene()
|
|
{
|
|
RestartCallback?.Invoke();
|
|
}
|
|
|
|
[Command(requiresAuthority = false)]
|
|
public void CmdRestartScene()
|
|
{
|
|
RpcResetScene();
|
|
networkManager.RestartScene();
|
|
}
|
|
|
|
private void FixedUpdate()
|
|
{
|
|
if (enemyList.Count == networkManager.maxMonsters || _level != 0) return;
|
|
|
|
if (networkManager.players.Count == networkManager.maxConnections )
|
|
{
|
|
for (int i = 0; i < networkManager.maxMonsters; i++)
|
|
{
|
|
GameObject go = Instantiate(enemyPrefab, new Vector3(80 + i, 0, 30.7f), Quaternion.identity);
|
|
|
|
NetworkServer.Spawn(go);
|
|
enemyList.Add(go);
|
|
}
|
|
}
|
|
}
|
|
}
|