Roman ff4639d2cf 任务:搭建基本的系统
1.编写普通小怪受击逻辑
(1.受击时获得受击方式和受击方向
(*:新建tweenNow变量记录正在播放的动画
(2.受击瞬间停止tweenNow(否则怪物的物理路径将被锁定在tween中
(*:向玩家类中添加字典,用来查询不同攻击方式的攻击倍率
(*:向怪物基类中添加CheckDead功能
(*:编写受击击飞效果,类似于player
(3.减少生命值,同时呼唤CheckDead
(4.若死亡,更改怪物状态为Dead,呼唤OnDead,编写OnDead,给予其范围内随机一个旋转角速度,同时关闭其碰撞体,同时开启一个协程,使其一定时间后被销毁。
(*:给普通怪物添加positionSource变量,记录游戏开始时怪物的位置
(*:给普通小怪类添加inPath变量,记录一下目前是否正在执行path动画
(5.若未死亡,则让怪物着地后Dotween到初始位置,结束后触发事件,重新开始巡逻
(*:修复速度无法控制怪物真实速度的问题
(*:我超,由于插件存在不可忽略的缺陷,要修改怪物的移动速度做不到自动化了,必须通过在Path组件自行输入持续时间,其实就是多做一个小学算数,呐
2021-11-28 23:34:14 +08:00

145 lines
4.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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