赞
踩
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Bullet : MonoBehaviour
- {
- private Rigidbody2D rigidbody;
- private SpriteRenderer spriteRenderer;
- // 是否击中
- private bool isHit = false;
- void Start()
- {
- rigidbody = GetComponent<Rigidbody2D>();
- spriteRenderer = GetComponent<SpriteRenderer>();
- // 施加一个向右的力,匀速运动
- rigidbody.AddForce(Vector2.right*300);
- }
-
- void Update()
- {
- if (isHit) return;
- // 朝Z轴旋转
- transform.Rotate(new Vector3(0,0,-15f));
- }
- private void OnTriggerEnter2D(Collider2D other)
- {
- if (other.tag == "Zombie")
- {
- isHit = true;
- // 击中后修改成击中图片
- spriteRenderer.sprite = GameManager.instance.GameConf.BulletHit;
- // 暂停自己的运动
- rigidbody.velocity = Vector2.zero;
- // 恢复重力下落
- rigidbody.gravityScale = 1;
- // 最后销毁(延迟调用)
- Invoke("Destroy",0.5f);
- }
- }
- private void Destroy()
- {
- Destroy(gameObject);
- }
- }

- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class ZombieManager : MonoBehaviour
- {
- public static ZombieManager Instance;
- private void Awake()
- {
- Instance = this;
- }
- private List<Zombie> zombies = new List<Zombie>();
- public void AddZombie(Zombie zombie)
- {
- zombies.Add(zombie);
- }
-
- /// <summary>
- /// 获取一个距离最近的僵尸
- /// </summary>
- /// <param name="lineNum">植物当前的行</param>
- /// <param name="pos">植物当前的坐标</param>
- /// <returns></returns>
- public Zombie GetZombieBylineMinDistance(int lineNum, Vector3 pos)
- {
- Zombie zombie = null;
- float dis = 10000;
- for (int i = 0; i < zombies.Count; ++i)
- {
- // 遍历所有僵尸中等于传进来的lineNum(植物当前的行),并且返回最小距离的僵尸
- if ((int)zombies[i].CurrGrid.Point.y == lineNum && Vector2.Distance(pos, zombies[i].transform.position) < dis)
- {
- dis = Vector2.Distance(pos, zombies[i].transform.position);
- zombie = zombies[i];
- }
- }
- return zombie;
- }
- }

- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- /// <summary>
- /// 植物的基类
- /// </summary>
- public class PeaShooter : PlantBase
- {
- public override float MaxHp
- {
- get { return 300; }
- }
- // 是否可以攻击
- private bool canAttack = true;
- // 攻击间隔
- private float attackCD = 1.4f;
- // 实例化子弹相对于豌豆射手的偏移量
- private Vector3 createBulletOffestPos = new Vector2(0.562f, 0.396f);
-
- protected override void OnInitForPlace()
- {
- // 每0.2s攻击
- InvokeRepeating("Attack",0,0.2f);
- }
-
- /// <summary>
- /// 攻击方法-循环检测
- /// </summary>
- private void Attack()
- {
- if (canAttack == false) return;
- // 从僵尸管理器获取一个离我最近的僵尸
- Zombie zombie = ZombieManager.Instance.GetZombieBylineMinDistance((int) currGrid.Point.y, transform.position);
- // 前方没有僵尸则跳出
- if (zombie == null) return;
- // 僵尸必须在草坪上
- if ((int)zombie.CurrGrid.Point.x == 8 &&
- Vector2.Distance(zombie.transform.position, zombie.CurrGrid.Position) > 1.5f) return;
- // 此使开始发射,实例化一个子弹
- Bullet bullet = GameObject.Instantiate<GameObject>(GameManager.instance.GameConf.Bullet1,
- transform.position + createBulletOffestPos, Quaternion.identity, transform).GetComponent<Bullet>();
- CDEnter();
- canAttack = false;
- }
-
- private void CDEnter()
- {
- // 开始计算冷却
- StartCoroutine(CalCD());
- }
- // 计算冷却时间
- IEnumerator CalCD()
- {
- yield return new WaitForSeconds(attackCD);
- // 冷却时间结束
- canAttack = true;
- }
-
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。