Files
2026-02-21 16:45:37 +08:00

116 lines
2.6 KiB
C#

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace Oculus.Platform.Samples.VrHoops
{
public abstract class Player : MonoBehaviour
{
public const uint MAX_BALLS = 6u;
private const float INITIAL_FORCE = 870f;
private const float RESPAWN_SECONDS = 2f;
private uint m_score;
private Text m_scoreUI;
private GameObject m_ballPrefab;
private BallEjector m_ballEjector;
private Queue<GameObject> m_balls = new Queue<GameObject>();
private GameObject m_heldBall;
private float m_nextSpawnTime;
public virtual uint Score
{
get
{
return m_score;
}
set
{
m_score = value;
if ((bool)m_scoreUI)
{
m_scoreUI.text = m_score.ToString();
}
}
}
public GameObject BallPrefab
{
set
{
m_ballPrefab = value;
}
}
protected bool HasBall
{
get
{
return m_heldBall != null;
}
}
private void Start()
{
m_ballEjector = base.transform.GetComponentInChildren<BallEjector>();
m_scoreUI = base.transform.parent.GetComponentInChildren<Text>();
m_scoreUI.text = "0";
}
public GameObject CreateBall()
{
if ((long)m_balls.Count >= 6L)
{
Object.Destroy(m_balls.Dequeue());
}
GameObject gameObject = Object.Instantiate(m_ballPrefab);
m_balls.Enqueue(gameObject);
gameObject.transform.position = m_ballEjector.transform.position;
gameObject.transform.SetParent(m_ballEjector.transform, true);
gameObject.GetComponent<Rigidbody>().useGravity = false;
gameObject.GetComponent<Rigidbody>().detectCollisions = false;
gameObject.GetComponent<DetectBasket>().Player = this;
return gameObject;
}
protected GameObject CheckSpawnBall()
{
PlatformManager.State currentState = PlatformManager.CurrentState;
if ((currentState == PlatformManager.State.WAITING_TO_PRACTICE_OR_MATCHMAKE || currentState == PlatformManager.State.PLAYING_A_LOCAL_MATCH || currentState == PlatformManager.State.PLAYING_A_NETWORKED_MATCH) && Time.time >= m_nextSpawnTime && !HasBall)
{
m_heldBall = CreateBall();
return m_heldBall;
}
return null;
}
protected GameObject ShootBall()
{
GameObject heldBall = m_heldBall;
m_heldBall = null;
heldBall.GetComponent<Rigidbody>().useGravity = true;
heldBall.GetComponent<Rigidbody>().detectCollisions = true;
heldBall.GetComponent<Rigidbody>().AddForce(m_ballEjector.transform.forward * 870f, ForceMode.Acceleration);
heldBall.transform.SetParent(base.transform.parent, true);
m_nextSpawnTime = Time.time + 2f;
return heldBall;
}
private void OnDestroy()
{
foreach (GameObject ball in m_balls)
{
Object.Destroy(ball);
}
}
}
}