Glow u igrici parovi
This commit is contained in:
@@ -13,9 +13,14 @@ public class Quiz2Controller : MonoBehaviour {
|
||||
|
||||
public GameObject[] answerModels;
|
||||
|
||||
public GameObject haloEffect;
|
||||
|
||||
// References to answers
|
||||
private List<GameObject> answers;
|
||||
|
||||
// References to answers halo prefabs
|
||||
private List<GameObject> 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<GameObject>();
|
||||
answersHaloEffects = new List<GameObject>();
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clears glow from all currently shown answers
|
||||
/// </summary>
|
||||
private void ClearCurrentSceneGlowEffect()
|
||||
{
|
||||
for (int i = 0; i < numberOfAnswers; i++)
|
||||
{
|
||||
int prefabIdx = questionIdx * numberOfAnswers + i;
|
||||
answersHaloEffects[prefabIdx].SetActive(false);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clears/Adds glow from a specific answer GameObject
|
||||
/// </summary>
|
||||
/// <param name="prefabIdx"></param>
|
||||
private void SetGlowEffect(int prefabIdx, bool showGlowEffect)
|
||||
{
|
||||
answersHaloEffects[prefabIdx - 1].SetActive(showGlowEffect);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Instantiates all answer objects
|
||||
/// </summary>
|
||||
@@ -195,12 +243,26 @@ public class Quiz2Controller : MonoBehaviour {
|
||||
answers.Add(answerGameObject);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds Halo prefabs to all answers
|
||||
/// </summary>
|
||||
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 {
|
||||
/// <summary>
|
||||
/// Updates score value - called from Update() method
|
||||
/// </summary>
|
||||
private void UpdateScore()
|
||||
private void UpdateScore(bool initialization = false)
|
||||
{
|
||||
if (!initialization)
|
||||
{
|
||||
scoreValue += GetScore();
|
||||
if (scoreValue < 0)
|
||||
scoreValue = 0;
|
||||
}
|
||||
scoreText.text = scoreValue.ToString();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user