181 lines
5.5 KiB
C#
181 lines
5.5 KiB
C#
|
using Photon.Pun;
|
||
|
using Photon.Pun.Demo.PunBasics;
|
||
|
using UnityEngine;
|
||
|
|
||
|
namespace cc.ivanli.pun01
|
||
|
{
|
||
|
public class PlayerManager : MonoBehaviourPunCallbacks, IPunObservable
|
||
|
{
|
||
|
[Tooltip("The Beams GameObject to control")]
|
||
|
[SerializeField]
|
||
|
private GameObject beams;
|
||
|
//True, when the user is firing
|
||
|
private bool IsFiring;
|
||
|
|
||
|
[Tooltip("The current Health of our player")]
|
||
|
public float Health = 1f;
|
||
|
|
||
|
[Tooltip("The local player instance. Use this to know if the local player is represented in the Scene")]
|
||
|
public static GameObject LocalPlayerInstance;
|
||
|
|
||
|
[Tooltip("The Player's UI GameObject Prefab")]
|
||
|
[SerializeField]
|
||
|
public GameObject PlayerUiPrefab;
|
||
|
|
||
|
void Awake()
|
||
|
{
|
||
|
if (beams == null)
|
||
|
{
|
||
|
Debug.LogError("<Color=Red><a>Missing</a></Color> Beams Reference.", this);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
beams.SetActive(false);
|
||
|
}
|
||
|
|
||
|
if (photonView.IsMine)
|
||
|
{
|
||
|
PlayerManager.LocalPlayerInstance = this.gameObject;
|
||
|
}
|
||
|
DontDestroyOnLoad(this.gameObject);
|
||
|
}
|
||
|
|
||
|
void Start()
|
||
|
{
|
||
|
CameraWork _cameraWork = this.gameObject.GetComponent<CameraWork>();
|
||
|
UnityEngine.SceneManagement.SceneManager.sceneLoaded += OnSceneLoaded;
|
||
|
|
||
|
|
||
|
if (_cameraWork != null)
|
||
|
{
|
||
|
if (photonView.IsMine)
|
||
|
{
|
||
|
_cameraWork.OnStartFollowing();
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
Debug.LogError("<Color=Red><a>Missing</a></Color> CameraWork Component on playerPrefab.", this);
|
||
|
}
|
||
|
|
||
|
if (PlayerUiPrefab != null)
|
||
|
{
|
||
|
GameObject _uiGo = Instantiate(PlayerUiPrefab);
|
||
|
_uiGo.SendMessage("SetTarget", this, SendMessageOptions.RequireReceiver);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
Debug.LogWarning("<Color=Red><a>Missing</a></Color> PlayerUiPrefab reference on player Prefab.", this);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void Update()
|
||
|
{
|
||
|
|
||
|
if (photonView.IsMine)
|
||
|
{
|
||
|
ProcessInputs();
|
||
|
if (Health <= 0f)
|
||
|
{
|
||
|
GameManager.Instance.LeaveRoom();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// trigger Beams active state
|
||
|
if (beams != null && IsFiring != beams.activeInHierarchy)
|
||
|
{
|
||
|
beams.SetActive(IsFiring);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void ProcessInputs()
|
||
|
{
|
||
|
if (Input.GetButtonDown("Fire1"))
|
||
|
{
|
||
|
if (!IsFiring)
|
||
|
{
|
||
|
IsFiring = true;
|
||
|
}
|
||
|
}
|
||
|
if (Input.GetButtonUp("Fire1"))
|
||
|
{
|
||
|
if (IsFiring)
|
||
|
{
|
||
|
IsFiring = false;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void OnTriggerEnter(Collider other)
|
||
|
{
|
||
|
if (!photonView.IsMine)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
// We are only interested in Beamers
|
||
|
// we should be using tags but for the sake of distribution, let's simply check by name.
|
||
|
if (!other.name.Contains("Beam"))
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
Health -= 0.1f;
|
||
|
}
|
||
|
|
||
|
void OnTriggerStay(Collider other)
|
||
|
{
|
||
|
// we dont' do anything if we are not the local player.
|
||
|
if (!photonView.IsMine)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
// We are only interested in Beamers
|
||
|
// we should be using tags but for the sake of distribution, let's simply check by name.
|
||
|
if (!other.name.Contains("Beam"))
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
// we slowly affect health when beam is constantly hitting us, so player has to move to prevent death.
|
||
|
Health -= 0.1f * Time.deltaTime;
|
||
|
}
|
||
|
|
||
|
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
|
||
|
{
|
||
|
if (stream.IsWriting)
|
||
|
{
|
||
|
// We own this player: send the others our data
|
||
|
stream.SendNext(IsFiring);
|
||
|
stream.SendNext(Health);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
// Network player, receive data
|
||
|
IsFiring = (bool)stream.ReceiveNext();
|
||
|
Health = (float)stream.ReceiveNext();
|
||
|
}
|
||
|
}
|
||
|
void OnSceneLoaded(UnityEngine.SceneManagement.Scene scene, UnityEngine.SceneManagement.LoadSceneMode loadingMode)
|
||
|
{
|
||
|
this.CalledOnLevelWasLoaded(scene.buildIndex);
|
||
|
}
|
||
|
void OnLevelWasLoaded(int level)
|
||
|
{
|
||
|
this.CalledOnLevelWasLoaded(level);
|
||
|
}
|
||
|
void CalledOnLevelWasLoaded(int level)
|
||
|
{
|
||
|
// check if we are outside the Arena and if it's the case, spawn around the center of the arena in a safe zone
|
||
|
if (!Physics.Raycast(transform.position, -Vector3.up, 5f))
|
||
|
{
|
||
|
transform.position = new Vector3(0f, 5f, 0f);
|
||
|
}
|
||
|
GameObject _uiGo = Instantiate(PlayerUiPrefab);
|
||
|
_uiGo.SendMessage("SetTarget", this, SendMessageOptions.RequireReceiver);
|
||
|
}
|
||
|
public override void OnDisable()
|
||
|
{
|
||
|
// Always call the base to remove callbacks
|
||
|
base.OnDisable();
|
||
|
UnityEngine.SceneManagement.SceneManager.sceneLoaded -= OnSceneLoaded;
|
||
|
}
|
||
|
}
|
||
|
}
|