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;
    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;

    [SerializeField] private int playerGold = 0;

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

    private float horizontalInput, verticalInput;
    Vector2 lastInput;

    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();

        Debug.Log($"IsAttacking: {anim.GetBool("IsAttacking")}");
    }

    //Player Movement
    private void PlayerMovement()
    {
        //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;
        
        if (rb.velocity != Vector2.zero) lastInput = new Vector2(horizontalInput, verticalInput).normalized;
    }

    //Player Attack
    private void PlayerAttack()
    {

        if (Input.GetMouseButton(0))
        {
            isAttacking = true;
            Debug.Log("PLayer Attacking: " + isAttacking);

            if (attackTimer <= maxAttackTimer) attackTimer += Time.deltaTime;
        }

        if (Input.GetMouseButtonUp(0) && isAttacking)
        {
            Vector2 origin = transform.position;
            Vector2 direction = lastInput;
            anim.SetBool("IsAttacking", true); //Perform attack Animation

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

            Debug.DrawRay(origin, direction * attackRange, Color.red, 1.5f);

            if (hit.collider != null)
            {
                Debug.Log($"Collider: {hit.collider}");

                if (hit.collider.TryGetComponent(out IHealthSystem healthSystem))
                {
                    Debug.Log($"Apply Damage!");
                    float damage = attackBaseValue + (attackBaseValue * attackTimer);
                    healthSystem.TakeDamage(damage);
                }
            }

            attackTimer = 0f; // Reset attack timer
            isAttacking = false;

            // Ensure the animation can play out by delaying the reset of IsAttacking
            StartCoroutine(ResetAttackAnimation());
        }
    }

    // Coroutine to delay resetting the attack animation
    private IEnumerator ResetAttackAnimation()
    {
        yield return new WaitForSeconds(0.1f);  // Adjust the delay as necessary
        anim.SetBool("IsAttacking", false);
    }


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

    //Set Animation Values
    private void PlayerAnimController()
    {
        anim.SetFloat("moveX", horizontalInput);
        anim.SetFloat("moveY", verticalInput);
        anim.SetFloat("LastMoveX", lastInput.x);
        anim.SetFloat("LastMoveY", lastInput.y);
        anim.SetFloat("Velocity", new Vector2(horizontalInput, verticalInput).normalized.magnitude);
    }

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