refactor
This commit is contained in:
81
Assets/jelycho/Code/Player/PlayerActor.cs
Normal file
81
Assets/jelycho/Code/Player/PlayerActor.cs
Normal file
@@ -0,0 +1,81 @@
|
||||
using RebootKit.Engine.Extensions;
|
||||
using RebootKit.Engine.Foundation;
|
||||
using RebootKit.Engine.Services.Simulation;
|
||||
using RebootKit.FPPKit;
|
||||
using Unity.Mathematics;
|
||||
using UnityEngine;
|
||||
using Logger = RebootKit.Engine.Foundation.Logger;
|
||||
|
||||
namespace RealityReboot.jelycho.Player {
|
||||
public class PlayerActor : Actor {
|
||||
static readonly Logger s_logger = new(nameof(PlayerActor));
|
||||
|
||||
[SerializeField] PlayerFPPLocomotion m_Locomotion;
|
||||
[SerializeField] FPPCamera m_Camera;
|
||||
[SerializeField] Animator m_Animator;
|
||||
|
||||
[SerializeField] Transform m_DragGutStartPosition;
|
||||
|
||||
[SerializeField] PhysicsObjectDragger m_PhysicsDragger;
|
||||
[SerializeField] FloatRange m_DragDistanceRange = new(1.0f, 5.0f);
|
||||
|
||||
[SerializeField] LineRenderer m_LineRenderer;
|
||||
|
||||
public override void OnTick() {
|
||||
m_PhysicsDragger.TargetWorldPosition = m_Camera.Camera.transform.position + m_Camera.Camera.transform.forward * 2.0f;
|
||||
|
||||
if (m_PhysicsDragger.Current.OrNull() != 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;
|
||||
}
|
||||
|
||||
UpdateAnimator();
|
||||
}
|
||||
|
||||
public void Jump() {
|
||||
m_Locomotion.Jump();
|
||||
}
|
||||
|
||||
public void Look(Vector2 input) {
|
||||
m_Camera.Rotate(input.x, input.y);
|
||||
}
|
||||
|
||||
public void MoveRight(float input) {
|
||||
float3 direction = Quaternion.AngleAxis(m_Camera.Yaw, Vector3.up) * Vector3.right;
|
||||
m_Locomotion.AddMovementInput(direction, input);
|
||||
}
|
||||
|
||||
public void MoveForward(float input) {
|
||||
float3 direction = Quaternion.AngleAxis(m_Camera.Yaw, Vector3.up) * Vector3.forward;
|
||||
m_Locomotion.AddMovementInput(direction, input);
|
||||
}
|
||||
|
||||
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() {
|
||||
float forwardNormalized = m_Locomotion.LocalVelocity.z / m_Locomotion.maxMovementSpeed;
|
||||
|
||||
m_Animator.SetFloat(AnimatorParamHashes.s_VelocityForwardNormalized, forwardNormalized);
|
||||
m_Animator.SetFloat(AnimatorParamHashes.s_VelocityRightNormalized, 0.0f);
|
||||
m_Animator.SetBool(AnimatorParamHashes.s_IsGrounded, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/jelycho/Code/Player/PlayerActor.cs.meta
Normal file
3
Assets/jelycho/Code/Player/PlayerActor.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6f6d4b18db6741e9a8b71255b18714ac
|
||||
timeCreated: 1744662033
|
||||
@@ -3,6 +3,7 @@ using System.Threading;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using RebootKit.Engine;
|
||||
using RebootKit.Engine.Foundation;
|
||||
using RebootKit.Engine.Main;
|
||||
using RebootKit.Engine.Services.Input;
|
||||
using RebootKit.FPPKit;
|
||||
using UnityEngine;
|
||||
|
||||
68
Assets/jelycho/Code/Player/PlayerFPPLocomotion.cs
Normal file
68
Assets/jelycho/Code/Player/PlayerFPPLocomotion.cs
Normal file
@@ -0,0 +1,68 @@
|
||||
using System;
|
||||
using RebootKit.Engine.Extensions;
|
||||
using Unity.Mathematics;
|
||||
using UnityEngine;
|
||||
|
||||
namespace RealityReboot.jelycho.Player {
|
||||
[RequireComponent(typeof(Rigidbody), typeof(CapsuleCollider))]
|
||||
public class PlayerFPPLocomotion : MonoBehaviour {
|
||||
[SerializeField] Rigidbody m_Rigidbody;
|
||||
|
||||
public float acceleration = 10.0f;
|
||||
public float accelerationBackwards = 2.0f;
|
||||
public float accelerationStrafe = 2.0f;
|
||||
public float maxMovementSpeed = 8.0f;
|
||||
public float maxMovementSpeedBackwards = 2.0f;
|
||||
public float maxStrafeSpeed = 2.0f;
|
||||
|
||||
public float3 Velocity => m_Rigidbody.linearVelocity;
|
||||
public float3 LocalVelocity => m_Rigidbody.transform.InverseTransformDirection(m_Rigidbody.linearVelocity);
|
||||
|
||||
float3 m_PendingInputValue;
|
||||
bool m_IsJumpRequested;
|
||||
|
||||
void Awake() {
|
||||
}
|
||||
|
||||
void FixedUpdate() {
|
||||
if (m_PendingInputValue.IsZero()) {
|
||||
return;
|
||||
}
|
||||
|
||||
float3 localVelocity = LocalVelocity;
|
||||
|
||||
m_PendingInputValue = math.normalize(m_PendingInputValue);
|
||||
float3 localInput = m_Rigidbody.transform.InverseTransformDirection(m_PendingInputValue);
|
||||
|
||||
float3 velocityToAdd = float3.zero;
|
||||
if (localInput.z > 0.0f) {
|
||||
if (localVelocity.z < maxMovementSpeed) {
|
||||
velocityToAdd.z += localInput.z * acceleration * Time.fixedDeltaTime;
|
||||
}
|
||||
} else {
|
||||
velocityToAdd.z += localInput.z * accelerationBackwards * Time.fixedDeltaTime;
|
||||
}
|
||||
|
||||
velocityToAdd.x += localInput.x * accelerationStrafe * Time.fixedDeltaTime;
|
||||
|
||||
float3 velocityToAddWorldSpace = m_Rigidbody.transform.TransformDirection(velocityToAdd);
|
||||
m_Rigidbody.AddForce(velocityToAddWorldSpace, ForceMode.VelocityChange);
|
||||
|
||||
m_PendingInputValue = float3.zero;
|
||||
}
|
||||
|
||||
void OnValidate() {
|
||||
if (m_Rigidbody == null) {
|
||||
m_Rigidbody = GetComponent<Rigidbody>();
|
||||
}
|
||||
}
|
||||
|
||||
public void AddMovementInput(float3 input, float scale) {
|
||||
m_PendingInputValue += input * scale;
|
||||
}
|
||||
|
||||
public void Jump() {
|
||||
m_IsJumpRequested = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/jelycho/Code/Player/PlayerFPPLocomotion.cs.meta
Normal file
3
Assets/jelycho/Code/Player/PlayerFPPLocomotion.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ee4e0741b71b4e0e81f8109748ea961d
|
||||
timeCreated: 1746663547
|
||||
Reference in New Issue
Block a user