本次加入了读报僵尸,首先导入读报僵尸素材,共有9种动画
创建预制体,并为每个预制体创建动画和脚本
预制体包括读报僵尸和读报僵尸的头、读报僵尸的身体三部分,分别加上动画和脚本。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class PaperZombie : ZombieBase
{
protected override int MaxHP => 700;
protected override float speed => 4;
protected override float attackValue => 100;
protected override GameObject Prefab => GameManager.Instance.GameConf.PaperZombie;
protected override bool isSpecial => true;
public override Vector2 offset => new Vector2(-0.6f,-0.4f);
private bool isLostPaper = false;
public override void SpecialMove()
{
// 如果当前网格为空跳过移动检测
if (currGrid == null) return;
currGrid = GridManager.Instance.GetGridByWorldPos(transform.position + (Vector3)offset);
// 当前网格中有植物 并且 他在我的左边 并且距离很近
// 而且他可以被僵尸吃
if (currGrid.HavePlant
&& CurrGrid.CurrPlantBase.ZombieCanEat
&& currGrid.CurrPlantBase.transform.position.x < transform.position.x + ((Vector3)offset).x
&& transform.position.x + ((Vector3)offset).x - currGrid.CurrPlantBase.transform.position.x < 0.3f)
{
// 攻击这个植物
State = ZombieState.Attack;
return;
}
// 如果我在最左边的网格 并且 我已经越过了它
else if (currGrid.Point.x == 0 && currGrid.Position.x - transform.position.x - ((Vector3)offset).x > 1f)
{
// 我们要走向终点 - 房子
Vector2 pos = transform.position + (Vector3)offset;
Vector2 target = new Vector2(-9.17f, -1.37f);
Vector2 dir = (target - pos).normalized * 3f;
transform.Translate((dir * (Time.deltaTime / 1)) / speed);
// 如果我距离终点很近,意味着游戏结束
if (Vector2.Distance(target, pos) < 0.05f)
{
// 触发游戏结束
LVManager.Instance.GameOver();
//BOSSLVmanager.Instance.GameOver();
}
return;
}
if (animator.GetCurrentAnimatorStateInfo(0).IsName("LostPaper")) return; //如果播放丢失报纸动画,不移动
else if (!animator.GetCurrentAnimatorStateInfo(0).IsName("HasPaperWalk") && //如果没有报纸 移动加速
!animator.GetCurrentAnimatorStateInfo(0).IsName("HasPaperAttack"))
transform.Translate((new Vector2(-1.33f * 2, 0) * (Time.deltaTime / 1)) / speed);
else
{
transform.Translate((new Vector2(-1.33f, 0) * (Time.deltaTime / 1)) / speed);
}
}
public override void InitZombieHpState()
{
zombieHpState = new ZombieHpState(
0,
new List<int>() { MaxHP, 400,100, },
new List<string>() { "HasPaperWalk", "LostPaperWalk", "LostHeadWalk" },
new List<string>() { "HasPaperAttack", "LostPaperAttack", "LostHeadAttack" },
new List<UnityAction>() { null, CheckLostPaper,CheckLostHead }
);
}
public override void OnDead()
{
if (!isSpecialDie) //如果不是特殊死法
{// 创建一个死亡身体
PaperZombie_DieBody body = PoolManager.Instance.GetObj(GameManager.Instance.GameConf.PaperZombie_Diebody).GetComponent<PaperZombie_DieBody>();
body.Init(animator.transform.position);
}
}
private void CheckLostPaper()
{
if (!isLostPaper)
{
// 报纸需要失去
isLostPaper = true;
// 播放失去报纸动画
animator.Play("LostPaper");
StartCoroutine(CheckLost());
}
}
IEnumerator CheckLost()
{
while (true)
{
yield return new WaitForSeconds(0.05f);
if (animator.GetCurrentAnimatorStateInfo(0).normalizedTime >= 1)
{
// 检测状态
CheckState();
}
}
}
/// <summary>
/// 检查掉头
/// </summary>
private void CheckLostHead()
{
if (!isLostHead)
{
// 头需要失去
isLostHead = true;
// 创建一个头
PaperZombie_Head head = PoolManager.Instance.GetObj(GameManager.Instance.GameConf.PaperZombie_Head).GetComponent<PaperZombie_Head>();
head.Init(animator.transform.position);
// 状态检测
CheckState();
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PaperZombie_Head : BaseEFObj
{
public override string AnimationName => "PaperZombie_Head";
public override GameObject PrefabForObjPool => GameManager.Instance.GameConf.PaperZombie_Head;
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PaperZombie_DieBody : BaseEFObj
{
public override string AnimationName => "PaperZombie_Diebody";
public override GameObject PrefabForObjPool => GameManager.Instance.GameConf.PaperZombie_Diebody;
/// <summary>
/// 用于炸死时的初始化
/// </summary>
public void InitForBoomDie(Vector2 pos)
{
Init(pos, "Zombie_BoomDie");
}
}