84 lines
2.3 KiB
C#
84 lines
2.3 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using DarkTonic.MasterAudio;
|
||
using DragonLi.Core;
|
||
using Mirror;
|
||
using UnityEngine;
|
||
|
||
public class Explosion : NetworkBehaviour
|
||
{
|
||
[SoundGroup] public string explosionSound;
|
||
|
||
public GameObject[] explosions;
|
||
|
||
public float radius = 3f; // 检测半径
|
||
public LayerMask playerLayer; // 玩家所在的Layer,避免检测其他物体
|
||
private int _desTime = 2;
|
||
|
||
[SyncVar]
|
||
public EnemyType curType;
|
||
public void Init(EnemyType type)
|
||
{
|
||
curType = type;
|
||
foreach (var obj in explosions)
|
||
{
|
||
obj.SetActive(false);
|
||
}
|
||
|
||
_desTime = 2;
|
||
switch (curType)
|
||
{
|
||
case EnemyType.Mortar:
|
||
DamagePlayersInRange();
|
||
explosionSound = "1.19";
|
||
explosions[0].SetActive(true);
|
||
break;
|
||
case EnemyType.Grenade:
|
||
DamagePlayersInRange();
|
||
explosionSound = "1.34";
|
||
explosions[1].SetActive(true);
|
||
break;
|
||
case EnemyType.Npc:
|
||
explosions[2].SetActive(true);
|
||
break;
|
||
case EnemyType.Virus:
|
||
explosionSound = "1.39";
|
||
explosions[3].SetActive(true);
|
||
_desTime = 5;
|
||
break;
|
||
}
|
||
if (isClient)
|
||
{
|
||
MasterAudio.PlaySound3DAtVector3(explosionSound, transform.position);
|
||
}
|
||
MonoSingleton<CoroutineTaskManager>.Instance.WaitSecondTodo(() =>
|
||
{
|
||
NetworkServer.Destroy(gameObject);
|
||
}, _desTime, this);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 检测范围内的玩家并执行受伤逻辑
|
||
/// </summary>
|
||
public void DamagePlayersInRange()
|
||
{
|
||
Collider[] hits = Physics.OverlapSphere(transform.position, radius, playerLayer);
|
||
|
||
foreach (Collider hit in hits)
|
||
{
|
||
Player player = hit.GetComponent<Player>(); // 假设玩家脚本叫 Player
|
||
if (player != null)
|
||
{
|
||
player.ApplyDamage(0,null,transform); // 调用受伤方法
|
||
}
|
||
}
|
||
}
|
||
|
||
// 绘制检测范围方便调试
|
||
private void OnDrawGizmosSelected()
|
||
{
|
||
Gizmos.color = Color.red;
|
||
Gizmos.DrawWireSphere(transform.position, radius);
|
||
}
|
||
}
|