mirror of
https://github.com/ryzij/Cube-Tower-Defense.git
synced 2026-07-14 01:56:57 +00:00
Теперь башня стреляет в первую очеред по ближайшему к дому врагу
This commit is contained in:
@@ -39,6 +39,8 @@ public class Enemy : MonoBehaviour
|
|||||||
get => _pathService;
|
get => _pathService;
|
||||||
set => _pathService = value;
|
set => _pathService = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int PathLenght => _path?.Count ?? -1;
|
||||||
|
|
||||||
public float Health
|
public float Health
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
public class Turret : MonoBehaviour
|
public class Turret : MonoBehaviour
|
||||||
{
|
{
|
||||||
@@ -24,6 +25,8 @@ public class Turret : MonoBehaviour
|
|||||||
|
|
||||||
private float _currentPrice;
|
private float _currentPrice;
|
||||||
|
|
||||||
|
private readonly Dictionary<Vector3, Enemy> _enemiesInRange = new();
|
||||||
|
|
||||||
public int Price => _price;
|
public int Price => _price;
|
||||||
public float Damage => _damage;
|
public float Damage => _damage;
|
||||||
public float Distance => _distance;
|
public float Distance => _distance;
|
||||||
@@ -86,10 +89,14 @@ public class Turret : MonoBehaviour
|
|||||||
|
|
||||||
private void Update()
|
private void Update()
|
||||||
{
|
{
|
||||||
SpawnBullet(Vector3.forward);
|
// SpawnBullet(Vector3.forward);
|
||||||
SpawnBullet(Vector3.back);
|
// SpawnBullet(Vector3.back);
|
||||||
SpawnBullet(Vector3.left);
|
// SpawnBullet(Vector3.left);
|
||||||
SpawnBullet(Vector3.right);
|
// SpawnBullet(Vector3.right);
|
||||||
|
|
||||||
|
var direction = GetMinPathEnemyDirection();
|
||||||
|
if (direction != Vector3.zero)
|
||||||
|
SpawnBullet(direction);
|
||||||
|
|
||||||
_reloadTimer += Time.deltaTime;
|
_reloadTimer += Time.deltaTime;
|
||||||
}
|
}
|
||||||
@@ -102,14 +109,50 @@ public class Turret : MonoBehaviour
|
|||||||
|
|
||||||
private void SpawnBullet(Vector3 direction)
|
private void SpawnBullet(Vector3 direction)
|
||||||
{
|
{
|
||||||
if (_reloadTimer < _reloadTime || !DetectEnemy(direction))
|
if (_reloadTimer < _reloadTime/* || !DetectEnemy(direction)*/)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
_bulletPrefab.Spawn(this, _bulletSpawnPoint.position, direction);
|
_bulletPrefab.Spawn(this, _bulletSpawnPoint.position, direction);
|
||||||
_reloadTimer = 0f;
|
_reloadTimer = 0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool DetectEnemy(Vector3 direction) =>
|
// private bool DetectEnemy(Vector3 direction) =>
|
||||||
Physics.Raycast(transform.position, direction, out var hit, _bulletPrefab.Distance + _distance) &&
|
// Physics.Raycast(transform.position, direction, out var hit, _bulletPrefab.Distance + _distance) &&
|
||||||
hit.transform.TryGetComponent(out Enemy _);
|
// hit.transform.TryGetComponent(out Enemy _);
|
||||||
|
|
||||||
|
private Enemy DetectEnemy(Vector3 direction)
|
||||||
|
{
|
||||||
|
if (Physics.Raycast(transform.position, direction, out var hit, _bulletPrefab.Distance + _distance) &&
|
||||||
|
hit.transform.TryGetComponent(out Enemy enemy))
|
||||||
|
return enemy;
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Vector3 GetMinPathEnemyDirection()
|
||||||
|
{
|
||||||
|
_enemiesInRange[Vector3.forward] = DetectEnemy(Vector3.forward);
|
||||||
|
_enemiesInRange[Vector3.back] = DetectEnemy(Vector3.back);
|
||||||
|
_enemiesInRange[Vector3.left] = DetectEnemy(Vector3.left);
|
||||||
|
_enemiesInRange[Vector3.right] = DetectEnemy(Vector3.right);
|
||||||
|
|
||||||
|
Vector3 direction = Vector3.zero;
|
||||||
|
Enemy minPathEnemy = null;
|
||||||
|
foreach (var (dir, enemy) in _enemiesInRange)
|
||||||
|
{
|
||||||
|
if (enemy == null)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (minPathEnemy == null || enemy.PathLenght < minPathEnemy.PathLenght)
|
||||||
|
{
|
||||||
|
direction = dir;
|
||||||
|
minPathEnemy = enemy;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (minPathEnemy == null)
|
||||||
|
return Vector3.zero;
|
||||||
|
|
||||||
|
return direction;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+9
-36
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user