Pedro Gomes 6e232f4276 Update voting & job selection systems
- voting now fully cancels and clears when atleast one player cancels or closes the voting UI
- when selecting a job and voting, the zone is stored on all players, allowing to load different zones / jobs
2024-07-24 22:36:36 +01:00

108 lines
3.5 KiB
C#

using ExitGames.Client.Photon;
using Photon.Pun;
using Photon.Realtime;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class JobInfoPanel : MonoBehaviour, IOnEventCallback
{
[Header("Components:")]
[SerializeField] private TMP_Text title;
[SerializeField] private TMP_Text description;
[SerializeField] private TMP_Text coinReward;
[SerializeField] private TMP_Text experienceReward;
[SerializeField] private TMP_Text reputationReward;
[SerializeField] private GameObject infoPanel;
[SerializeField] private Button closeButton;
[Header("Events:")]
[SerializeField] private GameEvent onInfoPanelClosed;
[Header("Listeners:")]
[SerializeField] private GameEventListener_JobInstance onJobSelected;
[SerializeField] private GameEventListener onJobsBoardReleased;
[SerializeField] private GameEventListener_Player onPlayerVoted;
[SerializeField] private GameEventListener onGameOptionsOpenned;
JobInstance networkJobInstance;
private void Awake()
{
onJobSelected.Response.AddListener((job) => SetupJobInfoPanel(job, true));
onJobSelected.Response.AddListener(Send_Others_OnJobSelected);
onJobsBoardReleased.Response.AddListener(() => ToggleInfoPanel(false));
onGameOptionsOpenned.Response.AddListener(() => ToggleInfoPanel(false));
onPlayerVoted.Response.AddListener((player) =>
{
if (player != PhotonNetwork.LocalPlayer)
ToggleInfoPanel(true);
});
closeButton.onClick.AddListener(() => ToggleInfoPanel(false));
}
private void SetupJobInfoPanel(JobInstance jobData, bool showPanel)
{
if (jobData.showJobTitlePrefix)
title.text = $"Job Title: {jobData.title}";
else
title.text = $"{jobData.title}";
description.text = jobData.description;
coinReward.text = jobData.coinReward.ToString();
experienceReward.text = jobData.experienceReward.ToString();
reputationReward.text = jobData.reputationReward.ToString();
if (showPanel)
ToggleInfoPanel(true);
}
public void ToggleInfoPanel(bool visible)
{
infoPanel.SetActive(visible);
if (!visible)
onInfoPanelClosed.Raise();
}
private void Send_Others_OnJobSelected(JobInstance jobInstance)
{
//if (PhotonNetwork.CurrentRoom.PlayerCount <= 1) return;
networkJobInstance = jobInstance;
string jsonJobData = JsonUtility.ToJson(networkJobInstance);
Debug.Log($"JobData: {jsonJobData}");
RaiseEventOptions raiseEventOptions = new RaiseEventOptions { Receivers = ReceiverGroup.Others };
PhotonNetwork.RaiseEvent(GameConstants.NetworkEventCodes.JobSelection, jsonJobData, raiseEventOptions, SendOptions.SendReliable);
}
public void OnEvent(EventData photonEvent)
{
if (photonEvent.Code == GameConstants.NetworkEventCodes.JobSelection)
{
string jsonJobData = (string)photonEvent.CustomData;
Debug.Log("Job selected: " + jsonJobData);
SetupJobInfoPanel(JsonUtility.FromJson<JobInstance>(jsonJobData), false);
JobManager.Instance.UpdateJobSelection(JsonUtility.FromJson<JobInstance>(jsonJobData));
}
}
public void OnEnable()
{
PhotonNetwork.AddCallbackTarget(this);
}
public void OnDisable()
{
PhotonNetwork.RemoveCallbackTarget(this);
}
}