//怪物生成
using UnityEngine;
using System.Collections;
public struct WaveMsg{
//该波次生成的怪物
public GameObject monster;
//时间间隔
public float waveInterval;
//生成个数
public float count;
//生成间隔
public float interval;
//怪物血量
public float hp;
public WaveMsg(GameObject monster,float waveInterval,float count,float interval,float hp){
this.monster = monster;
this.waveInterval = waveInterval;
this.count = count;
this.interval = interval;
this.hp=hp;
}
}
public class MonsterInit : MonoBehaviour {
//怪物预设体
public GameObject[] monsters;
//波次信息
public WaveMsg[] waveMsg;
//每波怪之间的计时器
private float waveTimer;
//每个怪之间的时间计时器
private float monsterTimer;
//波次计时器
private int count;
//怪物个数计时器
private int monsterCount;
//游戏开关
private bool gameBegin=true;
void Awake(){
waveMsg = new WaveMsg[] {
new WaveMsg (monsters [0], 3, 3, 2,10),
new WaveMsg (monsters [1], 2, 5, 1.5f,15),
new WaveMsg (monsters [2], 2, 7, 1f,20),
new WaveMsg (monsters [3], 2, 9, 0.8f,25),
new WaveMsg (monsters [4], 2, 10, 0.6f,30)
};
}
void Update(){
if (gameBegin) {
//波次计时器计时
waveTimer += Time.deltaTime;
if (waveTimer > waveMsg [count].waveInterval) {
//如果当前生成怪的个数不够,继续生成
if (monsterCount < waveMsg [count].count) {
monsterTimer += Time.deltaTime;
if (monsterTimer > waveMsg [count].interval) {
//生成怪物
GameObject currentMaster=Instantiate (waveMsg [count].monster, transform.position, Quaternion.identity)as GameObject;
//给新生成的怪物设置血量值
currentMaster.GetComponent<Monster>().hp=waveMsg[count].hp;
//怪物个数++
monsterCount++;
//计时器清零
monsterTimer = 0;
}
} else {
//波次加一
count++;
if (count < waveMsg.Length) {
monsterCount = 0;
waveTimer = 0;
} else {
//怪物生成结束
gameBegin = false;
}
}
}
}
}
}
//怪物加减血
using UnityEngine;
using System.Collections;
public class Monster : MonoBehaviour{
private Transform end;
private NavMeshAgent nav;
//血量
public float hp=10;
//玩家活着
public bool monsterAlive = true;
void Awake () {
end = GameObject.FindWithTag ("End").transform;
nav = GetComponent<NavMeshAgent> ();
}
void Start () {
nav.SetDestination (end.position);//设置导航目标
}
void Update(){
if (monsterAlive) {
if (hp<=0) {
//关闭导航
nav.Stop ();
//关闭导航组件
nav.enabled=false;
//切换死亡动画
GetComponent<Animation> ().CrossFade ("Dead");
//死亡动画播放完以后销毁
Destroy (gameObject, 1.8f);
//玩家死亡标志位
monsterAlive = false;
}
}
}
}
//建设炮塔
using UnityEngine;
using System.Collections;
public class TowerInit : MonoBehaviour {
RaycastHit hit;
//炮塔预设体
public GameObject towerPrefab;
void Update () {
Ray r = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast(r, out hit)) {
//如果该物体名字中包含‘Base’字符串,且基座没有子物体
if (hit.collider.name.Contains("Base")&&hit.transform.childCount==0) {
if (Input.GetMouseButtonDown(0)) {
//生成炮塔
GameObject currentTower=Instantiate(towerPrefab,hit.transform.position+Vector3.up*2.7f,Quaternion.identity)as GameObject;
//设置炮塔为基座的子物体
currentTower.transform.SetParent (hit.transform);
}
}
}
}
}
//炮塔攻击
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class GaussAttack : MonoBehaviour {
//攻击队列
Queue<Transform> monsters;
//当前攻击目标
Transform currentTarget;
//炮筒旋转速度
public float turnSpeed=6;
//发射特效
private GameObject muzzle;
//炮塔伤害值
public float damage=30;
void Awake () {
monsters = new Queue<Transform> ();
muzzle = transform.Find ("Base/Turret/Muzzle_1").gameObject;
}
void Update(){
if (currentTarget) {
//转向
LookAatTargetSmooth ();
//给敌人减血
currentTarget.GetComponent<Monster>().hp-=Time.deltaTime*damage;
//如果当前目标已经死亡
if (!currentTarget.GetComponent<Monster>().monsterAlive) {
//切换下一个目标
TargetSwitch ();
}
}
}
/// <summary>
/// 平滑旋转看向敌人
/// </summary>
void LookAatTargetSmooth(){
//方向向量
Vector3 dir=currentTarget.position-transform.position;
//目标旋转
Quaternion qua= Quaternion.LookRotation (dir);
//先找到炮筒
Transform gun = transform.GetChild (0).GetChild (0);
//炮筒转向目标四元数
gun.rotation=Quaternion.Lerp(gun.rotation,qua,Time.deltaTime*turnSpeed);
}
void OnTriggerEnter (Collider other) {
if (other.tag=="Monster") {
//进队列
monsters.Enqueue (other.transform);
//如果队列中没有攻击目标,即为空
if (currentTarget==null) {
TargetSwitch ();
}
}
}
void OnTriggerExit(Collider other){
//如果是怪物,且在队列内
if (other.tag=="Monster"&&other.transform==currentTarget) {
//切换下一个目标
TargetSwitch ();
}
}
/// <summary>
/// 切换目标
/// </summary>
void TargetSwitch(){
//如果队列中有数据
if (monsters.Count > 0) {
//队列中出来的怪物即为当前的攻击目标
currentTarget = monsters.Dequeue ();
//有怪开枪
muzzle.SetActive (true);
} else {
currentTarget = null;
//没怪停止开枪
muzzle.SetActive (false);
}
}
}
//游戏结束
using UnityEngine;
using System.Collections;
public class GameOver : MonoBehaviour {
//血量
public int hP = 10;
// 触发
void OnTriggerEnter (Collider other) {
if (other.tag=="Monster") {
//hp减血操作
if (--hP<=0) {
Debug.Log ("GameOver");
}
//销毁怪物
Destroy (other.gameObject);
}
}
}
//照相机随场景移动
using UnityEngine;
using System.Collections;
public class CameraMove : MonoBehaviour {
private float hor,ver;
public float moveSpeed=3;
void Update(){
hor = Input.GetAxis ("Horizontal");
ver = Input.GetAxis ("Vertical");
transform.position += new Vector3 (hor, 0, ver) * Time.deltaTime * moveSpeed;
transform.position = new Vector3 (Mathf.Clamp (transform.position.x, 24f, 40f), transform.position.y, Mathf.Clamp (transform.position.z, 2f, 35f));
}
}
塔防_Demo
最新推荐文章于 2019-12-26 23:30:39 发布