Beispiel #1
0
	/// <summary>
	/// Set a block in the world
	/// </summary>
	/// <param name="x">X position of the block</param>
	/// <param name="y">Y position of the block</param>
	/// <param name="z">Z position of the block</param>
	/// <param name="block">The block to set</param>
	/// <param name="update">If it should update the surrounding blocks</param>
	public void SetBlock(int x, int y, int z, Block block, bool update = true) {
		if (useChunks) {
			Chunk chunk = GetChunk(x, y, z);

			if (chunk != null) {
				chunk.SetBlock(x - chunk.pos.x, y - chunk.pos.y, z - chunk.pos.z, block);
				chunk.update = true;
			}
		} else {
			if (InRange(x) && InRange(y) && InRange(z)) {
				if (blocks[x, y, z] != null) {
					blocks[x, y, z].DestroyBlock();
				}

				block.InstantiateBlock(transform, new Vector3(x, y, z) + new Vector3(0.5f, 0.5f, 0.5f), x, y, z, blocks);

				blocks[x, y, z] = block;

				//Create sibling gameobject for collider
				if (!(block is AirBlock)) {
					GameObject colliderGameObject = new GameObject("Selection Collider");
					colliderGameObject.transform.SetParent(block.gameObject.transform, false);
					colliderGameObject.layer = 9;

					//Create collider
					Collider collider = blocks[x, y, z].GetCollider(colliderGameObject, Vector3.zero);
					if (collider != null) {
						colliders.Add(collider, blocks[x, y, z]);
					}
				}

				//Update surrounding blocks
				if (update) {
					for (int xx = -1; xx <= 1; xx++) {
						for (int yy = -1; yy <= 1; yy++) {
							for (int zz = -1; zz <= 1; zz++) {
								if (x != 0 && y != 0 && z != 0) {
									if (InRange(x + xx) && InRange(y + yy) && InRange(z + zz)) {
										if (blocks[x + xx, y + yy, z + zz] != null) {
											blocks[x + xx, y + yy, z + zz].UpdateBlock(x + xx, y + yy, z + zz, blocks);
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}