S老师 打飞机 学习

 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 /// <summary>
 5 /// 奖励
 6 /// </summary>
 7 public class Award : MonoBehaviour {
 8 
 9     public int type = 0; //0 gun   1 explode
10 
11     public float speed = 1.5f;
12 
13 
14     void Update(){
15         this.transform.Translate( Vector3.down*Time.deltaTime*speed );
16         if(this.transform.position.y<=-4.5f){
17             Destroy(this.gameObject);
18         }
19     }
20 }
Award
 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 
 5 /// <summary>
 6 /// 背景移动
 7 /// </summary>
 8 public class BackgroundTransform : MonoBehaviour {
 9 
10     public static float moveSpeed = 2f;
11     
12     void Update () {
13         this.transform.Translate( Vector3.down * moveSpeed * Time.deltaTime );
14         Vector3 postion = this.transform.position;
15         if(postion.y<=-8.52f){
16             this.transform.position = new Vector3(postion.x,postion.y+8.52f*2,postion.z );
17         }
18     }
19 }
BackgroundTransform
 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 /// <summary>
 5 /// 子弹
 6 /// </summary>
 7 public class Bullet : MonoBehaviour {
 8 
 9     public float speed = 2;
10     
11     // Update is called once per frame
12     void Update () {
13         transform.Translate( Vector3.up * speed *Time.deltaTime );
14         if(transform.position.y>4.3f){
15             Destroy(this.gameObject);
16         }
17     }
18 
19     void OnTriggerEnter2D(Collider2D other) {
20         if(other.tag=="Enemy"){
21             if(!other.GetComponent<Enemy>().isDeath){
22                 other.gameObject.SendMessage("BeHit");
23                 GameObject.Destroy(this.gameObject);
24             }
25         }
26     }
27 
28 }
Bullet
 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 public enum EnemyType{
 5     smallEnemy,
 6     middleEnemy,
 7     bigEnemy
 8 }
 9 
10 /// <summary>
11 /// 敌人
12 /// </summary>
13 public class Enemy : MonoBehaviour {
14 
15     public int hp = 1;
16 
17     public float speed = 2;
18 
19     public int score = 100;
20 
21     public EnemyType type= EnemyType.smallEnemy;
22 
23     public bool isDeath = false;
24 
25     public Sprite[] explosionSprites;
26 
27     private float timer=0;
28 
29     public int explosionAnimationFrame=10;
30 
31     private SpriteRenderer render;
32 
33     public float hitTimer = 0.2f;
34     private float resetHitTime ;
35 
36     public Sprite[] hitSprites;
37 
38     // Use this for initialization
39     void Start () {
40         render = this.GetComponent<SpriteRenderer>();
41 
42         resetHitTime=hitTimer;
43         hitTimer=0;
44     }
45     
46     // Update is called once per frame
47     void Update () {
48         this.transform.Translate( Vector3.down*speed*Time.deltaTime );
49         if(this.transform.position.y<=-5.6f){
50             Destroy(this.gameObject);
51         }
52 
53         if(isDeath){
54                 timer+=Time.deltaTime;// 0
55                 int frameIndex = (int)(timer/(1f/explosionAnimationFrame));
56                 if(frameIndex>=explosionSprites.Length){
57                     //destroy
58                     Destroy(this.gameObject);
59                 }else{
60                     render.sprite= explosionSprites[frameIndex];
61                 }
62         }else{
63             if(type==EnemyType.middleEnemy||type==EnemyType.bigEnemy){
64                 if(hitTimer>=0){
65                     hitTimer-=Time.deltaTime;
66 
67                     int frameIndex = (int)((resetHitTime-hitTimer)/(1f/explosionAnimationFrame));
68                     frameIndex%=2;
69                     render.sprite= hitSprites[frameIndex];
70                 }
71             }
72         }
73     }
74 
75     public void BeHit(){
76         hp-=1;
77         // explosion
78         if(hp<=0){
79             toDie();
80         }else{
81             hitTimer=resetHitTime;
82         }
83     }
84 
85     private void toDie(){
86         if(!isDeath){
87             isDeath=true;
88             GameManager._instance.score+=score;
89         }
90     }
91 }
Enemy
 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 public enum GameState{
 5     Runing,
 6     Pause
 7 }
 8 
 9 /// <summary>
