forked from oussamabonnor1/Ball-Fall-game-Unity2D
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathController.cs
94 lines (72 loc) · 2.82 KB
/
Controller.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
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class Controller : MonoBehaviour {
public GameObject[] balls;
public GameObject[] rareItems;
public GameObject background;
public Sprite[] BackgroundsSprites;
public GameObject badView;
public Movement m;
public Text timer;
public Camera cam1;
float maxWidth;
public float timeLeft;
public bool startedPlaying;
// Use this for initialization
void Start()
{
startedPlaying = false;
background.GetComponent<SpriteRenderer>().sprite = BackgroundsSprites[PlayerPrefs.GetInt("bg")];
if (PlayerPrefs.GetInt("bg") == 0) PlayerPrefs.SetInt("bg", 1);
else PlayerPrefs.SetInt("bg", 0);
ResizeBackground(background);
Vector3 upperCorner = new Vector3(Screen.width, Screen.height, 0.0f);
Vector3 targetWidth = cam1.ScreenToWorldPoint(upperCorner);
float ballWidth = (balls[0].GetComponent<Renderer>().bounds.extents.x) / 2f;
maxWidth = targetWidth.x - ballWidth;
timeLeft = 60;
}
void ResizeBackground(GameObject background)
{
SpriteRenderer sr = background.GetComponent<SpriteRenderer>();
if (sr == null) return;
transform.localScale = new Vector3(1, 1, 1);
float width = sr.sprite.bounds.size.x;
float height = sr.sprite.bounds.size.y;
float worldScreenHeight = Camera.main.orthographicSize * 2.1f;
float worldScreenWidth = worldScreenHeight / Screen.height * Screen.width;
Vector3 xWidth = background.transform.localScale;
xWidth.x = worldScreenWidth / width;
background.transform.localScale = xWidth;
Vector3 yHeight = background.transform.localScale;
yHeight.y = worldScreenHeight / height;
background.transform.localScale = yHeight;
}
void FixedUpdate()
{
if (startedPlaying)
{
timeLeft -= Time.deltaTime;
if (timeLeft <= 0)
{
timer.text = ": 0";
}
else timer.text = ": " + Mathf.RoundToInt(timeLeft);
}
}
public IEnumerator Spawn()
{
while (timeLeft > 0)
{
Vector3 spawnPosition = new Vector3(Random.Range(-maxWidth, maxWidth), transform.position.y, 0.0f);
Quaternion spawnRotation = Quaternion.identity;
int luck = Random.Range(0, 5);
GameObject ball;
if (luck != 0) ball = balls[Random.Range(0, balls.Length)];
else ball = rareItems[Random.Range(0, rareItems.Length)];
Instantiate(ball, spawnPosition, spawnRotation);
yield return new WaitForSeconds(Random.Range(0.5f, 0.8f));
}
}
}