Skip to content

Instantly share code, notes, and snippets.

@StillManic
Created March 24, 2016 20:38
Show Gist options
  • Save StillManic/051f75dc38271984f079 to your computer and use it in GitHub Desktop.
Save StillManic/051f75dc38271984f079 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
public class Main : MonoBehaviour {
public GameObject gem;
public int currentLayer = -1;
private int lastLayer = -1;
public Vector3 gridDimensions;
public Vector3 centerOfGridWorld = new Vector3();
private Vector3 centerOfGridGame = new Vector3();
private Vector3 centerIndex;
private static GameObject[,,] grid;
private int[] gridDims;
void Start () {
centerOfGridGame.x = centerOfGridWorld.x + (Constants.GRID_OFFSET * gridDimensions.x);
centerOfGridGame.y = centerOfGridWorld.y + (Constants.GRID_OFFSET * gridDimensions.y);
centerOfGridGame.z = centerOfGridWorld.z + (Constants.GRID_OFFSET * gridDimensions.z);
gridDims = new int[] {(int) gridDimensions.x, (int) gridDimensions.y, (int) gridDimensions.z};
//centerIndex = gridDimensions / 2;
centerIndex = new Vector3(gridDims[0] / 2, gridDims[1] / 2, gridDims[2] / 2);
grid = new GameObject[gridDims[0], gridDims[1], gridDims[2]];
FillGrid();
}
void Update () {
if (currentLayer != lastLayer)
ChangeLayer();
}
void FillGrid() {
//Vector3 worldOffset = Vector3.one * Constants.WORLD_OFFSET;
for (int x = 0; x < gridDims[0]; x++) {
for (int y = 0; y < gridDims[1]; y++) {
for (int z = 0; z < gridDims[2]; z++) {
int index = Random.Range(0, System.Enum.GetValues(typeof(GemDefinition.Gems)).Length - 1);
Vector3 current = new Vector3(x, y, z);
Vector3 pos = centerOfGridWorld + (Constants.GRID_OFFSET * (current - centerIndex))/* + worldOffset*/;
grid[x, y, z] = Instantiate(gem.transform.GetChild(index), pos, Quaternion.AngleAxis(90, Vector3.up)) as GameObject;
}
}
}
}
void ChangeLayer() {
lastLayer = currentLayer;
for (int x = 0; x < gridDims[0]; x++) {
for (int y = 0; y < gridDims[1]; y++) {
for (int z = 0; z < gridDims[2]; z++) {
grid[x, y, z].gameObject.SetActive(!(currentLayer > -1 && currentLayer != x));
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment