adde ServerPlayer

This commit is contained in:
DerTyp187
2021-10-15 14:18:45 +02:00
parent 1deb6add0d
commit add984e36c
4 changed files with 155 additions and 113 deletions

View File

@@ -26,11 +26,7 @@ DEAD
public class PlayerMaster : MonoBehaviour
{
[Header("PlayerMaster")]
[SerializeField] private List<GameObject> Players = new List<GameObject>(); //Contains All Players which are currently connected/in-game
[SerializeField] private List<int> Health = new List<int>();
[SerializeField] private List<int> Kills = new List<int>();
[SerializeField] private List<int> Deaths = new List<int>();
[SerializeField] private int defaultHp = 100;
[SerializeField] private List<ServerPlayer> Players = new List<ServerPlayer>(); //Contains All Players which are currently connected/in-game
//JUST FOR DEBUG
[SerializeField] private GameObject TestPlayer;
@@ -38,139 +34,60 @@ public class PlayerMaster : MonoBehaviour
private void Update()
{
//JUST FOR DEBUG
/*Debug.Log(GetHealthOfPlayer(TestPlayer));
SubstractHealthFromPlayer(TestPlayer, 1);*/
Players[0].AddKills(1);
}
private void Start()
{
Players.AddRange(GameObject.FindGameObjectsWithTag("Player")); //Add All Player-GameObjects into a List
//Init Health List
foreach(GameObject player in Players)
foreach(GameObject p in GameObject.FindGameObjectsWithTag("Player"))
{
Health.Add(defaultHp);
Kills.Add(0);
Deaths.Add(0);
Players.Add(new ServerPlayer(p));
}
InvokeRepeating("TestDamage", 3.0f, 3f);
}
public void TestDamage()
{
Players[0].RemoveHealth(10);
}
//Join
public void OnPlayerJoin(GameObject player) //When a Player joins
{
Debug.Log("Player joined"); //Give Console Feedback
if (!Players.Contains(player)) //If the Player is NOT in the "Players-List" (For Error Handling)
foreach (ServerPlayer p in Players)
{
Players.Add(player); //Add New Player To List
Health.Add(defaultHp); //Add New Health to the END of the list
Kills.Add(0);
Deaths.Add(0);
Debug.Log("Player added to list"); //Feedback
}
else
{
Debug.LogError("Player already exits in list"); //Error, because the "new" Player is already in the list -> !critical Anomaly!
if (p.Player == player)
{
Debug.Log("Joined Player already exits");
return;
}
}
Players.Add(new ServerPlayer(player));
}
//Leave
public void OnPlayerLeave(GameObject player) //When a Player leaves
{
Debug.Log("Player left");//Give Console Feedback
if (Players.Contains(player))//If the Player IS in the "Players-List" (For Error Handling)
foreach (ServerPlayer p in Players)
{
Players.Remove(player); //Remove the Player from List
Health.Remove(Players.IndexOf(player)); //Remove the specific Health of the Player
Kills.Remove(Players.IndexOf(player));
Deaths.Remove(Players.IndexOf(player));
}
else
{
Debug.LogError("Player not found in Players-list"); //Error, because the Player is NOT in the list -> !critical Anomaly!
if (p.Player == player)
{
Players.Remove(p);
}
}
}
//Health
private void CheckIfPlayerAlive(GameObject player)//Is a Player dead?
public int SyncHealth(GameObject player)
{
if (GetHealthOfPlayer(player) <= 0)
foreach (ServerPlayer p in Players)
{
Death(player);
if (p.Player == player)
{
return p.GetHealth();
}
}
}
public int GetHealthOfPlayer(GameObject player) //Get The Health value of a player
{
return Health[Players.IndexOf(player)];
}
public void SetHealthOfPlayer(GameObject player, int value) //z.B. wenn ein spieler getroffen wird und dmg bekommt
{
Health[Players.IndexOf(player)] = value;
CheckIfPlayerAlive(player);
}
public void SubstractHealthFromPlayer(GameObject player, int value) //z.B. wenn ein spieler getroffen wird und dmg bekommt
{
Health[Players.IndexOf(player)] -= value;
CheckIfPlayerAlive(player);
}
public void AddHealthToPlayer(GameObject player, int value) //z.B. wenn ein spieler geheilt wird
{
Health[Players.IndexOf(player)] += value;
}
//Kills
public int GetKillsOfPlayer(GameObject player)
{
return Kills[Players.IndexOf(player)];
}
public void AddKillsToPlayer(GameObject player, int value = 1)
{
Kills[Players.IndexOf(player)] += value;
}
public void SubstractKillsFromPlayer(GameObject player, int value = 1)
{
Kills[Players.IndexOf(player)] -= value;
}
public void SetKillsOfPlayer(GameObject player, int value = 1)
{
Kills[Players.IndexOf(player)] = value;
}
//Death
private void Death(GameObject deadPlayer, GameObject killerPlayer = null) //Player dies and and MAYBE another player gets a kill
{
if(killerPlayer != null)
{
//Add kill to killer
Kills[Players.IndexOf(killerPlayer)] += 1;
}
Deaths[Players.IndexOf(deadPlayer)] += 1;
//Deactivate deadPlayer
}
public int GetDeathsOfPlayer(GameObject player)
{
return Deaths[Players.IndexOf(player)];
}
public void AddDeathsToPlayer(GameObject player, int value = 1)
{
Deaths[Players.IndexOf(player)] += value;
}
public void SubstractDeathsFromPlayer(GameObject player, int value = 1)
{
Deaths[Players.IndexOf(player)] -= value;
}
public void SetDeathsOfPlayer(GameObject player, int value = 1)
{
Deaths[Players.IndexOf(player)] = value;
return -1;
}
}

View File

@@ -0,0 +1,114 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ServerPlayer
{
public GameObject Player;
public bool isAlive;
private int health;
private int kills;
private int deaths;
private const int defaultHp = 100;
public ServerPlayer(GameObject _Player, bool _isAlive = true, int _health = defaultHp, int _kills = 0, int _deaths = 0)
{
Player = _Player;
isAlive = _isAlive;
health = _health;
kills = _kills;
deaths = _deaths;
}
public void Respawn()
{
isAlive = true;
}
public void Die()
{
isAlive = false;
AddDeaths(1);
}
//Health
public void AddHealth(int value)
{
if (isAlive)
{
health += value;
}
}
public void RemoveHealth(int value)
{
if (isAlive)
{
health -= value;
if (health <= 0)
{
AddDeaths(1);
health = 0;
Die();
}
}
}
public void SetHealth(int value)
{
if (isAlive)
{
health = value;
if (health <= 0)
{
AddDeaths(1);
health = 0;
Die();
}
}
}
public int GetHealth()
{
return health;
}
//Kills
public void AddKills(int value)
{
kills += value;
}
public void RemoveKills(int value)
{
kills -= value;
}
public void SetKills(int value)
{
kills = value;
}
public int GetKills()
{
return kills;
}
//Deaths
public void AddDeaths(int value)
{
deaths += value;
}
public void RemoveDeaths(int value)
{
deaths -= value;
}
public void SetDeaths(int value)
{
deaths = value;
}
public int GetDeaths()
{
return deaths;
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c2e457ad00c1a044a924a5bc00776df9
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -20,7 +20,7 @@ public class Player : MonoBehaviour
private void Sync()
{
Debug.Log("Sync");
health = playerMaster.GetHealthOfPlayer(gameObject);
health = playerMaster.SyncHealth(gameObject);
}
public void SubstractHealth(int value)