10 /// 游戏管理
11 /// </summary>
12 public class GameManager : MonoBehaviour {
13 
14     public static GameManager _instance;
15 
16     public int score=0;
17 
18     private GUIText guiText;
19     public GameState gameState = GameState.Runing;
20 
21     void Awake(){
22         _instance=this;
23         guiText=GameObject.FindGameObjectWithTag("ScoreGUI").GetComponent<GUIText>();
24     }
25     
26     // Update is called once per frame
27     void Update () {
28         guiText.text="Score:"+score;
29     }
30     public void transfromGameState(){
31         if(gameState==GameState.Runing){
32             pauseGame();
33         }else if(gameState==GameState.Pause){
34             continueGame();
35         }
36     }
37 
38     public void pauseGame(){
39         Time.timeScale=0;// time.delatTime = 0
40         gameState=GameState.Pause;
41     }
42     public void continueGame(){
43         Time.timeScale=1;
44         gameState=GameState.Runing;
45     }
46 }
GameManager
 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 /// <summary>
 5 /// 游戏暂停
 6 /// </summary>
 7 public class GamePause : MonoBehaviour {
 8 
 9     void OnMouseUpAsButton() {
10         print ("click me!");
11         GameManager._instance.transfromGameState();
12 
13         GetComponent<AudioSource>().Play();
14     }
15 
16 }
GamePause
 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 /// <summary>
 5 /// 飞机的枪
 6 /// </summary>
 7 public class Gun : MonoBehaviour {
 8 
 9     public float rate =0.2f;
10 
11     public GameObject bullet;
12 
13 
14     public void fire(){
15         GameObject.Instantiate(bullet,transform.position,Quaternion.identity );
16     }
17 
18     public void openFire(){
19         InvokeRepeating("fire",1,rate);
20     }
21 
22     public void stopFire(){
23         CancelInvoke("fire");
24     }
25 
26 }
Gun
  1 using UnityEngine;
  2 using System.Collections;
  3 
  4 /// <summary>
  5 /// 飞机
  6 /// </summary>
  7 public class Hero : MonoBehaviour {
  8 
  9     public bool animation = true;
 10 
 11     public int frameCountPersconds = 10;
 12 
 13     public float timer  = 0;
 14 
 15     public Sprite[] sprites;
 16 
 17     public float superGunTime = 10f;
 18 
 19     public Gun gunTop;
 20     public Gun gunLeft;
 21     public Gun gunRight;
 22 
 23     private float resetSuperGunTime ;
 24 
 25     private SpriteRenderer spriteRender;
 26 
 27     private bool isMouseDown = false;
 28 
 29     private Vector3 lastMousePosition = Vector3.zero;
 30 
 31     private Transform hero;
 32 
 33     private int gunCount =1;
 34 
 35     void Start(){
 36         spriteRender = this.GetComponent<SpriteRenderer>();
 37         hero = GameObject.FindGameObjectWithTag("Player").transform;
 38 
 39         resetSuperGunTime = superGunTime;
 40         superGunTime=0;
 41 
 42         gunTop.openFire();
 43     }
 44 
 45     // Update is called once per frame
 46     void Update () {
 47         if(animation){
 48             timer+=Time.deltaTime;// 1f/frameCountPersconds
 49             int frameIndex = (int)(timer/(1f/frameCountPersconds));
 50             int frame = frameIndex%2;
 51             spriteRender.sprite = sprites[frame];
 52         }
 53         if(Input.GetMouseButtonDown(0)){
 54             isMouseDown=true;
 55         }
 56         if(Input.GetMouseButtonUp(0)){
 57             isMouseDown=false;
 58             lastMousePosition = Vector3.zero;
 59         }
 60         if(isMouseDown && GameManager._instance.gameState==GameState.Runing ){
 61             if(lastMousePosition!=Vector3.zero){
 62                 //Camera.main.ScreenToWorldPoint(Input.mousePosition)
 63                 //print (Camera.main.ScreenToWorldPoint(Input.mousePosition));
 64                 Vector3 offset = Camera.main.ScreenToWorldPoint(Input.mousePosition) -lastMousePosition;
 65 
 66                 transform.position = transform.position+offset;
 67                 checkPosition();
 68             }
 69             lastMousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
 70         }
 71         superGunTime-=Time.deltaTime;
 72         if(superGunTime>0){
 73             if(gunCount==1){
 74                 transformToSuperGun();
 75             }
 76         }else{
 77             if(gunCount==2){
 78                 tranformToNormalGun();
 79             }
 80         }
 81     }
 82 
 83     private void transformToSuperGun(){
 84         gunCount=2;
 85         gunLeft.openFire();
 86         gunRight.openFire();
 87         gunTop.stopFire();
 88     }
 89     private void tranformToNormalGun(){
 90         gunCount=1;
 91         
 92         gunLeft.stopFire();
 93         gunRight.stopFire();
 94         gunTop.openFire();
 95     }
 96 
 97 
 98     private void checkPosition(){
 99         //check x -2.22f +2.22f
100         // check y -3.9 3.4
101         Vector3 pos = transform.position;
102         float x = pos.x;
103         float y = pos.y;
104         if(x<-2.22f){
105             x=-2.22f;
106         }
107         if(x>2.22f){
108             x=2.22f;
109         }
110         if(y<-3.9f){
111             y=-3.9f;
112         }
113         if(y>3.4f){
114             y=3.4f;
115         }
116         transform.position= new Vector3(x,y,0);
117     }
118 
119     public void OnTriggerEnter2D(Collider2D collider){
120         if(collider.tag=="Award"){
121             GetComponent<AudioSource>().Play();
122             Award award = collider.GetComponent<Award>();
123             if(award.type==0){
124                 //transform gun
125                 superGunTime=resetSuperGunTime;
126                 Destroy(collider.gameObject);
127             }
128         }
129     }
130 }
Hero
 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 /// <summary>
 5 /// 敌人孵化
 6 /// </summary>
 7 public class Spawn : MonoBehaviour {
 8 
 9     public GameObject enemy0Prefab;
10     public GameObject enemy1Prefab;
11     public GameObject enemy2Prefab;
12 
13     
14     public GameObject awardType0Prefab;
15     public GameObject awardType1Prefab;
16     
17     public float enemy0Rate = 0.5f;
18     public float enemy1Rate = 5f;
19     public float enemy2Rate = 8f;
20 
21     public float awardType0Rate = 7;
22 
23     public float awardType1Rate = 10;
24 
25     // Use this for initialization
26     void Start () {
27         InvokeRepeating("createEnemy0",1,enemy0Rate);
28         InvokeRepeating("createEnemy1",3,enemy1Rate);
29         InvokeRepeating("createEnemy2",6,enemy2Rate);
30 
31         InvokeRepeating("createAwardType0",10,awardType0Rate);
32         InvokeRepeating("createAwardType1",10,awardType1Rate);
33 
34 
35     }
36     
37     // Update is called once per frame
38     void Update () {
39     
40     }
41     
42     public void createEnemy0(){
43         float x = Random.Range(-2.15f,2.15f);
44         GameObject.Instantiate(enemy0Prefab,new Vector3(x,transform.position.y,0),Quaternion.identity);
45         
46     }
47     public void createEnemy1(){
48         float x = Random.Range(-2.04f,2.04f);
49         GameObject.Instantiate(enemy1Prefab,new Vector3(x,transform.position.y,0),Quaternion.identity);
50         
51     }
52     public void createEnemy2(){
53         float x = Random.Range(-2.04f,2.04f);
54         GameObject.Instantiate(enemy2Prefab,new Vector3(x,transform.position.y,0),Quaternion.identity);
55         
56     }
57 
58 
59     public void createAwardType0(){
60         GetComponent<AudioSource>().Play();
61         float x = Random.Range(-2.1f,2.1f);
62         GameObject.Instantiate(awardType0Prefab,new Vector3(x,transform.position.y,0),Quaternion.identity);
63     }
64     public void createAwardType1(){
65         GetComponent<AudioSource>().Play();
66         float x = Random.Range(-2.1f,2.1f);
67         GameObject.Instantiate(awardType1Prefab,new Vector3(x,transform.position.y,0),Quaternion.identity);
68     }
69 }
Spawn

视频:https://pan.baidu.com/s/1jHTh3oy

项目:https://pan.baidu.com/s/1sljipdF

posted on 2017-03-06 16:40  void87  阅读(536)  评论(0编辑  收藏  举报

导航