Spaces:
Sleeping
Sleeping
Download project/Assets/Scripts/Game/DinoScoreboardUI.cs from alfarouqwebserver/SWS-Smarta: direct link, hf CLI and curl.
- Browser
- Download file 2.47 kB
-
https://huggingface.co/spaces/alfarouqwebserver/SWS-Smarta/resolve/main/project/Assets/Scripts/Game/DinoScoreboardUI.cs
- Command line
-
hf download hf://spaces/alfarouqwebserver/SWS-Smarta/project/Assets/Scripts/Game/DinoScoreboardUI.cs
-
curl -L -o DinoScoreboardUI.cs https://huggingface.co/spaces/alfarouqwebserver/SWS-Smarta/resolve/main/project/Assets/Scripts/Game/DinoScoreboardUI.cs
2.47 kB
| using System.Text; | |
| using TMPro; | |
| using UnityEngine; | |
| using UnityEngine.UI; | |
| public class DinoScoreboardUI : MonoBehaviour | |
| { | |
| public TMP_Text boardText; | |
| public Text boardTextLegacy; | |
| public float updateInterval = 0.3f; | |
| float nextUpdate; | |
| void Update() | |
| { | |
| if (Time.time < nextUpdate) | |
| return; | |
| nextUpdate = Time.time + updateInterval; | |
| DinoPlayer[] players = FindObjectsOfType<DinoPlayer>(); | |
| if (players == null || players.Length == 0) | |
| { | |
| SetText("Waiting for players..."); | |
| return; | |
| } | |
| DinoGameManager gm = DinoGameManager.Instance; | |
| if (gm != null && gm.IsMatchOver) | |
| { | |
| System.Array.Sort(players, (a, b) => GetWins(gm, b).CompareTo(GetWins(gm, a))); | |
| } | |
| else | |
| { | |
| System.Array.Sort(players, (a, b) => b.transform.position.z.CompareTo(a.transform.position.z)); | |
| } | |
| StringBuilder sb = new StringBuilder(); | |
| for (int i = 0; i < players.Length; i++) | |
| { | |
| DinoPlayer p = players[i]; | |
| string name = GetDisplayName(p); | |
| string status = p.IsAlive ? "Alive" : "Out"; | |
| string item = ItemName((ItemType)p.HeldItem); | |
| int wins = GetWins(gm, p); | |
| sb.AppendLine($"{name} | {status} | {item} | Wins:{wins}"); | |
| } | |
| SetText(sb.ToString().TrimEnd()); | |
| } | |
| string GetDisplayName(DinoPlayer p) | |
| { | |
| string n = p.PlayerName.ToString(); | |
| if (!string.IsNullOrWhiteSpace(n)) | |
| return n; | |
| if (p.Object != null) | |
| return $"P{p.Object.InputAuthority.PlayerId}"; | |
| return "Player"; | |
| } | |
| string ItemName(ItemType t) | |
| { | |
| switch (t) | |
| { | |
| case ItemType.Shield: return "Shield"; | |
| case ItemType.Speed: return "Speed"; | |
| case ItemType.Trap: return "Trap"; | |
| case ItemType.Projectile: return "Projectile"; | |
| default: return "-"; | |
| } | |
| } | |
| void SetText(string value) | |
| { | |
| if (boardText != null) boardText.text = value; | |
| if (boardTextLegacy != null) boardTextLegacy.text = value; | |
| } | |
| int GetWins(DinoGameManager gm, DinoPlayer p) | |
| { | |
| if (gm == null || p.Object == null) | |
| return 0; | |
| int id = p.Object.InputAuthority.PlayerId; | |
| if (id < 0 || id >= gm.WinCounts.Length) | |
| return 0; | |
| return gm.WinCounts.Get(id); | |
| } | |
| } | |