-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHelicopterControl.cs
167 lines (145 loc) · 5.71 KB
/
HelicopterControl.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HelicopterControl : MonoBehaviour {
public float speed = 0f;
private Rigidbody2D rigidBody;
private Animator myAnimator;
private Collider2D collider_t;
public GameObject launch;
public GameObject spawnerReference;
private AudioSource sound;
private bool killSound;
// Variables for releasing package
float rand;
float toReleasePackage;
bool releasePackage;
int rand1;
bool packageReleased;
// Variables for releasing bomb
float bomberRandomVar;
bool ifBomber;
float whereToReleaseBomb;
bool bombReleased;
public GameObject bombPrefab;
public GameObject bombInstance;
public static float fracPara = 0.2f;
public static float fracBomb = 0.1f;
public bool comesFromLeft;
// Use this for initialization
void Start () {
speed = 5f;
myAnimator = gameObject.GetComponent<Animator>();
rigidBody = GetComponent<Rigidbody2D>();
collider_t = GetComponent<Collider2D>();
rand1 = Random.Range(0, 2);
if(rand1 == 0)
rand = Random.Range(0.2f, 0.35f);
else
rand = Random.Range(0.65f, 0.8f);
toReleasePackage = Random.Range(0f, 1f);
releasePackage = false;
if (toReleasePackage < fracPara)
releasePackage = true;
bomberRandomVar = Random.Range(0f, 1f);
if (bomberRandomVar < fracBomb)
{
ifBomber = true;
bombReleased = false;
myAnimator.SetBool("ifBomber", true);
whereToReleaseBomb = Random.Range(0.25f, 0.75f);
}
else
ifBomber = false;
killSound = false;
}
void assignDirection(bool a)
{
comesFromLeft = a;
if(!comesFromLeft)
{
SpriteRenderer tempSpriteRenderer = GetComponent<SpriteRenderer>();
tempSpriteRenderer.flipX = true;
if(ifBomber)
{
//tempSpriteRenderer.flipX = true;
}
}
}
// Update is called once per frame
void Update () {
if(comesFromLeft)
rigidBody.velocity = new Vector2(speed, 0);
else
rigidBody.velocity = new Vector2(-speed, 0);
// Calculate threshold point
Vector3 point = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width * rand, 0, 0));
// Script for releasing paratrooper
if ((releasePackage) & (!SpawningScript.stopGame) & (spawnerReference.activeSelf))
{
if ((rigidBody.transform.position.x >= point.x) & !packageReleased & comesFromLeft)
{
GameObject newLaunch = (GameObject) Instantiate(launch, new Vector3(rigidBody.transform.position.x, rigidBody.transform.position.y - 0.5f, rigidBody.transform.position.z), Quaternion.identity);
//Sprite Rendering
SpriteRenderer m_SpriteRenderer = newLaunch.GetComponent<SpriteRenderer>();
if(rand >= 0.65f)
m_SpriteRenderer.flipX = true;
Physics2D.IgnoreCollision(newLaunch.GetComponent<Collider2D>(), GetComponent<Collider2D>());
packageReleased = true;
}
if ((rigidBody.transform.position.x <= point.x) & !packageReleased & !comesFromLeft)
{
GameObject newLaunch = (GameObject)Instantiate(launch, new Vector3(rigidBody.transform.position.x, rigidBody.transform.position.y - 0.5f, rigidBody.transform.position.z), Quaternion.identity);
//Sprite Rendering
SpriteRenderer m_SpriteRenderer = newLaunch.GetComponent<SpriteRenderer>();
if (rand >= 0.65f)
m_SpriteRenderer.flipX = true;
Physics2D.IgnoreCollision(newLaunch.GetComponent<Collider2D>(), GetComponent<Collider2D>());
packageReleased = true;
}
}
if(ifBomber)
{
point = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width * whereToReleaseBomb, 0, 0));
if ((rigidBody.transform.position.x >= point.x) & !bombReleased & comesFromLeft)
{
bombInstance = (GameObject)Instantiate(bombPrefab, new Vector3(rigidBody.transform.position.x, rigidBody.transform.position.y - 0.5f, rigidBody.transform.position.z), Quaternion.identity);
bombReleased = true;
}
if ((rigidBody.transform.position.x <= point.x) & !bombReleased & !comesFromLeft)
{
bombInstance = (GameObject)Instantiate(bombPrefab, new Vector3(rigidBody.transform.position.x, rigidBody.transform.position.y - 0.5f, rigidBody.transform.position.z), Quaternion.identity);
bombReleased = true;
}
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.name == "Paratrooper(Clone)")
{
Physics2D.IgnoreCollision(collision.gameObject.GetComponent<Collider2D>(), GetComponent<Collider2D>());
}
if (collision.gameObject.tag == "anyBullet")
{
NewBehaviourScript.finalScore += 10;
Destroy(collision.gameObject);
myAnimator.SetBool("gotHit", true);
if (!killSound)
{
sound = GetComponent<AudioSource>();
sound.Play(0);
killSound = true;
}
StartCoroutine(CollisionFunction());
//Destroy(gameObject);
}
}
IEnumerator CollisionFunction()
{
yield return new WaitForSeconds(0.5f);
Destroy(gameObject);
}
void OnBecameInvisible() {
Destroy(gameObject);
}
}