using System.Collections; using System.Collections.Generic; using UnityEngine; using Sirenix.OdinInspector; /// /// 伊斯兰小怪类 /// public class YiSiLan : Enemy { // _____ _ _ _ // | __ \ | | | (_) // | |__) | _| |__ | |_ ___ // | ___/ | | | '_ \| | |/ __| // | | | |_| | |_) | | | (__ // |_| \__,_|_.__/|_|_|\___| [FoldoutGroup("其他",false,0)][Header("怪物死后旋转速度的随机区间")] public float deadRotationRangeMax; [FoldoutGroup("其他",false,0)] public float deadRotationRangeMin; /// /// 抓到玩家后多长事件触发爆炸 /// [Header("抓到玩家后多长事件触发爆炸")][FoldoutGroup("伊斯兰")] public float boomTime; /// /// 被击飞力度的调整值 /// [Header("被攻击后击飞的力度调整值")][FoldoutGroup("其他",false,0)] public Vector2 hitToflyParameter; // _____ _ _ // | __ \ (_) | | // | |__) | __ ___ ____ _| |_ ___ // | ___/ '__| \ \ / / _` | __/ _ \ // | | | | | |\ V / (_| | || __/ // |_| |_| |_| \_/ \__,_|\__\___| private Rigidbody2D m_rigidbody; /// /// seek函数中的追踪目标 /// private Transform target; /// /// 记录此时玩家是否在爆炸范围内 /// private bool canHitPlayer; // _____ _ _ ____ _ // / ____| | | | _ \ | | // | | __ _| | | |_) | __ _ ___| | __ // | | / _` | | | _ < / _` |/ __| |/ / // | |___| (_| | | | |_) | (_| | (__| < // \_____\__,_|_|_|____/ \__,_|\___|_|\_\ void Start(){Init();}//初始化 void FixedUpdate(){if(state == State.seek)Seek();}//seek // _ _ _ // | \ | | | | // | \| | ___ _ __ _ __ ___ __ _| | // | . ` |/ _ \| '__| '_ ` _ \ / _` | | // | |\ | (_) | | | | | | | | (_| | | // |_| \_|\___/|_| |_| |_| |_|\__,_|_| private void Init(){ //初始化状态为Seek state = State.seek; //找到必要的组件 m_rigidbody = GetComponent(); target = FindObjectOfType().transform; } /// /// 寻找函数,每Fixed调用 /// private void Seek(){ //构造一个方向,1代表向右,-1代表向左 int dir = (target.position.x - transform.position.x > 0) ? 1:-1; //使刚体水平方向上获得速度 m_rigidbody.velocity = new Vector2(speed * dir, m_rigidbody.velocity.y); //矫正一下faceDir的问题,使其始终朝向玩家 transform.rotation = Quaternion. Euler (transform.rotation.x, ((dir == 1) ? 0:-180), transform.rotation.z); } /// /// 伊斯兰爆炸的时候Call这个,通过协程触发,因为爆炸有个延时 /// private void Boom(){ OnDead(); if(canHitPlayer){ FindObjectOfType().OnBeHit(ATK,(target.position.x - transform.position.x > 0) ? -1:1); //加一个扭矩,营造死亡的效果 m_rigidbody.AddTorque(Random.Range(deadRotationRangeMin,deadRotationRangeMax) * ((target.position.x - transform.position.x > 0) ? -1:1)); } } /// /// 被击飞的击飞效果处理 /// /// 被击方向 public void BeHitToFly(int dir){ m_rigidbody.velocity += new Vector2( //给予自身一个 -1 * dir * hitToflyParameter.x,//X方向为力度系数乘以受击方向 hitToflyParameter.y//Y方向为力度系数 //的绝对的速度 ); } /// /// 协程用,删除自己这个游戏物体 /// protected void Dead(){Destroy(gameObject);} // ______ _ // | ____| | | // | |____ _____ _ __ | |_ // | __\ \ / / _ \ '_ \| __| // | |___\ V / __/ | | | |_ // |______\_/ \___|_| |_|\__| protected override void OnTouchThePlayer(MyPlayer player){Invoke("Boom",boomTime);} protected override void OnDead(){ //标记当前状态为死亡 state = State.dead; //加一个扭矩,营造死亡的效果 m_rigidbody.AddTorque(Random.Range(deadRotationRangeMin,deadRotationRangeMax) * ((target.position.x - transform.position.x > 0) ? -1 : 1)); //关掉自己的碰撞体 GetComponent().enabled = false; //两秒后自己毁灭 Invoke("Dead",2f); } public override void OnBeHit(MyPlayer.AtkMethod hitMethod, int hitDir){ //让自己被击飞 BeHitToFly(hitDir); //结算生命值 HPLeft -= MyPlayer.atkMethodMagnification[hitMethod]; //看下死了没 //死了就记录下死亡时候的面部朝向,用来做死亡翻滚效果,然后再Call一下死亡事件 if(CheckDead()) {OnDead();} //被打飞了在着地前都不会挨打了 canBeHit = false; } // _____ _ _ _ _ // / ____| | | (_) (_) // | | ___ | | |_ ___ _ ___ _ __ // | | / _ \| | | / __| |/ _ \| '_ \ // | |___| (_) | | | \__ \ | (_) | | | | // \_____\___/|_|_|_|___/_|\___/|_| |_| //触发器代表炸弹范围,进入说明能炸到,离开说明炸不到了 protected override void OnTriggerEnter2D(Collider2D other){ base.OnTriggerEnter2D(other); canHitPlayer = true; } void OnTriggerExit2D(Collider2D other){canHitPlayer = false;} }