2025-09-04 21:03:43 +02:00
|
|
|
|
using System.Collections;
|
2025-08-07 02:02:46 +02:00
|
|
|
|
using UnityEngine;
|
2025-09-04 21:03:43 +02:00
|
|
|
|
using UnityEngine.AI;
|
2025-08-07 02:02:46 +02:00
|
|
|
|
|
|
|
|
|
|
public class EnemyAttack : MonoBehaviour
|
|
|
|
|
|
{
|
2025-09-04 21:03:43 +02:00
|
|
|
|
[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;
|
|
|
|
|
|
|
2025-09-04 21:03:43 +02:00
|
|
|
|
[Header("References")]
|
|
|
|
|
|
[SerializeField] private Transform player;
|
|
|
|
|
|
[SerializeField] private Animator animator;
|
|
|
|
|
|
[SerializeField] private NavMeshAgent agent;
|
|
|
|
|
|
|
2025-08-07 02:02:46 +02:00
|
|
|
|
private float lastAttackTime = 0f;
|
2025-09-04 21:03:43 +02:00
|
|
|
|
private bool canAttack = true;
|
|
|
|
|
|
private bool isAttacking = false;
|
|
|
|
|
|
private float attackCooldownTimer = 0f;
|
2025-08-07 02:02:46 +02:00
|
|
|
|
|
|
|
|
|
|
void Start()
|
|
|
|
|
|
{
|
2025-09-04 21:03:43 +02:00
|
|
|
|
// Najdi reference
|
|
|
|
|
|
GameObject playerObject = GameObject.FindWithTag("Player");
|
|
|
|
|
|
if (playerObject != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
player = playerObject.transform;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
{
|
2025-09-04 21:03:43 +02:00
|
|
|
|
// Update cooldown timeru
|
|
|
|
|
|
if (!canAttack)
|
|
|
|
|
|
{
|
|
|
|
|
|
attackCooldownTimer -= Time.deltaTime;
|
|
|
|
|
|
if (attackCooldownTimer <= 0f)
|
|
|
|
|
|
{
|
|
|
|
|
|
canAttack = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-08-07 02:02:46 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-04 21:03:43 +02:00
|
|
|
|
// Metoda pro pokus o <20>tok - vol<6F>na z EnemyMovement
|
|
|
|
|
|
public bool TryAttack()
|
2025-08-07 02:02:46 +02:00
|
|
|
|
{
|
2025-09-04 21:03:43 +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);
|
|
|
|
|
|
bool inRange = distanceToPlayer <= attackRange;
|
|
|
|
|
|
bool inAngle = IsPlayerInAttackAngle();
|
|
|
|
|
|
bool cooldownReady = Time.time - lastAttackTime >= attackRate;
|
|
|
|
|
|
|
|
|
|
|
|
Debug.Log($"TryAttack: range={inRange}, angle={inAngle}, cooldown={cooldownReady}, distance={distanceToPlayer}");
|
|
|
|
|
|
|
|
|
|
|
|
if (inRange && cooldownReady)
|
|
|
|
|
|
{
|
|
|
|
|
|
StartCoroutine(PerformAttack());
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Debug.Log("Attack conditions not met");
|
|
|
|
|
|
return false;
|
2025-08-07 02:02:46 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-04 21:03:43 +02:00
|
|
|
|
private bool IsPlayerInAttackAngle()
|
2025-08-07 02:02:46 +02:00
|
|
|
|
{
|
2025-09-04 21:03:43 +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);
|
|
|
|
|
|
|
|
|
|
|
|
// V<>t<EFBFBD><74> tolerance <20>hlu (zv<7A><76>eno z 22.5<EFBFBD> na 60<36>)
|
|
|
|
|
|
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);
|
|
|
|
|
|
bool inRange = distance <= attackRange * 1.1f;
|
|
|
|
|
|
bool inAngle = IsPlayerInAttackAngle();
|
|
|
|
|
|
|
|
|
|
|
|
return inRange && inAngle;
|
2025-08-07 02:02:46 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-04 21:03:43 +02:00
|
|
|
|
private IEnumerator PerformAttack()
|
2025-08-07 02:02:46 +02:00
|
|
|
|
{
|
2025-09-04 21:03:43 +02:00
|
|
|
|
isAttacking = true;
|
|
|
|
|
|
canAttack = false;
|
|
|
|
|
|
lastAttackTime = Time.time;
|
|
|
|
|
|
attackCooldownTimer = attackRate;
|
2025-08-07 02:02:46 +02:00
|
|
|
|
|
2025-09-04 21:03:43 +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
|
|
|
|
{
|
2025-09-04 21:03:43 +02:00
|
|
|
|
animator.SetTrigger("Attack");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Po<50>kej chv<68>li p<>ed aplikov<6F>n<EFBFBD>m po<70>kozen<65>
|
|
|
|
|
|
yield return new WaitForSeconds(0.3f);
|
|
|
|
|
|
|
|
|
|
|
|
// Aplikuj po<70>kozen<65>, pokud je hr<68><72> st<73>le v dosahu
|
|
|
|
|
|
if (player != null && Vector3.Distance(transform.position, player.position) <= attackRange * 1.2f)
|
|
|
|
|
|
{
|
|
|
|
|
|
var health = player.GetComponent<HealthManager>();
|
2025-08-07 02:02:46 +02:00
|
|
|
|
if (health != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
health.ModifyHealth(-attackDamage);
|
2025-09-04 21:03:43 +02:00
|
|
|
|
Debug.Log("Enemy attacked player for " + attackDamage + " damage!");
|
2025-08-07 02:02:46 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-09-04 21:03:43 +02:00
|
|
|
|
|
|
|
|
|
|
// Po<50>kej na dokon<6F>en<65> animace
|
|
|
|
|
|
yield return new WaitForSeconds(0.7f);
|
|
|
|
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
|
|
{
|
|
|
|
|
|
return canAttack && !isAttacking;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public bool IsAttacking()
|
|
|
|
|
|
{
|
|
|
|
|
|
return isAttacking;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public float GetCooldownProgress()
|
|
|
|
|
|
{
|
|
|
|
|
|
return Mathf.Clamp01(attackCooldownTimer / attackRate);
|
2025-08-07 02:02:46 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-04 21:03:43 +02:00
|
|
|
|
public void SetAttackState(bool state)
|
2025-08-07 02:02:46 +02:00
|
|
|
|
{
|
2025-09-04 21:03:43 +02:00
|
|
|
|
canAttack = state;
|
|
|
|
|
|
if (!state) isAttacking = false;
|
2025-08-07 02:02:46 +02:00
|
|
|
|
}
|
2025-09-04 21:03:43 +02:00
|
|
|
|
}
|