Created
March 27, 2015 00:06
-
-
Save iendsl/6ea5d5c0b592e31831ea to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class GridSystem : MonoBehaviour { | |
public static GridSystem grid; | |
public GameObject bottomLeftObject; | |
public GameObject topRightObject; | |
public float gridSize; | |
public int renderThingsThisManyGridSectorsAway; | |
GameObject player; | |
[HideInInspector] | |
public int gridDivisionsX = 0; | |
[HideInInspector] | |
public int gridDivisionsZ = 0; | |
float gridStartX; | |
float gridStartZ; | |
float gridEndX; | |
float gridEndZ; | |
float[] gridX; | |
float[] gridZ; | |
public Vector2[] rangeX; | |
public Vector2[] rangeZ; | |
public int playerSectorX; | |
public int playerSectorZ; | |
// Use this for initialization | |
void Awake () { | |
grid = this; | |
player = GameObject.FindGameObjectWithTag ("Player"); | |
gridStartX = bottomLeftObject.transform.position.x; | |
gridStartZ = bottomLeftObject.transform.position.z; | |
gridEndX = topRightObject.transform.position.x; | |
gridEndZ = topRightObject.transform.position.z; | |
for(float x = gridStartX; x< gridEndX; x+= gridSize){ | |
gridDivisionsX +=1; | |
} | |
for (float z = gridStartZ; z<gridEndZ; z+= gridSize) { | |
gridDivisionsZ +=1; | |
} | |
rangeX = new Vector2[gridDivisionsX]; | |
for (int x = 0; x<gridDivisionsX; x+=1) { | |
rangeX[x] = new Vector2 ((gridStartX + gridSize * x), (gridStartX + gridSize * (x + 1))); | |
} | |
rangeZ = new Vector2[gridDivisionsZ]; | |
for(int z = 0; z<gridDivisionsZ; z+=1){ | |
rangeZ[z] = new Vector2 ((gridStartZ + gridSize * z), (gridStartZ + gridSize * (z + 1))); | |
} | |
playerSectorX = GridSectorX (player.transform.position.x); | |
playerSectorZ = GridSectorZ (player.transform.position.z); | |
} | |
void Start(){ | |
} | |
// Update is called once per frame | |
void Update () { | |
if (player == null) { | |
player = GameObject.FindGameObjectWithTag("Player"); | |
} | |
//remember to comment this out later | |
//for (int x = 0; x<gridDivisionsX; x+=1) { | |
// Debug.DrawRay(new Vector3(rangeX[x].x,0,rangeX[0].y), new Vector3 (0,0,gridEndZ-gridStartZ),Color.green); | |
//} | |
//for (int z = 0; z<gridDivisionsZ; z+=1) { | |
// Debug.DrawRay (new Vector3(rangeZ[0].x, 0, rangeZ[z].y), new Vector3 (gridEndX-gridStartX,0,0), Color.cyan); | |
//} | |
//remember to comment this out later | |
playerSectorX = GridSectorX (player.transform.position.x); | |
playerSectorZ = GridSectorZ (player.transform.position.z); | |
} | |
public int GridSectorX (float objectX){ | |
int gridRegionX = 1000; | |
for (int x = 0; x<rangeX.Length; x+=1) { | |
if(objectX >= rangeX[x].x && objectX < rangeX[x].y){ | |
gridRegionX = x; | |
} | |
} | |
return gridRegionX; | |
} | |
public int GridSectorZ(float objectZ){ | |
int gridRegionZ = 1000; | |
for (int z = 0; z<rangeZ.Length; z+=1) { | |
if(objectZ >= rangeZ[z].x && objectZ < rangeZ[z].y){ | |
gridRegionZ = z; | |
} | |
} | |
return gridRegionZ; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment