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