Commit c761176b by Tom Koole

reduce parallax tiles to 9 indefinitely

parent 6737dd62
......@@ -7,12 +7,30 @@ public class SpaceParallaxLayerBehavior : MonoBehaviour {
public GameObject m_StarLayerPrefab = null;
public float m_ParallaxMultiplier = 1.0f;
float m_Scale = 0.0f;
public GameObject m_Container;
List<SpaceParallaxTileBehavior> m_Tiles = new List<SpaceParallaxTileBehavior>();
// 3x3 parallax tiles around the camera, don't need more
SpaceParallaxTileBehavior[,] m_Tiles = new SpaceParallaxTileBehavior[3,3];
// Use this for initialization
void Start () {
m_Scale = m_StarLayerPrefab.transform.localScale.x;
Vector3 cameraPosition = Camera.main.gameObject.transform.position;
int xPos = (int)(cameraPosition.x / m_Scale);
int yPos = (int)(cameraPosition.y / m_Scale);
for (int y = -1; y <= 1; y++) {
for (int x = -1; x <= 1; x++) {
Vector3 realSpace = new Vector3 ((xPos + x) * m_Scale, (yPos + y) * m_Scale, 0);
GameObject obj = Instantiate<GameObject> (m_StarLayerPrefab);
obj.transform.parent = m_Container.transform;
SpaceParallaxTileBehavior pl = obj.GetComponent<SpaceParallaxTileBehavior> ();
pl.OriginalPosition = realSpace;
m_Tiles[y + 1, x + 1] = pl;
}
}
}
// Update is called once per frame
......@@ -20,7 +38,7 @@ public class SpaceParallaxLayerBehavior : MonoBehaviour {
Vector3 cameraPosition = Camera.main.gameObject.transform.position;
// add new tiles if necessary
AddTiles (cameraPosition);
UpdateTilePositions (cameraPosition);
// update the layer offset for each tile
Vector3 offset = cameraPosition * m_ParallaxMultiplier;
......@@ -30,7 +48,7 @@ public class SpaceParallaxLayerBehavior : MonoBehaviour {
}
// add new tiles if necessary
void AddTiles(Vector3 cameraPosition) {
void UpdateTilePositions(Vector3 cameraPosition) {
int xPos = (int)(cameraPosition.x / m_Scale);
int yPos = (int)(cameraPosition.y / m_Scale);
......@@ -38,21 +56,7 @@ public class SpaceParallaxLayerBehavior : MonoBehaviour {
for( int y = -1; y <= 1; y++) {
for( int x = -1; x <= 1; x++) {
Vector3 realSpace = new Vector3((xPos + x) * m_Scale, (yPos + y) * m_Scale, 0);
bool found = false;
foreach (SpaceParallaxTileBehavior tile in m_Tiles) {
if(closeEnough(tile.OriginalPosition, realSpace)) {
found = true;
break;
}
}
if(!found) {
GameObject obj = Instantiate<GameObject>(m_StarLayerPrefab);
SpaceParallaxTileBehavior pl = obj.GetComponent<SpaceParallaxTileBehavior>();
pl.OriginalPosition = realSpace;
m_Tiles.Add(pl);
}
m_Tiles [y + 1, x + 1].OriginalPosition = realSpace;
}
}
......@@ -61,6 +65,7 @@ public class SpaceParallaxLayerBehavior : MonoBehaviour {
bool closeEnough(Vector3 a, Vector3 b) {
return closeEnough (a.x, b.x) && closeEnough (a.y, b.y);
}
bool closeEnough(float a, float b) {
return (a >= b - m_Scale * 0.25f && a <= b + m_Scale * 0.25f);
}
......
fileFormatVersion: 2
guid: ea2a5216ffb8d4b319d09e8d7782120c
folderAsset: yes
DefaultImporter:
userData:
assetBundleName:
m_EditorVersion: 5.1.0f3
m_EditorVersion: 5.3.6f1
m_StandardAssetsVersion: 0
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment