mirror of
https://github.com/DerTyp7/defrain-shooter-unity.git
synced 2025-10-29 20:52:10 +01:00
Merge branch 'player-master' into game-master
This commit is contained in:
72
Assets/Scripts/GameManager/PlayerMaster.cs
Normal file
72
Assets/Scripts/GameManager/PlayerMaster.cs
Normal file
@@ -0,0 +1,72 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class PlayerMaster : MonoBehaviour
|
||||
{
|
||||
[Header("PlayerMaster")]
|
||||
[SerializeField] private List<ServerPlayer> Players = new List<ServerPlayer>(); //Contains All Players which are currently connected/in-game
|
||||
|
||||
//JUST FOR DEBUG
|
||||
[SerializeField] private GameObject TestPlayer;
|
||||
|
||||
private void Update()
|
||||
{
|
||||
//JUST FOR DEBUG
|
||||
Players[0].AddKills(1);
|
||||
}
|
||||
|
||||
|
||||
private void Start()
|
||||
{
|
||||
foreach(GameObject p in GameObject.FindGameObjectsWithTag("Player"))
|
||||
{
|
||||
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
|
||||
{
|
||||
foreach (ServerPlayer p in Players)
|
||||
{
|
||||
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
|
||||
{
|
||||
foreach (ServerPlayer p in Players)
|
||||
{
|
||||
if (p.Player == player)
|
||||
{
|
||||
Players.Remove(p);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int SyncHealth(GameObject player)
|
||||
{
|
||||
foreach (ServerPlayer p in Players)
|
||||
{
|
||||
if (p.Player == player)
|
||||
{
|
||||
return p.GetHealth();
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/GameManager/PlayerMaster.cs.meta
Normal file
11
Assets/Scripts/GameManager/PlayerMaster.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cb5ee5f3d2923ee4faeaf50ed35ab2f5
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
114
Assets/Scripts/GameManager/ServerPlayer.cs
Normal file
114
Assets/Scripts/GameManager/ServerPlayer.cs
Normal 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;
|
||||
}
|
||||
|
||||
}
|
||||
11
Assets/Scripts/GameManager/ServerPlayer.cs.meta
Normal file
11
Assets/Scripts/GameManager/ServerPlayer.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c2e457ad00c1a044a924a5bc00776df9
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
35
Assets/Scripts/Player/Player.cs
Normal file
35
Assets/Scripts/Player/Player.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class Player : MonoBehaviour
|
||||
{
|
||||
[SerializeField] int health;
|
||||
[SerializeField] float SyncIntervalSeconds = 5.0f;
|
||||
[SerializeField] GameObject GameManager;
|
||||
|
||||
private PlayerMaster playerMaster;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
playerMaster = GameManager.GetComponent<PlayerMaster>();
|
||||
InvokeRepeating("Sync", 3.0f, SyncIntervalSeconds);
|
||||
}
|
||||
|
||||
|
||||
private void Sync()
|
||||
{
|
||||
Debug.Log("Sync");
|
||||
health = playerMaster.SyncHealth(gameObject);
|
||||
}
|
||||
|
||||
public void SubstractHealth(int value)
|
||||
{
|
||||
health -= value;
|
||||
}
|
||||
|
||||
public void AddHealth(int value)
|
||||
{
|
||||
health += value;
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Player/Player.cs.meta
Normal file
11
Assets/Scripts/Player/Player.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3317f24e780855847830f5662153b41d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -80,7 +80,7 @@ public class PlayerController : MonoBehaviour
|
||||
//Jump
|
||||
if (Input.GetButtonDown("Jump") && isGrounded)
|
||||
{
|
||||
Debug.Log("Jump");
|
||||
//Debug.Log("Jump");
|
||||
velocityY += Mathf.Sqrt(jumpHeight * -2f * gravity);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user