Skip to content

Commit e1531db

Browse files
authored
Add files via upload
1 parent 24ae986 commit e1531db

18 files changed

+830
-0
lines changed

Attracted.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using UnityEngine;
2+
using System.Collections;
3+
4+
class Attracted : MonoBehaviour
5+
{
6+
string text = ((int)(Random.value * 100)).ToString();
7+
8+
void Start()
9+
{
10+
gameObject.GetComponent<TextMesh>().text = text;
11+
}
12+
13+
public GameObject attractedTo;
14+
public float strengthOfAttraction = 1.0f;
15+
16+
void FixedUpdate ()
17+
{
18+
Vector3 direction = attractedTo.transform.position - transform.position;
19+
gameObject.GetComponent<Rigidbody2D>().AddForce(strengthOfAttraction * direction);
20+
21+
//Vector3 relativePos = (attractedTo.transform.position + new Vector3(0, 1.5f, 0)) - transform.position;
22+
//Quaternion rotation = Quaternion.LookRotation(relativePos);
23+
//Quaternion current = transform.localRotation;
24+
//transform.localRotation = Quaternion.Slerp(current, rotation, Time.deltaTime);
25+
//transform.Translate(0, 0, 3 * Time.deltaTime);
26+
27+
}
28+
/*
29+
float RotationSpeed = 100f;
30+
float OrbitDegrees = 2f;
31+
void Update()
32+
{
33+
//transform.Rotate(Vector3.up, RotationSpeed * Time.deltaTime);
34+
transform.RotateAround(attractedTo.transform.position, new Vector3(0, 0, 1), OrbitDegrees);
35+
}
36+
*/
37+
}

BetterObjectPool.cs

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
using UnityEngine;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
5+
// By Anish Dhesikan
6+
public abstract class BetterObjectPool : MonoBehaviour {
7+
8+
public GameObject objectPrefab;
9+
10+
// initialMax: How many objects should the pool initially contain?
11+
public int initialMax = 100;
12+
// minInPool: What is the smallest number of items this pool should contain at any point?
13+
public int minInPool = 20;
14+
15+
/*
16+
* -- How does it work? --
17+
* You can set 3 thresholds: lowerBound, middleBound, and upperBound.
18+
* At any time, if the percent of active objects from the pool hits
19+
* the lowerBound or upperBound thresholds, the object pool will begin to dynamically
20+
* create or destroy objects to get the number back to the middle threshold.
21+
* Try out the demo in ExampleScenes for more info.
22+
*/
23+
public float upperBound = 0.75f; // between 0 and 1
24+
public float middleBound = 0.5f; // between 0 and 1
25+
public float lowerBound = 0.25f; // between 0 and 1
26+
27+
28+
// maxInstantiatesPerFrame: How many times can Instantiate() be called in a single frame?
29+
// For mobile and for best performance in general, keep this number low.
30+
// Currently, numbers less than 1 are not supported.
31+
public int maxInstantiatesPerFrame = 1;
32+
33+
// maxDestroysPerFrame: How many times can Destroy() be called in a single frame?
34+
// For mobile and for best performance in general, keep this number low.
35+
// Currently, numbers less than 1 are not supported.
36+
public int maxDestroysPerFrame = 1;
37+
38+
protected HashSet<GameObject> activeObjectPool = new HashSet<GameObject>();
39+
protected Transform activeObjectPoolParent;
40+
protected List<GameObject> inactiveObjectPool = new List<GameObject>();
41+
protected Transform inactiveObjectPoolParent;
42+
43+
private int totalCount = 0;
44+
45+
private float percentActive = 1;
46+
47+
// Use this for initialization
48+
public virtual void Awake () {
49+
activeObjectPoolParent = new GameObject ("ActiveObjectPool " + objectPrefab.name).transform;
50+
inactiveObjectPoolParent = new GameObject ("InactiveObjectPool" + objectPrefab.name).transform;
51+
inactiveObjectPoolParent.gameObject.SetActive (false);
52+
InstantiateMultiple (Mathf.FloorToInt(initialMax * middleBound));
53+
}
54+
55+
// Update is called once per frame
56+
public virtual void Update () {
57+
UpdatePercentActive ();
58+
if (percentActive > upperBound) {
59+
StopAllCoroutines ();
60+
StartCoroutine (ScaleUp());
61+
} else if (percentActive < lowerBound) {
62+
StopAllCoroutines ();
63+
StartCoroutine (ScaleDown());
64+
}
65+
}
66+
67+
IEnumerator ScaleUp () {
68+
while (percentActive > middleBound) {
69+
//Mathf.FloorToInt (Mathf.Pow (totalCount, 0.5f))
70+
int numToInstantiate = Mathf.Min (maxInstantiatesPerFrame, Mathf.CeilToInt(totalCount / 200f));
71+
InstantiateMultiple (numToInstantiate);
72+
UpdatePercentActive ();
73+
yield return 0;
74+
}
75+
}
76+
77+
IEnumerator ScaleDown () {
78+
if (percentActive > 0 && totalCount > minInPool) {
79+
while (percentActive < middleBound) {
80+
DestroyMultiple (maxDestroysPerFrame);
81+
UpdatePercentActive ();
82+
yield return 0;
83+
}
84+
}
85+
}
86+
87+
public virtual bool ObjectIsActive (GameObject instance) {
88+
return activeObjectPool.Contains (instance);
89+
}
90+
91+
public virtual void RemoveInstanceFromPool (GameObject instance) {
92+
StopAllCoroutines ();
93+
activeObjectPool.Remove (instance);
94+
inactiveObjectPool.Add (instance);
95+
instance.transform.parent = inactiveObjectPoolParent;
96+
}
97+
98+
public virtual GameObject GetInstanceFromPool () {
99+
StopAllCoroutines ();
100+
GameObject curObject = null;
101+
if (inactiveObjectPool.Count <= 0) {
102+
InstantiateOne ();
103+
}
104+
105+
curObject = inactiveObjectPool [0] as GameObject;
106+
// curObject.SetActive (true);
107+
inactiveObjectPool.Remove (curObject);
108+
activeObjectPool.Add (curObject);
109+
curObject.transform.parent = activeObjectPoolParent;
110+
111+
return curObject;
112+
}
113+
114+
public virtual GameObject GetInstanceFromPool (Vector3 position, Quaternion rotation) {
115+
GameObject curObject = GetInstanceFromPool ();
116+
if (curObject != null) {
117+
curObject.transform.position = position;
118+
curObject.transform.rotation = rotation;
119+
}
120+
return curObject;
121+
}
122+
123+
// Instantiates, deactivates, and adds one to the pool
124+
private void InstantiateOne () {
125+
GameObject newObject = Instantiate (objectPrefab) as GameObject;
126+
// newObject.SetActive (false);
127+
inactiveObjectPool.Add (newObject);
128+
newObject.transform.parent = inactiveObjectPoolParent;
129+
totalCount++;
130+
}
131+
132+
private void InstantiateMultiple (int count) {
133+
for (int i = 0; i < count; i++) {
134+
InstantiateOne ();
135+
}
136+
}
137+
138+
private void DestroyOne () {
139+
if (inactiveObjectPool.Count > 0) {
140+
GameObject curObject = inactiveObjectPool [0] as GameObject;
141+
inactiveObjectPool.Remove (curObject);
142+
DestroyImmediate (curObject);
143+
totalCount--;
144+
}
145+
}
146+
147+
private void DestroyMultiple (int count) {
148+
for (int i = 0; i < count; i++) {
149+
DestroyOne ();
150+
}
151+
}
152+
153+
public string GetInfoText () {
154+
return "Percent Active: " + percentActive * 100 + "%" + "\n" +
155+
"Number Active: " + activeObjectPool.Count + "\n" +
156+
"Number Inactive: " + inactiveObjectPool.Count;
157+
}
158+
159+
private void UpdatePercentActive () {
160+
percentActive = (float) activeObjectPool.Count / totalCount;
161+
162+
163+
// if (percentActive < lowerBound) {
164+
// Debug.Log ("Percent Active: " + percentActive);
165+
// Debug.Log ("TotalCount: " + totalCount);
166+
// }
167+
}
168+
}

BlackHole.cs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using UnityEngine;
2+
using System.Collections;
3+
4+
public class BlackHole : MonoBehaviour {
5+
6+
public float divisorDelay = 13.52f;
7+
public float nextDivisor = 0f;
8+
public CircleCollider2D collider;
9+
10+
public static int rand;
11+
12+
float threshold;
13+
float scoreRadius;
14+
15+
void OnTriggerEnter2D(Collider2D col)
16+
{
17+
int entered;
18+
19+
if (GameObject.FindObjectOfType<ScoreManager>().hasLost)
20+
{
21+
return;
22+
}
23+
24+
if (int.TryParse(col.gameObject.GetComponent<TextMesh>().text, out entered))
25+
{
26+
if (entered % rand == 0)
27+
{
28+
GameObject.FindObjectOfType<ScoreManager>().score++;
29+
}
30+
else
31+
{
32+
GameObject.FindObjectOfType<ScoreManager>().score--;
33+
}
34+
}
35+
Destroy(col.gameObject);
36+
}
37+
38+
// Update is called once per frame
39+
void FixedUpdate () {
40+
41+
if (GameObject.FindObjectOfType<ScoreManager>().hasLost)
42+
{
43+
return;
44+
}
45+
46+
threshold = FindObjectOfType<BoxLauncher>().debugValue;
47+
collider = transform.GetComponent<CircleCollider2D>();
48+
int score = FindObjectOfType<ScoreManager>().score;
49+
if (score > 0)
50+
{
51+
scoreRadius = score / 100;
52+
53+
} else
54+
{
55+
scoreRadius = 0;
56+
}
57+
58+
collider.radius = 0.6f + (threshold / 2f) + scoreRadius;
59+
60+
nextDivisor -= Time.deltaTime;
61+
62+
if (nextDivisor <= 0)
63+
{
64+
nextDivisor = divisorDelay;
65+
rand = ((int)Random.Range(1, 9));
66+
gameObject.GetComponent<TextMesh>().text = rand.ToString();
67+
}
68+
}
69+
}

BlackHoleSprite.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using UnityEngine;
2+
using System.Collections;
3+
4+
public class BlackHoleSprite : MonoBehaviour {
5+
6+
Vector3 scale;
7+
float newScale;
8+
9+
// Update is called once per frame
10+
void FixedUpdate () {
11+
newScale = GameObject.FindObjectOfType<BlackHole>().collider.radius * 2;
12+
scale = new Vector3(newScale, newScale, newScale);
13+
transform.localScale = scale;
14+
}
15+
}

BoxLauncher.cs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
using UnityEngine;
2+
using System.Collections;
3+
4+
public class BoxLauncher : MonoBehaviour {
5+
6+
public GameObject objectPrefab;
7+
//public string currLauncher = "LeftLauncher";
8+
9+
public float fireDelay = 6.76f; //6.76 for triforce music, 7.164 for techno music
10+
public float fireVelocity = 22f;
11+
public float cooldown = 1.6f;
12+
public float nextFire = 0f;
13+
14+
public float spawnThreshold = 0.4f;
15+
public int frequency = 3;
16+
public FFTWindow fftWindow;
17+
public float debugValue;
18+
19+
private float[] samples = new float[1024]; //MUST BE A POWER OF TWO
20+
21+
/*
22+
// for moving the launcher as camera goes up
23+
void Update()
24+
{
25+
Vector3 pos = transform.position;
26+
float camYChange = Camera.main.GetComponent<CameraMover>().targetY -
27+
Camera.main.GetComponent<CameraMover>().transform.position.y;
28+
pos.y = Mathf.Lerp(transform.position.y, transform.position.y + camYChange, 1 * Time.deltaTime);
29+
transform.position = pos;
30+
}
31+
*/
32+
33+
void FixedUpdate()
34+
{
35+
if (nextFire > 0)
36+
{
37+
nextFire -= Time.deltaTime;
38+
} else
39+
{
40+
nextFire = fireDelay;
41+
}
42+
cooldown -= Time.deltaTime;
43+
}
44+
45+
// Update is called once per frame
46+
void Update() {
47+
48+
if (GameObject.FindObjectOfType<ScoreManager>().hasLost)
49+
{
50+
return;
51+
}
52+
53+
AudioListener.GetSpectrumData(samples, 0, fftWindow);
54+
55+
debugValue = samples[frequency];
56+
57+
if (nextFire <= 3.38) // 3.38 for triforce music, 3.582 for techno
58+
{
59+
if (cooldown <= 0 && samples[frequency] > spawnThreshold)
60+
{
61+
cooldown = 1.6f;
62+
GameObject copy;
63+
if (gameObject.transform.name == "TopLauncher")
64+
{
65+
copy = (GameObject)Instantiate(
66+
objectPrefab,
67+
transform.position,
68+
transform.rotation * Quaternion.Euler(0, 0, 180));
69+
} else
70+
{
71+
copy = (GameObject)Instantiate(
72+
objectPrefab,
73+
transform.position,
74+
transform.rotation);
75+
}
76+
copy.GetComponent<Rigidbody2D>().velocity = transform.rotation * new Vector2(0, fireVelocity);
77+
}
78+
}
79+
}
80+
}

CameraMover.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using UnityEngine;
2+
using System.Collections;
3+
4+
public class CameraMover : MonoBehaviour {
5+
6+
public float targetY = 0;
7+
8+
// Update is called once per frame
9+
void Update () {
10+
Vector3 pos = transform.position;
11+
pos.y = Mathf.Lerp(transform.position.y, targetY, 1 * Time.deltaTime);
12+
transform.position = pos;
13+
}
14+
}

0 commit comments

Comments
 (0)