quakeball/Assets/Scripts/Interface/StatScreen.cs

133 lines
4.3 KiB
C#
Raw Permalink Normal View History

2020-08-08 21:18:17 +02:00
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.InputSystem;
using TMPro;
using NeonTea.Quakeball.Networking;
2020-08-10 02:08:09 +02:00
using NeonTea.Quakeball.Game;
2020-08-08 21:18:17 +02:00
using System.Collections.Generic;
namespace NeonTea.Quakeball.Interface {
public class StatScreen : MonoBehaviour {
2020-08-10 17:22:01 +02:00
[Header("General")]
2020-08-10 02:08:09 +02:00
public Color RowColor = new Color();
2020-08-08 21:18:17 +02:00
public RectTransform BackroundPanel;
public GameObject RowPrefab;
2020-08-10 17:22:01 +02:00
public RectTransform Header;
public RectTransform DescriptorRow;
2020-08-08 21:18:17 +02:00
public bool ForceOpen;
2020-08-10 17:22:01 +02:00
[Header("Player Team name colors")]
2020-08-10 02:08:09 +02:00
public Color SunColor = new Color();
public Color MoonColor = new Color();
2020-08-08 21:18:17 +02:00
private bool Open;
2020-08-10 17:22:01 +02:00
private float HeaderOffset;
2020-08-08 21:18:17 +02:00
private InputAction OpenStatScreenAction;
private List<RectTransform> Rows = new List<RectTransform>();
private bool LastRowIsDark = true;
private Color DarkColor;
private int LastRowAmount = 0;
private float LastUpdate;
public void Start() {
OpenStatScreenAction = new InputAction("Open stats", binding: "<Keyboard>/tab");
OpenStatScreenAction.Enable();
DarkColor = new Color(0, 0, 0, 0);
DarkColor.a = RowColor.a;
BackroundPanel.gameObject.SetActive(false);
2020-08-10 17:22:01 +02:00
HeaderOffset = Header.rect.height + DescriptorRow.rect.height;
2020-08-08 21:18:17 +02:00
}
public void Update() {
if (OpenStatScreenAction.ReadValue<float>() > 0 || ForceOpen) {
Open = true;
} else {
Open = false;
}
BackroundPanel.gameObject.SetActive(Open);
if (Open && (Time.time - LastUpdate) > 0.5f && Net.Singleton.Instance != null) {
UpdateRows();
2020-08-09 19:33:23 +02:00
LastUpdate = Time.time;
2020-08-08 21:18:17 +02:00
}
}
public void UpdateRows() {
int rowAmount = Net.Singleton.Instance.PlayerList.Count;
if (rowAmount != LastRowAmount) {
UpdateRowAmount();
LastRowAmount = rowAmount;
}
for (int i = 0; i < rowAmount; i++) {
NetPlayer player = Net.Singleton.Instance.PlayerList[i];
2020-08-10 02:08:09 +02:00
Color playerColor = new Color(1, 1, 1, 1);
switch (player.Team) {
case Team.Sun:
playerColor = SunColor;
break;
case Team.Moon:
playerColor = MoonColor;
break;
}
TMP_Text NickText = Rows[i].GetChild(0).GetComponent<TMP_Text>();
NickText.color = playerColor;
NickText.text = player.Nick;
2020-08-08 21:18:17 +02:00
int ping = (int)(player.Ping * 1000);
Rows[i].GetChild(1).GetComponent<TMP_Text>().text = $"{ping} ms";
}
2020-08-10 02:08:09 +02:00
2020-08-08 21:18:17 +02:00
}
public void UpdateRowAmount() {
int rowAmount = Net.Singleton.Instance.PlayerList.Count;
while (Rows.Count < rowAmount) {
AddRow();
}
while (Rows.Count > rowAmount) {
RemoveRow();
}
}
public void AddRow() {
GameObject rowobj = GameObject.Instantiate(RowPrefab);
2020-08-10 02:50:25 +02:00
rowobj.transform.SetParent(BackroundPanel, false);
2020-08-08 21:18:17 +02:00
RectTransform row = rowobj.GetComponent<RectTransform>();
Rows.Add(row);
Vector2 offsetMin = row.offsetMin;
offsetMin.x = 0;
row.offsetMin = offsetMin;
Vector2 offsetMax = row.offsetMax;
offsetMax.x = 0;
row.offsetMax = offsetMax;
Vector2 pos = row.anchoredPosition;
2020-08-10 17:22:01 +02:00
pos.y = 20 - (row.rect.height * Rows.Count) - HeaderOffset;
2020-08-08 21:18:17 +02:00
row.anchoredPosition = pos;
Image rowImage = row.GetComponent<Image>();
if (LastRowIsDark) {
rowImage.color = RowColor;
} else {
rowImage.color = DarkColor;
}
LastRowIsDark = !LastRowIsDark;
}
public void RemoveRow() {
if (Rows.Count > 0) {
GameObject.Destroy(Rows[Rows.Count - 1].gameObject);
Rows.RemoveAt(Rows.Count - 1);
LastRowIsDark = !LastRowIsDark;
}
}
}
}