xi/Assets/scripts/Bomb.cs
杨号敬 1a8a42cc18 ore1
2024-11-29 15:53:13 +08:00

88 lines
2.5 KiB
C#

using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bomb : MonoBehaviour
{
public float harm;
public GameObject bomb;
public float explosionRadius = 500f; // 爆炸半径
public float maxDamage = 100f; // 最大伤害
private List<GameObject> allEnemies = new List<GameObject>();
private List<GameObject> allOres = new List<GameObject>();
private void Start()
{
Invoke("DesSelf", 1f);
GameObject[] allObjects = GameObject.FindObjectsOfType<GameObject>();
foreach (GameObject obj in allObjects)
{
if (obj.tag == "enemy")
{
allEnemies.Add(obj);
}
else if (obj.tag == "ore")
allOres.Add(obj);
}
}
private void Update()
{
allEnemies.Clear();
allOres.Clear();
GameObject[] allObjects = GameObject.FindObjectsOfType<GameObject>();
foreach (GameObject obj in allObjects)
{
if (obj.tag == "enemy")
{
allEnemies.Add(obj);
}
else if (obj.tag == "ore")
allOres.Add(obj);
}
}
void Explode()
{
// 遍历所有敌人对象,判断是否在爆炸半径内并造成伤害
foreach (GameObject enemyObj in allEnemies)
{
if (enemyObj == null) return;
float distance = Vector3.Distance(transform.position, enemyObj.transform.position);
if (distance <= explosionRadius)
{
Enemy enemy = enemyObj.GetComponent<Enemy>();
enemy.EnemyHpDown(maxDamage);
}
}
// 遍历所有矿石对象,判断是否在爆炸半径内并造成伤害
foreach (GameObject oreObj in allOres)
{
float distance = Vector3.Distance(transform.position, oreObj.transform.position);
if (distance <= explosionRadius)
{
Ore ore = oreObj.GetComponent<Ore>();
if (ore == null) return;
ore.OreHpDown(maxDamage);
}
}
}
void OnCollisionEnter(Collision collision)
{
Explode();
DesSelf();
}
void DesSelf()
{
Explode();
//爆炸特效
GameObject Bonmfire = Instantiate(bomb,this.gameObject.transform.position, this.gameObject.transform.rotation);
Destroy(Bonmfire, 1f);
camfollow camfollow=Camera.main.GetComponent<camfollow>();
camfollow.Shake();
Destroy(this.gameObject);
}
}