| 1 | namespace Oculus.Platform.Samples.VrHoops |
| 2 | { |
| 3 | using UnityEngine; |
| 4 | using UnityEngine.UI; |
| 5 | using Oculus.Platform.Models; |
| 6 | |
| 7 | public class PlayerArea : MonoBehaviour |
| 8 | { |
| 9 | // the prefab for the ball that players will shoot |
| 10 | [SerializeField] private GameObject m_ballPrefab = null; |
| 11 | |
| 12 | // cached gameobject that where the player camera will move to |
| 13 | private GameObject m_playerHead; |
| 14 | |
| 15 | // cached Text component where we'll render the player's name |
| 16 | private Text m_nameText; |
| 17 | |
| 18 | // cached component used to align the backboard movement between devices |
| 19 | private P2PNetworkGoal m_p2pGoal; |
| 20 | |
| 21 | public Player Player |
| 22 | { |
| 23 | get { return m_playerHead.GetComponent<Player>(); } |
| 24 | } |
| 25 | |
| 26 | public Text NameText |
| 27 | { |
| 28 | get { return m_nameText; } |
| 29 | } |
| 30 | |
| 31 | void Awake() |
| 32 | { |
| 33 | m_playerHead = gameObject.transform.Find("Player Head").gameObject; |
| 34 | m_nameText = gameObject.GetComponentsInChildren<Text>()[1]; |
| 35 | m_p2pGoal = gameObject.GetComponentInChildren<P2PNetworkGoal> (); |
| 36 | } |
| 37 | |
| 38 | public T SetupForPlayer<T>(string name) where T : Player |
| 39 | { |
| 40 | var oldplayer = m_playerHead.GetComponent<Player>(); |
| 41 | if (oldplayer) Destroy(oldplayer); |
| 42 | |
| 43 | var player = m_playerHead.AddComponent<T>(); |
| 44 | player.BallPrefab = m_ballPrefab; |
| 45 | m_nameText.text = name; |
| 46 | |
| 47 | if (player is RemotePlayer) |
| 48 | { |
| 49 | (player as RemotePlayer).Goal = m_p2pGoal; |
| 50 | m_p2pGoal.SendUpdates = false; |
| 51 | } |
| 52 | else if (player is LocalPlayer) |
| 53 | { |
| 54 | m_p2pGoal.SendUpdates = true; |
| 55 | } |
| 56 | else |
| 57 | { |
| 58 | m_p2pGoal.SendUpdates = false; |
| 59 | } |
| 60 | |
| 61 | return player; |
| 62 | } |
| 63 | } |
| 64 | } |