summaryrefslogtreecommitdiff
path: root/Thronefall_v1.0/Decompile/PlayerMovement.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Thronefall_v1.0/Decompile/PlayerMovement.cs')
-rw-r--r--Thronefall_v1.0/Decompile/PlayerMovement.cs152
1 files changed, 152 insertions, 0 deletions
diff --git a/Thronefall_v1.0/Decompile/PlayerMovement.cs b/Thronefall_v1.0/Decompile/PlayerMovement.cs
new file mode 100644
index 0000000..db7c18a
--- /dev/null
+++ b/Thronefall_v1.0/Decompile/PlayerMovement.cs
@@ -0,0 +1,152 @@
+using System.Collections;
+using Pathfinding.RVO;
+using Rewired;
+using UnityEngine;
+
+[RequireComponent(typeof(CharacterController))]
+[RequireComponent(typeof(RVOController))]
+public class PlayerMovement : MonoBehaviour
+{
+ public float speed = 4f;
+
+ public float sprintSpeed = 12f;
+
+ public Transform meshParent;
+
+ public float maxMeshRotationSpeed = 360f;
+
+ public Animator meshAnimator;
+
+ private Hp hp;
+
+ private Transform viewTransform;
+
+ private CharacterController controller;
+
+ private Player input;
+
+ private Quaternion desiredMeshRotation;
+
+ private RVOController rvoController;
+
+ private float yVelocity;
+
+ private bool heavyArmorEquipped;
+
+ private bool racingHorseEquipped;
+
+ private bool moving;
+
+ private bool sprinting;
+
+ private bool sprintingToggledOn;
+
+ public static PlayerMovement instance;
+
+ [SerializeField]
+ private Equippable heavyArmorPerk;
+
+ [SerializeField]
+ private Equippable warHorsePerk;
+
+ private Vector3 velocity;
+
+ public bool Moving => moving;
+
+ public bool Sprinting => sprinting;
+
+ public Vector3 Velocity => velocity;
+
+ private void Awake()
+ {
+ instance = this;
+ }
+
+ public void TeleportTo(Vector3 _position)
+ {
+ controller.enabled = false;
+ controller.transform.position = _position;
+ StartCoroutine(EnableControllerNextFrame(controller));
+ }
+
+ private void Start()
+ {
+ PlayerManager.RegisterPlayer(this);
+ viewTransform = Camera.main.transform;
+ controller = GetComponent<CharacterController>();
+ input = ReInput.players.GetPlayer(0);
+ rvoController = GetComponent<RVOController>();
+ hp = GetComponent<Hp>();
+ heavyArmorEquipped = PerkManager.IsEquipped(heavyArmorPerk);
+ racingHorseEquipped = PerkManager.IsEquipped(warHorsePerk);
+ }
+
+ private void Update()
+ {
+ Vector2 vector = new Vector2(input.GetAxis("Move Vertical"), input.GetAxis("Move Horizontal"));
+ if (LocalGamestate.Instance.PlayerFrozen)
+ {
+ vector = Vector2.zero;
+ }
+ Vector3 normalized = Vector3.ProjectOnPlane(viewTransform.forward, Vector3.up).normalized;
+ Vector3 normalized2 = Vector3.ProjectOnPlane(viewTransform.right, Vector3.up).normalized;
+ velocity = Vector3.zero;
+ velocity += normalized * vector.x;
+ velocity += normalized2 * vector.y;
+ velocity = Vector3.ClampMagnitude(velocity, 1f);
+ if (input.GetButtonDown("Sprint Toggle"))
+ {
+ sprintingToggledOn = !sprintingToggledOn;
+ }
+ if (sprintingToggledOn && input.GetButton("Sprint"))
+ {
+ sprintingToggledOn = false;
+ }
+ sprinting = (input.GetButton("Sprint") || sprintingToggledOn) && hp.HpPercentage >= 1f;
+ velocity *= (sprinting ? sprintSpeed : speed);
+ if (heavyArmorEquipped && DayNightCycle.Instance.CurrentTimestate == DayNightCycle.Timestate.Night)
+ {
+ velocity *= PerkManager.instance.heavyArmor_SpeedMultiplyer;
+ }
+ if (racingHorseEquipped)
+ {
+ velocity *= PerkManager.instance.racingHorse_SpeedMultiplyer;
+ }
+ rvoController.velocity = velocity;
+ moving = velocity.sqrMagnitude > 0.1f;
+ if (moving)
+ {
+ desiredMeshRotation = Quaternion.LookRotation(velocity.normalized, Vector3.up);
+ }
+ if (desiredMeshRotation != meshParent.rotation)
+ {
+ meshParent.rotation = Quaternion.RotateTowards(meshParent.rotation, desiredMeshRotation, maxMeshRotationSpeed * Time.deltaTime);
+ }
+ meshAnimator.SetBool("Moving", moving);
+ meshAnimator.SetBool("Sprinting", sprinting);
+ if (controller.enabled)
+ {
+ if (controller.isGrounded)
+ {
+ yVelocity = 0f;
+ }
+ else
+ {
+ yVelocity += -9.81f * Time.deltaTime;
+ }
+ velocity += Vector3.up * yVelocity;
+ controller.Move(velocity * Time.deltaTime);
+ }
+ }
+
+ private void OnDisable()
+ {
+ meshAnimator.SetBool("Moving", value: false);
+ }
+
+ private IEnumerator EnableControllerNextFrame(CharacterController controller)
+ {
+ yield return new WaitForEndOfFrame();
+ controller.enabled = true;
+ }
+}