Files
3DBlobici-WorkingTitle/3D blobici/Assets/Scripts/Enemy/EnemyAttack.cs

194 lines
5.6 KiB
C#
Raw Normal View History

using System.Collections;
2025-08-07 02:02:46 +02:00
using UnityEngine;
using UnityEngine.AI;
2025-08-07 02:02:46 +02:00
public class EnemyAttack : MonoBehaviour
{
[Header("Attack Settings")]
[SerializeField] private float attackRange = 2f;
2025-08-07 02:02:46 +02:00
[SerializeField] private float attackRate = 2f;
[SerializeField] private float attackDamage = 10f;
[SerializeField] private float attackAngle = 45f;
[Header("References")]
[SerializeField] private Transform player;
[SerializeField] private Animator animator;
[SerializeField] private NavMeshAgent agent;
2025-09-05 17:47:06 +02:00
2025-08-07 02:02:46 +02:00
private float lastAttackTime = 0f;
private bool canAttack = true;
private bool isAttacking = false;
private float attackCooldownTimer = 0f;
2025-09-05 17:47:06 +02:00
private PlayerSkillHandler skillHandler;
2025-08-07 02:02:46 +02:00
void Start()
{
// Najdi reference
GameObject playerObject = GameObject.FindWithTag("Player");
if (playerObject != null)
{
player = playerObject.transform;
}
2025-09-05 17:47:06 +02:00
skillHandler = player.GetComponent<PlayerSkillHandler>();
if (animator == null) animator = GetComponent<Animator>();
if (agent == null) agent = GetComponent<NavMeshAgent>();
lastAttackTime = -attackRate; // Umo<6D>n<EFBFBD> <20>tok hned na za<7A><61>tku
2025-08-07 02:02:46 +02:00
}
void Update()
{
// Update cooldown timeru
if (!canAttack)
{
attackCooldownTimer -= Time.deltaTime;
if (attackCooldownTimer <= 0f)
{
canAttack = true;
}
}
2025-08-07 02:02:46 +02:00
}
2025-09-06 00:44:27 +02:00
public float GetAttackRange() => attackRange;
// Metoda pro pokus o <20>tok - vol<6F>na z EnemyMovement
public bool TryAttack()
2025-08-07 02:02:46 +02:00
{
if (player == null || !canAttack || isAttacking)
{
Debug.Log($"Attack failed: player={player != null}, canAttack={canAttack}, isAttacking={isAttacking}");
return false;
}
// Kontrola vzd<7A>lenosti a <20>hlu k hr<68><72>i
float distanceToPlayer = Vector3.Distance(transform.position, player.position);
2025-09-06 00:44:27 +02:00
bool inRange = IsPlayerInAttackRange();
bool inAngle = IsPlayerInAttackAngle();
bool cooldownReady = Time.time - lastAttackTime >= attackRate;
if (inRange && cooldownReady)
{
StartCoroutine(PerformAttack());
return true;
}
2025-09-06 00:44:27 +02:00
Debug.Log("Attack conditions not met. Conditions: range: " + inRange + "angle: " + inAngle + "cooldown: " + cooldownReady);
return false;
2025-08-07 02:02:46 +02:00
}
private bool IsPlayerInAttackAngle()
2025-08-07 02:02:46 +02:00
{
if (player == null) return false;
Vector3 directionToPlayer = (player.position - transform.position).normalized;
// Ignoruj Y osu pro v<>po<70>et <20>hlu
directionToPlayer.y = 0;
Vector3 forward = transform.forward;
forward.y = 0;
float angle = Vector3.Angle(forward, directionToPlayer);
return angle <= attackAngle; // Pou<6F><75>v<EFBFBD>me cel<65> attackAngle, ne polovinu
}
public bool IsPlayerInAttackRange()
{
if (player == null) return false;
float distance = Vector3.Distance(transform.position, player.position);
2025-09-06 00:44:27 +02:00
bool inRange = distance <= attackRange * 1.2f;
2025-09-06 00:44:27 +02:00
return inRange;
2025-08-07 02:02:46 +02:00
}
private IEnumerator PerformAttack()
2025-08-07 02:02:46 +02:00
{
isAttacking = true;
canAttack = false;
lastAttackTime = Time.time;
attackCooldownTimer = attackRate;
2025-08-07 02:02:46 +02:00
// Ulo<6C> p<>vodn<64> stav agenta
bool wasStopped = agent.isStopped;
// Zastav pohyb b<>hem <20>toku
agent.isStopped = true;
// Spustit animaci <20>toku
if (animator != null)
2025-08-07 02:02:46 +02:00
{
animator.SetTrigger("Attack");
}
// Po<50>kej chv<68>li p<>ed aplikov<6F>n<EFBFBD>m po<70>kozen<65>
2025-09-06 00:44:27 +02:00
yield return new WaitForSeconds(0.1f);
2025-09-05 17:47:06 +02:00
bool playerDashing = skillHandler.IsDashing();
// Aplikuj po<70>kozen<65>, pokud je hr<68><72> st<73>le v dosahu
2025-09-05 17:47:06 +02:00
if (player != null && Vector3.Distance(transform.position, player.position) <= attackRange * 1.2f && !playerDashing)
{
var health = player.GetComponent<HealthManager>();
2025-08-07 02:02:46 +02:00
if (health != null)
{
health.ModifyHealth(-attackDamage);
Debug.Log("Enemy attacked player for " + attackDamage + " damage!");
2025-08-07 02:02:46 +02:00
}
}
// Po<50>kej na dokon<6F>en<65> animace
2025-09-06 00:44:27 +02:00
yield return new WaitForSeconds(0.3f);
// Obnov pohyb pouze pokud nebyl p<>vodn<64> zastaven<65>
if (!wasStopped)
{
agent.isStopped = false;
}
isAttacking = false;
// canAttack se nastav<61> v Update pomoc<6F> timeru
Debug.Log("Attack finished, cooldown started");
}
// Metoda pro vizu<7A>ln<6C> reprezentaci <20>to<74>n<EFBFBD>ho <20>hlu
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, attackRange);
Gizmos.color = Color.yellow;
Vector3 leftDir = Quaternion.Euler(0, -attackAngle / 2, 0) * transform.forward;
Vector3 rightDir = Quaternion.Euler(0, attackAngle / 2, 0) * transform.forward;
Gizmos.DrawRay(transform.position, leftDir * attackRange);
Gizmos.DrawRay(transform.position, rightDir * attackRange);
Gizmos.DrawLine(transform.position + leftDir * attackRange, transform.position + rightDir * attackRange);
}
// Public metody pro komunikaci s EnemyMovement
public bool CanAttack()
{
2025-09-06 00:44:27 +02:00
return canAttack && !isAttacking && IsPlayerInAttackRange();
}
public bool IsAttacking()
{
return isAttacking;
}
public float GetCooldownProgress()
{
return Mathf.Clamp01(attackCooldownTimer / attackRate);
2025-08-07 02:02:46 +02:00
}
public void SetAttackState(bool state)
2025-08-07 02:02:46 +02:00
{
canAttack = state;
if (!state) isAttacking = false;
2025-08-07 02:02:46 +02:00
}
}