feat: waiting for all propositions on PC & add some SFX

This commit is contained in:
Morgan - 6 Freedom 2024-01-28 02:41:16 +01:00
parent a19f158828
commit 09f06fb715
3 changed files with 127 additions and 52 deletions

BIN
Assets/Audio SFX/camera-flash-sound-effect.mp3 (Stored with Git LFS) Normal file

Binary file not shown.

View File

@ -0,0 +1,23 @@
fileFormatVersion: 2
guid: b4d0bdddc893f476a8eb330e58108608
AudioImporter:
externalObjects: {}
serializedVersion: 7
defaultSettings:
serializedVersion: 2
loadType: 0
sampleRateSetting: 0
sampleRateOverride: 44100
compressionFormat: 1
quality: 1
conversionMode: 0
preloadAudioData: 0
platformSettingOverrides: {}
forceToMono: 0
normalize: 1
loadInBackground: 0
ambisonic: 0
3D: 1
userData:
assetBundleName:
assetBundleVariant:

View File

@ -15,9 +15,8 @@ public class RoomManager : MonoBehaviour
/// TextMeshPro that show the value of the current rooom code
/// </summary>
public TextMeshProUGUI roomCodeLabel;
public AudioClip playerJoinSFX;
public List<TextMeshProUGUI> waitingForPlayersLabels = new List<TextMeshProUGUI>();
private Dictionary<string, TextMeshProUGUI> waitingPlayersById = new Dictionary<string, TextMeshProUGUI>();
public PromptList promptList;
[Header("Explanation Page")]
public GameObject explanationPage;
@ -31,10 +30,13 @@ public class RoomManager : MonoBehaviour
public TextMeshProUGUI propositionCounter;
public float propositionTime = 60;
public List<TextMeshProUGUI> waitingForPropositionsLabels = new List<TextMeshProUGUI>();
private Dictionary<string, TextMeshProUGUI> propositionLabelsByID = new Dictionary<string, TextMeshProUGUI>();
private DateTime endOfPropositionDate = DateTime.MinValue;
private bool allPlayersHasProposedTwoPictures = false;
private Dictionary<string, Proposition[]> propositionsPerPlayers = new Dictionary<string, Proposition[]>();
[Header("Other")]
public PromptList promptList;
private Room myRoom = null;
public float votingTime = 20;
@ -94,13 +96,34 @@ private void Update()
TimeSpan duration = endOfPropositionDate - DateTime.Now;
propositionCounter.text = ((int)duration.TotalSeconds).ToString("D1");
foreach (TextMeshProUGUI tmp in waitingForPropositionsLabels)
//foreach labels
foreach (var labelByID in propositionLabelsByID)
{
if (tmp.gameObject.activeSelf)
//if the label is connected to a player
if (labelByID.Value.gameObject.activeSelf)
{
//check if this player has send 2 propositions
bool playerHasAnswerBoth = true;
foreach (Proposition p in propositionsPerPlayers[labelByID.Key])
{
if (string.IsNullOrEmpty(p.photoUrl))
{
playerHasAnswerBoth = false;
}
}
//if its the case
if (playerHasAnswerBoth)
{
//put player label in green if finished
labelByID.Value.color = Color.green;
}
}
}
if (allPlayersHasProposedTwoPictures || duration.TotalMilliseconds <= 0)
{
SendRoomState(GameState.MakeVote);
}
}
}
@ -111,7 +134,6 @@ private void SendRoomState(GameState _newState)
private void ResetAllPlayerLabels()
{
waitingPlayersById.Clear();
for (int i = 0; i < waitingForPlayersLabels.Count; i++)
{
waitingForPlayersLabels[i].text = $"Waiting for P{i + 1}";
@ -226,11 +248,6 @@ private int GenerateRandomAvailableCode(List<int> _impossibleCodes)
return random;
}
public void PlayerSendProposition(Proposition _proposition)
{
}
/// <summary>
/// Called when the first player clicked "Start"
/// </summary>
@ -240,53 +257,30 @@ public void HostHasStartedGame()
waitingForPlayersPage.SetActive(false);
endOfPropositionDate = DateTime.Now.AddSeconds(propositionTime);
propositionLabelsByID.Clear();
//display only correct numbers of labels
for (int i = 0; i < waitingForPropositionsLabels.Count; i++)
{
TextMeshProUGUI tmp = waitingForPropositionsLabels[i];
tmp.gameObject.SetActive(i < myRoom.players.Count);
}
}
//registers the labels per player ID
List<Player> orderedPlayers = myRoom.GetPlayerList().OrderBy(x => x.creationDate).ToList();
for (int i = 0; i < orderedPlayers.Count; i++)
{
propositionLabelsByID.Add(orderedPlayers[i].id, waitingForPropositionsLabels[i]);
waitingForPlayersLabels[i].text = orderedPlayers[i].name;
}
/// <summary>
/// Start the proposition timer
/// </summary>
public void StartPropositionTimer()
{
//Register all propositions of each players
foreach (Player p in myRoom.GetOrderedPlayerList())
{
List<Proposition> propositionsForPlayer = myRoom.GetPropositionsByPlayer(p);
propositionsPerPlayers.Add(p.id, propositionsForPlayer.ToArray());
}
}
/// <summary>
/// Automatically called when the proposition timer has finished
/// </summary>
public void PropositionTimerFinished()
{
}
/// <summary>
/// Start the voting timer
/// </summary>
public void StartVotingTimer()
{
}
/// <summary>
/// Automatically called when the voting timer has finished
/// </summary>
public void VotingTimerFinished()
{
}
/// <summary>
/// Automatically called when a proposition is updated (someone has voted or a picture has been proposed)
/// </summary>
public void OnPropositionUpdate()
{
}
@ -347,8 +341,14 @@ private void OnRoomUpdate(object sender, ValueChangedEventArgs value)
Debug.LogError(value.DatabaseError.Message);
return;
}
if (value.Snapshot.Value == null)
{
Debug.Log("Trying to update room, but it's empty. Maybe you are exiting the app, so it's ok", this);
return;
}
string JSON = value.Snapshot.GetRawJsonValue();
Debug.Log($"your room has been updated :\n{JSON}");
GameState lastState = (GameState)myRoom.currentState;
try
{
myRoom = JsonConvert.DeserializeObject<Room>(JSON);
@ -358,19 +358,50 @@ private void OnRoomUpdate(object sender, ValueChangedEventArgs value)
Debug.LogException(ex);
}
//this is done only when entering a new state
if (lastState != (GameState)myRoom.currentState)
{
OnNewGameStateStarted();
}
else
{
//this is done each time something change
switch (myRoom.currentState)
{
case (int)GameState.WaitingForOtherPlayersToJoin:
UpdateConnectedPlayerList(myRoom.GetPlayerList());
break;
case (int)GameState.Explanation:
break;
case (int)GameState.MakeProposition:
CheckPlayersPropositions();
break;
default:
break;
}
}
}
/// <summary>
/// Called when we enter in a new game state
/// </summary>
private void OnNewGameStateStarted()
{
switch (myRoom.currentState)
{
case (int)GameState.WaitingForOtherPlayersToJoin:
UpdateConnectedPlayerList(myRoom.GetPlayerList());
Debug.Log("New State : WaitingForOtherPlayersToJoin");
break;
case (int)GameState.Explanation:
Debug.Log("New State : Explanation");
waitingForPlayersPage.SetActive(false);
explanationPage.SetActive(true);
endOfExplanationDate = DateTime.Now.AddSeconds(explanationTime);
AudioSource.PlayClipAtPoint(counterSFX, Vector3.zero);
break;
case (int)GameState.MakeProposition:
Debug.Log("New State : MakeProposition");
HostHasStartedGame();
break;
default:
@ -378,19 +409,37 @@ private void OnRoomUpdate(object sender, ValueChangedEventArgs value)
}
}
/// <summary>
/// Will update allPlayersHasProposedTwoPictures to true or false
/// </summary>
private void CheckPlayersPropositions()
{
allPlayersHasProposedTwoPictures = true;
foreach (var propositionsByPlayer in propositionsPerPlayers)
{
foreach (Proposition p in propositionsByPlayer.Value)
{
if (string.IsNullOrEmpty(p.photoUrl))
allPlayersHasProposedTwoPictures = false;
}
}
}
/// <summary>
/// Update the player labels on the WaitingForPlayer page
/// </summary>
/// <param name="_players"></param>
private void UpdateConnectedPlayerList(List<Player> _players)
{
AudioSource.PlayClipAtPoint(playerJoinSFX, Vector3.zero);
ResetAllPlayerLabels();
Debug.Log($"players count = {_players.Count}");
List<Player> orderedPlayers = _players.OrderBy(x => x.creationDate).ToList();
for (int i = 0; i < orderedPlayers.Count; i++)
{
waitingPlayersById.Add(orderedPlayers[i].id, waitingForPlayersLabels[i]);
Debug.Log($"player {i} = {orderedPlayers[i].name}");
waitingForPlayersLabels[i].text = orderedPlayers[i].name;
}