Added base Round Logic and base map with characters
This commit is contained in:
@@ -10,9 +10,11 @@ public class PlayerHealth : NetworkBehaviour
|
||||
public NetworkVariable<int> currentHealth = new NetworkVariable<int>(
|
||||
100,
|
||||
NetworkVariableReadPermission.Everyone,
|
||||
NetworkVariableWritePermission.Owner
|
||||
NetworkVariableWritePermission.Server
|
||||
);
|
||||
|
||||
public NetworkVariable<bool> isDead = new NetworkVariable<bool>(false);
|
||||
|
||||
public override void OnNetworkSpawn()
|
||||
{
|
||||
currentHealth.OnValueChanged += OnHealthChanged;
|
||||
@@ -20,8 +22,10 @@ public class PlayerHealth : NetworkBehaviour
|
||||
if (IsServer)
|
||||
{
|
||||
currentHealth.Value = maxHealth;
|
||||
healthText.text = $"Health: {maxHealth}";
|
||||
RoundManager.Current?.RegisterPlayer(OwnerClientId);
|
||||
}
|
||||
|
||||
healthText.text = $"Health: {maxHealth}";
|
||||
}
|
||||
|
||||
private void OnHealthChanged(int _previousValue, int _newValue)
|
||||
@@ -34,8 +38,11 @@ public class PlayerHealth : NetworkBehaviour
|
||||
public void TakeDamage(int _damageAmount)
|
||||
{
|
||||
if (!IsServer) return;
|
||||
if(isDead.Value) return;
|
||||
|
||||
currentHealth.Value -= _damageAmount;
|
||||
if (RoundManager.Current == null || !RoundManager.Current.IsRoundActive) return;
|
||||
|
||||
currentHealth.Value = Mathf.Max(0, currentHealth.Value - _damageAmount);
|
||||
|
||||
if (currentHealth.Value <= 0)
|
||||
{
|
||||
@@ -45,18 +52,50 @@ public class PlayerHealth : NetworkBehaviour
|
||||
|
||||
private void Die()
|
||||
{
|
||||
if(!IsServer) return;
|
||||
|
||||
isDead.Value = true;
|
||||
OnDiedClientRpc();
|
||||
|
||||
// RoundManager.Current?.OnPlayerDied(this);
|
||||
}
|
||||
|
||||
[ClientRpc]
|
||||
private void OnDiedClientRpc()
|
||||
{
|
||||
Debug.Log($"Player {OwnerClientId} died");
|
||||
gameObject.SetActive(false);
|
||||
SetPhysicsAndVisualsActive(false);
|
||||
}
|
||||
|
||||
public override void OnNetworkDespawn()
|
||||
{
|
||||
currentHealth.OnValueChanged -= OnHealthChanged;
|
||||
}
|
||||
|
||||
public void Respawn(Vector3 _spawnPosition)
|
||||
{
|
||||
if(!IsServer) return;
|
||||
|
||||
isDead.Value = false;
|
||||
currentHealth.Value = maxHealth;
|
||||
RespawnClientRpc(_spawnPosition);
|
||||
}
|
||||
|
||||
[ClientRpc]
|
||||
private void RespawnClientRpc(Vector3 _spawnPosition)
|
||||
{
|
||||
SetPhysicsAndVisualsActive(true);
|
||||
transform.position = _spawnPosition;
|
||||
}
|
||||
|
||||
private void SetPhysicsAndVisualsActive(bool _isActive)
|
||||
{
|
||||
foreach (Collider collider in GetComponentsInChildren<Collider>())
|
||||
collider.enabled = _isActive;
|
||||
|
||||
foreach (MeshRenderer meshRenderer in GetComponentsInChildren<MeshRenderer>())
|
||||
meshRenderer.enabled = _isActive;
|
||||
|
||||
GetComponent<Rigidbody>().isKinematic = !_isActive;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user