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.

130 lines
3.9 KiB

using ExitGames.Client.Photon;
using Photon.Pun;
using Photon.Realtime;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SelectionGUI : MonoBehaviourPunCallbacks {
public static SelectionGUI Instance {
get; set;
}
public List<Stat> stats;
public List<SelectionUICircuit> uICircuits;
public List<UICircuitCount> uICircuitCounts;
public List<ReadyPlayer> readyPlayers;
public Text countdownText;
public Text pointsText;
public Image readyCheckmark;
public Image readyImage;
public Button readyButton;
public RectTransform stockHolder;
void Awake() {
Instance = this;
}
void Start() {
List<CircuitCost> circuitCosts = Selection.Instance.circuitCosts;
foreach (SelectionUICircuit uICircuit in uICircuits) {
int cost = circuitCosts.Find(c => c.type.Equals(uICircuit.type)).cost;
uICircuit.SetCost(cost);
}
foreach (Player player in PhotonNetwork.CurrentRoom.Players.Values) {
if (player.Equals(PhotonNetwork.LocalPlayer))
continue;
ReadyPlayer readyPlayer = readyPlayers.Find(p => !p.gameObject.activeSelf);
readyPlayer.Player = player;
readyPlayer.gameObject.SetActive(true);
}
pointsText.text = Selection.Instance.CurrentPoints.ToString();
readyImage.material = ColorManager.GetUiBy(PhotonNetwork.LocalPlayer.ActorNumber);
}
void Update() {
countdownText.text = Selection.Instance.selectionTime.ToString("0");
}
public void OnUICircuitPointerEnter(SelectionUICircuit uICircuit) {
foreach (Stat stat in stats)
stat.SetByType(uICircuit.type);
}
public void OnUICircuitPointerExit(SelectionUICircuit uICircuit) {
}
public void OnUICircuitPointerClick(SelectionUICircuit uICircuit) {
AddCircuitToSelection(uICircuit.type);
}
private RectTransform draggedCircuit;
private CircuitType draggedType;
public void OnUICircuitBeginDrag(SelectionUICircuit uICircuit) {
bool enoughPoints = Selection.Instance.CanAdd(uICircuit.type);
if (enoughPoints){
draggedCircuit = Instantiate((RectTransform)uICircuit.transform.Find("Image"), GameObject.Find("Canvas").transform);
draggedType = uICircuit.type;
}
}
public void OnUICircuitDrag(SelectionUICircuit uICircuit) {
if (draggedCircuit)
draggedCircuit.position = Input.mousePosition;
}
public void OnUICircuitEndDrag(SelectionUICircuit uICircuit) {
if (draggedCircuit) {
if (RectTransformUtility.RectangleContainsScreenPoint(stockHolder, Input.mousePosition)) {
AddCircuitToSelection(draggedType);
}
Destroy(draggedCircuit.gameObject);
draggedCircuit = null;
}
}
public void AddCircuitToSelection(CircuitType type) {
if (Selection.Instance.AddCircuitToSelection(type, out int newCount)) {
uICircuitCounts.Find(c => c.type.Equals(type)).Count = newCount;
pointsText.text = Selection.Instance.CurrentPoints.ToString();
}
}
public void RemoveCircuitFromSelection(UICircuitCount uICircuitCount) {
CircuitType type = uICircuitCount.type;
if (Selection.Instance.RemoveCircuitFromSelection(type, out int newCount)) {
uICircuitCount.Count = newCount;
pointsText.text = Selection.Instance.CurrentPoints.ToString();
}
}
public override void OnPlayerPropertiesUpdate(Player target, Hashtable changedProps) {
if (changedProps.ContainsKey("IsReady")) {
ReadyPlayer readyPlayer = readyPlayers.FindAll(p => p.gameObject.activeSelf).Find(p => p.Player.Equals(target));
bool ready = (bool)changedProps["IsReady"];
if (readyPlayer) {
readyPlayer.SetReady(ready);
}
if (target.Equals(PhotonNetwork.LocalPlayer) && ready) {
readyCheckmark.material = ColorManager.GetUiBy(PhotonNetwork.LocalPlayer.ActorNumber);
}
bool allReady = true;
foreach (Player player in PhotonNetwork.CurrentRoom.Players.Values) {
if (!(bool)player.CustomProperties["IsReady"])
allReady = false;
}
if (allReady)
Selection.Instance.SelectionDone();
}
}
public void OnReadyButtonClicked() {
PhotonNetwork.LocalPlayer.SetCustomProperties(new Hashtable() { ["IsReady"] = true });
}
}