58 lines
1.5 KiB
C#
58 lines
1.5 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using DarkTonic.MasterAudio;
|
||
using DragonLi.Core;
|
||
using Mirror;
|
||
using UnityEngine;
|
||
|
||
public class Explosion : NetworkBehaviour
|
||
{
|
||
public GameObject[] explosions;
|
||
|
||
public float radius = 3f; // 检测半径
|
||
public LayerMask playerLayer; // 玩家所在的Layer,避免检测其他物体
|
||
public int damage = 10;
|
||
public void Init(int id)
|
||
{
|
||
foreach (var obj in explosions)
|
||
{
|
||
obj.SetActive(false);
|
||
}
|
||
explosions[id].SetActive(true);
|
||
switch (id)
|
||
{
|
||
case 0:
|
||
DamagePlayersInRange();
|
||
break;
|
||
}
|
||
MonoSingleton<CoroutineTaskManager>.Instance.WaitSecondTodo(() =>
|
||
{
|
||
NetworkServer.Destroy(gameObject);
|
||
}, 2f, 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(damage,null,transform); // 调用受伤方法
|
||
}
|
||
}
|
||
}
|
||
|
||
// 绘制检测范围方便调试
|
||
private void OnDrawGizmosSelected()
|
||
{
|
||
Gizmos.color = Color.red;
|
||
Gizmos.DrawWireSphere(transform.position, radius);
|
||
}
|
||
}
|