Snaparazzi/Assets/Scripts/QuestionHandler.cs

81 lines
2.3 KiB
C#
Raw Normal View History

2024-01-28 01:40:12 +00:00
using System.Collections.Generic;
using System.Linq;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
2024-01-28 01:40:12 +00:00
public class QuestionHandler : MonoBehaviour
{
public GameObject managers;
public Button submitButton;
2024-01-28 01:40:12 +00:00
public TextMeshProUGUI explainText;
public PromptList promptList;
private GameManager gameManager;
2024-01-28 01:40:12 +00:00
private List<Question> player2questions;
private int currentQuestion = 0;
2024-01-28 01:40:12 +00:00
void OnEnable()
{
gameManager = managers.GetComponent<GameManager>();
2024-01-28 01:40:12 +00:00
player2questions = gameManager.myRoom.GetQuestionsByPlayer(gameManager.currentPlayer);
2024-01-28 15:19:30 +00:00
Redraw(currentQuestion);
2024-01-28 01:40:12 +00:00
}
void OnDisable()
{
player2questions = null;
currentQuestion = 0;
2024-01-28 01:40:12 +00:00
}
void Redraw(int currentQuestion)
{
Prompt prompt = promptList.prompts.Find(x => x.id == player2questions[currentQuestion].promptId);
//Debug.Log(JsonUtility.ToJson(prompt));
2024-01-28 01:40:12 +00:00
explainText.SetText(prompt.en);
CameraManager cameraManager = gameObject.GetComponent<CameraManager>();
cameraManager.WebcamResume();
}
2024-01-28 14:42:32 +00:00
int GetPropRef(Player player)
2024-01-28 01:40:12 +00:00
{
return gameManager.myRoom.questions[currentQuestion].propositions.First(x => x.Value.owner.id == player.id).Key;
2024-01-28 01:40:12 +00:00
}
public void OnSubmitButton()
{
submitButton.interactable = false;
submitButton.GetComponent<TextMeshProUGUI>().text = "Uploading...";
managers.GetComponent<StorageManager>().UploadPhoto(
2024-01-28 01:40:12 +00:00
gameManager.myRoom.code,
gameManager.currentPlayer.id,
currentQuestion,
GetPropRef(gameManager.currentPlayer),
succeed =>
{
if(!succeed)
{
Debug.LogError("Photo upload failed. please do something", this);
return;
}
2024-01-28 01:40:12 +00:00
if (currentQuestion == 1)
{
gameManager.WaitForPlayers();
return;
}
else
{
submitButton.interactable = true;
submitButton.GetComponent<TextMeshProUGUI>().text = "Submit";
currentQuestion++;
Redraw(currentQuestion);
}
});
2024-01-28 01:40:12 +00:00
}
}