twinStick/twinStickCrawler/Assets/Scripts/Creatures/Player/PlayerAttack.cs
2024-12-23 21:34:57 +01:00

111 lines
2.7 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using Unity.Mathematics;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.Events;
public class PlayerAttack : MonoBehaviour
{
private InputManager _inputManager;
private LayerMask _enemyLayermask;
private Animate _animate;
private Collider _hurtbox;
private string[] _availableAttacks;
public void initialize(InputManager inputManager, LayerMask enemyLayermask, Animate animate, Collider hurtbox, string[] availableAttacks)
{
_inputManager = inputManager;
_enemyLayermask = enemyLayermask;
_hurtbox = hurtbox;
_animate = animate;
_availableAttacks = availableAttacks;
OnEnable();
}
#region Event Setup
private void OnEnable()
{
if (_animate != null)
{
_animate.OnReset += _onResetHandler;
_animate.OnStartDamage += _onStartDamageHandler;
_animate.OnEndDamage += _onEndDamageHandler;
_animate.OnAnimationEnd += _onAnimationEndHandler;
}
}
private void OnDisable()
{
_animate.OnReset -= _onResetHandler;
_animate.OnStartDamage -= _onStartDamageHandler;
_animate.OnEndDamage -= _onEndDamageHandler;
_animate.OnAnimationEnd -= _onAnimationEndHandler;
}
private void _onResetHandler()
{
_canAttack = true;
}
private void _onAnimationEndHandler()
{
print("tf");
_attacking = false;
}
private void _onStartDamageHandler()
{
StartCoroutine(DealDamage());
}
private void _onEndDamageHandler()
{
_damageEnded = true;
}
#endregion
private bool _attacking=false;
private bool _canAttack = true;
private int _attack = 0;
public bool Attack()
{
if (_canAttack && _inputManager.attackPerformed)
{
if (_attack >= 2 || _animate.IsCurrentAnimation("Blend Tree"))
{
_attack = 0;
}
_animate.TriggerAnimation(_availableAttacks[_attack]);
_canAttack = false;
_attack++;
_attacking = true;
}
return _attacking;
}
private bool _damageEnded = true;
IEnumerator DealDamage()
{
_damageEnded = false;
while (!_damageEnded)
{
Collider[] hitColliders = Physics.OverlapBox(_hurtbox.bounds.center, _hurtbox.bounds.extents, Quaternion.identity, _enemyLayermask);
for (int i = 0; i < hitColliders.Length; i++)
{
//damage code
//hitColliders
}
yield return null;
}
}
}