From 09f06fb7157598e2d44ec8ca74676df87796770c Mon Sep 17 00:00:00 2001 From: Morgan - 6 Freedom Date: Sun, 28 Jan 2024 02:41:16 +0100 Subject: [PATCH] feat: waiting for all propositions on PC & add some SFX --- .../Audio SFX/camera-flash-sound-effect.mp3 | 3 + .../camera-flash-sound-effect.mp3.meta | 23 +++ Assets/Scripts/RoomManager.cs | 153 ++++++++++++------ 3 files changed, 127 insertions(+), 52 deletions(-) create mode 100644 Assets/Audio SFX/camera-flash-sound-effect.mp3 create mode 100644 Assets/Audio SFX/camera-flash-sound-effect.mp3.meta diff --git a/Assets/Audio SFX/camera-flash-sound-effect.mp3 b/Assets/Audio SFX/camera-flash-sound-effect.mp3 new file mode 100644 index 0000000..5f0933e --- /dev/null +++ b/Assets/Audio SFX/camera-flash-sound-effect.mp3 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:58f18e86e44bfeff7f980abbb29e1050edc4b66f0c3dbda376b224b772ff0b20 +size 33364 diff --git a/Assets/Audio SFX/camera-flash-sound-effect.mp3.meta b/Assets/Audio SFX/camera-flash-sound-effect.mp3.meta new file mode 100644 index 0000000..d4da1a3 --- /dev/null +++ b/Assets/Audio SFX/camera-flash-sound-effect.mp3.meta @@ -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: diff --git a/Assets/Scripts/RoomManager.cs b/Assets/Scripts/RoomManager.cs index 7ff3c24..164e17f 100644 --- a/Assets/Scripts/RoomManager.cs +++ b/Assets/Scripts/RoomManager.cs @@ -15,9 +15,8 @@ public class RoomManager : MonoBehaviour /// TextMeshPro that show the value of the current rooom code /// public TextMeshProUGUI roomCodeLabel; + public AudioClip playerJoinSFX; public List waitingForPlayersLabels = new List(); - private Dictionary waitingPlayersById = new Dictionary(); - 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 waitingForPropositionsLabels = new List(); + private Dictionary propositionLabelsByID = new Dictionary(); private DateTime endOfPropositionDate = DateTime.MinValue; private bool allPlayersHasProposedTwoPictures = false; + private Dictionary propositionsPerPlayers = new Dictionary(); [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 _impossibleCodes) return random; } - public void PlayerSendProposition(Proposition _proposition) - { - - } - /// /// Called when the first player clicked "Start" /// @@ -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 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; + } - /// - /// Start the proposition timer - /// - public void StartPropositionTimer() - { + //Register all propositions of each players + foreach (Player p in myRoom.GetOrderedPlayerList()) + { + List propositionsForPlayer = myRoom.GetPropositionsByPlayer(p); + propositionsPerPlayers.Add(p.id, propositionsForPlayer.ToArray()); + } - } - - - /// - /// Automatically called when the proposition timer has finished - /// - public void PropositionTimerFinished() - { - - } - - /// - /// Start the voting timer - /// - public void StartVotingTimer() - { - - } - - - /// - /// Automatically called when the voting timer has finished - /// - public void VotingTimerFinished() - { - - } - - /// - /// Automatically called when a proposition is updated (someone has voted or a picture has been proposed) - /// - 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(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; + } + } + } + + /// + /// Called when we enter in a new game state + /// + 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) } } + /// + /// Will update allPlayersHasProposedTwoPictures to true or false + /// + private void CheckPlayersPropositions() + { + allPlayersHasProposedTwoPictures = true; + + foreach (var propositionsByPlayer in propositionsPerPlayers) + { + foreach (Proposition p in propositionsByPlayer.Value) + { + if (string.IsNullOrEmpty(p.photoUrl)) + allPlayersHasProposedTwoPictures = false; + } + } + } + + /// /// Update the player labels on the WaitingForPlayer page /// /// private void UpdateConnectedPlayerList(List _players) { + AudioSource.PlayClipAtPoint(playerJoinSFX, Vector3.zero); ResetAllPlayerLabels(); Debug.Log($"players count = {_players.Count}"); List 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; }