Skip to content

Commit

Permalink
feat(UI): adding crosshair and life ✨
Browse files Browse the repository at this point in the history
  • Loading branch information
MisterAzix committed Jan 20, 2023
1 parent 8b6764b commit be3ae2f
Show file tree
Hide file tree
Showing 6 changed files with 486 additions and 15 deletions.
2 changes: 2 additions & 0 deletions Assets/Code/Scripts/NetworkManagerUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class NetworkManagerUI : NetworkBehaviour
[SerializeField] private Button hostBtn;
[SerializeField] private Button clientBtn;
[SerializeField] private TMP_InputField serverInput;
[SerializeField] private GameObject gameUI;
// [SerializeField] private TMP_InputField playerNameInputField;
// [SerializeField] private GameObject mainMenuUI;

Expand Down Expand Up @@ -53,6 +54,7 @@ private void Client()
private void HandleClientConnected(ulong clientId)
{
gameObject.SetActive(false);
gameUI.SetActive(true);
}

// private void AssignPlayerName()
Expand Down
29 changes: 17 additions & 12 deletions Assets/Code/Scripts/PlayerStats.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,26 @@ public class PlayerStats : NetworkBehaviour
[SerializeField] public NetworkVariable<FixedString64Bytes> networkPlayerName = new NetworkVariable<FixedString64Bytes>("");
[SerializeField] private TMP_Text playerName;


public override void OnNetworkSpawn()
{
base.OnNetworkSpawn();
/* if (IsOwner)
{
AssignPlayerNameServerRpc();
StartCoroutine(DestroyMenu());
} */
playerHealth.OnValueChanged += HealthOnValueChanged;
// networkPlayerName.OnValueChanged += HandlePlayerNameChanged;
}

private void HealthOnValueChanged(int prevHealth, int nextHealth)
{
Debug.Log($"[{DateTime.Now.ToString("HH:mm:ss\\Z")}] {OwnerClientId}: now have {nextHealth}hp!");
//Debug.Log(OwnerClientId + "-> prevHealth : " + prevHealth + "newtHealth : " + nextHealth + "ClientId");
if (nextHealth <= 0)
{
KillPlayer();
}
}

private IEnumerator DestroyMenu()
{
yield return new WaitForSeconds(.3f);
GameObject.Find("NetworkManagerUI").SetActive(false);

}

private void KillPlayer()
{
Debug.Log($"[{DateTime.Now.ToString("HH:mm:ss\\Z")}] {OwnerClientId}: Died!");
Expand All @@ -64,13 +54,28 @@ private IEnumerator DeathCoroutine()
transform.GetChild(0).GetComponent<PlayerCameraMovement>().enabled = true;
transform.GetComponent<ShootScript>().enabled = true;

ResetHealthServerRpc();
ResetHealthServerRpc(OwnerClientId);
}

[ServerRpc(RequireOwnership = false)]
private void ResetHealthServerRpc()
private void ResetHealthServerRpc(ulong clientId)
{
playerHealth.Value = maxPlayerHealth;

NotifyHealthChangedClientRpc(maxPlayerHealth, new ClientRpcParams
{
Send = new ClientRpcSendParams
{
TargetClientIds = new ulong[] { clientId }
}
});
}


[ClientRpc]
public void NotifyHealthChangedClientRpc(int playerHealth, ClientRpcParams clientRpcParams = default)
{
GameObject.Find("Life").GetComponent<TMP_Text>().text = playerHealth >= 0 ? playerHealth.ToString() : "0";
}

/* [ServerRpc(RequireOwnership = false)]
Expand Down
5 changes: 4 additions & 1 deletion Assets/Code/Scripts/ShootScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ private void ShootServerRpc()
GameObject projectile = Instantiate(bullet, firePosition.position, cam.rotation);
NetworkObject projectileNetworkObject = projectile.GetComponent<NetworkObject>();
projectileNetworkObject.Spawn(true);
projectileNetworkObject.ChangeOwnership(OwnerClientId);
if (projectileNetworkObject.OwnerClientId != OwnerClientId)
{
projectileNetworkObject.ChangeOwnership(OwnerClientId);
}
}
}
6 changes: 4 additions & 2 deletions Assets/Code/Scripts/bulletScript.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.Collections;
using System.Collections.Generic;
using TMPro;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.UI;

public class bulletScript : NetworkBehaviour
{
Expand Down Expand Up @@ -100,9 +102,9 @@ public void UpdatePlayerHealthServerRpc(int damage, ulong clientId)
}

[ClientRpc]
public void NotifyHealthChangedClientRpc( int playerHealth, ClientRpcParams clientRpcParams = default)
public void NotifyHealthChangedClientRpc(int playerHealth, ClientRpcParams clientRpcParams = default)
{

GameObject.Find("Life").GetComponent<TMP_Text>().text = playerHealth >= 0 ? playerHealth.ToString() : "0";
}

[ServerRpc(RequireOwnership = false)]
Expand Down
1 change: 1 addition & 0 deletions Assets/Level/Prefabs/Player.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,7 @@ MonoBehaviour:
byte0060: 0
byte0061: 0
playerName: {fileID: 2677922592013009780}
healthText: {fileID: 0}
--- !u!1 &8522208699560867738
GameObject:
m_ObjectHideFlags: 0
Expand Down
Loading

0 comments on commit be3ae2f

Please sign in to comment.