타워 기능 추가 및 개선
This commit is contained in:
@@ -75,6 +75,13 @@ public class BuildManager : MonoBehaviour
|
||||
{
|
||||
_ghostMaterial.color = canPlace ? new Color(0, 1, 0, 0.5f) : new Color(1, 0, 0, 0.5f);
|
||||
}
|
||||
|
||||
// 미리보기 타워의 사거리 표시기를 켭니다.
|
||||
TowerRangeOverlay overlay = _ghostInstance.GetComponentInChildren<TowerRangeOverlay>();
|
||||
if (overlay != null)
|
||||
{
|
||||
overlay.ShowRange(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,7 +127,7 @@ public class BuildManager : MonoBehaviour
|
||||
_ghostInstance = Instantiate(selectedTurret.ghostPrefab);
|
||||
|
||||
// [추가] 고스트의 크기도 데이터에 맞게 조정
|
||||
_ghostInstance.transform.localScale = new Vector3(selectedTurret.size.x, 5f, selectedTurret.size.y);
|
||||
_ghostInstance.transform.localScale = new Vector3(selectedTurret.size.x, 1f, selectedTurret.size.y);
|
||||
}
|
||||
|
||||
private void DestroyGhost()
|
||||
@@ -175,7 +182,7 @@ public class BuildManager : MonoBehaviour
|
||||
|
||||
// [추가] 토대의 비주얼 크기를 타워 사이즈에 맞게 조정
|
||||
// x와 z는 타워의 가로/세로 사이즈를 따르고, y(높이)는 1로 유지합니다.
|
||||
siteObj.transform.localScale = new Vector3(data.size.x, 5f, data.size.y);
|
||||
siteObj.transform.localScale = new Vector3(data.size.x, 1f, data.size.y);
|
||||
|
||||
// 4. 컴포넌트 존재 여부 체크
|
||||
ConstructionSite siteScript = siteObj.GetComponent<ConstructionSite>();
|
||||
|
||||
75
Assets/Scripts/Tower/AreaTowerAttack.cs
Normal file
75
Assets/Scripts/Tower/AreaTowerAttack.cs
Normal file
@@ -0,0 +1,75 @@
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class AreaTowerAttack : MonoBehaviour
|
||||
{
|
||||
[Header("Tower Settings")]
|
||||
public float range = 15f; // 넓은 사거리
|
||||
public float fireRate = 0.5f; // 발사 속도가 느림 (큰 데미지)
|
||||
public GameObject explosiveProjectilePrefab; // 새로운 폭발 투사체 프리팹
|
||||
public Transform firePoint; // 투사체가 발사될 위치
|
||||
|
||||
private float _fireCountdown = 0f;
|
||||
private Transform _target;
|
||||
|
||||
void Update()
|
||||
{
|
||||
UpdateTarget(); // 가장 가까운 적 찾기
|
||||
|
||||
if (_target == null) return;
|
||||
|
||||
// 발사 간격 관리
|
||||
if (_fireCountdown <= 0f)
|
||||
{
|
||||
AreaShoot();
|
||||
_fireCountdown = 1f / fireRate;
|
||||
}
|
||||
|
||||
_fireCountdown -= Time.deltaTime;
|
||||
}
|
||||
|
||||
void UpdateTarget()
|
||||
{
|
||||
GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemy");
|
||||
float shortestDistance = Mathf.Infinity;
|
||||
GameObject nearestEnemy = null;
|
||||
|
||||
foreach (GameObject enemy in enemies)
|
||||
{
|
||||
float distanceToEnemy = Vector3.Distance(transform.position, enemy.transform.position);
|
||||
if (distanceToEnemy < shortestDistance)
|
||||
{
|
||||
shortestDistance = distanceToEnemy;
|
||||
nearestEnemy = enemy;
|
||||
}
|
||||
}
|
||||
|
||||
if (nearestEnemy != null && shortestDistance <= range)
|
||||
{
|
||||
_target = nearestEnemy.transform;
|
||||
}
|
||||
else
|
||||
{
|
||||
_target = null;
|
||||
}
|
||||
}
|
||||
|
||||
void AreaShoot()
|
||||
{
|
||||
// 타겟의 현재 위치를 폭발 투사체의 목표 지점으로 설정
|
||||
GameObject projectileGo = Instantiate(explosiveProjectilePrefab, firePoint.position, firePoint.rotation);
|
||||
ExplosiveProjectile explosiveProjectile = projectileGo.GetComponent<ExplosiveProjectile>();
|
||||
|
||||
if (explosiveProjectile != null)
|
||||
{
|
||||
// 투사체에게 '목표 지점'을 전달 (추적 아님!)
|
||||
explosiveProjectile.SetTargetPosition(_target.position);
|
||||
}
|
||||
}
|
||||
|
||||
void OnDrawGizmosSelected()
|
||||
{
|
||||
Gizmos.color = Color.red;
|
||||
Gizmos.DrawWireSphere(transform.position, range);
|
||||
}
|
||||
}
|
||||
58
Assets/Scripts/Tower/ExplosiveProjectile.cs
Normal file
58
Assets/Scripts/Tower/ExplosiveProjectile.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class ExplosiveProjectile : MonoBehaviour
|
||||
{
|
||||
public float speed = 10f;
|
||||
public float explosionRadius = 3f;
|
||||
public float damage = 50f;
|
||||
public GameObject explosionEffectPrefab;
|
||||
|
||||
// 유니티 인스펙터에서 "Enemy" 레이어를 선택할 수 있게 합니다.
|
||||
public LayerMask enemyLayer;
|
||||
|
||||
private Vector3 _targetPosition;
|
||||
|
||||
public void SetTargetPosition(Vector3 position)
|
||||
{
|
||||
_targetPosition = position;
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
transform.position = Vector3.MoveTowards(transform.position, _targetPosition, speed * Time.deltaTime);
|
||||
|
||||
if (Vector3.Distance(transform.position, _targetPosition) < 0.1f)
|
||||
{
|
||||
Explode();
|
||||
}
|
||||
}
|
||||
|
||||
void Explode()
|
||||
{
|
||||
if (explosionEffectPrefab != null)
|
||||
{
|
||||
Instantiate(explosionEffectPrefab, transform.position, transform.rotation);
|
||||
}
|
||||
|
||||
// 핵심 수정: enemyLayer에 해당하는 오브젝트만 감지합니다.
|
||||
Collider[] colliders = Physics.OverlapSphere(transform.position, explosionRadius, enemyLayer);
|
||||
|
||||
foreach (Collider hitCollider in colliders)
|
||||
{
|
||||
IDamageable damageable = hitCollider.GetComponentInParent<IDamageable>();
|
||||
if (damageable != null)
|
||||
{
|
||||
damageable.TakeDamage(damage);
|
||||
Debug.Log($"[폭발 적중] {hitCollider.name}에게 {damage} 데미지!");
|
||||
}
|
||||
}
|
||||
|
||||
Destroy(gameObject);
|
||||
}
|
||||
|
||||
void OnDrawGizmosSelected()
|
||||
{
|
||||
Gizmos.color = Color.yellow;
|
||||
Gizmos.DrawWireSphere(transform.position, explosionRadius);
|
||||
}
|
||||
}
|
||||
43
Assets/Scripts/Tower/TowerRangeOverlay.cs
Normal file
43
Assets/Scripts/Tower/TowerRangeOverlay.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class TowerRangeOverlay : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private GameObject _rangeSprite;
|
||||
private TowerAttack _towerAttack;
|
||||
private AreaTowerAttack _areaTowerAttack;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
_towerAttack = GetComponentInParent<TowerAttack>();
|
||||
_areaTowerAttack = GetComponentInParent<AreaTowerAttack>();
|
||||
ShowRange(false);
|
||||
}
|
||||
|
||||
public void UpdateRangeScale()
|
||||
{
|
||||
if (_rangeSprite == null) return;
|
||||
|
||||
float currentRange = 0;
|
||||
if (_towerAttack != null) currentRange = _towerAttack.range;
|
||||
else if (_areaTowerAttack != null) currentRange = _areaTowerAttack.range;
|
||||
|
||||
// 1. BuildManager가 건드린 현재 오브젝트(고스트 루트)의 스케일을 가져옵니다.
|
||||
Vector3 myScale = transform.localScale;
|
||||
|
||||
// 2. 월드 크기 고정 계산
|
||||
// 월드에서 보여야 할 지름은 (currentRange * 2)입니다.
|
||||
// 자식의 스케일 * 부모의 스케일 = 월드 스케일이므로,
|
||||
// 자식의 스케일 = (원하는 월드 스케일) / 부모의 스케일 입니다.
|
||||
float finalScaleX = (currentRange * 2f) / myScale.x;
|
||||
float finalScaleZ = (currentRange * 2f) / myScale.z;
|
||||
|
||||
// 3. 자식(Sprite)에게 계산된 스케일 적용
|
||||
_rangeSprite.transform.localScale = new Vector3(finalScaleX, finalScaleZ, 1f);
|
||||
}
|
||||
|
||||
public void ShowRange(bool show)
|
||||
{
|
||||
_rangeSprite.SetActive(show);
|
||||
if (show) UpdateRangeScale();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user