133 lines
5.3 KiB
C#
Executable File
133 lines
5.3 KiB
C#
Executable File
using RebootKit.Engine.Extensions;
|
|
using RebootKit.Engine.Foundation;
|
|
using RebootKit.Engine.Services.Simulation;
|
|
using Unity.Mathematics;
|
|
using UnityEngine;
|
|
using Logger = RebootKit.Engine.Foundation.Logger;
|
|
|
|
namespace RebootReality.jelycho.Player {
|
|
public class PlayerActor : Actor {
|
|
static readonly Logger s_logger = new Logger(nameof(PlayerActor));
|
|
|
|
[SerializeField] Animator m_Animator;
|
|
|
|
[Header("Movement")]
|
|
[SerializeField] PlayerFPPLocomotion m_Locomotion;
|
|
|
|
[Header("Camera")]
|
|
[SerializeField] FPPCamera m_Camera;
|
|
[SerializeField] CameraSpring m_CameraSpring;
|
|
[SerializeField] Transform m_HeadBoneTransform;
|
|
|
|
[SerializeField, Range(0.0f, 1.0f), Tooltip("Percentage of run speed")]
|
|
float m_EnableCameraBobbingPercentThreshold = 0.5f;
|
|
|
|
[SerializeField] float m_SprintCameraBobbing = 1.0f;
|
|
[SerializeField] float m_RunCameraBobbing = 0.5f;
|
|
[SerializeField] float m_IdleCameraBobbing = 0.0f;
|
|
[SerializeField] float m_CameraBobbingTransitionSpeed = 5.0f;
|
|
|
|
float m_TargetCameraBobbing = 0.0f;
|
|
float m_CurrentCameraBobbing = 0.0f;
|
|
|
|
[Header("Dragging")]
|
|
[SerializeField] Transform m_DragGutStartPosition;
|
|
[SerializeField] PhysicsObjectDragger m_PhysicsDragger;
|
|
[SerializeField] FloatRange m_DragDistanceRange = new FloatRange(1.0f, 5.0f);
|
|
[SerializeField] LineRenderer m_LineRenderer;
|
|
|
|
public override void OnBeginPlay() {
|
|
m_CameraSpring.Initialize();
|
|
}
|
|
|
|
public override void OnTick() {
|
|
m_Camera.Tick();
|
|
|
|
m_Locomotion.YawRotation = m_Camera.Yaw;
|
|
|
|
// Camera bobbing
|
|
if (m_Locomotion.IsGrounded &&
|
|
m_Locomotion.SpeedXZ >= m_Locomotion.runSpeed * m_EnableCameraBobbingPercentThreshold) {
|
|
if (m_Locomotion.IsSprinting) {
|
|
m_TargetCameraBobbing = m_SprintCameraBobbing;
|
|
} else {
|
|
m_TargetCameraBobbing = m_RunCameraBobbing;
|
|
}
|
|
} else {
|
|
m_TargetCameraBobbing = m_IdleCameraBobbing;
|
|
}
|
|
|
|
m_CurrentCameraBobbing = Mathf.MoveTowards(m_CurrentCameraBobbing,
|
|
m_TargetCameraBobbing,
|
|
m_CameraBobbingTransitionSpeed * Time.deltaTime);
|
|
m_Camera.SetBobbing(m_CurrentCameraBobbing);
|
|
|
|
m_PhysicsDragger.TargetWorldPosition = m_Camera.Camera.transform.position +
|
|
m_Camera.Camera.transform.forward * 2.0f;
|
|
|
|
if (m_PhysicsDragger.Current.OrNull() is not null) {
|
|
m_LineRenderer.enabled = true;
|
|
m_LineRenderer.SetPosition(0, m_LineRenderer.transform.InverseTransformPoint(m_DragGutStartPosition.position));
|
|
m_LineRenderer.SetPosition(1, m_LineRenderer.transform.InverseTransformPoint(m_PhysicsDragger.Current.position));
|
|
} else {
|
|
m_LineRenderer.enabled = false;
|
|
}
|
|
|
|
Vector3 actorUp = transform.up;
|
|
Vector3 actorRight = transform.right;
|
|
Vector3 actorForward = transform.forward;
|
|
m_CameraSpring.UpdateSpring(Time.deltaTime, actorUp, actorRight, actorForward);
|
|
|
|
UpdateAnimator();
|
|
}
|
|
|
|
public void WarpTo(float3 position) {
|
|
m_Locomotion.WarpTo(position);
|
|
}
|
|
|
|
public void SetSprint(bool isSprinting) {
|
|
m_Locomotion.SetSprint(isSprinting);
|
|
}
|
|
|
|
public void Jump() {
|
|
m_Locomotion.Jump();
|
|
}
|
|
|
|
public void Look(Vector2 input) {
|
|
m_Camera.Rotate(input.x, input.y);
|
|
}
|
|
|
|
public void SetMoveInput(Vector2 input) {
|
|
float3 direction = Quaternion.AngleAxis(transform.eulerAngles.y, Vector3.up) *
|
|
new float3(input.x, 0.0f, input.y);
|
|
m_Locomotion.SetWishDirection(direction);
|
|
}
|
|
|
|
public void StartDrag() {
|
|
GameObject pickedGameObject = m_Camera.Sensor.Sense();
|
|
if (pickedGameObject != null && pickedGameObject.TryGetComponent(out Rigidbody rigidbody)) {
|
|
m_PhysicsDragger.Grab(rigidbody);
|
|
}
|
|
}
|
|
|
|
public void StopDrag() {
|
|
m_PhysicsDragger.Drop();
|
|
}
|
|
|
|
struct AnimatorParamHashes {
|
|
public static readonly int s_VelocityForwardNormalized = Animator.StringToHash("VelocityForwardNormalized");
|
|
public static readonly int s_VelocityRightNormalized = Animator.StringToHash("VelocityRightNormalized");
|
|
public static readonly int s_IsGrounded = Animator.StringToHash("IsGrounded");
|
|
}
|
|
|
|
void UpdateAnimator() {
|
|
Vector3 localVelocity = m_Locomotion.LocalVelocity;
|
|
float forwardNormalized = localVelocity.z / m_Locomotion.runSpeed;
|
|
|
|
m_Animator.SetFloat(AnimatorParamHashes.s_VelocityForwardNormalized, forwardNormalized);
|
|
m_Animator.SetFloat(AnimatorParamHashes.s_VelocityRightNormalized, 0.0f);
|
|
|
|
m_Animator.SetBool(AnimatorParamHashes.s_IsGrounded, m_Locomotion.IsGrounded);
|
|
}
|
|
}
|
|
} |