ProjectZ/Assets/Scripts/Network/NetworkGameManager.cs

154 lines
3.8 KiB
C#
Raw Normal View History

2024-02-19 21:00:36 +03:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;
using System;
2024-03-19 17:42:38 +03:00
using Characters;
2024-02-19 21:00:36 +03:00
2024-03-21 18:28:37 +03:00
public enum GameSounds : byte
{
FirstBlood = 0,
StartSound,
PlayerDied,
}
2024-02-19 21:00:36 +03:00
public class NetworkGameManager : NetworkBehaviour
{
public static NetworkGameManager singleton { get; private set; }
private CustomNetworkManager networkManager;
2024-03-17 00:30:59 +03:00
2024-03-20 00:58:47 +03:00
private int _level;
2024-03-21 18:28:37 +03:00
public AudioSource AudioSource;
2024-03-17 00:30:59 +03:00
public GameObject enemyPrefab;
2024-03-20 00:58:47 +03:00
public List<GameObject> enemyList = new();
2024-02-19 21:00:36 +03:00
public GameObject winner;
2024-03-17 00:30:59 +03:00
public bool gameStart = false;
2024-03-20 00:58:47 +03:00
public event Action RestartCallback;
2024-03-21 18:28:37 +03:00
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;
}
}
2024-02-19 21:00:36 +03:00
void Awake()
{
if (singleton != null && singleton != this)
{
Destroy(this);
}
else
{
singleton = this;
}
}
void Start()
{
2024-03-20 00:58:47 +03:00
_level = PlayerPrefs.GetInt("Level");
2024-03-21 18:28:37 +03:00
_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");
2024-02-19 21:00:36 +03:00
networkManager = NetworkManager.singleton.GetComponent<CustomNetworkManager>();
networkManager.OnLiveStateCallback += OnLiveStateCallback;
// foreach (var playerObject in networkManager.players)
// {
// var player = playerObject.GetComponent<Pawn>();
// player.OnLiveState += OnLiveState;
// }
}
2024-03-21 18:28:37 +03:00
private int _bufPlayerCount = 0;
2024-02-19 21:00:36 +03:00
[Server]
void OnLiveStateCallback()
{
2024-03-21 18:28:37 +03:00
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!
}
2024-02-19 21:00:36 +03:00
if (networkManager.dead.Count == networkManager.players.Count)
{
CmdRestartScene();
}
2024-03-17 00:30:59 +03:00
if (networkManager.alive.Count == 1 && networkManager.players.Count > 1)
{
SetWinner(networkManager.alive[0].gameObject);
}
2024-02-19 21:00:36 +03:00
}
2024-03-21 18:28:37 +03:00
[ClientRpc]
public void RpcPlaySound(GameSounds type)
{
AudioSource.PlayOneShot(GetClipByType(type));
}
2024-02-19 21:00:36 +03:00
[ClientRpc]
public void SetWinner(GameObject gameObject)
{
winner = gameObject;
CmdRestartScene();
}
[ClientRpc]
void RpcResetScene()
{
RestartCallback?.Invoke();
}
[Command(requiresAuthority = false)]
public void CmdRestartScene()
{
RpcResetScene();
networkManager.RestartScene();
}
2024-03-17 00:30:59 +03:00
private void FixedUpdate()
{
2024-03-20 18:03:21 +03:00
if (enemyList.Count == networkManager.maxMonsters || _level != 0) return;
2024-03-17 00:30:59 +03:00
2024-03-20 00:58:47 +03:00
if (networkManager.players.Count == networkManager.maxConnections )
2024-03-17 00:30:59 +03:00
{
for (int i = 0; i < networkManager.maxMonsters; i++)
{
GameObject go = Instantiate(enemyPrefab, new Vector3(80 + i, 0, 30.7f), Quaternion.identity);
2024-03-20 00:58:47 +03:00
2024-03-17 00:30:59 +03:00
NetworkServer.Spawn(go);
enemyList.Add(go);
}
}
}
2024-02-19 21:00:36 +03:00
}