using UnityEngine; #if hPHOTON2 using Photon.Pun; #endif namespace Passer.Humanoid { using Tracking; /// /// Interface to a \ref HumanoidControl Humanoid on a Site /// /// As humanoids are not present when you design a site, you cannot refer to them directly. /// This component enables you to execute functions on a Humanoid which has entered the Site. /// It attaches to the Humanoid when it has entered the Site /// /// #if hPHOTON2 public partial class HumanoidInterface : MonoBehaviourPunCallbacks, IHumanoidMovement, Pawn.IHumanoidPossessions { #else public partial class HumanoidInterface : MonoBehaviour, IHumanoidMovement, Pawn.IHumanoidPossessions { #endif /// /// The %humanoid in the Site /// protected HumanoidControl humanoid; public Bone attachedBone = Bone.None; private void Awake() { HumanoidControl humanoidInScene = GetComponentInParent(); if (humanoidInScene == null) { HumanoidControl[] humanoids = FindObjectsOfType(); if (humanoids.Length == 1) { this.humanoid = humanoids[0]; AttachToHumanoid(this.humanoid); } } HumanoidControl.onNewHumanoid += HumanoidControl_onNewHumanoid; } private void HumanoidControl_onNewHumanoid(HumanoidControl humanoid) { if (this == null) return; this.humanoid = humanoid; if (humanoid.isRemote == false) AttachToHumanoid(humanoid); //this.transform.SetParent(this.humanoid.transform); } protected void AttachToHumanoid(HumanoidControl humanoid) { if (attachedBone == Bone.None) { this.transform.SetParent(humanoid.transform); this.transform.localPosition = Vector3.zero; //position; this.transform.localRotation = Quaternion.identity; //rotation; } else { HumanoidTarget.TargetedBone targetedBone = humanoid.GetBone(attachedBone); this.transform.SetParent(targetedBone.bone.transform); this.transform.localPosition = Vector3.zero; this.transform.localRotation = Quaternion.identity; } } #if hPHOTON2 [PunRPC] protected virtual void RpcAttachHumanoid(int nwId, int humanoidId) { Debug.Log("Received humanoid attachement for " + nwId + " " + humanoidId); HumanoidControl[] foundHumanoids = FindObjectsOfType(); foreach (HumanoidControl foundHumanoid in foundHumanoids) { if (foundHumanoid == null) continue; if (foundHumanoid.nwId == (ulong)nwId && foundHumanoid.humanoidId == humanoidId) { this.humanoid = foundHumanoid; this.transform.SetParent(foundHumanoid.transform); this.transform.localPosition = Vector3.zero; this.transform.localRotation = Quaternion.identity; return; } } Debug.LogWarning("Could not find humanoid for attachement"); } #endif #region IHumanoidMovement /// /// Lets the Humanoid jump up with the given take off velocity /// /// The vertical velocity to start the jump public void Jump(float takeoffVelocity) { if (humanoid == null) return; humanoid.Jump(takeoffVelocity); } /// Set the rotation angle along the Y axis public void Rotation(float yAngle) { if (humanoid == null) return; humanoid.Rotation(yAngle); } #endregion #region IHumanoidPossessions /// /// Try to add the GameObject to the Possessions of the Humanoid /// /// The GameObject of the Possession to add /// This does nothing if the gamObject is not a Possession public void TryAddToPossessions(GameObject gameObject) { Possessable possession = gameObject.GetComponent(); if (possession == null) return; AddToPossessions(possession); } /// /// Add the Possession to the Possessions of the Humanoid /// /// The Posession to add public void AddToPossessions(Possessable newPossession) { if (humanoid == null) return; VisitorPossessions humanoidPossessions = humanoid.GetComponentInChildren(); if (humanoidPossessions == null) return; humanoidPossessions.Add(newPossession); } #endregion #region Avatar Manager private GameObject originalAvatar; /// /// Change the avatar of the Humanoid. /// /// The new avatar for the Humanoid /// The avatar will be restored to the original avatar when the Humanoid leaves the Site again. public void ChangeAvatar(GameObject avatar) { if (this.humanoid == null || !(this.humanoid is HumanoidControl)) return; originalAvatar = Instantiate(humanoid.avatarRig.gameObject); originalAvatar.SetActive(false); humanoid.ChangeAvatar(avatar); } private void OnDestroy() { if (this.humanoid == null || !(this.humanoid is HumanoidControl)) return; // Do not change the avatar when the application quits // as the humanoid is being destroyed if (!quitting && originalAvatar != null) { originalAvatar.SetActive(true); humanoid.ChangeAvatar(originalAvatar); Destroy(originalAvatar); } } private bool quitting = false; private void OnApplicationQuit() { quitting = true; } #endregion public void LeaveSite() { #if hPHOTON2 photonView.RPC("RpcLeaveSite", RpcTarget.All); #endif } #if hPHOTON2 [PunRPC] private void RpcLeaveSite() { if (humanoid.isRemote) return; SiteNavigator siteNavigator = humanoid.GetComponentInChildren(); siteNavigator.GoBack(); } #endif } }