277 lines
7.1 KiB
C#
277 lines
7.1 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Firebase.Database;
|
|
using Firebase.Extensions;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using UnityEngine.UIElements;
|
|
|
|
/// <summary>
|
|
/// This is the game state manager on the phone side
|
|
/// </summary>
|
|
public class GameManager : MonoBehaviour
|
|
{
|
|
private GameState currentState;
|
|
private List<Player> players = new List<Player>();
|
|
|
|
[Header("Temp variables")]
|
|
public bool isRoomExist = true;
|
|
public bool isPlayerExist = true;
|
|
|
|
|
|
[Header("Other component")]
|
|
public float explanationTime = 4f;
|
|
private float currentExplanationTime = 0;
|
|
|
|
[Header("Home Component")]
|
|
public TMP_InputField roomCodeField;
|
|
public TextMeshProUGUI roomError;
|
|
public TMP_InputField playerNameField;
|
|
public TextMeshProUGUI nameError;
|
|
public UnityEngine.UI.Button submitNewPlayer;
|
|
|
|
|
|
[Header("WaitingRoom Component")]
|
|
public TextMeshProUGUI listPlayersUI;
|
|
public GameObject submitStartGame;
|
|
|
|
|
|
[Header("Pages")]
|
|
[SerializeField]
|
|
public GameObject HomeConnection;
|
|
[SerializeField]
|
|
public GameObject WaitingRoom;
|
|
[SerializeField]
|
|
public GameObject BeforeStart;
|
|
[SerializeField]
|
|
public GameObject TakePicture;
|
|
[SerializeField]
|
|
public GameObject VotePicture;
|
|
[SerializeField]
|
|
public GameObject WaitingOtherPlayers;
|
|
[SerializeField]
|
|
public GameObject EndGame;
|
|
|
|
|
|
|
|
private DatabaseReference realtimeDB;
|
|
private Room myRoom;
|
|
|
|
private void Awake()
|
|
{
|
|
FirebaseInitializer.Instance.onFirebaseReady += Initialize;
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
currentExplanationTime = explanationTime;
|
|
HomeConnection.SetActive(true);
|
|
}
|
|
|
|
public GameState GetCurrentState()
|
|
{
|
|
return currentState;
|
|
}
|
|
|
|
private void Initialize()
|
|
{
|
|
FirebaseInitializer.Instance.onFirebaseReady -= Initialize;
|
|
realtimeDB = FirebaseDatabase.DefaultInstance.RootReference;
|
|
Debug.Log("Realtime DB initialized");
|
|
|
|
//TODO MARINE : enable the submit button here. Disabled it at start
|
|
}
|
|
|
|
/// <summary>
|
|
/// Send your name and game room to the server
|
|
/// </summary>
|
|
public void PlayerValidateNameAndServerRoom(string _name, string _code)
|
|
{
|
|
//check if the room exists, if not display an error message
|
|
realtimeDB.Child("rooms").Child(_code).GetValueAsync().ContinueWithOnMainThread(task =>
|
|
{
|
|
if (task.IsFaulted)
|
|
{
|
|
Debug.LogException(task.Exception);
|
|
}
|
|
else if (task.IsCompleted)
|
|
{
|
|
DataSnapshot snapshot = task.Result;
|
|
if (snapshot == null)
|
|
{
|
|
Debug.LogError("The room doesn't exists");
|
|
roomError.text = "Error: the room doesn't exists";
|
|
roomError.gameObject.SetActive(true);
|
|
}
|
|
else
|
|
{
|
|
myRoom = JsonUtility.FromJson<Room>(snapshot.GetRawJsonValue());
|
|
realtimeDB.Child("rooms").Child(_code).ChildChanged += OnRoomUpdate;
|
|
}
|
|
}
|
|
});
|
|
string playerName = playerNameField.text;
|
|
string roomCode = roomCodeField.text;
|
|
|
|
// send to the server the room code and the player name
|
|
|
|
// MORGANE ENVOIE AU SERV
|
|
|
|
// answer isPlayerExist and isRoomExist
|
|
|
|
//if succeed, then the player to the server
|
|
if (isPlayerExist && isRoomExist)
|
|
{
|
|
currentState = GameState.WaitingForOtherPlayersToJoin;
|
|
WaitingRoom.SetActive(true);
|
|
HomeConnection.SetActive(false);
|
|
|
|
|
|
//TEMP
|
|
Player player = new Player();
|
|
player.SetName(playerName);
|
|
player.id = "1";
|
|
|
|
players.Add(player);
|
|
if (players.Count == 1)
|
|
{
|
|
submitStartGame.SetActive(true);
|
|
}
|
|
|
|
Player player2 = new Player();
|
|
player2.SetName("3J");
|
|
player2.id = "2";
|
|
Player player3 = new Player();
|
|
player3.SetName("3J");
|
|
player3.id = "3";
|
|
Player player4 = new Player();
|
|
player4.SetName("3J");
|
|
player4.id = "4";
|
|
Player player5 = new Player();
|
|
player5.SetName("3J");
|
|
player5.id = "5";
|
|
Player player6 = new Player();
|
|
player6.SetName("3J");
|
|
player6.id = "6";
|
|
|
|
|
|
players.Add(player2);
|
|
players.Add(player3);
|
|
players.Add(player4);
|
|
players.Add(player5);
|
|
players.Add(player6);
|
|
UpdateDisplayedListUser();
|
|
}
|
|
// else we show the errors
|
|
roomError.gameObject.SetActive(!isRoomExist);
|
|
nameError.gameObject.SetActive(!isPlayerExist);
|
|
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// Call this only by the first player
|
|
/// </summary>
|
|
public void StartGame()
|
|
{
|
|
// send Start Game
|
|
|
|
currentState = GameState.Explanation;
|
|
WaitingRoom.SetActive(false);
|
|
BeforeStart.SetActive(true);
|
|
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// Dislay on the screen the current prompt ask to you and another player
|
|
/// Also show all the camera stuff
|
|
/// </summary>
|
|
/// <param name="_prompt">The prompt to display</param>
|
|
public void MakeAProposition(Prompt _prompt)
|
|
{
|
|
currentState = GameState.MakeProposition;
|
|
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create a proposition from the picture taken and send it to server.
|
|
/// Then go to next proposition (if any) or the page "WaitingForOtherPlayers"
|
|
/// </summary>
|
|
public void SubmitProposition()
|
|
{
|
|
Proposition temp = new Proposition();
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// Display the voting page
|
|
/// </summary>
|
|
public void StartToVote()
|
|
{
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// Choose one result and send it to the server
|
|
/// Then go to the next vote (if any) or to the page "WaitingForOtherPlayers"
|
|
/// </summary>
|
|
public void Vote()
|
|
{
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// Display the UI of the end screen
|
|
/// </summary>
|
|
public void DisplayEndScreen()
|
|
{
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// Automatically called when something change in your room
|
|
/// </summary>
|
|
private void OnRoomUpdate(object sender, ChildChangedEventArgs e)
|
|
{
|
|
myRoom = JsonUtility.FromJson<Room>(e.Snapshot.GetRawJsonValue());
|
|
switch (currentState)
|
|
{
|
|
case GameState.WaitingForOtherPlayersToJoin:
|
|
{
|
|
// players = new list en fonction de ce qu'envoie fangh
|
|
UpdateDisplayedListUser();
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
private void UpdateDisplayedListUser()
|
|
{
|
|
listPlayersUI.text = string.Empty;
|
|
for (int i = 0; i < players.Count; i++)
|
|
{
|
|
listPlayersUI.text += "\n" + players[i].name;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
public enum GameState
|
|
{
|
|
EnteringName,
|
|
WaitingForOtherPlayersToJoin,
|
|
Explanation,
|
|
MakeProposition,
|
|
PropositionsSent,
|
|
WaitingForOtherPlayers,
|
|
VotingTime,
|
|
Ending
|
|
}
|
|
|
|
|