GameController.cs 2.4 KB

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