GameController.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System;
  5. public class GameController : MonoBehaviour
  6. {
  7. public AimCrossHair[] aimCrossHairs;
  8. [NonSerialized] public float[] scores;
  9. [NonSerialized] public float time;
  10. [NonSerialized] public bool gameStart = false;
  11. [NonSerialized] public bool gameOver = false;
  12. [NonSerialized] public float targetDistance;
  13. public GameObject arrowPrefab;
  14. public static GameController ins;
  15. void Start()
  16. {
  17. ins = this;
  18. InitGame();
  19. InitDistanceSelectView();
  20. SetDisplayDistanceSelectView(true);
  21. }
  22. void Update()
  23. {
  24. if (gameStart && !gameOver)
  25. {
  26. if (time > 0) time -= Time.deltaTime;
  27. else
  28. {
  29. time = 0;
  30. gameOver = true;
  31. //添加结算界面
  32. transform.Find("PKGameSettleView").gameObject.SetActive(true);
  33. }
  34. }
  35. }
  36. public void HitTarget(float score, int playerIndex = -1)
  37. {
  38. if (!gameStart || gameOver) return;
  39. if (playerIndex >= 0)
  40. {
  41. scores[playerIndex] += score;
  42. HitTargetNumberNew.Create(score, playerIndex);
  43. }
  44. }
  45. private void InitDistanceSelectView()
  46. {
  47. DistanceSelectView distanceSelectView = transform.Find("DistanceSelectView").GetComponent<DistanceSelectView>();
  48. distanceSelectView.OnClickSelectDistance = HandleSelectDistance;
  49. }
  50. public void SetDisplayDistanceSelectView(bool active)
  51. {
  52. transform.Find("DistanceSelectView").gameObject.SetActive(active);
  53. }
  54. public void HandleSelectDistance(float distance)
  55. {
  56. SetDisplayDistanceSelectView(false);
  57. targetDistance = distance;
  58. foreach (var item in FindObjectsOfType<TargetBodyNew>()) item.SetDistance(distance);
  59. StartGame();
  60. }
  61. public void InitGame()
  62. {
  63. scores = new float[] { 0, 0 };
  64. time = 60;
  65. gameStart = false;
  66. gameOver = false;
  67. foreach (var item in FindObjectsOfType<ArrowNew>())
  68. {
  69. if (item && item.gameObject) Destroy(item.gameObject);
  70. }
  71. foreach (var item in FindObjectsOfType<PointSignLastHit>())
  72. {
  73. if (item) item.Hide();
  74. }
  75. }
  76. public void StartGame()
  77. {
  78. gameStart = true;
  79. }
  80. }