From dd09c6f72f4c94c59859346c14070aea8fb17780 Mon Sep 17 00:00:00 2001 From: Mediha Zukic Date: Mon, 22 May 2017 13:47:09 +0300 Subject: [PATCH] Glow u igrici parovi --- Assets/Scenes/EntryScene.unity | 2 +- Assets/Scenes/Quiz2Scene.unity | 1 + Assets/Scripts/Quiz2Controller.cs | 106 ++++++++++++++++++++++++------ Assets/Scripts/Quiz4Controller.cs | 2 +- 4 files changed, 90 insertions(+), 21 deletions(-) diff --git a/Assets/Scenes/EntryScene.unity b/Assets/Scenes/EntryScene.unity index dbcdf9e..cd810db 100644 --- a/Assets/Scenes/EntryScene.unity +++ b/Assets/Scenes/EntryScene.unity @@ -519,7 +519,7 @@ MonoBehaviour: m_TypeName: UnityEngine.UI.MaskableGraphic+CullStateChangedEvent, UnityEngine.UI, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null m_FontData: - m_Font: {fileID: 12800000, guid: ea60ddb36cd742f43afccf15d3ffe272, type: 3} + m_Font: {fileID: 12800000, guid: 34b69fa3cc23fa1498ecdf2650846c9d, type: 3} m_FontSize: 70 m_FontStyle: 0 m_BestFit: 0 diff --git a/Assets/Scenes/Quiz2Scene.unity b/Assets/Scenes/Quiz2Scene.unity index 3e7c29b..e274554 100644 --- a/Assets/Scenes/Quiz2Scene.unity +++ b/Assets/Scenes/Quiz2Scene.unity @@ -876,6 +876,7 @@ MonoBehaviour: - {fileID: 1326664960808868, guid: 29eeb6bf5cef40942abd867490425eb6, type: 2} - {fileID: 1476576601628434, guid: f30480f046aa8234a8645f8bf4663891, type: 2} - {fileID: 1283141416758514, guid: e4cb59c48e25ba84a945d869e623f641, type: 2} + haloEffect: {fileID: 1264499954100932, guid: 6ead2541b42799549a0f85e8d7a8305f, type: 2} --- !u!1001 &1451760075 Prefab: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/Quiz2Controller.cs b/Assets/Scripts/Quiz2Controller.cs index a97bb02..dc17921 100644 --- a/Assets/Scripts/Quiz2Controller.cs +++ b/Assets/Scripts/Quiz2Controller.cs @@ -13,9 +13,14 @@ public class Quiz2Controller : MonoBehaviour { public GameObject[] answerModels; + public GameObject haloEffect; + // References to answers private List answers; + // References to answers halo prefabs + private List answersHaloEffects; + private const int numberOfAnswers = 2; private const int scoreCorrectAnswer = 10; @@ -30,7 +35,7 @@ public class Quiz2Controller : MonoBehaviour { private int scoreValue; - private int questionIdx = 0; + private int questionIdx; private float counterValue; @@ -38,6 +43,12 @@ public class Quiz2Controller : MonoBehaviour { private int selectedAnswerIdx; + private const int counterDisplayAnswersTotalValue = 3; + + private float counterDisplayAnswersValue; + + private bool counterDisplayAnswersStarted = false; + // Positions for answers 1, 2, 3 and 4 private Vector3[] answerPositions = new Vector3[] { @@ -114,35 +125,47 @@ public class Quiz2Controller : MonoBehaviour { // Use this for initialization void Start () { + // Init counters and score + questionIdx = 0; scoreValue = 0; counterValue = 0; counterPerQuestionValue = 0; + counterDisplayAnswersValue = 0; + counterDisplayAnswersStarted = false; totalTimePerQuestion = totalGameTime / questions.Count; answers = new List(); + answersHaloEffects = new List(); UpdateCounter(); - UpdateScore(); + UpdateScore(true); InstantiateAnswers(); + InstantiateHaloPrefabs(); UpdateScene(); } // Update is called once per frame - void Update () { - - // TODO: Game over + void Update () + { + Debug.Log(counterDisplayAnswersValue.ToString()); + // Main counter + // Game over if (counterValue > totalGameTime) SceneManager.LoadScene("EntryScene"); counterValue += Time.deltaTime; - UpdateCounter(); - // Next pair - if(counterPerQuestionValue > totalTimePerQuestion) + // Load next pair + if((counterDisplayAnswersValue > counterDisplayAnswersTotalValue) && counterDisplayAnswersStarted || (counterPerQuestionValue > totalTimePerQuestion && !counterDisplayAnswersStarted)) { - counterPerQuestionValue = 0; + // Load next pair LoadNextPair(); } - else + + if (counterDisplayAnswersValue < counterDisplayAnswersTotalValue && counterDisplayAnswersStarted) + counterDisplayAnswersValue += Time.deltaTime; + + if (counterPerQuestionValue < totalTimePerQuestion) counterPerQuestionValue += Time.deltaTime; + // Select answers event handler if (Input.GetMouseButtonDown(0)) @@ -152,15 +175,40 @@ public class Quiz2Controller : MonoBehaviour { if (Physics.Raycast(ray, out hit, 100)) { - Debug.Log(hit.transform.gameObject.name); + if (counterDisplayAnswersStarted) + return; + // Debug.Log(hit.transform.gameObject.name); + counterDisplayAnswersStarted = true; + // Set glow selectedAnswerIdx = GetAnswerIdx(hit.transform.gameObject.name); - counterPerQuestionValue = 0; - LoadNextPair(); - + SetGlowEffect(selectedAnswerIdx, true); + // Update score + UpdateScore(); } } } + /// + /// Clears glow from all currently shown answers + /// + private void ClearCurrentSceneGlowEffect() + { + for (int i = 0; i < numberOfAnswers; i++) + { + int prefabIdx = questionIdx * numberOfAnswers + i; + answersHaloEffects[prefabIdx].SetActive(false); + } + } + + /// + /// Clears/Adds glow from a specific answer GameObject + /// + /// + private void SetGlowEffect(int prefabIdx, bool showGlowEffect) + { + answersHaloEffects[prefabIdx - 1].SetActive(showGlowEffect); + } + /// /// Instantiates all answer objects /// @@ -195,12 +243,26 @@ public class Quiz2Controller : MonoBehaviour { answers.Add(answerGameObject); } + /// + /// Adds Halo prefabs to all answers + /// + private void InstantiateHaloPrefabs() + { + for (int i = 0; i < answers.Count; i++) + { + GameObject answerPrefab = Instantiate(haloEffect) as GameObject; + answerPrefab.SetActive(false); + answerPrefab.transform.SetParent(answers[i].transform, false); + answersHaloEffects.Add(answerPrefab); + } + } + void LoadNextPair() { - scoreValue += GetScore(); - if (scoreValue < 0) - scoreValue = 0; - UpdateScore(); + counterPerQuestionValue = 0; + counterDisplayAnswersValue = 0; + counterDisplayAnswersStarted = false; + ClearCurrentSceneGlowEffect(); questionIdx = questionIdx + 1; if (questionIdx == questions.Count) SceneManager.LoadScene("EntryScene"); @@ -211,8 +273,14 @@ public class Quiz2Controller : MonoBehaviour { /// /// Updates score value - called from Update() method /// - private void UpdateScore() + private void UpdateScore(bool initialization = false) { + if (!initialization) + { + scoreValue += GetScore(); + if (scoreValue < 0) + scoreValue = 0; + } scoreText.text = scoreValue.ToString(); } diff --git a/Assets/Scripts/Quiz4Controller.cs b/Assets/Scripts/Quiz4Controller.cs index e1447c5..62415db 100644 --- a/Assets/Scripts/Quiz4Controller.cs +++ b/Assets/Scripts/Quiz4Controller.cs @@ -211,7 +211,7 @@ public class Quiz4Controller : MonoBehaviour { /// private void InstantiateHaloPrefabs() { - for (int i = 0; i < answers.Count(); i++) + for (int i = 0; i < answers.Count; i++) { GameObject answerPrefab = Instantiate(haloEffect) as GameObject; answerPrefab.SetActive(false);