using System.Collections; using System.Collections.Generic; using UnityEngine; using DG.Tweening; using Sirenix.OdinInspector; /// /// 炸弹类,控制木马喷射和召唤的炸弹 /// public class Bommer : MonoBehaviour { // _____ _ _ _ // | __ \ | | | (_) // | |__) | _| |__ | |_ ___ // | ___/ | | | '_ \| | |/ __| // | | | |_| | |_) | | | (__ // |_| \__,_|_.__/|_|_|\___| /// /// 可以被炸弹炸到的物体,实现一些炸与被炸的功能 /// public interface I_CanBeBoomedObj{ /// /// 被炸的时候触发 /// void BeBoomed(float atk, int dir); Transform ObjTransform(); } [HideInInspector] public Rigidbody2D m_rigidbody; // _____ _ _ // | __ \ (_) | | // | |__) | __ ___ ____ _| |_ ___ // | ___/ '__| \ \ / / _` | __/ _ \ // | | | | | |\ V / (_| | || __/ // |_| |_| |_| \_/ \__,_|\__\___| private TrojanHorse owner; /// /// 爆炸会受影响的东西,只能是伊斯兰、木马或者玩家 /// private List boomingObj; // _____ _ _ ____ _ // / ____| | | | _ \ | | // | | __ _| | | |_) | __ _ ___| | __ // | | / _` | | | _ < / _` |/ __| |/ / // | |___| (_| | | | |_) | (_| | (__| < // \_____\__,_|_|_|____/ \__,_|\___|_|\_\ void Start(){ Init(); } // _ _ _ // | \ | | | | // | \| | ___ _ __ _ __ ___ __ _| | // | . ` |/ _ \| '__| '_ ` _ \ / _` | | // | |\ | (_) | | | | | | | | (_| | | // |_| \_|\___/|_| |_| |_| |_|\__,_|_| private void Init(){ //找到必须的物体和组件 owner = FindObjectOfType(); boomingObj = new List(); m_rigidbody = GetComponent(); } /// /// 爆炸的瞬间执行 /// private void Boom(){ //对于每一个范围内的被炸物体 foreach(I_CanBeBoomedObj obj in boomingObj){ //执行被炸事件 obj.BeBoomed(owner.ATK, (obj.ObjTransform().position.x - transform.position.x > 0) ? 1 : 1 ); } //销毁自己 Destroy(gameObject); } // _____ _ _ _ _ // / ____| | | (_) (_) // | | ___ | | |_ ___ _ ___ _ __ // | | / _ \| | | / __| |/ _ \| '_ \ // | |___| (_) | | | \__ \ | (_) | | | | // \_____\___/|_|_|_|___/_|\___/|_| |_| //当与物体碰上 void OnCollisionEnter2D(Collision2D other){ //直接爆炸,不管是什么 Boom(); } //当有东西进入爆炸范围 void OnTriggerEnter2D(Collider2D other){ //看看是不是可被炸对象(对象需要实现被炸接口) if(other.TryGetComponent(out I_CanBeBoomedObj obj)){ //是则将其加入被炸对象列表 boomingObj.Add(obj); } } //当有东西离开爆炸范围 void OnTriggerExit2D(Collider2D other){ //看看是不是可被炸对象(对象需要实现被炸接口) if(other.TryGetComponent(out I_CanBeBoomedObj obj)){ //是则将其移除出被炸对象列表 boomingObj.Remove(obj); } } }