Last active
October 6, 2017 06:44
-
-
Save AndrewWeston9/efe070f6309521a034e62270c71e95b7 to your computer and use it in GitHub Desktop.
WorldManager.cs Snippet: Placing blocks in the world, setting up the different brick objects and flag object.
This file contains 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
public void placeSingleBlock (GameObject block, Vector3 position, Transform parentObjectTransform) | |
{ | |
Vector3 blockpos; | |
if (block.tag == "Flag") | |
{ | |
blockpos = new Vector3 (position.x, position.z - 0.5f, position.y); | |
} | |
else | |
{ | |
blockpos = new Vector3 (position.x, position.z, position.y); | |
} | |
GameObject thisBrick = UnityEngine.Object.Instantiate (block, blockpos, Quaternion.identity); | |
thisBrick.transform.SetParent (parentObjectTransform, false); | |
} | |
/// Build up a local copy of the scene from information in the region block. Also needs to end | |
/// up in a view class, using appropriate methods to query the RegionBlock information. | |
/// Removes all current views of that block. | |
public void placeBlocks (Transform parentObjectTransform) | |
{ | |
//Load brick gameobjects | |
GameObject GrassBrick = Resources.Load<GameObject>("Bricks/GrassBrick"); | |
GameObject TimberBrick = Resources.Load<GameObject>("Bricks/TimberBrick"); | |
GameObject BrickBrick = Resources.Load<GameObject>("Bricks/BrickBrick"); | |
GameObject CrystalBrick = Resources.Load<GameObject>("Bricks/CrystalBrick"); | |
GameObject WoodResourceBrick = Resources.Load<GameObject> ("Bricks/WoodResourceBrick"); | |
GameObject DirtResourceBrick = Resources.Load<GameObject> ("Bricks/DirtResourceBrick"); | |
GameObject CrystalResourceBrick = Resources.Load<GameObject> ("Bricks/CrystalResourceBrick"); | |
GameObject Flag = Resources.Load<GameObject> ("Bricks/Flag"); | |
for (int i = parentObjectTransform.childCount - 1; i >= 0; i--) | |
{ | |
Transform child = parentObjectTransform.GetChild (i); | |
UnityEngine.Object.Destroy(child.gameObject); | |
} | |
for (int i = 0; i < blockSize; i += 1) | |
{ | |
for (int j = 0; j < blockSize; j += 1) | |
{ | |
// Debug.Log ("Block at " + i + " , " + j + " ==" + blockStructure[getIndex (i, j)] + " - " + timeLastChanged); | |
for (int k = 0; k < MaxBlockHeight; k += 1) | |
{ | |
if (blockStructure[getIndex (i, j, k)].brickType == 1) | |
{ | |
Vector3 pos = new Vector3 (i, j, k); | |
placeSingleBlock (GrassBrick, pos, parentObjectTransform); | |
// Debug.Log ("Block at " + i + " , " + j); | |
} | |
if (blockStructure[getIndex (i, j, k)].brickType == 2) | |
{ | |
Vector3 pos = new Vector3 (i, j, k); | |
placeSingleBlock (TimberBrick, pos, parentObjectTransform); | |
// Debug.Log ("Block at " + i + " , " + j); | |
} | |
if (blockStructure[getIndex (i, j, k)].brickType == 3) | |
{ | |
Vector3 pos = new Vector3 (i, j, k); | |
placeSingleBlock (BrickBrick, pos, parentObjectTransform); | |
// Debug.Log ("Block at " + i + " , " + j); | |
} | |
if (blockStructure[getIndex (i, j, k)].brickType == 4) | |
{ | |
Vector3 pos = new Vector3 (i, j, k); | |
placeSingleBlock (CrystalBrick, pos, parentObjectTransform); | |
// Debug.Log ("Block at " + i + " , " + j); | |
} | |
if (blockStructure[getIndex (i, j, k)].brickType == 5) | |
{ | |
Vector3 pos = new Vector3 (i, j, k); | |
placeSingleBlock (WoodResourceBrick, pos, parentObjectTransform); | |
// Debug.Log ("Block at " + i + " , " + j); | |
} | |
if (blockStructure[getIndex (i, j, k)].brickType == 6) | |
{ | |
Vector3 pos = new Vector3 (i, j, k); | |
placeSingleBlock (DirtResourceBrick, pos, parentObjectTransform); | |
// Debug.Log ("Block at " + i + " , " + j); | |
} | |
if (blockStructure[getIndex (i, j, k)].brickType == 7) | |
{ | |
Vector3 pos = new Vector3 (i, j, k); | |
placeSingleBlock (CrystalResourceBrick, pos, parentObjectTransform); | |
// Debug.Log ("Block at " + i + " , " + j); | |
} | |
if (blockStructure[getIndex (i, j, k)].brickType == 10) | |
{ | |
Vector3 pos = new Vector3 (i, j, k); | |
placeSingleBlock (Flag, pos, parentObjectTransform); | |
// Debug.Log ("Block at " + i + " , " + j); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment