Files
DefendNJ/Assets/_DefendNJ/Scripts/Guns/GunAiComponent.cs
2025-10-14 18:17:45 +08:00

217 lines
6.5 KiB
C#

using System.Collections;
using System.Collections.Generic;
using DragonLi.Core;
using DragonLi.Frame;
using EPOOutline;
using Mirror;
using UnityEngine;
public class GunAiComponent : MonoBehaviour
{
[Header("References")]
public Transform showPos; // 子弹和火光生成位置
public GameObject bulletPre; // 子弹预制体
public GameObject firePre; // 火焰特效预制体 (Muzzle Flash)
[Header("Settings")]
[Range(0f, 1f)]
public float hpPercentage = 0.1f; // 本组件血量占父体总血量的比例
public float fireInterval = 0.5f; // 射击间隔 (秒)
public float bulletSpeed = 20f; // 子弹飞行速度
public float aimRadius = 1.5f; // 以玩家为中心的随机子弹目标半径
public bool isLookPlayer;
public bool isQteComponent;//是否是Qte组件
private bool _isStartLookPlayer;
public bool isDead;
public bool isFire;
private float _nextShootTime;
//private Outlinable outlinable;
public Transform target;
void Awake()
{
// outlinable = GetComponent<Outlinable>();
// if (outlinable != null) outlinable.enabled = false;
}
void Start()
{
_nextShootTime = Time.time;
isDead = false;
isFire = false;
_isStartLookPlayer = false;
}
public void Play()
{
if (isDead) return;
isFire = true;
}
public void Stop()
{
isFire = false;
}
void Update()
{
if ( isDead||!isFire) return;
// if (isLookPlayer || _isStartLookPlayer)
// {
// // 朝向玩家
// Vector3 dir = (target.position - transform.position).normalized;
// transform.rotation = Quaternion.LookRotation(dir);
// }
// 射击控制
// if (Time.time >= _nextShootTime)
// {
// // 计算子弹目标点:以玩家位置为中心的随机范围
// Vector2 randCircle = Random.insideUnitCircle * aimRadius;
// Vector3 targetPos = target.position + new Vector3(randCircle.x, 0, randCircle.y);
// EnemyShoot(targetPos);
// _nextShootTime = Time.time + fireInterval;
// }
}
/// <summary>
/// 外部调用射击
/// </summary>
public void EnemyShoot(Vector3 targetPos)
{
if (isDead) return;
CmdFire(targetPos);
}
/// <summary>
/// 执行射击逻辑
/// </summary>
private void CmdFire(Vector3 targetPos)
{
// 火光特效
if (firePre != null && showPos != null)
{
GameObject fire=Instantiate(firePre, showPos.position, showPos.rotation, transform);
NetworkServer.Spawn(fire);
CoroutineTaskManager.Instance.WaitSecondTodo(() =>
{
NetworkServer.Destroy(fire);
}, 1f);
}
if (bulletPre == null || showPos == null )
return;
// 生成子弹
GameObject bullet = Instantiate(bulletPre, showPos.position, Quaternion.identity,transform);
NetworkServer.Spawn(bullet);
bullet.SetActive(true);
if (bullet.GetComponent<Bullet>())
{
bullet.GetComponent<Bullet>().OnSpawn(-1,targetPos,20);
}
Rigidbody rb = bullet.GetComponent<Rigidbody>();
if (rb != null)
{
// 忽略 Y 轴,只计算水平面方向
Vector3 fixedTargetPos = new Vector3(targetPos.x, showPos.position.y, targetPos.z);
Vector3 shootDir = (fixedTargetPos - showPos.position).normalized;
rb.velocity = shootDir * bulletSpeed;
}
CoroutineTaskManager.Instance.WaitSecondTodo(() =>
{
NetworkServer.Destroy(bullet);
}, 5f);
// 自动销毁子弹,避免泄漏
//Destroy(bullet, 5f);
}
public void StatQteAttack()
{
_isStartLookPlayer = true;
}
public void QteAttack(Vector3 targetPos)
{
EnemyShoot(targetPos);
}
public void StopQteAttack()
{
_isStartLookPlayer = false;
}
public float arcHeight = 2f; // 抛物线最高点高度
public float flightTime = 1.2f; // 导弹飞行时长
//抛物线攻击
// public void ShootMissile(Vector3 from, Vector3 to, float flightTime = 1.2f)
// {
// GameObject m = Instantiate(bulletPre, from, Quaternion.identity);
// LeviathanBullet arc = m.GetComponent<LeviathanBullet>();
// arc.Initialize(from, to, flightTime);
// }
//
// //弧度攻击
// public void FireThreeMissiles(Vector3 from, Vector3 to)
// {
// var go = Instantiate(bulletPre, from, Quaternion.identity);
// var sm = go.GetComponent<LeviathanBullet>();
// sm.Initialize(
// startPos: from,
// targetPos: to,
// flightTime: flightTime,
// arcHeight: arcHeight
// );
// }
//
// //激光攻击
// public IEnumerator FireQteMissiles()
// {
// Debug.LogError("显示激光");
// var go = Instantiate(bulletPre, showPos.position, Quaternion.identity);
// LineRenderer lr = go.GetComponent<LineRenderer>();
// if (lr != null)
// {
// lr.SetPosition(0, showPos.position);
// lr.SetPosition(1, GameManager.Ins.player.transform.position);
// }
// yield return new WaitForSeconds(1f);
// GameManager.Ins.player.GetComponent<IDamagable>().ApplyDamage(GameManager.Ins.player.GetComponent<Player>().Health, null, null);
// yield return new WaitForSeconds(1.5f);
// Destroy(go);
// }
//
//
// public void CmdFire2(Vector3 targetPos)
// {
// // 火光特效
// if (firePre != null && showPos != null)
// Instantiate(firePre, showPos.position, showPos.rotation, transform);
//
// if (bulletPre == null || showPos == null || _player == null)
// return;
//
// // 生成子弹
// GameObject bullet = Instantiate(bulletPre, showPos.position, Quaternion.LookRotation(targetPos - showPos.position));
// bullet.SetActive(true);
//
// // 设置飞行方向
// if (bullet.TryGetComponent<BossBullet>(out var enemyBullet))
// {
// enemyBullet.SetTarget(targetPos, bulletSpeed);
// }
//
// // 自动销毁子弹,避免泄漏
// Destroy(bullet, 5f);
// }
}