religion/Assets/Scripts/YiSiLan.cs
Roman 7d2c965521 任务:搭建基本的系统
1.编写伊斯兰小怪逻辑
(1.继承于Enemy类
(2.初始化时直接为Seek状态
(3.每帧执行Seek函数,让其水平方向有冲向玩家的速度
(4.当触发Touch玩家事件,伊斯兰触发协程Boom
(*.给伊斯兰类添加触发器监听,当玩家进入触发器范围,标记可以炸到玩家;当玩家离开触发器,标记无法炸到玩家
(5.当Boom执行时,标记自身为dead,关闭自身碰撞体,抄一下NormalEnemy的被击飞函数,并使刚体开始旋转。同时判定玩家是否可被炸到,若能,击飞玩家并造成相对的伤害值。
(6.从NormalEnemy代码复制过来了受击逻辑
*:整理伊斯兰代码

至此,伊斯兰小怪的逻辑基本开发完毕

*修复攻击时能够转身的问题

2.编写屏幕和手柄震动相关逻辑
(1.设计震动类
(2.设计静态方法ShakeScream和ShakePad,其中ShakePad为协程,调用时请使用StartContinue。

3.为玩家起跳添加屏幕晃动和手柄震动
2021-12-05 00:40:50 +08:00

167 lines
5.9 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 YiSiLan : Enemy
{
// _____ _ _ _
// | __ \ | | | (_)
// | |__) | _| |__ | |_ ___
// | ___/ | | | '_ \| | |/ __|
// | | | |_| | |_) | | | (__
// |_| \__,_|_.__/|_|_|\___|
[FoldoutGroup("其他",false,0)][Header("怪物死后旋转速度的随机区间")]
public float deadRotationRangeMax;
[FoldoutGroup("其他",false,0)]
public float deadRotationRangeMin;
/// <summary>
/// 抓到玩家后多长事件触发爆炸
/// </summary>
[Header("抓到玩家后多长事件触发爆炸")][FoldoutGroup("伊斯兰")]
public float boomTime;
/// <summary>
/// 被击飞力度的调整值
/// </summary>
[Header("被攻击后击飞的力度调整值")][FoldoutGroup("其他",false,0)]
public Vector2 hitToflyParameter;
// _____ _ _
// | __ \ (_) | |
// | |__) | __ ___ ____ _| |_ ___
// | ___/ '__| \ \ / / _` | __/ _ \
// | | | | | |\ V / (_| | || __/
// |_| |_| |_| \_/ \__,_|\__\___|
private Rigidbody2D m_rigidbody;
/// <summary>
/// seek函数中的追踪目标
/// </summary>
private Transform target;
/// <summary>
/// 记录此时玩家是否在爆炸范围内
/// </summary>
private bool canHitPlayer;
// _____ _ _ ____ _
// / ____| | | | _ \ | |
// | | __ _| | | |_) | __ _ ___| | __
// | | / _` | | | _ < / _` |/ __| |/ /
// | |___| (_| | | | |_) | (_| | (__| <
// \_____\__,_|_|_|____/ \__,_|\___|_|\_\
void Start(){Init();}//初始化
void FixedUpdate(){if(state == State.seek)Seek();}//seek
// _ _ _
// | \ | | | |
// | \| | ___ _ __ _ __ ___ __ _| |
// | . ` |/ _ \| '__| '_ ` _ \ / _` | |
// | |\ | (_) | | | | | | | | (_| | |
// |_| \_|\___/|_| |_| |_| |_|\__,_|_|
private void Init(){
//初始化状态为Seek
state = State.seek;
//找到必要的组件
m_rigidbody = GetComponent<Rigidbody2D>();
target = FindObjectOfType<MyPlayer>().transform;
}
/// <summary>
/// 寻找函数每Fixed调用
/// </summary>
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);
}
/// <summary>
/// 伊斯兰爆炸的时候Call这个通过协程触发因为爆炸有个延时
/// </summary>
private void Boom(){
OnDead();
if(canHitPlayer){
FindObjectOfType<MyPlayer>().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));
}
}
/// <summary>
/// 被击飞的击飞效果处理
/// </summary>
/// <param name="dir">被击方向</param>
public void BeHitToFly(int dir){
m_rigidbody.velocity += new Vector2( //给予自身一个
-1 * dir * hitToflyParameter.x,//X方向为力度系数乘以受击方向
hitToflyParameter.y//Y方向为力度系数
//的绝对的速度
);
}
/// <summary>
/// 协程用,删除自己这个游戏物体
/// </summary>
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<BoxCollider2D>().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;}
}