Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
3.2 KiB

  1. namespace Oculus.Platform.Samples.VrHoops
  2. {
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using System.Collections.Generic;
  6. // The base Player component manages the balls that are in play. Besides spawning new balls,
  7. // old balls are destroyed when too many are around or the Player object itself is destroyed.
  8. public abstract class Player : MonoBehaviour {
  9. // maximum number of balls allowed at a time
  10. public const uint MAX_BALLS = 6;
  11. // the initial force to impart when shooting a ball
  12. private const float INITIAL_FORCE = 870f;
  13. // delay time before a new ball will spawn.
  14. private const float RESPAWN_SECONDS = 2.0f;
  15. // current score for the player
  16. private uint m_score;
  17. // cached reference to the Text component to render the score
  18. private Text m_scoreUI;
  19. // prefab for the GameObject representing a ball
  20. private GameObject m_ballPrefab;
  21. // gameobject for the position and orientation of where the ball will be shot
  22. private BallEjector m_ballEjector;
  23. // queue of active balls for the player to make sure too many arent in play
  24. private Queue<GameObject> m_balls = new Queue<GameObject>();
  25. // reference to a ball that hasn't been shot yet and is tied to the camera
  26. private GameObject m_heldBall;
  27. // when to spawn a new ball
  28. private float m_nextSpawnTime;
  29. #region Properties
  30. public virtual uint Score
  31. {
  32. get { return m_score; }
  33. set
  34. {
  35. m_score = value;
  36. if (m_scoreUI)
  37. {
  38. m_scoreUI.text = m_score.ToString();
  39. }
  40. }
  41. }
  42. public GameObject BallPrefab
  43. {
  44. set { m_ballPrefab = value; }
  45. }
  46. protected bool HasBall
  47. {
  48. get { return m_heldBall != null; }
  49. }
  50. #endregion
  51. void Start()
  52. {
  53. m_ballEjector = transform.GetComponentInChildren<BallEjector>();
  54. m_scoreUI = transform.parent.GetComponentInChildren<Text>();
  55. m_scoreUI.text = "0";
  56. }
  57. public GameObject CreateBall()
  58. {
  59. if (m_balls.Count >= MAX_BALLS)
  60. {
  61. Destroy(m_balls.Dequeue());
  62. }
  63. var ball = Instantiate(m_ballPrefab);
  64. m_balls.Enqueue(ball);
  65. ball.transform.position = m_ballEjector.transform.position;
  66. ball.transform.SetParent(m_ballEjector.transform, true);
  67. ball.GetComponent<Rigidbody>().useGravity = false;
  68. ball.GetComponent<Rigidbody>().detectCollisions = false;
  69. ball.GetComponent<DetectBasket>().Player = this;
  70. return ball;
  71. }
  72. protected GameObject CheckSpawnBall()
  73. {
  74. switch (PlatformManager.CurrentState)
  75. {
  76. case PlatformManager.State.WAITING_TO_PRACTICE_OR_MATCHMAKE:
  77. case PlatformManager.State.PLAYING_A_LOCAL_MATCH:
  78. case PlatformManager.State.PLAYING_A_NETWORKED_MATCH:
  79. if (Time.time >= m_nextSpawnTime && !HasBall)
  80. {
  81. m_heldBall = CreateBall();
  82. return m_heldBall;
  83. }
  84. break;
  85. }
  86. return null;
  87. }
  88. protected GameObject ShootBall()
  89. {
  90. GameObject ball = m_heldBall;
  91. m_heldBall = null;
  92. ball.GetComponent<Rigidbody>().useGravity = true;
  93. ball.GetComponent<Rigidbody>().detectCollisions = true;
  94. ball.GetComponent<Rigidbody>().AddForce(m_ballEjector.transform.forward * INITIAL_FORCE, ForceMode.Acceleration);
  95. ball.transform.SetParent(transform.parent, true);
  96. m_nextSpawnTime = Time.time + RESPAWN_SECONDS;
  97. return ball;
  98. }
  99. void OnDestroy()
  100. {
  101. foreach (var ball in m_balls)
  102. {
  103. Destroy(ball);
  104. }
  105. }
  106. }
  107. }