358 lines
9.6 KiB
C#
358 lines
9.6 KiB
C#
using Lean.Pool;
|
|
using System.Collections;
|
|
using UnityEngine;
|
|
|
|
namespace DemonBoss.Magic
|
|
{
|
|
public class CrystalShooterAI : MonoBehaviour
|
|
{
|
|
[Header("Shooting Configuration")]
|
|
[Tooltip("Transform point from which projectiles are fired")]
|
|
public Transform muzzle;
|
|
|
|
[Tooltip("Fireball prefab (projectile with its own targeting logic)")]
|
|
public GameObject fireballPrefab;
|
|
|
|
[Tooltip("Seconds between shots")]
|
|
public float fireRate = 0.7f;
|
|
|
|
[Tooltip("Maximum number of shots before auto-despawn")]
|
|
public int maxShots = 10;
|
|
|
|
[Tooltip("Wait time after last shot before despawn")]
|
|
public float despawnDelay = 3f;
|
|
|
|
[Header("Rotation Configuration")]
|
|
[Tooltip("Yaw rotation speed in degrees per second")]
|
|
public float turnSpeed = 120f;
|
|
|
|
[Tooltip("Idle spin speed when no target (degrees/s, 0 = disabled)")]
|
|
public float idleSpinSpeed = 30f;
|
|
|
|
[Tooltip("Aiming accuracy in degrees (smaller = stricter)")]
|
|
public float aimTolerance = 5f;
|
|
|
|
[Header("Targeting (Turret-Side Only)")]
|
|
[Tooltip("Auto-find player on start by tag")]
|
|
public bool autoFindPlayer = true;
|
|
|
|
[Tooltip("Player tag to search for")]
|
|
public string playerTag = "Player";
|
|
|
|
[Tooltip("Max range for allowing shots")]
|
|
public float maxShootingRange = 50f;
|
|
|
|
[Header("Effects")]
|
|
[Tooltip("Enable or disable muzzle flash & sound effects when firing")]
|
|
public bool useShootEffects = true;
|
|
|
|
[Tooltip("Particle effect at shot (pooled)")]
|
|
public GameObject muzzleFlashPrefab;
|
|
|
|
[Tooltip("Shoot sound (played on AudioSource)")]
|
|
public AudioClip shootSound;
|
|
|
|
[Header("Debug")]
|
|
[Tooltip("Enable debug logging")]
|
|
public bool enableDebug = false;
|
|
|
|
[Tooltip("Show gizmos in Scene View")]
|
|
public bool showGizmos = true;
|
|
|
|
private Transform target;
|
|
private AudioSource audioSource;
|
|
private Coroutine shootingCoroutine;
|
|
private bool isActive = false;
|
|
private int shotsFired = 0;
|
|
private float lastShotTime = 0f;
|
|
private Transform crystalTransform;
|
|
|
|
private void Awake()
|
|
{
|
|
crystalTransform = transform;
|
|
|
|
audioSource = GetComponent<AudioSource>();
|
|
if (audioSource == null && shootSound != null)
|
|
{
|
|
audioSource = gameObject.AddComponent<AudioSource>();
|
|
audioSource.playOnAwake = false;
|
|
audioSource.spatialBlend = 1f;
|
|
}
|
|
|
|
if (muzzle == null)
|
|
{
|
|
// Try to find a child named "muzzle"; fallback to self
|
|
Transform muzzleChild = crystalTransform.Find("muzzle");
|
|
if (muzzleChild != null)
|
|
{
|
|
muzzle = muzzleChild;
|
|
if (enableDebug) Debug.Log("[CrystalShooterAI] Found muzzle child");
|
|
}
|
|
else
|
|
{
|
|
muzzle = crystalTransform;
|
|
if (enableDebug) Debug.LogWarning("[CrystalShooterAI] Using crystal center as muzzle");
|
|
}
|
|
}
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
if (enableDebug) Debug.Log("[CrystalShooterAI] Crystal activated");
|
|
|
|
if (autoFindPlayer && target == null)
|
|
FindPlayer();
|
|
|
|
StartShooting();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update tick: rotate towards target or idle spin.
|
|
/// </summary>
|
|
private void Update()
|
|
{
|
|
if (!isActive) return;
|
|
RotateTowardsTarget();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Attempts to find the player by tag (for turret-only aiming).
|
|
/// </summary>
|
|
private void FindPlayer()
|
|
{
|
|
GameObject player = GameObject.FindGameObjectWithTag(playerTag);
|
|
if (player != null)
|
|
{
|
|
SetTarget(player.transform);
|
|
if (enableDebug) Debug.Log("[CrystalShooterAI] Automatically found player");
|
|
}
|
|
else if (enableDebug)
|
|
{
|
|
Debug.LogWarning("[CrystalShooterAI] Cannot find player with tag: " + playerTag);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets the turret's aiming target (does NOT propagate to projectiles).
|
|
/// </summary>
|
|
public void SetTarget(Transform newTarget)
|
|
{
|
|
target = newTarget;
|
|
if (enableDebug && target != null)
|
|
Debug.Log($"[CrystalShooterAI] Set target: {target.name}");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Starts the timed shooting routine (fires until maxShots, then despawns).
|
|
/// </summary>
|
|
public void StartShooting()
|
|
{
|
|
if (isActive) return;
|
|
isActive = true;
|
|
shotsFired = 0;
|
|
|
|
shootingCoroutine = StartCoroutine(ShootingCoroutine());
|
|
if (enableDebug) Debug.Log("[CrystalShooterAI] Starting shooting");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Stops the shooting routine immediately.
|
|
/// </summary>
|
|
public void StopShooting()
|
|
{
|
|
isActive = false;
|
|
|
|
if (shootingCoroutine != null)
|
|
{
|
|
StopCoroutine(shootingCoroutine);
|
|
shootingCoroutine = null;
|
|
}
|
|
|
|
if (enableDebug) Debug.Log("[CrystalShooterAI] Stopped shooting");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Main shooting loop: checks aim/range → spawns fireball → waits fireRate.
|
|
/// After finishing, waits a short delay and despawns the turret.
|
|
/// </summary>
|
|
private IEnumerator ShootingCoroutine()
|
|
{
|
|
while (shotsFired < maxShots && isActive)
|
|
{
|
|
if (CanShoot())
|
|
{
|
|
FireFireball();
|
|
shotsFired++;
|
|
lastShotTime = Time.time;
|
|
}
|
|
|
|
yield return new WaitForSeconds(fireRate);
|
|
}
|
|
|
|
if (enableDebug) Debug.Log($"[CrystalShooterAI] Finished shooting ({shotsFired} shots)");
|
|
|
|
yield return new WaitForSeconds(despawnDelay);
|
|
DespawnCrystal();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Aiming/range gate for firing.
|
|
/// </summary>
|
|
private bool CanShoot()
|
|
{
|
|
if (target == null) return false;
|
|
|
|
float distanceToTarget = Vector3.Distance(crystalTransform.position, target.position);
|
|
if (distanceToTarget > maxShootingRange) return false;
|
|
|
|
Vector3 directionToTarget = (target.position - crystalTransform.position).normalized;
|
|
float angleToTarget = Vector3.Angle(crystalTransform.forward, directionToTarget);
|
|
|
|
return angleToTarget <= aimTolerance;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Spawns a fireball oriented towards the turret's current aim direction.
|
|
/// </summary>
|
|
private void FireFireball()
|
|
{
|
|
if (fireballPrefab == null || muzzle == null)
|
|
{
|
|
if (enableDebug) Debug.LogWarning("[CrystalShooterAI] Missing fireball prefab or muzzle");
|
|
return;
|
|
}
|
|
|
|
Vector3 shootDirection;
|
|
if (target != null)
|
|
{
|
|
Vector3 targetCenter = target.position + Vector3.up * 1f;
|
|
shootDirection = (targetCenter - muzzle.position).normalized;
|
|
}
|
|
else
|
|
{
|
|
shootDirection = crystalTransform.forward;
|
|
}
|
|
|
|
Vector3 spawnPosition = muzzle.position;
|
|
Quaternion spawnRotation = Quaternion.LookRotation(shootDirection);
|
|
|
|
LeanPool.Spawn(fireballPrefab, spawnPosition, spawnRotation);
|
|
|
|
PlayShootEffects();
|
|
|
|
if (enableDebug)
|
|
{
|
|
Debug.Log($"[CrystalShooterAI] Shot #{shotsFired + 1} at {spawnPosition} dir: {shootDirection}");
|
|
Debug.DrawRay(spawnPosition, shootDirection * 8f, Color.red, 2f);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Plays muzzle VFX and shoot SFX (if enabled).
|
|
/// </summary>
|
|
private void PlayShootEffects()
|
|
{
|
|
if (!useShootEffects) return;
|
|
|
|
if (muzzleFlashPrefab != null && muzzle != null)
|
|
{
|
|
GameObject flash = LeanPool.Spawn(muzzleFlashPrefab, muzzle.position, muzzle.rotation);
|
|
LeanPool.Despawn(flash, 2f);
|
|
}
|
|
|
|
if (audioSource != null && shootSound != null)
|
|
{
|
|
audioSource.PlayOneShot(shootSound);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Smooth yaw rotation towards target; idles by spinning when no target.
|
|
/// </summary>
|
|
private void RotateTowardsTarget()
|
|
{
|
|
if (target != null)
|
|
{
|
|
Vector3 directionToTarget = target.position - crystalTransform.position;
|
|
directionToTarget.y = 0f;
|
|
|
|
if (directionToTarget != Vector3.zero)
|
|
{
|
|
Quaternion targetRotation = Quaternion.LookRotation(directionToTarget);
|
|
crystalTransform.rotation = Quaternion.RotateTowards(
|
|
crystalTransform.rotation,
|
|
targetRotation,
|
|
turnSpeed * Time.deltaTime
|
|
);
|
|
}
|
|
}
|
|
else if (idleSpinSpeed > 0f)
|
|
{
|
|
crystalTransform.Rotate(Vector3.up, idleSpinSpeed * Time.deltaTime);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Despawns the turret via Lean Pool.
|
|
/// </summary>
|
|
public void DespawnCrystal()
|
|
{
|
|
if (enableDebug) Debug.Log("[CrystalShooterAI] Despawning crystal");
|
|
StopShooting();
|
|
LeanPool.Despawn(gameObject);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Forces immediate despawn (e.g., boss death).
|
|
/// </summary>
|
|
public void ForceDespawn()
|
|
{
|
|
if (enableDebug) Debug.Log("[CrystalShooterAI] Forced despawn");
|
|
DespawnCrystal();
|
|
}
|
|
|
|
/// <summary> Returns crystal state information. </summary>
|
|
public bool IsActive() => isActive;
|
|
|
|
public int GetShotsFired() => shotsFired;
|
|
|
|
public int GetRemainingShots() => Mathf.Max(0, maxShots - shotsFired);
|
|
|
|
public float GetTimeSinceLastShot() => Time.time - lastShotTime;
|
|
|
|
/// <summary>
|
|
/// Gizmos for range and aim visualization.
|
|
/// </summary>
|
|
private void OnDrawGizmosSelected()
|
|
{
|
|
if (!showGizmos) return;
|
|
|
|
Gizmos.color = Color.red;
|
|
Gizmos.DrawWireSphere(transform.position, maxShootingRange);
|
|
|
|
if (target != null)
|
|
{
|
|
Gizmos.color = Color.yellow;
|
|
Gizmos.DrawLine(transform.position, target.position);
|
|
|
|
if (muzzle != null)
|
|
{
|
|
Gizmos.color = Color.green;
|
|
Vector3 forward = transform.forward * 5f;
|
|
Vector3 right = Quaternion.AngleAxis(aimTolerance, transform.up) * forward;
|
|
Vector3 left = Quaternion.AngleAxis(-aimTolerance, transform.up) * forward;
|
|
|
|
Gizmos.DrawLine(muzzle.position, muzzle.position + right);
|
|
Gizmos.DrawLine(muzzle.position, muzzle.position + left);
|
|
Gizmos.DrawLine(muzzle.position, muzzle.position + forward);
|
|
}
|
|
}
|
|
|
|
if (muzzle != null)
|
|
{
|
|
Gizmos.color = Color.blue;
|
|
Gizmos.DrawWireSphere(muzzle.position, 0.2f);
|
|
}
|
|
}
|
|
}
|
|
} |