202 lines
8.4 KiB
C#
Executable File
202 lines
8.4 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, 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("Character")]
|
|
[SerializeField] Transform m_CharacterRootTransform;
|
|
[SerializeField] Transform m_HeadBoneTransform;
|
|
[SerializeField] Transform m_HeadAimTargetTransform;
|
|
[SerializeField] Transform m_CharacterForwardTransform;
|
|
|
|
[SerializeField, Range(0.0f, 90.0f)] float m_CharacterRotateDeadAngle = 5.0f;
|
|
[SerializeField, Range(0.0f, 90.0f)] float m_CharacterRotateSoftAngle = 90.0f;
|
|
|
|
[SerializeField] float m_CharacterRotateSpeed = 180.0f;
|
|
[SerializeField] float m_CharacterRotateFastSpeed = 720.0f;
|
|
|
|
float m_CharacterTurnVelocity = 0.0f;
|
|
|
|
[Header("Dragging")]
|
|
[SerializeField] Transform m_DragGutStartPosition;
|
|
[SerializeField] PhysicsObjectDragger m_PhysicsDragger;
|
|
[SerializeField] FloatRange m_DragDistanceRange = new FloatRange(1.0f, 5.0f);
|
|
|
|
public float3 LookDirection {
|
|
get {
|
|
float pitchRad = math.radians(-m_Camera.Pitch);
|
|
float yawRad = math.radians(m_Camera.Yaw);
|
|
return new float3(math.sin(yawRad) * math.cos(pitchRad),
|
|
math.sin(pitchRad),
|
|
math.cos(yawRad) * math.cos(pitchRad));
|
|
}
|
|
}
|
|
|
|
public override void OnBeginPlay() {
|
|
m_CameraSpring.Initialize();
|
|
}
|
|
|
|
public override void OnTick(float deltaTime) {
|
|
// Camera Stuff
|
|
m_Camera.Tick();
|
|
|
|
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);
|
|
|
|
Vector3 actorUp = transform.up;
|
|
Vector3 actorRight = transform.right;
|
|
Vector3 actorForward = transform.forward;
|
|
m_CameraSpring.UpdateSpring(Time.deltaTime, actorUp, actorRight, actorForward);
|
|
|
|
// Character rotation
|
|
float3 targetCharacterForward = math.normalize(LookDirection.With(y: 0.0f));
|
|
float3 currentCharacterForward = math.normalize(m_CharacterForwardTransform.forward.With(y: 0.0f));
|
|
|
|
float angleRad = math.acos(math.clamp(math.dot(targetCharacterForward, currentCharacterForward) / (math.length(targetCharacterForward) * math.length(currentCharacterForward)), -1f, 1f));
|
|
float angleDeg = math.degrees(angleRad);
|
|
|
|
bool rotateCharacter = false;
|
|
float rotateCharacterSpeed = m_CharacterRotateSpeed;
|
|
m_CharacterTurnVelocity = 0.0f;
|
|
|
|
if (math.abs(angleDeg) > m_CharacterRotateDeadAngle) {
|
|
if (math.abs(angleDeg) < m_CharacterRotateSoftAngle) {
|
|
rotateCharacter = true;
|
|
} else {
|
|
rotateCharacter = true;
|
|
rotateCharacterSpeed = m_CharacterRotateFastSpeed;
|
|
}
|
|
}
|
|
|
|
float velocityForward = m_Locomotion.Velocity.z;
|
|
|
|
if (!rotateCharacter && math.abs(velocityForward) > 0.01f) {
|
|
rotateCharacter = true;
|
|
}
|
|
|
|
if (rotateCharacter) {
|
|
m_CharacterTurnVelocity = rotateCharacterSpeed * deltaTime;
|
|
|
|
Vector3 newForward = Vector3.RotateTowards(currentCharacterForward,
|
|
targetCharacterForward,
|
|
math.radians(m_CharacterTurnVelocity),
|
|
0.0f);
|
|
m_CharacterForwardTransform.forward = newForward;
|
|
}
|
|
|
|
// Aim Target adjustment
|
|
m_HeadAimTargetTransform.position = (float3)m_HeadBoneTransform.position + LookDirection * 5.0f;
|
|
|
|
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(m_Camera.Yaw, 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();
|
|
}
|
|
|
|
public void PrimaryAction() {
|
|
m_Animator.SetTrigger(AnimatorParamHashes.Attack);
|
|
}
|
|
|
|
public void SecondaryAction() {
|
|
m_Animator.SetTrigger(AnimatorParamHashes.Block);
|
|
}
|
|
|
|
struct AnimatorParamHashes {
|
|
public static readonly int VelocityForwardNormalized = Animator.StringToHash("VelocityForwardNormalized");
|
|
public static readonly int VelocityRightNormalized = Animator.StringToHash("VelocityRightNormalized");
|
|
public static readonly int TurnVelocity = Animator.StringToHash("TurnVelocity");
|
|
public static readonly int IsGrounded = Animator.StringToHash("IsGrounded");
|
|
|
|
public static readonly int Attack = Animator.StringToHash("Attack");
|
|
public static readonly int Block = Animator.StringToHash("Block");
|
|
}
|
|
|
|
void UpdateAnimator() {
|
|
Vector3 localVelocity = m_CharacterForwardTransform.InverseTransformDirection(m_Locomotion.Velocity);
|
|
float forwardNormalized = localVelocity.z / m_Locomotion.runSpeed;
|
|
float rightNormalized = localVelocity.x / m_Locomotion.runSpeed;
|
|
|
|
float turnVelocity = m_CharacterTurnVelocity;
|
|
if (math.abs(forwardNormalized) > 0.01f ||
|
|
math.abs(rightNormalized) > 0.01f ||
|
|
!m_Locomotion.IsGrounded) {
|
|
turnVelocity = 0.0f;
|
|
}
|
|
|
|
m_Animator.SetFloat(AnimatorParamHashes.VelocityForwardNormalized, forwardNormalized);
|
|
m_Animator.SetFloat(AnimatorParamHashes.VelocityRightNormalized, rightNormalized);
|
|
m_Animator.SetFloat(AnimatorParamHashes.TurnVelocity, turnVelocity);
|
|
|
|
m_Animator.SetBool(AnimatorParamHashes.IsGrounded, m_Locomotion.IsGrounded);
|
|
}
|
|
}
|
|
} |