refactor: move all files into Study_One folder
This commit is contained in:
139
Study_One/Assets/Player.cs
Normal file
139
Study_One/Assets/Player.cs
Normal file
@@ -0,0 +1,139 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class Player : MonoBehaviour
|
||||
{
|
||||
private Animator anim;
|
||||
private Rigidbody2D rb;
|
||||
|
||||
|
||||
[Header("Attack details")]
|
||||
[SerializeField] private float attackRadius;
|
||||
[SerializeField] private Transform attackPoint;
|
||||
[SerializeField] private LayerMask whatIsEnemy;
|
||||
|
||||
|
||||
[Header("Movement details")]
|
||||
[SerializeField] private float moveSpeed = 8f;
|
||||
[SerializeField] private float jumpForce = 12;
|
||||
private bool facingRight = true;
|
||||
private float xInput;
|
||||
private bool canMove = true;
|
||||
private bool canJump = true;
|
||||
|
||||
[Header("Collision details")]
|
||||
[SerializeField] private float groundCheckDistance;
|
||||
[SerializeField] private LayerMask whatIsGround;
|
||||
private bool isGrounded;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
rb = GetComponent<Rigidbody2D>();
|
||||
anim = GetComponentInChildren<Animator>();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
|
||||
{
|
||||
HandleCollision();
|
||||
HandleInput();
|
||||
HandleMovement();
|
||||
HandleAnimations();
|
||||
HandleFlip();
|
||||
}
|
||||
|
||||
public void DamageEnemies()
|
||||
{
|
||||
Collider2D[] enemyColliders = Physics2D.OverlapCircleAll(attackPoint.position, attackRadius, whatIsEnemy);
|
||||
foreach (Collider2D enemy in enemyColliders)
|
||||
{
|
||||
enemy.GetComponent<Enemy>().TakeDamage();
|
||||
}
|
||||
}
|
||||
|
||||
public void EnableMovementAndJump(bool enable)
|
||||
{
|
||||
canMove = enable;
|
||||
canJump = enable;
|
||||
}
|
||||
|
||||
private void HandleInput()
|
||||
{
|
||||
xInput = Input.GetAxisRaw("Horizontal");
|
||||
|
||||
if (Input.GetKeyDown(KeyCode.K))
|
||||
{
|
||||
TryToJump();
|
||||
}
|
||||
if (Input.GetKeyDown(KeyCode.J))
|
||||
{
|
||||
TryToAttack();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void TryToAttack()
|
||||
{
|
||||
if(isGrounded && canMove)
|
||||
{
|
||||
anim.SetTrigger("attack");
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleAnimations()
|
||||
{
|
||||
anim.SetFloat("xVelocity", rb.linearVelocity.x);
|
||||
anim.SetFloat("yVelocity", rb.linearVelocity.y);
|
||||
anim.SetBool("isGrounded", isGrounded);
|
||||
}
|
||||
|
||||
private void HandleMovement()
|
||||
{
|
||||
if(canMove)
|
||||
{
|
||||
rb.linearVelocity = new Vector2(xInput * moveSpeed, rb.linearVelocity.y);
|
||||
}
|
||||
else
|
||||
{
|
||||
rb.linearVelocity = new Vector2(0, rb.linearVelocity.y);
|
||||
}
|
||||
}
|
||||
|
||||
private void TryToJump()
|
||||
{
|
||||
if (isGrounded && canJump)
|
||||
{
|
||||
rb.linearVelocity = new Vector2(rb.linearVelocity.x, jumpForce);
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleCollision()
|
||||
{
|
||||
isGrounded = Physics2D.Raycast(transform.position, Vector2.down, groundCheckDistance, whatIsGround);
|
||||
}
|
||||
|
||||
private void HandleFlip()
|
||||
{
|
||||
if (rb.linearVelocity.x > 0 && facingRight == false)
|
||||
{
|
||||
Flip();
|
||||
}
|
||||
else if (rb.linearVelocity.x < 0 && facingRight == true)
|
||||
{
|
||||
Flip();
|
||||
}
|
||||
}
|
||||
|
||||
private void Flip()
|
||||
{
|
||||
transform.Rotate(0, 180, 0);
|
||||
facingRight = !facingRight;
|
||||
}
|
||||
|
||||
private void OnDrawGizmos()
|
||||
{
|
||||
Gizmos.DrawLine(transform.position, transform.position + new Vector3(0, -groundCheckDistance));
|
||||
Gizmos.DrawWireSphere(attackPoint.position, attackRadius);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user