Newer
Older
TheVengeance-Project-IADE-Unity2D / Assets / Scripts / Player / PlayerController.cs
using System.Collections;
using System.Collections.Generic;
using System.Xml.Serialization;
using UnityEngine;

public class PlayerController : MonoBehaviour, IHealthSystem
{
    private Rigidbody2D rb;
    [SerializeField] private Animator anim;

    public bool flashActive;
    [SerializeField]
    public float flashLength = 0f;
    [SerializeField]
    public float flashCounter = 0f;
    private SpriteRenderer playerSprite;

    //private Transform attackBoxTransform;
    //private BoxCollider2D attackBoxCollider;

    [Header("Attribute Settings: ")]
    [SerializeField] private float health = 100f;
    [SerializeField] private float mana = 100f;
    [SerializeField] private float speed = 0f;

    [Header("Combat Settings: ")]
    [SerializeField] private bool isAttacking = false;
    [SerializeField] private float attackBaseValue = 20f;
    [SerializeField] private float attackRange = 1f;
    [SerializeField] private float attackTimer = 0f;
    [SerializeField] private float maxAttackTimer = 1.5f;

    private float horizontalInput, verticalInput;

    public bool normalAttack;
    public bool strongAttack;
    public bool shield;

    public int normalPlayerAttack = 10;
    public int strongPlayerAttack = 15;
    public int defensePlayer = 5;

    private void Awake()
    {
        rb = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
        playerSprite = GetComponent<SpriteRenderer>();
    }

    void Start()
    {

    }

    private void Update()
    {
        PlayerMovement();
        PlayerDefend();
        PlayerAttack();
        PlayerAnimController();
    }

    //Player Movement
    private void PlayerMovement()
    {
        if (!isAttacking)
        {
            //Get Keyboard Input values
            horizontalInput = Input.GetAxisRaw("Horizontal");
            verticalInput = Input.GetAxisRaw("Vertical");

            //Set velocity based on the input
            rb.velocity = new Vector2(horizontalInput, verticalInput).normalized * speed;
        }
    }

    //Player Attack
    private void PlayerAttack()
    {
        if (Input.GetMouseButtonDown(0))
        {
            rb.velocity = Vector3.zero; //Sets the character velocity to 0
            isAttacking = true;
            attackTimer = 0f; // Reset attack timer

            Vector2 origin = transform.position;
            Vector2 direction = new Vector2(horizontalInput, verticalInput).normalized;

            if (direction == Vector2.zero)
                direction = new Vector2(anim.GetFloat("LastMoveX"), anim.GetFloat("LastMoveY"));

            RaycastHit2D hit = Physics2D.Raycast(origin, direction, attackRange);

            if (hit.collider != null)
            {
                if (hit.collider.TryGetComponent(out IHealthSystem healthSystem))
                {
                    healthSystem.TakeDamage(attackBaseValue);
                }
            }

            anim.SetBool("IsAttacking", true);
        }

        if (isAttacking)
        {
            attackTimer += Time.deltaTime;

            if (attackTimer >= maxAttackTimer)
            {
                isAttacking = false;
                anim.SetBool("IsAttacking", false);
            }
        }
    }


    private void PlayerDefend() => shield = Input.GetKey(KeyCode.Space);

    private void PlayerAnimController()
    {
        anim.SetFloat("moveX", horizontalInput);
        anim.SetFloat("moveY", verticalInput);

        if (isAttacking)
        {
            //Detect what was the last input
            Vector2 vec = new Vector2(horizontalInput, verticalInput);
            anim.SetFloat("LastMoveX", vec.x);
            anim.SetFloat("LastMoveY", vec.y);
        }
    }

    public void TakeDamage(float ammount) => health -= ammount;
    public void Heal(float ammount) => health += ammount;
    public float GetHealth() => health;
    public bool IsDead() => health <= 0f ? true : false;
}