Files
BoostBaller/Assets/Scripts/GameManager.cs
2020-01-27 16:31:38 +01:00

114 lines
3.5 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
[RequireComponent(typeof(AudioSource))]
public class GameManager : MonoBehaviour
{
public AudioClip goalSound;
[Header("Tankstellen")]
public int scorePlayer1 = 0;
public int scorePlayer2 = 0;
public int maxGoals = 1;
[Header("Player")]
public float inputTimeout = 0.3f;
public Transform player1;
public Transform player2;
public GameObject[] players;
[Header("Boll")]
public Transform ball;
private Transform player1Initial, player2Initial, ballInitial;
[Header("Misc")]
public Vector3[] initialPositions;
public Quaternion[] initialRotations;
/* UI */
public Text scoreLabel;
public Text winLabel;
private AudioSource audioSource;
// Start is called before the first frame update
void Start()
{
this.players = GameObject.FindGameObjectsWithTag("Player");
this.initialPositions = new Vector3[this.players.Length];
this.initialRotations = new Quaternion[this.players.Length];
for (int i = 0; i < this.players.Length; i++)
{
this.initialPositions[i] = this.players[i].transform.position;
this.initialRotations[i] = this.players[i].transform.rotation;
}
this.audioSource = GetComponent<AudioSource>();
this.player1Initial = new GameObject().transform;
this.player1Initial.position = this.player1.position;
this.player1Initial.rotation = this.player1.rotation;
this.player2Initial = new GameObject().transform;
this.player2Initial.position = this.player2.position;
this.player2Initial.rotation = this.player2.rotation;
this.ballInitial = new GameObject().transform;
this.ballInitial.position = this.ball.position;
winLabel.transform.gameObject.SetActive(false);
}
// Update is called once per frame
void Update()
{
//Debug.Log("Calculating matrix: Vector(" + Time.deltaTime * Random.Range(12, 123) + ")");
//Debug.Log("Hacking pentagon: in pr0gress...");
this.scoreLabel.text = this.scorePlayer1 + " : " + this.scorePlayer2;
if (this.scorePlayer1 >= maxGoals)
{
winLabel.text = "Player 1 wins the game!";
winLabel.transform.gameObject.SetActive(true);
Time.timeScale = 0.0f;
}
if (this.scorePlayer2 >= maxGoals)
{
winLabel.text = "Player 2 wins the game!";
winLabel.transform.gameObject.SetActive(true);
Time.timeScale = 0.0f;
}
if (this.scorePlayer2 >= maxGoals || this.scorePlayer1 >= maxGoals)
{
if (Input.GetKeyDown(KeyCode.F5))
{
Time.timeScale = 1;
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}
}
public void ResetPositions()
{
this.audioSource.clip = this.goalSound;
this.audioSource.Play();
for (int i = 0; i < this.players.Length; i++)
{
this.players[i].transform.position = this.initialPositions[i];
this.players[i].transform.rotation = this.initialRotations[i];
this.players[i].GetComponent<Rigidbody>().velocity = Vector3.zero;
this.players[i].GetComponent<PlayerController>().InputTimeout(this.inputTimeout);
}
this.ball.position = this.ballInitial.position;
this.ball.GetComponent<Rigidbody>().velocity = Vector3.zero;
}
}