twinStick/twinStickCrawler/Assets/Scripts/Creatures/Player/PlayerAttack.cs

145 lines
4.3 KiB
C#
Raw Normal View History

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;
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;
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 AnimationCurve _comboCurve;
public void initialize(InputManager inputManager, LayerMask enemyLayermask, Animate animate, Collider hurtbox, string[] availableAttacks, float baseDamage, float comboStep, AnimationCurve comboCurve)
{
2024-12-23 17:13:28 +00:00
_inputManager = inputManager;
_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;
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;
}
2024-12-24 13:34:15 +00:00
_canAttack = true;
_attacking = false;
_damageEnded = true;
_currentDamageMultiplier = new Dictionary<int, float>();
2024-12-23 20:34:57 +00:00
}
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-24 13:34:15 +00:00
_currentDamageMultiplier = new Dictionary<int, float>();
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-23 20:34:57 +00:00
private void _onEndDamageHandler()
{
_damageEnded = true;
}
#endregion
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-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 23:46:26 +00:00
List<Collider> damagedColliders = new List<Collider>();
2024-12-24 13:34:15 +00:00
_damageEnded = false;
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))
{
2024-12-24 13:34:15 +00:00
print(enemy.gameObject.GetComponent<Vitals>().Health);
2024-12-24 18:58:18 +00:00
enemy.gameObject.GetComponent<Vitals>().Health = -CalculateDamage(enemy);
2024-12-23 23:46:26 +00:00
damagedColliders.Add(enemy);
}
2024-12-23 20:34:57 +00:00
}
yield return null;
}
2024-12-24 13:34:15 +00:00
}
Dictionary<int, float> _currentDamageMultiplier = new Dictionary<int, float>();
float CalculateDamage(Collider enemy)
{
// If enemy is hit for the first time during "Combo" he gets added to the dictionary to get its own combo mult
if (!_currentDamageMultiplier.ContainsKey(enemy.GetInstanceID()))
{
_currentDamageMultiplier.Add(enemy.GetInstanceID(), 0f);
}
// If enemy gets hit again its Combo mult is incremented
else
{
Random rnd = new Random();
_currentDamageMultiplier[enemy.GetInstanceID()] += _comboStep * (rnd.Next(1, 10) / 10f);
}
2024-12-23 23:46:26 +00:00
2024-12-24 13:34:15 +00:00
return _baseDamage * _comboCurve.Evaluate(_currentDamageMultiplier[enemy.GetInstanceID()]);
}
}