2024-12-19 19:21:36 +00:00
|
|
|
using System;
|
2024-12-23 20:34:57 +00:00
|
|
|
using System.Collections;
|
2024-12-23 17:13:28 +00:00
|
|
|
using System.Collections.Generic;
|
2024-12-23 20:34:57 +00:00
|
|
|
using Unity.Mathematics;
|
2024-12-23 17:13:28 +00:00
|
|
|
using Unity.VisualScripting;
|
2024-12-19 19:21:36 +00:00
|
|
|
using UnityEngine;
|
2024-12-23 17:13:28 +00:00
|
|
|
using UnityEngine.Events;
|
2024-12-23 23:46:26 +00:00
|
|
|
using Random = System.Random;
|
2024-12-19 19:21:36 +00:00
|
|
|
|
|
|
|
public class PlayerAttack : MonoBehaviour
|
|
|
|
{
|
|
|
|
private InputManager _inputManager;
|
|
|
|
private LayerMask _enemyLayermask;
|
2024-12-23 17:13:28 +00:00
|
|
|
private Animate _animate;
|
2024-12-23 20:34:57 +00:00
|
|
|
private Collider _hurtbox;
|
|
|
|
private string[] _availableAttacks;
|
2024-12-23 23:46:26 +00:00
|
|
|
private float _baseDamage;
|
|
|
|
private float _comboStep;
|
|
|
|
private float _currentComboStep;
|
|
|
|
private AnimationCurve _comboCurve;
|
|
|
|
public void initialize(InputManager inputManager, LayerMask enemyLayermask, Animate animate, Collider hurtbox, string[] availableAttacks, float baseDamage, float comboStep, AnimationCurve comboCurve)
|
2024-12-19 19:21:36 +00:00
|
|
|
{
|
2024-12-23 17:13:28 +00:00
|
|
|
_inputManager = inputManager;
|
2024-12-19 19:21:36 +00:00
|
|
|
_enemyLayermask = enemyLayermask;
|
2024-12-23 20:34:57 +00:00
|
|
|
_hurtbox = hurtbox;
|
2024-12-23 17:13:28 +00:00
|
|
|
_animate = animate;
|
2024-12-23 20:34:57 +00:00
|
|
|
_availableAttacks = availableAttacks;
|
2024-12-23 23:46:26 +00:00
|
|
|
_baseDamage = baseDamage;
|
|
|
|
_comboStep = comboStep;
|
|
|
|
_comboCurve = comboCurve;
|
|
|
|
_currentComboStep = 0f;
|
2024-12-23 20:34:57 +00:00
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
_attacking = false;
|
2024-12-23 23:46:26 +00:00
|
|
|
_currentComboStep = 0f;
|
2024-12-23 20:34:57 +00:00
|
|
|
}
|
2024-12-23 17:13:28 +00:00
|
|
|
|
2024-12-23 20:34:57 +00:00
|
|
|
private void _onStartDamageHandler()
|
|
|
|
{
|
|
|
|
StartCoroutine(DealDamage());
|
2024-12-19 19:21:36 +00:00
|
|
|
}
|
2024-12-23 20:34:57 +00:00
|
|
|
|
|
|
|
private void _onEndDamageHandler()
|
|
|
|
{
|
|
|
|
_damageEnded = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
2024-12-19 19:21:36 +00:00
|
|
|
|
2024-12-23 20:34:57 +00:00
|
|
|
private bool _attacking=false;
|
2024-12-23 17:13:28 +00:00
|
|
|
private bool _canAttack = true;
|
2024-12-23 20:34:57 +00:00
|
|
|
private int _attack = 0;
|
|
|
|
public bool Attack()
|
2024-12-19 19:21:36 +00:00
|
|
|
{
|
2024-12-23 17:13:28 +00:00
|
|
|
if (_canAttack && _inputManager.attackPerformed)
|
|
|
|
{
|
2024-12-23 20:34:57 +00:00
|
|
|
if (_attack >= 2 || _animate.IsCurrentAnimation("Blend Tree"))
|
2024-12-23 17:13:28 +00:00
|
|
|
{
|
2024-12-23 20:34:57 +00:00
|
|
|
_attack = 0;
|
2024-12-23 17:13:28 +00:00
|
|
|
}
|
|
|
|
|
2024-12-23 20:34:57 +00:00
|
|
|
_animate.TriggerAnimation(_availableAttacks[_attack]);
|
2024-12-23 17:13:28 +00:00
|
|
|
|
|
|
|
_canAttack = false;
|
|
|
|
_attack++;
|
2024-12-23 20:34:57 +00:00
|
|
|
_attacking = true;
|
2024-12-23 17:13:28 +00:00
|
|
|
}
|
2024-12-23 20:34:57 +00:00
|
|
|
|
|
|
|
return _attacking;
|
2024-12-23 17:13:28 +00:00
|
|
|
}
|
|
|
|
|
2024-12-23 20:34:57 +00:00
|
|
|
private bool _damageEnded = true;
|
|
|
|
IEnumerator DealDamage()
|
2024-12-23 17:13:28 +00:00
|
|
|
{
|
2024-12-23 20:34:57 +00:00
|
|
|
_damageEnded = false;
|
2024-12-23 23:46:26 +00:00
|
|
|
List<Collider> damagedColliders = new List<Collider>();
|
2024-12-23 20:34:57 +00:00
|
|
|
while (!_damageEnded)
|
|
|
|
{
|
|
|
|
Collider[] hitColliders = Physics.OverlapBox(_hurtbox.bounds.center, _hurtbox.bounds.extents, Quaternion.identity, _enemyLayermask);
|
2024-12-23 23:46:26 +00:00
|
|
|
|
|
|
|
foreach (Collider enemy in hitColliders)
|
2024-12-23 20:34:57 +00:00
|
|
|
{
|
2024-12-23 23:46:26 +00:00
|
|
|
if (!damagedColliders.Contains(enemy))
|
|
|
|
{
|
|
|
|
damagedColliders.Add(enemy);
|
|
|
|
enemy.gameObject.GetComponent<Vitals>().Health -= _baseDamage * _comboCurve.Evaluate(_currentComboStep);
|
|
|
|
}
|
2024-12-23 20:34:57 +00:00
|
|
|
}
|
|
|
|
yield return null;
|
|
|
|
}
|
2024-12-23 23:46:26 +00:00
|
|
|
|
|
|
|
Random rnd = new Random();
|
|
|
|
_currentComboStep += _comboStep * (rnd.Next(1, 10)/10f);
|
2024-12-19 19:21:36 +00:00
|
|
|
}
|
|
|
|
}
|