57 lines
1.4 KiB
C#
57 lines
1.4 KiB
C#
using UnityEngine;
|
|
|
|
public class PlayerCombat : MonoBehaviour
|
|
{
|
|
public float attackRange = 3f;
|
|
public int damage = 100;
|
|
public KeyCode keyboardAttack = KeyCode.F;
|
|
|
|
private void Update()
|
|
{
|
|
bool uiBlocksCombat =
|
|
DialogueUI.Instance != null && DialogueUI.Instance.IsOpen ||
|
|
QuestLogUI.Instance != null && QuestLogUI.Instance.IsOpen;
|
|
|
|
if (uiBlocksCombat)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (Input.GetKeyDown(keyboardAttack) || Input.GetMouseButtonDown(0))
|
|
{
|
|
AttackNearestEnemy();
|
|
}
|
|
}
|
|
|
|
private void AttackNearestEnemy()
|
|
{
|
|
Enemy nearest = null;
|
|
float nearestDistance = attackRange;
|
|
Enemy[] enemies = FindObjectsOfType<Enemy>();
|
|
|
|
foreach (Enemy enemy in enemies)
|
|
{
|
|
if (enemy == null || enemy.IsDead)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
float distance = Vector3.Distance(transform.position, enemy.transform.position);
|
|
if (distance <= nearestDistance)
|
|
{
|
|
nearestDistance = distance;
|
|
nearest = enemy;
|
|
}
|
|
}
|
|
|
|
if (nearest != null)
|
|
{
|
|
nearest.TakeDamage(damage);
|
|
}
|
|
else if (NotificationUI.Instance != null)
|
|
{
|
|
NotificationUI.Instance.Show("Нет врага в радиусе атаки");
|
|
}
|
|
}
|
|
}
|