unity打飞机(一) 飞机的移动动画化切换,子弹的生成,超级子弹的生成和炸弹的数量,飞机音效

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class HeroMove : MonoBehaviour {

	//判断鼠标是否点击
	public bool isMouseDown = false;
	//当鼠标点击时获取当前坐标
	private Vector3 lastMousePosition = Vector3.zero;

	//飞机飞行图片切换动画
	private SpriteRenderer render;
	//飞机切换图画数组
	public Sprite[] heroSprite;
	//飞机切换图画数组
	public Sprite[] overHeroSprite;
	//飞机切换动画的时间
	private float time;

	//子弹生成位置
	public Transform bulletLocation;
	//子弹预设物
	public Transform bulletModel;
	//子弹生成时间
	private float bulletRate = 0.2f;
	private float bulletTime = 0;

	//超级子弹时间
	private int superBulletNum = 0;
	//超级子弹生成预设物
	public Transform superBullet;

	//炸弹数量
	private int bombNum = 0;

	//音频控件
	private AudioSource audioSource;
	//子弹音频播放
	public AudioClip bulletClip;
	//得到子弹音频播放
	public AudioClip getBulletClip;
	//炸弹音频播放
	public AudioClip bombClip;
	//得到炸弹音频播放
	public AudioClip getBombClip;

	//游戏结束音频播放
	public AudioClip overClip;

	private bool over = false;

	// Use this for initialization
	void Start () {
		render = GetComponent ();
		audioSource = GetComponent ();
	}
	
	// Update is called once per frame
	void Update () {
		//飞机的飞行图片切换
		time += Time.deltaTime;
		int heroTime = (int)(time / (1f / 10));
		int renderInedx = heroTime % 2;
		if (over) {
			if (renderInedx < overHeroSprite.Length) {
				render.sprite =  overHeroSprite [renderInedx];
			}else {
				Destroy (gameObject);
				over = false;
			}
		} else {
			if (renderInedx < heroSprite.Length) {
				render.sprite = heroSprite [renderInedx];
			} 
		}			

		//当鼠标左键按下时
		if (Input.GetMouseButtonDown(0)) {
			isMouseDown = true;
		}else if (Input.GetMouseButtonUp(0)) {
			//当鼠标左键抬起时 
			isMouseDown = false;
			lastMousePosition = Vector3.zero;
		}

		//当鼠标左键按下并且按下时间超过0f
		if (isMouseDown && Time.timeScale > 0f) {
			if (lastMousePosition != Vector3.zero) {
				//获取鼠标点击坐标和当前飞机左边的偏移量
				Vector3 offSet = Camera.main.ScreenToWorldPoint(Input.mousePosition) - lastMousePosition;
				//让飞机的坐标加上偏移量使移动
				transform.position += offSet;
				//限制坐标使飞机不能越界
				CheckPosition();

			}
			//屏幕坐标转游戏实际坐标
			lastMousePosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
		}

		//创建子弹
		bulletTime += Time.deltaTime;
		if (bulletTime > bulletRate) {
			if (superBulletNum > 0) {
				superBulletNum--;
			}
			CareBullet ();
			bulletTime = 0;
			//Time.timeScale = 0;
		}

	}

	//限制飞机范围
	void CheckPosition(){
		Vector3 heroPos = transform.position;
		// x +-2.8  y +-4.5
		heroPos.x = Mathf.Clamp(heroPos.x, -2.8f, 2.8f);
		heroPos.y = Mathf.Clamp(heroPos.y, -4.5f, 4.5f);
		transform.position = new Vector3 (heroPos.x, heroPos.y, 0f);
	}

	//生成子弹
	void CareBullet(){
		//子弹音效播放
		audioSource.clip = bulletClip;
		audioSource.Play();
		if (superBulletNum > 0) {
			//生成预设物
			var bu = Instantiate (superBullet);
			//设定预设物的坐标
			//bu.position = new Vector3(bulletLocation.position.x - 1.5f, bulletLocation.position.y - 1, 0f);
			bu.position = bulletLocation.position;

		} else {
			//生成预设物
			var bu = Instantiate (bulletModel);
			//设定预设物的坐标
			bu.position = bulletLocation.position;

		}
	}

	//触发器
	void OnTriggerEnter2D(Collider2D other){
		if (other.GetComponent ().type == DownType.SuperBullet) {
			//得到超级子弹音效播放
			audioSource.clip = getBulletClip;
			audioSource.Play ();
			superBulletNum = 50;
		} else if(other.GetComponent ().type == DownType.Bomb){
			//得到炸弹音效播放
			audioSource.clip = getBombClip;
			audioSource.Play ();
			bombNum++;
			ButtonAndText.bombNum = bombNum;
		} else if (other.tag == "Enemy") {
			//游戏结束音效播放
			audioSource.clip = overClip;
			audioSource.Play ();
			over = true;
			ButtonAndText.isOver = true;
		}
	}

	//点击炸弹
	public void OnClickBomb(){
		if (bombNum > 0) {
			audioSource.clip = bombClip;
			audioSource.Play ();
			bombNum--;
			ButtonAndText.bombNum = bombNum;
			GameObject[] enemyGO = GameObject.FindGameObjectsWithTag ("Enemy");
			for (int i = 0; i < enemyGO.Length; i++) {
				GameObject obj = enemyGO [i];
				obj.GetComponent ().EnemyBomb ();
			}
		}
	}

}

 

你可能感兴趣的:(Unity,3D)