Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.InputSystem;
- using UnityEngine.XR;
- public class Player : MonoBehaviour
- {
- //states
- public PlayerState currentState;
- public PlayerIdleState idleState;
- public PlayerJumpState jumpState;
- public PlayerMoveState moveState;
- public PlayerCrouchState crouchState;
- public PlayerSlideState slideState;
- public PlayerAttackState attackState;
- [Header("Core Components")]
- public Combat combat;
- [Header("Components")]
- public Rigidbody2D rb;
- public Animator anim;
- public CapsuleCollider2D playerCollider;
- [Header("handles inputs")]
- //inputs
- public PlayerInput playerInput;
- public Vector2 moveInput;
- public bool runPressed;
- public bool attackPressed;
- public bool jumpPressed;
- public bool jumpReleased;
- [Header("Movement variables")]
- public float walkSpeed;
- public float runSpeed;
- [Header("Jump variables")]
- public float jumpForce;
- public float jumpCutMultiplier = 0.5f;
- public float normalGravity;
- public float fallGravity;
- public float jumpGravity;
- public int faceDirection = 1;
- [Header("Ground check")]
- public Transform groundCheck;
- public float groundCheckRadius;
- public LayerMask groundLayer;
- public bool isGrounded;
- [Header("Crouch variables")]
- public Transform headCheck;
- public float headCheckRadius = 0.2f;
- [Header("Slide Settings and variables")]
- public float slideDuration = 0.6f;
- public float slideSpeed = 12f;
- public float slideStopDuration = 0.15f;
- public float slideHeight;
- public Vector2 slideOffset;
- public float normalHeight;
- public Vector2 normalOffset;
- [SerializeField] private bool isSliding;
- private void Awake()
- {
- idleState = new PlayerIdleState(this);
- jumpState = new PlayerJumpState(this);
- moveState = new PlayerMoveState(this);
- crouchState = new PlayerCrouchState(this);
- slideState = new PlayerSlideState(this);
- attackState = new PlayerAttackState(this);
- }
- private void Start()
- {
- rb.gravityScale = normalGravity;
- ChangeState(idleState);
- }
- void Update()
- {
- currentState.Update();
- if (!isSliding)
- {
- Flip();
- }
- HandleAnimations();
- }
- void FixedUpdate()
- {
- currentState.FixedUpdate();
- CheckGrounded();
- }
- public void ChangeState(PlayerState newState)
- {
- if (currentState != null)
- {
- currentState.Exit();
- }
- currentState = newState;
- if (currentState != null)
- {
- currentState.Enter();
- }
- }
- public void ApplyVariableGravity()
- {
- if (rb.linearVelocity.y < -0.1f) // falling
- {
- rb.gravityScale = fallGravity;
- }
- else if (rb.linearVelocity.y > 0.1f) //rising
- {
- rb.gravityScale = jumpGravity;
- }
- else
- {
- rb.gravityScale = normalGravity;
- }
- }
- void CheckGrounded()
- {
- isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, groundLayer);
- }
- public bool CheckForCeiling()
- {
- return Physics2D.OverlapCircle(headCheck.position, headCheckRadius, groundLayer);
- }
- void HandleAnimations()
- {
- anim.SetBool("isGrounded", isGrounded);
- anim.SetFloat("yVelocity", rb.linearVelocity.y);
- }
- void Flip()
- {
- if (moveInput.x > 0.1f)
- {
- faceDirection = 1;
- }
- else if (moveInput.x < -0.1f)
- {
- faceDirection = -1;
- }
- transform.localScale = new Vector3(faceDirection, 1, 1);
- }
- public void AttackAnimationFinished()
- {
- currentState.AttackAnimationFinished();
- }
- public void OnMove(InputValue value)
- {
- moveInput = value.Get<Vector2>();
- }
- public void OnRun(InputValue value)
- {
- runPressed = value.isPressed;
- }
- public void OnAttack(InputValue value)
- {
- if (value.isPressed)
- {
- attackPressed = value.isPressed;
- }
- }
- public void OnJump(InputValue value)
- {
- if (value.isPressed)
- {
- jumpPressed = true;
- jumpReleased = false;
- }
- else
- {
- jumpReleased = true;
- }
- }
- public void SetColliderNormal()
- {
- if (playerCollider == null) playerCollider = GetComponent<CapsuleCollider2D>();
- playerCollider.size = new Vector2(playerCollider.size.x, normalHeight);
- playerCollider.offset = normalOffset;
- }
- public void SetColliderSlide()
- {
- if (playerCollider == null) playerCollider = GetComponent<CapsuleCollider2D>();
- playerCollider.size = new Vector2(playerCollider.size.x, slideHeight);
- playerCollider.offset = slideOffset;
- }
- private void OnDrawGizmos()
- {
- Gizmos.color = Color.red;
- Gizmos.DrawWireSphere(groundCheck.position, groundCheckRadius);
- Gizmos.color = Color.blue;
- Gizmos.DrawWireSphere(headCheck.position, headCheckRadius);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment