You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

356 lines
9.8 KiB

using Photon.Pun;
using Photon.Realtime;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class GameGUI : MonoBehaviourPunCallbacks {
public static GameGUI Instance {
get; set;
}
private Camera cam;
public Animator animator;
void Awake() {
Instance = this;
cam = Camera.main;
}
public void StartGUI() {
List<CircuitAmount> circuitAmounts;
if (!PhotonNetwork.OfflineMode)
circuitAmounts = GameManager.Instance.circuitAmounts;
else
circuitAmounts = new List<CircuitAmount>() {
new CircuitAmount(CircuitType.Basic, 10),
new CircuitAmount(CircuitType.Capacitor, 10),
new CircuitAmount(CircuitType.Generator, 10),
new CircuitAmount(CircuitType.Speeder, 10)
};
int actorNumber = PhotonNetwork.LocalPlayer.ActorNumber;
Material material = ColorManager.GetUiBy(actorNumber);
foreach (UICircuitCount uICircuitCount in uICircuitCounts) {
uICircuitCount.SetPlayerMaterial(material);
uICircuitCount.Count = circuitAmounts.Find(c => c.type.Equals(uICircuitCount.type)).amount;
}
arrowImage.material = material;
animator.SetTrigger("Toggle Stock");
}
public void OnUICircuitBeginDrag(GameUICircuit uICircuit) {
UICircuitCount uICircuitCount = uICircuitCounts.Find(u => u.type.Equals(uICircuit.type));
if (uICircuitCount.Count > 0) {
Vector3 pos = uICircuit.transform.position;
CircuitType type = uICircuit.type;
Game.Instance.SpawnDraggableCircuit(pos, type, uICircuitCount);
}
}
void Update() {
UpdateMenuStats();
UpdateChargeComparison();
UpdateWinnerBanner();
}
//Playing game time
[Header("Play time")]
public Text timeText;
public void SetTimeText(float time) {
int seconds = (int)time % 60;
int minutes = (int)time / 60;
string text = minutes.ToString("00") + ":" + seconds.ToString("00");
timeText.text = text;
}
//Charge Comparison
[Header("Charge Comparison")]
public RectTransform[] comparisonCharges;
public void UpdateChargeComparison() {
float[] charges = {0, 0, 0, 0, 0, 0, 0, 0, 0};
foreach (Circuit circuit in Game.Instance.Circuits) {
if (ColorManager.Instance.TryGetColor(circuit.ActorNumber, out int index))
charges[index] += circuit.Charge;
}
float allCharge = 0;
foreach (float charge in charges)
allCharge += charge;
if (allCharge == 0)
allCharge = 1;
float prePart = 0;
for (int i = 0; i < charges.Length; i++) {
float charge = charges[i];
float part = charge / allCharge + prePart;
RectTransform rect = comparisonCharges[i];
rect.anchorMin = new Vector2(prePart, rect.anchorMin.y);
rect.anchorMax = new Vector2(part, rect.anchorMax.y);
prePart = part;
}
}
//Circuit Stock Menu
[Header("Circuit Stock Menu")]
public RectTransform stockPanel;
public Image arrowImage;
public List<GameUICircuit> uICircuits;
public List<UICircuitCount> uICircuitCounts;
//Winner banner
[Header("Winner Banner")]
public Transform winnerBannerPanel;
public Image winnerBannerImage;
public float bannerAlpha;
[Tooltip("Fade duration in seconds")]
public float alphaFadeTime;
public GameObject victoryHeader;
public GameObject defeatHeader;
public Button returnToMainMenuButton;
private bool winnerBannerFading;
public void ShowWinnerBannerPanel(int winnerActorNumber) {
winnerBannerImage.material = ColorManager.GetUiBy(winnerActorNumber);
winnerBannerImage.canvasRenderer.SetAlpha(0.01f);
winnerBannerFading = true;
bool localWin = winnerActorNumber == PhotonNetwork.LocalPlayer.ActorNumber;
victoryHeader.SetActive(localWin);
defeatHeader.SetActive(!localWin);
winnerBannerPanel.gameObject.SetActive(true);
}
private void UpdateWinnerBanner() {
if (winnerBannerFading)
winnerBannerImage.CrossFadeAlpha(bannerAlpha, alphaFadeTime, false);
}
public void ReturnToMainMenu() {
returnToMainMenuButton.interactable = false;
PhotonNetwork.Disconnect();
}
public override void OnDisconnected(DisconnectCause cause) {
Debug.Log("You left the game");
SceneManager.LoadScene(0);
}
//Circuit Information Menu
[Header("Circuit Information Menu")]
public float menuDistanceToCircuit;
[Space(5)]
public Text menuHeaderText;
public Transform connectionBorders;
public Transform connectionCenters;
public GameObject connectionBorderPrefab;
public GameObject connectionCenterPrefab;
public GameObject circuitInfoPanel;
[Space(5)]
public GameObject capacityRow;
public GameObject chargeRateRow;
public GameObject sendRateRow;
public GameObject sendSpeedRow;
public GameObject rangeRow;
public GameObject connectionsRow;
public Color inactiveLevelColor;
[Space(5)]
public Text upgradeCostText;
public Text levelNowText;
public Text levelNextText;
public RectTransform progressBar;
public RectTransform emptyBar;
[Space(5)]
public Button destroyButton;
public Image destroyImage;
public GameObject destroyConfirmPanel;
[HideInInspector]
public Circuit currentMenuCircuit;
public void OpenCircuitMenu(Circuit circuit) {
currentMenuCircuit = circuit;
UpdateMenuStats();
//ApplyMenuPosition();
circuitInfoPanel.SetActive(true);
}
public void CloseCircuitMenu(Circuit circuit) {
if (currentMenuCircuit == circuit) {
CloseCircuitMenu();
}
}
public void CloseCircuitMenu() {
circuitInfoPanel.SetActive(false);
destroyConfirmPanel.SetActive(false);
currentMenuCircuit = null;
}
public void UpdateMenuStats() {
Circuit circuit = currentMenuCircuit;
if (circuit == null)
return;
int actorNumber = circuit.ActorNumber;
CircuitType type = circuit.type;
List<Connection> connections = circuit.GoingOutConnections;
int level = circuit.level;
CircuitLevels levels = CircuitManager.Instance.GetLevelsByType(type);
float upgradeCharge = circuit.UpgradeCharge;
float upgradeCost = levels.GetValuesByLevel(level).upgradeCost;
int maxConnections = levels.GetValuesByLevel(level).connections;
//circuitInfoPanel.GetComponent<Image>().material = circuitMaterials[actorNumber];
string header = type.ToString("g") + " Level " + level;
menuHeaderText.text = header;
//Clear old connection visuals
for (int i = 0; i < connectionBorders.childCount; i++)
Destroy(connectionBorders.GetChild(i).gameObject);
for (int i = 0; i < connectionCenters.childCount; i++)
Destroy(connectionCenters.GetChild(i).gameObject);
for (int i = 0; i < maxConnections; i++) {
Instantiate(connectionBorderPrefab, connectionBorders);
GameObject center = Instantiate(connectionCenterPrefab, connectionCenters);
int connectionActorNumber;
if (i >= connections.Count)
connectionActorNumber = 0;
else
connectionActorNumber = circuit.ActorNumber;
center.GetComponent<Image>().material = ColorManager.GetUiBy(connectionActorNumber);
}
ApplyStat(capacityRow, ValueType.Capacity);
ApplyStat(chargeRateRow, ValueType.ChargeRate);
ApplyStat(sendRateRow, ValueType.SendRate);
ApplyStat(sendSpeedRow, ValueType.SendSpeed);
ApplyStat(rangeRow, ValueType.Range);
ApplyStat(connectionsRow, ValueType.Connections);
void ApplyStat(GameObject row, ValueType valueType) {
float[] valueLevels = levels.GetValueLevelsByValueType(valueType);
List<string> parts = new List<string> {
valueLevels[0].ToString(),
valueLevels[1].ToString(),
valueLevels[2].ToString()
};
string color = ColorUtility.ToHtmlStringRGBA(inactiveLevelColor);
string colorTag = "<color=#" + color + ">";
for (int i = 0; i < 3; i++) {
if (i != level - 1) {
string colored = parts[i];
parts[i] = colorTag + colored + "</color>";
}
}
parts.Insert(1, colorTag + " - </color>");
parts.Insert(3, colorTag + " - </color>");
string text = "";
foreach (string part in parts)
text += part;
row.transform.Find("Value").GetComponent<Text>().text = text;
}
string next = level == 3 ? "" : (level + 1).ToString();
upgradeCostText.text = upgradeCost.ToString();
levelNowText.text = level.ToString();
levelNextText.text = next;
float width = upgradeCharge / upgradeCost * emptyBar.rect.width;
progressBar.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, 0, width);
progressBar.GetComponent<Image>().material = ColorManager.GetUiBy(actorNumber);
bool canDestroy = actorNumber == PhotonNetwork.LocalPlayer.ActorNumber && !circuit.IsAttacked;
if (!canDestroy)
OnCannotDestroy();
destroyButton.interactable = canDestroy;
destroyImage.material = ColorManager.GetUiBy(actorNumber);
}
public void OnDestroyCircuitConfirmButtonClicked() {
if (currentMenuCircuit == null)
return;
destroyConfirmPanel.SetActive(false);
uICircuitCounts.Find(c => c.type.Equals(currentMenuCircuit.type)).Count++;
Game.Instance.DestroyCircuit(currentMenuCircuit);
CloseCircuitMenu(currentMenuCircuit);
}
public void OnCancelDestroyCircuitButtonClicked() {
destroyConfirmPanel.SetActive(false);
}
public void OnCannotDestroy() {
destroyConfirmPanel.SetActive(false);
destroyButton.interactable = false;
}
public void ApplyMenuPosition() {
Circuit circuit = currentMenuCircuit;
if (circuit == null)
return;
float scaleFactor = transform.Find("Canvas").GetComponent<Canvas>().scaleFactor;
RectTransform rTransform = (RectTransform)circuitInfoPanel.transform;
Vector3 dir = Vector3.forward;
Vector3 greaterDir = (circuit.GetBaseBounds().extents.magnitude + menuDistanceToCircuit) * dir;
Vector3 worldPos = circuit.transform.position + greaterDir;
Vector3 screenPos = cam.WorldToScreenPoint(worldPos);
screenPos.x += rTransform.rect.width / 2;
float m = 50 * scaleFactor;
float w = rTransform.rect.width;
float h = rTransform.rect.height;
float x = screenPos.x;
float y = screenPos.y;
if (x > Screen.width - w / 2 - m)
x = Screen.width - w / 2 - m;
if (y > Screen.height - h / 2 - m)
y = Screen.height - h / 2 - m;
if (x < w / 2 + m)
x = w / 2 + m;
if (y < h / 2 + m)
y = h / 2 + m;
screenPos.x = x;
screenPos.y = y;
rTransform.position = screenPos;
}
}