Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class MultiplayerServer : MonoBehaviour {
- public List<ClientInfo> clientList;
- public List<ClientInfo> deadClients;
- public int updateRate = 20;
- private float currentTimeBetweenUpdates;
- private PhotonView view;
- void Awake ()
- {
- if(PhotonNetwork.isMasterClient)
- {
- clientList = new List<ClientInfo>();
- view = GetComponent<PhotonView>();
- }
- else
- {
- this.enabled = false;
- }
- }
- void FixedUpdate ()
- {
- currentTimeBetweenUpdates -= Time.fixedDeltaTime;
- if(currentTimeBetweenUpdates <= 0)
- {
- MultiplayerUpdate();
- currentTimeBetweenUpdates = (float)updateRate / 1000f;
- }
- }
- private void MultiplayerUpdate()
- {
- RemoveDeadClients();
- string[] _playerNames = GetPlayerNames();
- int[] _playerKills = GetPlayerKills();
- int[] _playerDeaths = GetPlayerDeaths();
- view.RPC("UpdateLeaderBoard", PhotonTargets.All, _playerNames, _playerKills, _playerDeaths);
- }
- private void RemoveDeadClients()
- {
- for(int i=clientList.Count-1; i >= 0; i--)
- {
- if(clientList[i].ClientDead())
- {
- Debug.Log("A user went missing! Moving player " + clientList[i].client.clientName + " to dead clients!");
- deadClients.Add(clientList[i]);
- clientList.RemoveAt(i);
- }
- }
- }
- private void RegisterNewClients()
- {
- MultiplayerClient[] allClients = GameObject.FindObjectsOfType<MultiplayerClient>();
- foreach(MultiplayerClient selectedClient in allClients)
- {
- if(selectedClient.setup == false)
- {
- clientList.Add(new ClientInfo(selectedClient.clientName, selectedClient));
- selectedClient.GetComponent<PhotonView>().RPC("SyncClientInfo", PhotonTargets.AllBuffered, true, selectedClient.clientName);
- Debug.Log("Found new user! Registered player " + selectedClient.clientName + " as new client!");
- }
- }
- }
- private void SpawnPlayer(Character selectedCharacter, TeamId team)
- {
- }
- private string[] GetPlayerNames()
- {
- List<string> names = new List<string>();
- for(int i=0; i<clientList.Count; i++)
- {
- names.Add(clientList[i].client.name);
- }
- return names.ToArray();
- }
- private int[] GetPlayerKills()
- {
- List<int> kills = new List<int>();
- for(int i=0; i<clientList.Count; i++)
- {
- kills.Add(clientList[i].kills);
- }
- return kills.ToArray();
- }
- private int[] GetPlayerDeaths()
- {
- List<int> deaths = new List<int>();
- for(int i=0; i<clientList.Count; i++)
- {
- deaths.Add(clientList[i].deaths);
- }
- return deaths.ToArray();
- }
- }
- public class ClientInfo
- {
- public string name;
- public MultiplayerClient client;
- public int kills;
- public int deaths;
- public PlayerInfo playerInfo;
- public ClientInfo(string n, MultiplayerClient c)
- {
- this.name = n;
- this.client = c;
- this.kills = 0;
- this.deaths = 0;
- this.playerInfo = new PlayerInfo();
- }
- ///<summary>
- ///This checks to see if the connection is still "living" aka the connection object is still instantiated.
- ///</summary>
- public bool ClientDead()
- {
- if(client == null)
- {
- return true;
- }
- return false;
- }
- ///<summary>
- ///Adds a kill to the client.
- ///</summary>
- public void AddKill()
- {
- kills++;
- }
- ///<summary>
- ///Adds a death to the client.
- ///</summary>
- public void AddDeath()
- {
- deaths++;
- }
- ///<summary>
- ///Calculates and returns KD ratio.
- ///</summary>
- public float KdRatio()
- {
- int killCount = kills;
- int deathCount = deaths;
- if(kills < 1)
- {
- killCount = 1;
- }
- if(deaths < 1)
- {
- deathCount = 1;
- }
- return ((float)killCount/(float)deathCount);
- }
- }
- public class PlayerInfo
- {
- public float health = 100f;
- public float maxHealth = 100f;
- public TeamId currentTeam = TeamId.None;
- public GameObject player = null;
- ///<summary>
- ///Adds change to health.
- ///Returns if the player is alive.
- ///</summary>
- public bool ChangeHealth(float change)
- {
- health += change;
- if(health > 0)
- {
- if(health > maxHealth)
- {
- health = maxHealth;
- }
- }
- else
- {
- health = 0;
- if(player != null)
- {
- PhotonNetwork.Destroy(player);
- }
- return false;
- }
- return true;
- }
- public void SetValues(float maxH)
- {
- health = maxH;
- maxHealth = maxH;
- }
- }
- public enum TeamId {
- Red,
- Blue,
- None
- }
- public enum Characters {
- EDE,
- Time_Master
- }
Add Comment
Please, Sign In to add comment