Added PlayerMovement and other
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
using UnityEngine;
|
||||
using Unity.Netcode;
|
||||
using TMPro;
|
||||
using System;
|
||||
|
||||
public class PlayerHealth : NetworkBehaviour
|
||||
{
|
||||
[SerializeField] private int maxHealth = 100;
|
||||
[SerializeField] private TMP_Text healthText;
|
||||
|
||||
public NetworkVariable<int> currentHealth = new NetworkVariable<int>(
|
||||
100,
|
||||
@@ -15,56 +15,41 @@ public class PlayerHealth : NetworkBehaviour
|
||||
|
||||
public NetworkVariable<bool> isDead = new NetworkVariable<bool>(false);
|
||||
|
||||
public override void OnNetworkSpawn()
|
||||
public event Action<int, int> HealthChanged;
|
||||
public event Action Died;
|
||||
|
||||
private bool isServer;
|
||||
|
||||
public void Init(bool _isServer)
|
||||
{
|
||||
isServer = _isServer;
|
||||
|
||||
currentHealth.OnValueChanged += OnHealthChanged;
|
||||
|
||||
if (IsServer)
|
||||
if(isServer)
|
||||
{
|
||||
currentHealth.Value = maxHealth;
|
||||
RoundManager.Current?.RegisterPlayer(OwnerClientId);
|
||||
}
|
||||
|
||||
healthText.text = $"Health: {maxHealth}";
|
||||
|
||||
HealthChanged?.Invoke(currentHealth.Value, maxHealth);
|
||||
}
|
||||
|
||||
private void OnHealthChanged(int _previousValue, int _newValue)
|
||||
{
|
||||
Debug.Log($"Health changed from {_previousValue} to {_newValue}");
|
||||
HealthChanged?.Invoke(_newValue, maxHealth);
|
||||
|
||||
healthText.text = $"Health: {_newValue}";
|
||||
}
|
||||
|
||||
public void TakeDamage(int _damageAmount)
|
||||
{
|
||||
if (!IsServer) return;
|
||||
if(isDead.Value) return;
|
||||
|
||||
if (RoundManager.Current == null || !RoundManager.Current.IsRoundActive) return;
|
||||
|
||||
currentHealth.Value = Mathf.Max(0, currentHealth.Value - _damageAmount);
|
||||
|
||||
if (currentHealth.Value <= 0)
|
||||
if(_newValue <= 0)
|
||||
{
|
||||
Die();
|
||||
}
|
||||
}
|
||||
|
||||
private void Die()
|
||||
public void TakeDamage(int _damageAmount)
|
||||
{
|
||||
if(!IsServer) return;
|
||||
|
||||
isDead.Value = true;
|
||||
OnDiedClientRpc();
|
||||
if (!isServer) return;
|
||||
if(isDead.Value) return;
|
||||
|
||||
// RoundManager.Current?.OnPlayerDied(this);
|
||||
}
|
||||
|
||||
[ClientRpc]
|
||||
private void OnDiedClientRpc()
|
||||
{
|
||||
Debug.Log($"Player {OwnerClientId} died");
|
||||
SetPhysicsAndVisualsActive(false);
|
||||
currentHealth.Value = Mathf.Max(0, currentHealth.Value - _damageAmount);
|
||||
}
|
||||
|
||||
public override void OnNetworkDespawn()
|
||||
@@ -72,15 +57,36 @@ public class PlayerHealth : NetworkBehaviour
|
||||
currentHealth.OnValueChanged -= OnHealthChanged;
|
||||
}
|
||||
|
||||
private void Die()
|
||||
{
|
||||
if(!isServer) return;
|
||||
|
||||
isDead.Value = true;
|
||||
OnDiedClientRpc();
|
||||
}
|
||||
|
||||
public void Respawn(Vector3 _spawnPosition)
|
||||
{
|
||||
if(!IsServer) return;
|
||||
if(!isServer) return;
|
||||
|
||||
isDead.Value = false;
|
||||
currentHealth.Value = maxHealth;
|
||||
RespawnClientRpc(_spawnPosition);
|
||||
}
|
||||
|
||||
public void PushCurrentHealthValue()
|
||||
{
|
||||
HealthChanged?.Invoke(currentHealth.Value, maxHealth);
|
||||
}
|
||||
|
||||
[ClientRpc]
|
||||
private void OnDiedClientRpc()
|
||||
{
|
||||
Debug.Log($"Player {OwnerClientId} died");
|
||||
SetPhysicsAndVisualsActive(false);
|
||||
Died?.Invoke();
|
||||
}
|
||||
|
||||
[ClientRpc]
|
||||
private void RespawnClientRpc(Vector3 _spawnPosition)
|
||||
{
|
||||
@@ -96,6 +102,15 @@ public class PlayerHealth : NetworkBehaviour
|
||||
foreach (MeshRenderer meshRenderer in GetComponentsInChildren<MeshRenderer>())
|
||||
meshRenderer.enabled = _isActive;
|
||||
|
||||
GetComponent<Rigidbody>().isKinematic = !_isActive;
|
||||
Rigidbody rigidbody = GetComponent<Rigidbody>();
|
||||
if (rigidbody == null) return;
|
||||
|
||||
rigidbody.isKinematic = !_isActive;
|
||||
|
||||
if (_isActive)
|
||||
{
|
||||
rigidbody.linearVelocity = Vector3.zero;
|
||||
rigidbody.angularVelocity = Vector3.zero;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user