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

111 lines
2.7 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;
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;
public void initialize(InputManager inputManager, LayerMask enemyLayermask, Animate animate, Collider hurtbox, string[] availableAttacks)
{
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;
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;
}
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 20:34:57 +00:00
_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;
}
}
}