Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- using UnityEngine.UI;
- public class GameController : MonoBehaviour
- {
- // Text objects
- public Text ScoreText;
- public Text GameOverText;
- public Text RestartText;
- public Text PlayerHealthText;
- // Variables
- private bool gameOver;
- private bool restart;
- private int score;
- private PlayerController playerController;
- private int playerHitPoint;
- private ControllerInput controllerInput;
- // Use this for initialization
- void Start ()
- {
- gameOver = false;
- restart = false;
- RestartText.text = "";
- GameOverText.text = "";
- score = 0;
- UpdateScore();
- playerController = GameObject.Find("Player").GetComponent<PlayerController>();
- UpdatePlayerHealth();
- controllerInput = GameObject.Find("ControllerInput").GetComponent<ControllerInput>();
- }
- // Update is called once per frame
- void Update () {
- if (restart)
- {
- if (Input.GetKeyDown(KeyCode.R))
- {
- SceneManager.LoadScene(1);
- }
- }
- UpdatePlayerHealth();
- if (gameOver)
- {
- controllerInput.closeSerialPort();
- RestartText.text = "Press R to restart";
- restart = true;
- }
- if (Input.GetKeyDown(KeyCode.Q))
- {
- controllerInput.closeSerialPort();
- SceneManager.LoadScene(0);
- }
- }
- // Add score as an int
- public void AddScore(int scoreValue)
- {
- score += scoreValue;
- UpdateScore();
- }
- // Update score text
- private void UpdateScore()
- {
- ScoreText.text = "Score: " + score;
- }
- // Update player's health
- private void UpdatePlayerHealth()
- {
- if (playerController != null)
- {
- playerHitPoint = playerController.GetHitPoints();
- }
- PlayerHealthText.text = "HP: " + playerHitPoint;
- if (playerHitPoint == 0 || playerController == null)
- {
- GameOver();
- PlayerHealthText.text = "HP: 0";
- }
- }
- public void GameOver()
- {
- GameOverText.text = "Game Over";
- gameOver = true;
- }
- public bool GetGameOver()
- {
- return gameOver;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement