This repository has been archived by the owner on Jun 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameBuilder.cs
128 lines (115 loc) · 4.16 KB
/
GameBuilder.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
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
#if UNITY_5_3
using UnityEngine.SceneManagement;
#endif
public class GameBuilder : MonoBehaviour {
public static float horizontalBounderies = 5.45f;
public static float handleSpeed = 5f;
public static float bricksAnimSpeed = 8f;
public static int destroyedBricks = 0;
public static float speed = 2f;
public static readonly Color baseColor = Color.green;
public static readonly Color secondaryColor = Color.blue;
Vector3 lastBallPos;
static GameObject gameGO ;
GameObject ball;
void Start () {
Build();
StartCoroutine("UnStuck");
}
void Update () {
if (ball.transform.position.y < -1f || destroyedBricks >= 21 )
Restart ();
}
void Restart(){
destroyedBricks = 0;
Destroy (gameGO);
Build ();
}
private void LevelComplete(){
Destroy (gameGO);
Build ();
}
void Build(){
gameGO = new GameObject();
//Player Assembly
GameObject player = GameObject.CreatePrimitive (PrimitiveType.Cube);
player.transform.position = Vector3.zero;
player.transform.localScale = new Vector3 (4f, 1f, 1f);
player.GetComponent<Renderer> ().material.color = Color.Lerp (baseColor, secondaryColor, .5f);
player.GetComponent<Collider> ().isTrigger = true;
player.AddComponent<PlayerController> ();
player.name = "Player";
player.GetComponent<BoxCollider> ().center = new Vector3 (0f, 0.45f);
player.GetComponent<BoxCollider> ().size = new Vector3 (1f, .1f, 1f);
//Camera Config
Camera.main.transform.position = new Vector3(0f, 5f,-10f);
Camera cam = Camera.main;
cam.clearFlags = CameraClearFlags.SolidColor;
cam.backgroundColor = new Color(.2f,.25f,.25f);
//Light Creation
GameObject light = new GameObject ();
light.name = "light";
light.AddComponent<Light> ();
Light lightComponent = light.GetComponent<Light> ();
lightComponent.type = LightType.Directional;
lightComponent.color = new Color (.9f, .9f, .8f);
light.transform.Rotate (45f, 0f, 0f);
light.transform.SetParent (gameGO.transform);
//Ball Config
ball = GameObject.CreatePrimitive(PrimitiveType.Sphere);
ball.transform.position = new Vector3 (0f, 1.1f);
ball.GetComponent<Renderer> ().material.color = Color.white;
ball.AddComponent<BallBehaviour> ();
ball.transform.SetParent (player.transform);
ball.AddComponent<Rigidbody> ().isKinematic = true;
// player is outplaced here to keep the ball as its parent
player.transform.SetParent (gameGO.transform);
//Brick Builder
GameObject brick = GameObject.CreatePrimitive(PrimitiveType.Cube);
brick.transform.localScale = new Vector3 (2f, 1f, 1f);
brick.GetComponent<Collider> ().isTrigger = true;
brick.AddComponent<BrickBehaviour> ();
GameObject bricksParent = new GameObject ();
bricksParent.name = "Bricks";
GameObject[] bricks = new GameObject[21];
int xPos = -4;
int yPos = 9;
for (int i = 0; i < bricks.Length; i++) {
if (xPos < 3)
xPos++;
else {
xPos = -3;
yPos--;
}
Vector3 pos = new Vector3 (2.1f * xPos, 1.1f * yPos, Random.Range(-.1f,.1f) );
bricks [i] = (GameObject)Instantiate (brick, pos, Quaternion.identity);
bricks [i].GetComponent<Renderer> ().material.color = Color.Lerp (Color.blue, Color.green, Random.Range(.3f,.7f));
bricks [i].transform.SetParent (bricksParent.transform);
}
bricksParent.transform.SetParent (gameGO.transform);
Destroy (brick);
lastBallPos = ball.transform.position;
}
///<summary>For misterious floathing point mathematical reasons, sometimes the ball gets stucked on the screen
///edges,. This coroutine handles it</summary>
IEnumerator UnStuck(){
//TODO: use dinamic values for the limits
while (true) {
if (BallBehaviour.isMoving) {
bool isYStuck = (Mathf.Abs (lastBallPos.y - ball.transform.position.y) < 0.1) &&
(Mathf.Abs(ball.transform.position.y) > 10f );
bool isXStuck = (Mathf.Abs (lastBallPos.x - ball.transform.position.x) < 0.1) &&
(ball.transform.position.x > 6.5f);
if (isYStuck || isXStuck) {
ball.transform.position = new Vector3 (0, 2f, 0f);
ball.GetComponent<BallBehaviour> ().SetBallSpeedDirection (Vector2.up);
}
lastBallPos = ball.transform.position;
}
yield return new WaitForSeconds(5f);
}
}
}