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

153 lines
5.2 KiB
C#
Raw Normal View History

2024-12-23 20:34:57 +00:00
using System.Collections;
2024-12-23 17:13:28 +00:00
using System.Collections.Generic;
using UnityEngine;
2024-12-23 23:46:26 +00:00
using Random = System.Random;
2024-12-24 20:40:18 +00:00
namespace Creatures.Player.Movement
{
2024-12-24 20:40:18 +00:00
public class PlayerAttack : MonoBehaviour
{
2024-12-24 20:40:18 +00:00
private InputManager _inputManager;
private LayerMask _enemyLayermask;
private Animate _animate;
private Collider _hurtbox;
private string[] _availableAttacks;
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)
{
_inputManager = inputManager;
_enemyLayermask = enemyLayermask;
_hurtbox = hurtbox;
_animate = animate;
_availableAttacks = availableAttacks;
_baseDamage = baseDamage;
_comboStep = comboStep;
_comboCurve = comboCurve;
2024-12-23 20:34:57 +00:00
2024-12-24 20:40:18 +00:00
OnEnable();
}
2024-12-23 20:34:57 +00:00
2024-12-24 20:40:18 +00:00
#region Event Setup
2024-12-23 20:34:57 +00:00
2024-12-24 20:40:18 +00:00
private void OnEnable()
2024-12-23 20:34:57 +00:00
{
2024-12-24 20:40:18 +00:00
if (_animate != null)
{
_animate.OnReset += _onResetHandler;
_animate.OnStartDamage += _onStartDamageHandler;
_animate.OnEndDamage += _onEndDamageHandler;
_animate.OnAnimationEnd += _onAnimationEndHandler;
}
2024-12-24 13:34:15 +00:00
2024-12-24 20:40:18 +00:00
_canAttack = true;
_attacking = false;
_damageEnded = true;
_currentDamageMultiplier = new Dictionary<int, float>();
}
2024-12-23 20:34:57 +00:00
2024-12-24 20:40:18 +00:00
private void OnDisable()
{
_animate.OnReset -= _onResetHandler;
_animate.OnStartDamage -= _onStartDamageHandler;
_animate.OnEndDamage -= _onEndDamageHandler;
_animate.OnAnimationEnd -= _onAnimationEndHandler;
}
2024-12-23 20:34:57 +00:00
2024-12-24 20:40:18 +00:00
private void _onResetHandler()
{
_canAttack = true;
}
2024-12-23 20:34:57 +00:00
2024-12-24 20:40:18 +00:00
private void _onAnimationEndHandler()
{
_attacking = false;
_currentDamageMultiplier = new Dictionary<int, float>();
_lastDamageMultiplier = new Dictionary<int, float>();
}
2024-12-23 17:13:28 +00:00
2024-12-24 20:40:18 +00:00
private void _onStartDamageHandler()
{
StartCoroutine(DealDamage());
}
2024-12-23 20:34:57 +00:00
2024-12-24 20:40:18 +00:00
private void _onEndDamageHandler()
{
_damageEnded = true;
}
2024-12-23 20:34:57 +00:00
2024-12-24 20:40:18 +00:00
#endregion
2024-12-24 20:40:18 +00:00
private bool _attacking=false;
private bool _canAttack = true;
private int _attack = 0;
public bool Attack()
2024-12-23 17:13:28 +00:00
{
2024-12-24 20:40:18 +00:00
if (_canAttack && _inputManager.attackPerformed)
2024-12-23 17:13:28 +00:00
{
2024-12-24 20:40:18 +00:00
if (_attack >= 2 || _animate.IsCurrentAnimation("Blend Tree"))
{
_attack = 0;
}
2024-12-23 17:13:28 +00:00
2024-12-24 20:40:18 +00:00
_animate.TriggerAnimation(_availableAttacks[_attack]);
2024-12-23 17:13:28 +00:00
2024-12-24 20:40:18 +00:00
_canAttack = false;
_attack++;
_attacking = true;
}
2024-12-23 20:34:57 +00:00
2024-12-24 20:40:18 +00:00
return _attacking;
}
2024-12-23 17:13:28 +00:00
2024-12-24 20:40:18 +00:00
private bool _damageEnded = true;
Dictionary<int, float> _lastDamageMultiplier = new Dictionary<int, float>();
Dictionary<int, float> _currentDamageMultiplier = new Dictionary<int, float>();
IEnumerator DealDamage()
2024-12-23 20:34:57 +00:00
{
2024-12-24 20:40:18 +00:00
List<Collider> damagedColliders = new List<Collider>();
_currentDamageMultiplier = new Dictionary<int, float>();
_damageEnded = false;
2024-12-23 23:46:26 +00:00
2024-12-24 20:40:18 +00:00
while (!_damageEnded)
2024-12-23 20:34:57 +00:00
{
2024-12-24 20:40:18 +00:00
Collider[] hitColliders = Physics.OverlapBox(_hurtbox.bounds.center, _hurtbox.bounds.extents, Quaternion.identity, _enemyLayermask);
foreach (Collider enemy in hitColliders)
2024-12-23 23:46:26 +00:00
{
2024-12-24 20:40:18 +00:00
if (!damagedColliders.Contains(enemy))
{
2024-12-27 19:01:20 +00:00
print(enemy.gameObject.GetComponent<Vitals>());
2024-12-24 20:40:18 +00:00
enemy.gameObject.GetComponent<Vitals>().Health = -CalculateDamage(enemy);
damagedColliders.Add(enemy);
}
2024-12-23 23:46:26 +00:00
}
2024-12-24 20:40:18 +00:00
yield return null;
2024-12-23 20:34:57 +00:00
}
2024-12-24 20:40:18 +00:00
_lastDamageMultiplier = _currentDamageMultiplier;
2024-12-23 20:34:57 +00:00
}
2024-12-24 13:34:15 +00:00
2024-12-24 20:40:18 +00:00
float CalculateDamage(Collider enemy)
2024-12-24 13:34:15 +00:00
{
2024-12-24 20:40:18 +00:00
print(_lastDamageMultiplier.ContainsKey(enemy.GetInstanceID()));
// If enemy is hit for the first time during "Combo" he gets added to the dictionary to get its own combo mult
if (!_lastDamageMultiplier.ContainsKey(enemy.GetInstanceID()))
{
_currentDamageMultiplier.Add(enemy.GetInstanceID(), 0f);
}
// If enemy gets hit again its Combo mult is incremented
else if(_lastDamageMultiplier.ContainsKey(enemy.GetInstanceID()))
{
_currentDamageMultiplier.Add(enemy.GetInstanceID(), _lastDamageMultiplier[enemy.GetInstanceID()]);
Random rnd = new Random();
_currentDamageMultiplier[enemy.GetInstanceID()] += _comboStep * (rnd.Next(1, 10) / 10f);
}
return _baseDamage * _comboCurve.Evaluate(_currentDamageMultiplier[enemy.GetInstanceID()]);
2024-12-24 13:34:15 +00:00
}
}
}