Added start countdown
Small UI changes Fixed initialization order for the GameManager
This commit is contained in:
73
Assets/Scripts/Countdown.cs
Normal file
73
Assets/Scripts/Countdown.cs
Normal file
@@ -0,0 +1,73 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class Countdown : MonoBehaviour
|
||||
{
|
||||
|
||||
public float seconds = 3;
|
||||
public bool lockControls = true;
|
||||
public AudioClip tickSound;
|
||||
public AudioClip startSound;
|
||||
public AudioSource audioSource;
|
||||
public GameObject[] enableOnZero;
|
||||
public GameObject[] disableOnZero;
|
||||
public string finalText = "Go!";
|
||||
public float finalTextScreentime = 0.5f; //How long should we display the final text
|
||||
public Text uiText;
|
||||
|
||||
public float remaining;
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
if (this.lockControls)
|
||||
{
|
||||
GameObject.FindGameObjectWithTag("GameManager").GetComponent<GameManager>().LockControls(this.seconds);
|
||||
}
|
||||
|
||||
this.remaining = this.seconds;
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
int second = (int)Mathf.Ceil(this.remaining);
|
||||
this.UpdateText(second);
|
||||
|
||||
|
||||
if(this.remaining <= 0)
|
||||
{
|
||||
this.ProcessGameObjects(this.enableOnZero, true);
|
||||
this.ProcessGameObjects(this.disableOnZero, false);
|
||||
}
|
||||
|
||||
if(this.remaining <= -finalTextScreentime)
|
||||
{
|
||||
this.uiText.gameObject.SetActive(false);
|
||||
this.enabled = false;
|
||||
}
|
||||
|
||||
this.remaining -= Time.deltaTime;
|
||||
}
|
||||
|
||||
private void UpdateText(int second)
|
||||
{
|
||||
string newText = this.remaining > 0 ? second.ToString() : this.finalText;
|
||||
|
||||
if(newText != this.uiText.text)
|
||||
{
|
||||
this.uiText.text = newText;
|
||||
this.audioSource.PlayOneShot(newText == this.finalText ? this.startSound : this.tickSound);
|
||||
}
|
||||
}
|
||||
|
||||
private void ProcessGameObjects(GameObject[] objectList, bool enable)
|
||||
{
|
||||
for (int i = 0; i < objectList.Length; i++)
|
||||
{
|
||||
objectList[i].SetActive(enable);
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Countdown.cs.meta
Normal file
11
Assets/Scripts/Countdown.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4521a81efd1bf4242ae19ce6c321b40b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -36,7 +36,7 @@ public class GameManager : MonoBehaviour
|
||||
|
||||
private AudioSource audioSource;
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
void Awake()
|
||||
{
|
||||
this.players = GameObject.FindGameObjectsWithTag("Player");
|
||||
this.initialPositions = new Vector3[this.players.Length];
|
||||
@@ -71,14 +71,14 @@ public class GameManager : MonoBehaviour
|
||||
|
||||
if (this.scorePlayer1 >= maxGoals)
|
||||
{
|
||||
winLabel.text = "Player 1 wins the game!";
|
||||
winLabel.text = "Player 1 won!";
|
||||
winLabel.transform.gameObject.SetActive(true);
|
||||
Time.timeScale = 0.0f;
|
||||
}
|
||||
|
||||
if (this.scorePlayer2 >= maxGoals)
|
||||
{
|
||||
winLabel.text = "Player 2 wins the game!";
|
||||
winLabel.text = "Player 2 won!";
|
||||
winLabel.transform.gameObject.SetActive(true);
|
||||
Time.timeScale = 0.0f;
|
||||
}
|
||||
@@ -110,4 +110,12 @@ public class GameManager : MonoBehaviour
|
||||
this.ball.GetComponent<Rigidbody>().velocity = Vector3.zero;
|
||||
|
||||
}
|
||||
|
||||
public void LockControls(float duration)
|
||||
{
|
||||
for (int i = 0; i < this.players.Length; i++)
|
||||
{
|
||||
this.players[i].GetComponent<PlayerController>().InputTimeout(duration);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,7 +85,6 @@ public class PlayerController : MonoBehaviour
|
||||
transform.Rotate(Vector3.up, -this.rotationSpeed * Time.deltaTime);
|
||||
}
|
||||
|
||||
Debug.Log(movementDirection);
|
||||
//Forward
|
||||
if (movementDirection != 0)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user