-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.cs
144 lines (120 loc) · 3.8 KB
/
player.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using UnityEngine;
using System.Collections;
using UnityEngine.Advertisements;
public class player : MonoBehaviour {
public int MoveDistance;
float startpos;
float endpos;
float diffrence;
bool move;
public GameObject playerShooter;
public GameObject playerBullet;
public float shootTimer = 0f;
public int positionX;
public Animator ani;
public bool alive;
public int scorem;
public AudioSource audioSource;
public AudioClip explosion;
public int mode;
public GameObject bmi;
public GameObject bossmi;
// Use this for initialization
void Start () {
ani = this.GetComponentInChildren<Animator>();
mode = 1;
alive = true;
audioSource = this.GetComponent<AudioSource> ();
bmi = GameObject.FindGameObjectWithTag ("block_mode");
if (bmi != null) {
mode = 2;
}
bossmi = GameObject.FindGameObjectWithTag ("boss_mode");
if (bossmi != null) {
mode = 3;
}
}
// Update is called once per frame
void Update () {
if (alive) {
Controls ();
scorem +=1;
}
transform.position = new Vector2 (positionX, transform.position.y);
if (shootTimer > 0) {
shootTimer -= 1 * Time.deltaTime;
}
}
void OnCollisionEnter2D(Collision2D coll){
if (coll.collider.gameObject.tag == "enemy") {
gameOver();
//Application.LoadLevel("restart_test_scene");
//GameObject.Destroy(gameObject);
}
}
void FixedUpdate(){
}
IEnumerator som(){
yield return new WaitForSeconds (2);
switch (mode) {
case 1:
Advertisement.Show ();
Application.LoadLevel ("restart_test_scene");
break;
case 2:
Application.LoadLevel ("block_restart_test_scene");
break;
case 3:
Application.LoadLevel("boss_restart");
break;
}
}
void Controls(){
if (Input.GetKeyDown (KeyCode.UpArrow) && transform.position.y < 3 ) {
//this.GetComponent<Rigidbody2D>().position = new Vector2(this.GetComponent<Rigidbody2D>().position.x,this.GetComponent<Rigidbody2D>().position.y + MoveDistance);
transform.position = new Vector2(this.transform.position.x,transform.position.y + MoveDistance);
}
if (Input.GetKeyDown (KeyCode.DownArrow)&& transform.position.y > -3 ) {
//this.GetComponent<Rigidbody2D>().position = new Vector2(this.GetComponent<Rigidbody2D>().position.x,this.GetComponent<Rigidbody2D>().position.y - MoveDistance);
transform.position = new Vector2(this.transform.position.x,transform.position.y - MoveDistance);
}
if (Input.GetKeyDown ("space")&& shootTimer <= 0 && alive) {
Instantiate (playerBullet, playerShooter.transform.position, playerShooter.transform.rotation);
shootTimer = 0.2f;
}
//touch controls
if (Input.touchCount > 0) {
Touch touch = Input.GetTouch (0);
switch (touch.phase) {
case TouchPhase.Began:
startpos = touch.position.y;
break;
case TouchPhase.Ended:
endpos = touch.position.y;
if (transform.position.y > -3) {
if (startpos - endpos > 20) {
this.GetComponent<Rigidbody2D> ().position = new Vector2 (this.GetComponent<Rigidbody2D> ().position.x, this.GetComponent<Rigidbody2D> ().position.y - MoveDistance);
}
}
if (transform.position.y < 3) {
if (startpos - endpos < -20) {
this.GetComponent<Rigidbody2D> ().position = new Vector2 (this.GetComponent<Rigidbody2D> ().position.x, this.GetComponent<Rigidbody2D> ().position.y + MoveDistance);
}
}
if(!(startpos - endpos <-20) && !(startpos - endpos > 20) &&(alive)){
Instantiate (playerBullet, playerShooter.transform.position, playerShooter.transform.rotation);
shootTimer = 0.25f;
}
break;
}
}
}
public void gameOver(){
alive = false;
audioSource.clip = explosion;
audioSource.Play();
StartCoroutine("som");
ani.SetBool("isDead",true);
this.GetComponent<Collider2D>().enabled = false;
}
}