using UnityEngine; using UHFPS.Tools; using UHFPS.Scriptable; using Newtonsoft.Json.Linq; namespace UHFPS.Runtime { public abstract class PlayerItemBehaviour : MonoBehaviour, ISaveableCustom { private Animator animator; private PlayerManager playerManager; private PlayerStateMachine playerStateMachine; private LookController lookController; private ExamineController examineController; private MotionController motionController; private Vector3 wallHitVel; private Transform motionTransform; private Quaternion defaultMotionRot; private Vector3 defaultMotionPos; public bool EnableWallDetection = true; public bool EnableMotionPreset = true; public bool EnableExternalMotion = true; // wall detection public LayerMask WallHitMask; public float WallHitRayDistance = 0.5f; public float WallHitRayRadius = 0.3f; public float WallHitAmount = 1f; public float WallHitTime = 0.2f; public Vector3 WallHitRayOffset; public bool ShowRayGizmos = true; // item motion public MotionBlender MotionBlender = new(); public MotionPreset MotionPreset; // external motions public ExternalMotions ExternalMotions = new(); /// /// The pivot point of the item object that will be used for the motion preset effects. /// [field: SerializeField] public Transform MotionPivot { get; set; } /// /// The object of the item which will be enabled or disabled, usually a child object. /// [field: SerializeField] public GameObject ItemObject { get; set; } /// /// Animator component of the Item Object. /// public Animator Animator { get { if(animator == null) animator = ItemObject.GetComponentInChildren(); return animator; } } /// /// PlayerManager component. /// public PlayerManager PlayerManager { get { if (playerManager == null) playerManager = GetComponentInParent(); return playerManager; } } /// /// PlayerStateMachine component. /// public PlayerStateMachine PlayerStateMachine { get { if (playerStateMachine == null) playerStateMachine = PlayerManager.GetComponent(); return playerStateMachine; } } /// /// LookController component. /// public LookController LookController { get { if (lookController == null) lookController = PlayerManager.GetComponentInChildren(); return lookController; } } /// /// ExamineController component. /// public ExamineController ExamineController { get { if (examineController == null) examineController = PlayerManager.GetComponentInChildren(); return examineController; } } /// /// MotionController component. /// public MotionController CameraMotions { get { if (motionController == null) motionController = PlayerManager.GetComponentInChildren(); return motionController; } } /// /// PlayerItemsManager component. /// public PlayerItemsManager PlayerItems { get => PlayerManager.PlayerItems; } /// /// Check if the item is interactive. False, for example when the inventory is opened, object is dragged etc. /// public bool CanInteract => PlayerItems.CanInteract; /// /// The name of the item that will be displayed in the list. /// public virtual string Name => "Item"; /// /// Check whether the item can be switched. /// public virtual bool IsBusy() => false; /// /// Check whether the item is equipped. /// public virtual bool IsEquipped() => ItemObject.activeSelf; /// /// Check whether the item can be combined in inventory. /// public virtual bool CanCombine() => false; public virtual void Start() { if (EnableMotionPreset && MotionPreset != null) { motionTransform = MotionPivot != null ? MotionPivot : CameraMotions.HandsMotionTransform; defaultMotionRot = motionTransform.localRotation; defaultMotionPos = motionTransform.localPosition; MotionBlender.Init(MotionPreset, motionTransform, PlayerStateMachine); } if (EnableExternalMotion) ExternalMotions.Init(CameraMotions); } public virtual void OnDestroy() { if (EnableMotionPreset && MotionPreset != null && MotionBlender != null) MotionBlender.Dispose(); } private void Update() { if (IsEquipped()) { if (EnableWallDetection) { Vector3 forward = PlayerItems.transform.forward; Vector3 origin = PlayerItems.transform.TransformPoint(WallHitRayOffset); if (Physics.SphereCast(origin, WallHitRayRadius, forward, out RaycastHit hit, WallHitRayDistance, WallHitMask)) OnItemBlocked(hit.distance, true); else OnItemBlocked(0f, false); } if (EnableMotionPreset && MotionPreset != null && motionTransform != null) { MotionBlender.BlendMotions(Time.deltaTime, out var position, out var rotation); Vector3 newPosition = defaultMotionPos + position; Quaternion newRotation = defaultMotionRot * rotation; motionTransform.SetLocalPositionAndRotation(newPosition, newRotation); } } OnUpdate(); } /// /// Apply an external camera motion effect, such as an wobble or impact. /// public void ApplyEffect(string eventID) { if (!EnableExternalMotion || ExternalMotions == null) return; ExternalMotions.ApplyEffect(eventID); } /// /// Will be called when the ray going from the camera hits the wall to prevent the player item from being clipped. /// public virtual void OnItemBlocked(float hitDistance, bool blocked) { float value = GameTools.Remap(0f, WallHitRayDistance, 0f, 1f, hitDistance); Vector3 backward = Vector3.back * WallHitAmount; Vector3 result = Vector3.Lerp(backward, Vector3.zero, blocked ? value : 1f); transform.localPosition = Vector3.SmoothDamp(transform.localPosition, result, ref wallHitVel, WallHitTime); } /// /// Will be called every frame like the classic Update() function. /// public virtual void OnUpdate() { } /// /// Will be called when a combinable item is combined with this inventory item. /// public virtual void OnItemCombine(InventoryItem combineItem) { } /// /// Will be called when PlayerItemsManager selects an item. /// public abstract void OnItemSelect(); /// /// Will be called when PlayerItemsManager deselects an item. /// public abstract void OnItemDeselect(); /// /// Will be called when PlayerItemsManager activates an item. /// public abstract void OnItemActivate(); /// /// Will be called when PlayerItemsManager deactivates an item. /// public abstract void OnItemDeactivate(); public virtual void OnDrawGizmosSelected() { bool selected = false; #if UNITY_EDITOR selected = UnityEditor.Selection.activeGameObject == gameObject; #endif if (ShowRayGizmos && EnableWallDetection && selected) { Vector3 forward = PlayerItems.transform.forward; Vector3 origin = PlayerItems.transform.TransformPoint(WallHitRayOffset); Vector3 p2 = origin + forward * WallHitRayDistance; Gizmos.color = Color.yellow; GizmosE.DrawWireCapsule(origin, p2, WallHitRayRadius); } } public virtual StorableCollection OnCustomSave() { return new(); } public virtual void OnCustomLoad(JToken data) { } } }