using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Sirenix.OdinInspector;
///
/// 管理基督小怪召出来的闪电攻击
///
public class LightningAttackAdministrator : MonoBehaviour
{
// _____ _ _ _
// | __ \ | | | (_)
// | |__) | _| |__ | |_ ___
// | ___/ | | | '_ \| | |/ __|
// | | | |_| | |_) | | | (__
// |_| \__,_|_.__/|_|_|\___|
///
/// 单道闪电的持续时间
///
[Header("单道闪电的持续时间")]
public float lightingTime;
///
/// 两道闪电之间间隔的时间
///
[Header("两道闪电之间间隔的时间")]
public float lightingIntervalTime;
///
/// 攻击方向,产生这个闪电的怪物攻击时指定
///
[Header("攻击方向,产生这个闪电的怪物攻击时指定")][ReadOnly]
public int dir;
///
/// 这个攻击器的主人。请在发动攻击的时候指定。用来通知主人,本次攻击结束
///
public JiDu owner;
///
/// 基督小怪攻击硬直
///
public float Stiff;
// _____ _ _
// | __ \ (_) | |
// | |__) | __ ___ ____ _| |_ ___
// | ___/ '__| \ \ / / _` | __/ _ \
// | | | | | |\ V / (_| | || __/
// |_| |_| |_| \_/ \__,_|\__\___|
[SerializeField][ListDrawerSettings][ReadOnly]
private List lightings;
// _____ _ _ ____ _
// / ____| | | | _ \ | |
// | | __ _| | | |_) | __ _ ___| | __
// | | / _` | | | _ < / _` |/ __| |/ /
// | |___| (_| | | | |_) | (_| | (__| <
// \_____\__,_|_|_|____/ \__,_|\___|_|\_\
void Start(){
Init();
StartCoroutine("ATK");
}
// _ _ _
// | \ | | | |
// | \| | ___ _ __ _ __ ___ __ _| |
// | . ` |/ _ \| '__| '_ ` _ \ / _` | |
// | |\ | (_) | | | | | | | | (_| | |
// |_| \_|\___/|_| |_| |_| |_|\__,_|_|
///
/// 初始化函数
///
private void Init(){
//处理一下攻击器朝向问题
if(dir == -1) transform.localScale = new Vector3(
transform.localScale.x * -1,
transform.localScale.y,
transform.localScale.z
);
//初始化闪电列表,用来控制每一个小闪电
lightings = new List(transform.childCount);
for (int i = 0; i < transform.childCount; i++){
Transform temp = transform.GetChild(i);
lightings.Add(
temp.gameObject.AddComponent()
);
//初始化小闪电的主人和状态
lightings[i].state = Lighting.State.wait;
lightings[i].owner = this;
}
}
///
/// 攻击函数协程
///
private IEnumerator ATK(){
//按顺序遍历每一个小闪电
foreach(Lighting l in lightings){
//如果处于等待状态
if(l.state == Lighting.State.wait){
//如果不是第一个闪电,则需要等待每个闪电的间隔的时间
if(l != lightings[0])yield return new WaitForSeconds(lightingIntervalTime);
//等待结束后令闪电攻击
l.gameObject.SetActive(true);
l.state = Lighting.State.atk;
//然后攻击持续每个小闪电的持续时间
yield return new WaitForSeconds(lightingTime);
//等待结束后关闭这个小闪电
l.gameObject.SetActive(false);
l.state = Lighting.State.over;
//循环至下一个小闪电
}
}
//遍历结束后进入攻击硬直等待
yield return new WaitForSeconds(Stiff);
//等待结束后通知主人攻击结束,并销毁攻击器
owner.OnAtkEnd();
Destroy(gameObject);
}
// _______ _ _____ _
// |__ __| | |/ ____| |
// | | ___ ___ | | | | | __ _ ___ ___
// | |/ _ \ / _ \| | | | |/ _` / __/ __|
// | | (_) | (_) | | |____| | (_| \__ \__ \
// |_|\___/ \___/|_|\_____|_|\__,_|___/___/
///
/// 单道小闪电类,主要控制一下碰撞体
///
private class Lighting : MonoBehaviour
{
///
/// 闪电的状态种类
///
public enum State{wait,atk,over};
///
/// 闪电的当前状态
///
public State state;
///
/// 这道闪电的主人,一个攻击器
///
public LightningAttackAdministrator owner;
void OnTriggerEnter2D(Collider2D other){
if (other.TryGetComponent(out MyPlayer player)){
player.OnBeHit(owner.owner.ATK,
((owner.transform.position.x - player.transform.position.x > 0) ? 1 : -1)
);
}
}
}
}