fgm24/Assets/Scripts/Player/PlayerMovement.cs

99 lines
2.9 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-04 04:56:01 +01:00
using UnityEngine.InputSystem.DualShock;
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
{
2024-02-03 21:38:42 +01:00
public PlayerAnimationHandler animationHandler;
2024-02-02 22:43:39 +01:00
public float moveSpeed = 5f;
private Rigidbody2D rb;
private bool right = false;
2024-02-03 14:07:03 +01:00
[Header("Whipping")]
[SerializeField]
RopeWhipAttack whipAttack;
2024-02-04 09:52:47 +01:00
public float whipSmashSpeed = 2f;
public float whipSmashDamageMult = 2f;
2024-02-03 14:07:03 +01:00
[SerializeField]
private float whipMoveSpeed = 25f;
[SerializeField]
private float maxWhipMoveSpeed = 30f;
2024-02-03 16:21:32 +01:00
private PlayerInput playerInput;
2024-02-04 02:58:23 +01:00
private HealthComponent hp;
2024-02-04 09:52:47 +01:00
private PlayerCollideAttack attack;
2024-02-04 02:58:23 +01:00
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-04 02:58:23 +01:00
hp = GetComponent<HealthComponent>();
2024-02-04 10:31:50 +01:00
attack = GetComponent<PlayerCollideAttack>();
2024-02-02 22:43:39 +01:00
2024-02-03 21:35:06 +01:00
if (gameObject.name == "Player2")
{
playerInput.useArrowKeys = true;
}
2024-02-04 04:56:01 +01:00
if (playerInput.controller != null)
{
var pad = (DualShockGamepad)Gamepad.all.ElementAtOrDefault(playerInput.PlayerNum);
if (pad == null) return;
if (playerInput.PlayerNum == 1)
pad.SetLightBarColor(Color.red);
}
2024-02-02 22:43:39 +01:00
}
2024-02-04 06:27:28 +01:00
private void Update()
{
if (playerInput.movement != Vector2.zero)
animationHandler.Run(true);
else
animationHandler.Run(false);
}
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
}
2024-02-04 09:12:58 +01:00
void OnCollisionStay2D(Collision2D collision)
{
if (collision.collider.gameObject.CompareTag("Enemy"))
{ // Other object is an enemy
2024-02-04 09:52:47 +01:00
if (whipAttack.IsBeingWhipped && rb.velocity.magnitude > attack.speedYouNeedToNotTakeDamageFromRammingOrSomtmh) {
// collision.collider.gameObject.GetComponent<HealthComponent>().TakeDamage(rb.velocity.magnitude * whipSmashDamageMult);
} else {
hp.TakeDamage(1f);
}
2024-02-04 09:12:58 +01:00
}
}
2024-02-02 22:43:39 +01:00
}