本次完成植物 杨桃的制作
该植物和射手类似,不同点是一次能射出五发子弹,而且方向各不相同
代码
杨桃
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Star : PlantBase
{
public override float MaxHp
{
get
{
return 580;
}
}
protected override float attackCD => 1.4f;
protected override int attackValue => 20;
// 是否可以攻击
private bool canAttack;
// 创建子弹的偏移量
private Vector3 creatBulletOffsetPos = new Vector2(0.562f, 0.386F);
private Vector3 creatBulletOffsetPos1= new Vector2(-0.562f, 0.386F);
private Vector3 creatBulletOffsetPos2 = new Vector2(0, 0.686F);
private Vector3 creatBulletOffsetPos3 = new Vector2(0.562f, -0.386F);
private Vector3 creatBulletOffsetPos4 = new Vector2(-0.462f, -0.686F);
protected override void OnInitForPlace()
{
canAttack = true;
// 可能要攻击
InvokeRepeating("Attack", 0, 0.2f);
}
/// <summary>
/// 攻击方法-循环检测
/// </summary>
private void Attack()
{
if (canAttack == false) return;
// 从僵尸管理器 获取一个离我最近的僵尸,寻找逻辑为左右和正上方的线以及左下和右下
ZombieBase zombie = ZombieManager.Instance.GetZombieByLineMinDistance((int)currGrid.Point.y, transform.position, (int)currGrid.Point.x);
// 没有僵尸 跳出
if (zombie == null) return;
// 僵尸必须在草坪上 否则跳出
if (zombie.CurrGrid.Point.x == 8 && Vector2.Distance(zombie.transform.position, zombie.CurrGrid.Position) > 1.5f) return;
// 从这里开始,都是可以正常攻击的
// 同时创建5颗子弹 并赋予不同的方向和速度
StarBullet bullet = PoolManager.Instance.GetObj(GameManager.Instance.GameConf.StarBullet).GetComponent<StarBullet>();
StarBullet bullet1 = PoolManager.Instance.GetObj(GameManager.Instance.GameConf.StarBullet).GetComponent<StarBullet>();
StarBullet bullet2 = PoolManager.Instance.GetObj(GameManager.Instance.GameConf.StarBullet).GetComponent<StarBullet>();
StarBullet bullet3 = PoolManager.Instance.GetObj(GameManager.Instance.GameConf.StarBullet).GetComponent<StarBullet>();
StarBullet bullet4 = PoolManager.Instance.GetObj(GameManager.Instance.GameConf.StarBullet).GetComponent<StarBullet>();
bullet.Init(attackValue, transform.position + creatBulletOffsetPos,0,1); //正右的
bullet1.Init(attackValue, transform.position + creatBulletOffsetPos1,0,-1); //正左的
bullet2.Init(attackValue, transform.position + creatBulletOffsetPos2,-1,0); //正上方的
bullet3.Init(attackValue, transform.position + creatBulletOffsetPos3,1,1); //右下
bullet4.Init(attackValue, transform.position + creatBulletOffsetPos4,1,-1); //左下
CDEnter();
canAttack = false;
}
/// <summary>
/// 进入CD
/// </summary>
private void CDEnter()
{
StartCoroutine(CalCD());
}
/// <summary>
/// 计算冷却时间
/// </summary>
IEnumerator CalCD()
{
yield return new WaitForSeconds(attackCD);
canAttack = true;
}
}
杨桃的子弹
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class StarBullet : MonoBehaviour
{
private Rigidbody2D rigidbody;
private SpriteRenderer spriteRenderer;
// 攻击力
private int attackValue;
// 是否击中
private bool isHit;
public void Init(int attackValue,Vector2 pos,float gravity,float direction) //direction 0表示不动,1向右,-1向左
{
transform.position = pos;
rigidbody = GetComponent<Rigidbody2D>();
spriteRenderer = GetComponent<SpriteRenderer>();
if(direction==1)
rigidbody.AddForce(Vector2.right*300);
else if(direction==-1)
rigidbody.AddForce(Vector2.left * 300);
this.attackValue = attackValue;
if(gravity==1)
{
rigidbody.AddForce(Vector2.down * 300);
}
else if (gravity == -1)
{
rigidbody.AddForce(Vector2.up * 300);
}
rigidbody.gravityScale = 0;
isHit = false;
}
void Update()
{
if (isHit) return;
if (transform.position.x>7.7F || transform.position.x < -11.77F || transform.position.y > 5.17F || transform.position.y < -5.6F)
{
Destroy();
return;
}
transform.Rotate(new Vector3(0,0,-15f));
}
private void OnTriggerEnter2D(Collider2D coll)
{
if (coll.GetComponentInParent<ZombieBase>() == null) return;
if (isHit) return;
if (coll.tag == "Zombie")
{
isHit = true;
// 播放僵尸被攻击的音效
if (coll.GetComponentInParent<ZombieBase>().name == "BucketheadZombie(Clone)" || coll.GetComponentInParent<ZombieBase>().name == "IceZombie(Clone)"
|| coll.GetComponentInParent<ZombieBase>().name == "ScreenDoorZombie(Clone)")
AudioManager.Instance.PlayEFAudio(GameManager.Instance.GameConf.SteelHurtByPea);
// 播放僵尸被攻击的音效
else
AudioManager.Instance.PlayEFAudio(GameManager.Instance.GameConf.ZombieHurtForPea);
// 让僵尸受伤
coll.GetComponentInParent<ZombieBase>().Hurt(attackValue);
// 暂停自身的运动
rigidbody.velocity = Vector2.zero;
// 销毁自身
Destroy();
}
}
private void Destroy()
{
PoolManager.Instance.PushObj(GameManager.Instance.GameConf.Bullet1,gameObject);
}
}
寻敌逻辑
/// <summary>
/// //获取同一横线和正上方以及左下方右下方最近的僵尸
/// </summary>
/// <returns></returns>
public ZombieBase GetZombieByLineMinDistance(int lineNum, Vector3 pos,int lineNumx)
{
ZombieBase zombie = null;
float dis = 10000;
for (int i = 0; i < zombies.Count; i++)
{
if ( ( zombies[i].CurrGrid.Point.y == lineNum || (zombies[i].CurrGrid.Point.x==lineNumx && zombies[i].CurrGrid.Point.y > lineNum)
|| ((zombies[i].CurrGrid.Point.x-lineNumx== zombies[i].CurrGrid.Point.y-lineNum)&&(zombies[i].CurrGrid.Point.y<lineNum)) //y=x
||(((zombies[i].CurrGrid.Point.x - lineNumx == lineNum-zombies[i].CurrGrid.Point.y) && (zombies[i].CurrGrid.Point.y < lineNum))) //y=-x
)
&& Vector2.Distance(pos, zombies[i].transform.position) < dis
)
{
dis = Vector2.Distance(pos, zombies[i].transform.position);
zombie = zombies[i];
}
}
return zombie;
}