Advertisement
Guest User

gamecntrl

a guest
Dec 11th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5. using UnityEngine.UI;
  6.  
  7. public class GameController : MonoBehaviour
  8. {
  9. // Text objects
  10. public Text ScoreText;
  11. public Text GameOverText;
  12. public Text RestartText;
  13. public Text PlayerHealthText;
  14.  
  15. // Variables
  16. private bool gameOver;
  17. private bool restart;
  18. private int score;
  19. private PlayerController playerController;
  20. private int playerHitPoint;
  21. private ControllerInput controllerInput;
  22. // Use this for initialization
  23. void Start ()
  24. {
  25. gameOver = false;
  26. restart = false;
  27. RestartText.text = "";
  28. GameOverText.text = "";
  29. score = 0;
  30. UpdateScore();
  31. playerController = GameObject.Find("Player").GetComponent<PlayerController>();
  32. UpdatePlayerHealth();
  33. controllerInput = GameObject.Find("ControllerInput").GetComponent<ControllerInput>();
  34. }
  35.  
  36. // Update is called once per frame
  37. void Update () {
  38.  
  39. if (restart)
  40. {
  41. if (Input.GetKeyDown(KeyCode.R))
  42. {
  43. SceneManager.LoadScene(1);
  44. }
  45. }
  46.  
  47. UpdatePlayerHealth();
  48.  
  49. if (gameOver)
  50. {
  51. controllerInput.closeSerialPort();
  52. RestartText.text = "Press R to restart";
  53. restart = true;
  54. }
  55.  
  56. if (Input.GetKeyDown(KeyCode.Q))
  57. {
  58. controllerInput.closeSerialPort();
  59. SceneManager.LoadScene(0);
  60. }
  61. }
  62.  
  63. // Add score as an int
  64. public void AddScore(int scoreValue)
  65. {
  66. score += scoreValue;
  67. UpdateScore();
  68. }
  69.  
  70. // Update score text
  71. private void UpdateScore()
  72. {
  73. ScoreText.text = "Score: " + score;
  74. }
  75.  
  76. // Update player's health
  77. private void UpdatePlayerHealth()
  78. {
  79. if (playerController != null)
  80. {
  81. playerHitPoint = playerController.GetHitPoints();
  82. }
  83. PlayerHealthText.text = "HP: " + playerHitPoint;
  84. if (playerHitPoint == 0 || playerController == null)
  85. {
  86. GameOver();
  87. PlayerHealthText.text = "HP: 0";
  88. }
  89. }
  90.  
  91. public void GameOver()
  92. {
  93. GameOverText.text = "Game Over";
  94. gameOver = true;
  95. }
  96.  
  97. public bool GetGameOver()
  98. {
  99. return gameOver;
  100. }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement