using System.Collections; using System.Collections.Generic; using UnityEngine; using Sirenix.OdinInspector; /// /// 敌人的基类,包含一些基本的功能和事件的虚函数 /// public class Enemy : MonoBehaviour { // _____ _ _ _ // | __ \ | | | (_) // | |__) | _| |__ | |_ ___ // | ___/ | | | '_ \| | |/ __| // | | | |_| | |_) | | | (__ // |_| \__,_|_.__/|_|_|\___| /// /// 生命值上限 /// [FoldoutGroup("属性")][Header("生命值上限")] public float HP; /// /// 攻击力 /// [FoldoutGroup("属性")][Header("攻击力")] public float ATK; /// /// 速度 /// [FoldoutGroup("属性")][Header("移动速度")] public float speed; /// /// 打死后掉多少金币 /// [FoldoutGroup("属性")][Header("掉落金币数")] public int coin; /// /// 怪物拥有的几种状态 /// public enum State{wander,seek,atk,dead}; /// /// 此时怪物能否被攻击 /// [FoldoutGroup("状态")][Header("当前是否能被攻击")][ReadOnly] public bool canBeHit = true; // _____ _ _ // | __ \ (_) | | // | |__) | __ ___ ____ _| |_ ___ // | ___/ '__| \ \ / / _` | __/ _ \ // | | | | | |\ V / (_| | || __/ // |_| |_| |_| \_/ \__,_|\__\___| /// /// 当前生命值 /// [ReadOnly][SerializeField][ProgressBar(0,10,0.15f,0.47f,0.74f)][FoldoutGroup("状态")] protected float HPLeft; /// /// 当前状态 /// [EnumPaging][SerializeField][ReadOnly][Header("当前状态")][FoldoutGroup("状态")] protected State state; // ______ _ // | ____| | | // | |____ _____ _ __ | |_ // | __\ \ / / _ \ '_ \| __| // | |___\ V / __/ | | | |_ // |______\_/ \___|_| |_|\__| /// /// 当怪物死的时候Call这个函数 /// protected virtual void OnDead(){} /// /// 当怪物触碰到玩家的时候Call这个 /// protected virtual void OnTouchThePlayer(MyPlayer player){} /// /// 当怪物被打的时候触发 /// /// 攻击方式,枚举类型,具体看MyPlayer /// 受击方向,-1左,1右 public virtual void OnBeHit(MyPlayer.AtkMethod hitMethod,int hitDir){} /// /// 当怪物发现玩家的时候Call这个 /// protected virtual void OnFindThePlayer(Transform target){} /// /// 当怪物着地的时候触发一次 /// protected virtual void OnRetouchedTheGround(){} // _ _ _ // | \ | | | | // | \| | ___ _ __ _ __ ___ __ _| | // | . ` |/ _ \| '__| '_ ` _ \ / _` | | // | |\ | (_) | | | | | | | | (_| | | // |_| \_|\___/|_| |_| |_| |_|\__,_|_| /// /// 看看死了没 /// protected bool CheckDead(){return !(HPLeft > 0);} // _____ _ _ _ _ // / ____| | | (_) (_) // | | ___ | | |_ ___ _ ___ _ __ // | | / _ \| | | / __| |/ _ \| '_ \ // | |___| (_) | | | \__ \ | (_) | | | | // \_____\___/|_|_|_|___/_|\___/|_| |_| protected void OnCollisionEnter2D(Collision2D other)//当有物体碰上 { if(other.collider.gameObject.TryGetComponent(out MyPlayer player)) {OnTouchThePlayer(player);}//如果创到的是玩家,则Call事件 //如果被镰刀创到,Call一下OnBeHit事件,传入攻击方式和攻击来袭方向 else if(other.collider.gameObject.TryGetComponent(out Sickle sickle)) {OnBeHit(MyPlayer.AtkMethod.镰刀, (transform.position.x - sickle.transform.position.x > 0) ? -1 : 1); Destroy(sickle.gameObject);} else if(other.gameObject.tag == "地面") {OnRetouchedTheGround();} } protected virtual void OnTriggerEnter2D(Collider2D other) { if(other.gameObject.TryGetComponent(out MyPlayer player)) {OnFindThePlayer(other.transform);}//如果监视范围出现玩家,则Call事件 } }