fgm24/Assets/Scripts/Player/PlayerMovement.cs

97 lines
2.6 KiB
C#
Raw Normal View History

2024-02-02 22:43:39 +01:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
2024-02-03 15:21:45 +01:00
using System.Linq;
2024-02-02 22:43:39 +01:00
2024-02-03 16:21:32 +01:00
[RequireComponent(typeof(PlayerInput))]
2024-02-03 14:07:03 +01:00
public class PlayerMovement : MonoBehaviour
2024-02-02 22:43:39 +01:00
{
public float moveSpeed = 5f;
private Rigidbody2D rb;
private bool right = false;
private bool vibrate = false;
[SerializeField] private float stepCooldown = 0.05f;
[SerializeField] private float stepVibrationTime = 0.05f;
[SerializeField] private GameObject rumble;
2024-02-03 14:07:03 +01:00
[Header("Whipping")]
[SerializeField]
RopeWhipAttack whipAttack;
[SerializeField]
private float whipMoveSpeed = 25f;
[SerializeField]
private float maxWhipMoveSpeed = 30f;
2024-02-03 16:21:32 +01:00
private PlayerInput playerInput;
2024-02-02 22:43:39 +01:00
private void Start()
{
rb = GetComponent<Rigidbody2D>();
2024-02-03 16:21:32 +01:00
playerInput = GetComponent<PlayerInput>();
2024-02-02 22:43:39 +01:00
StartCoroutine(ToggleWithDelay());
}
void Update()
{
2024-02-03 16:21:32 +01:00
if (playerInput.movement != Vector2.zero)
2024-02-03 15:45:11 +01:00
{
2024-02-02 22:43:39 +01:00
RumbleWalk();
2024-02-03 15:45:11 +01:00
GetComponent<PlayerAnimationHandler>().Run();
}
2024-02-02 22:43:39 +01:00
}
private void FixedUpdate()
{
2024-02-03 14:07:03 +01:00
if (whipAttack.IsBeingWhipped)
{
2024-02-03 15:03:38 +01:00
//float sign = Vector2.Dot((whipAttack.joint.position - whipAttack.otherPlayerAttack.joint.position).normalized, movement.normalized);
2024-02-03 14:07:03 +01:00
Vector2 ropeDir = whipAttack.otherPlayerAttack.joint.position - whipAttack.joint.position;
Vector2 tangent = new Vector2(-ropeDir.y, ropeDir.x).normalized;
2024-02-03 15:03:38 +01:00
2024-02-03 16:21:32 +01:00
rb.AddForce(Vector2.Dot(playerInput.movement, tangent) * tangent * whipMoveSpeed);
2024-02-03 14:07:03 +01:00
rb.velocity = Vector2.ClampMagnitude(rb.velocity, maxWhipMoveSpeed);
}
else if (whipAttack.IsWhippingOtherPlayer)
{
2024-02-03 16:21:32 +01:00
playerInput.movement = Vector2.zero;
2024-02-03 14:07:03 +01:00
}
else
{
2024-02-03 16:21:32 +01:00
rb.AddForce(playerInput.movement * moveSpeed);
2024-02-03 14:07:03 +01:00
}
2024-02-02 22:43:39 +01:00
}
private void RumbleWalk()
{
2024-02-03 16:21:32 +01:00
if (vibrate && playerInput.controller != null)
2024-02-02 22:43:39 +01:00
{
if (right)
{
2024-02-03 16:21:32 +01:00
rumble.GetComponent<RumbleManager>().RumblePulse(0.0f, 0.004f, stepVibrationTime, playerInput.PlayerNum);
2024-02-02 22:43:39 +01:00
right = false;
}
else if (!right)
{
2024-02-03 16:21:32 +01:00
rumble.GetComponent<RumbleManager>().RumblePulse(0.004f, 0.0f, stepVibrationTime, playerInput.PlayerNum);
2024-02-02 22:43:39 +01:00
right = true;
}
vibrate = false;
}
}
private IEnumerator ToggleWithDelay()
{
while (true)
{
vibrate = !vibrate;
yield return new WaitForSeconds(stepCooldown);
}
}
}