-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Part 8, water, active water, and terrain manipulation is here!
- Loading branch information
Showing
16 changed files
with
3,294 additions
and
46 deletions.
There are no files selected for viewing
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
124 changes: 124 additions & 0 deletions
124
Assets/VoxelProjectSeries/Scripts/Camera/TerrainInteractor.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using Unity.Mathematics; | ||
using UnityEngine; | ||
|
||
public class TerrainInteractor : MonoBehaviour | ||
{ | ||
public bool ReplaceBlockInPlace = false; | ||
public ToolMode toolMode; | ||
public ToolType toolType; | ||
public int radiusToAffect = 2; | ||
public int voxelIDToPlace = 4; | ||
|
||
|
||
void Update() | ||
{ | ||
if (Input.GetKeyDown(KeyCode.Space)) | ||
{ | ||
ReplaceBlockInPlace = !ReplaceBlockInPlace; | ||
} | ||
if (Input.GetKeyDown(KeyCode.Tab)) | ||
{ | ||
int indexOf = (int)toolMode; | ||
toolMode = indexOf == 1 ? ToolMode.Single : ToolMode.Continuous; | ||
} | ||
if (Input.GetKeyDown(KeyCode.M)) | ||
{ | ||
toolMode = ToolMode.Continuous; | ||
toolType = ToolType.SingleBlock; | ||
voxelIDToPlace = 240; | ||
} | ||
|
||
if (toolMode == ToolMode.Single ? Input.GetMouseButtonDown(1) : Input.GetMouseButton(1)) | ||
{ | ||
//Spawn water as demo | ||
if (GetBlockCoordAtRay(out Vector3 chunkPos, out Vector3 blockPos)) | ||
{ | ||
if (blockPos.y < 0) | ||
return; | ||
|
||
float v = voxelIDToPlace == 240 ? 1 : 0; | ||
if (toolType == ToolType.SingleBlock) | ||
WorldManager.Instance.SetVoxelAtCoord(chunkPos, math.round(blockPos), new Voxel() { ID = voxelIDToPlace, ActiveValue = v }); | ||
else | ||
{ | ||
for (int x = -radiusToAffect; x < radiusToAffect; x++) | ||
for (int y = -radiusToAffect; y < radiusToAffect; y++) | ||
for (int z = -radiusToAffect; z < radiusToAffect; z++) | ||
{ | ||
Vector3 modPos = math.round(blockPos + new Vector3(x, y, z)); | ||
if ((modPos.y < 0 && voxelIDToPlace != 0) || (modPos.y < 1 && voxelIDToPlace == 0)) | ||
continue; | ||
WorldManager.Instance.SetVoxelAtCoord(chunkPos, blockPos + new Vector3(x, y, z), new Voxel() { ID = voxelIDToPlace, ActiveValue = v }); | ||
|
||
} | ||
} | ||
} | ||
|
||
} | ||
} | ||
|
||
bool GetBlockCoordAtRay(out Vector3 ChunkPos, out Vector3 blockPos) | ||
{ | ||
if (Physics.Raycast(new Ray(transform.position, transform.forward), out var hitInfo)) | ||
{ | ||
if (hitInfo.collider.transform.GetComponent<Chunk>() != null) | ||
{ | ||
ChunkPos = hitInfo.collider.transform.GetComponent<Chunk>().chunkPosition; | ||
blockPos = math.floor(hitInfo.point - ChunkPos); | ||
if (hitInfo.normal.x != 0) | ||
{ | ||
blockPos.x = math.round(blockPos.x); | ||
} | ||
else | ||
{ | ||
blockPos.x = math.floor(blockPos.x); | ||
} | ||
if (hitInfo.normal.y != 0) | ||
{ | ||
blockPos.y = math.round(blockPos.y); | ||
} | ||
else | ||
{ | ||
blockPos.y = math.floor(blockPos.y); | ||
} | ||
if (hitInfo.normal.z != 0) | ||
{ | ||
blockPos.z = math.round(blockPos.z); | ||
} | ||
else | ||
{ | ||
blockPos.z = math.floor(blockPos.z); | ||
} | ||
|
||
if (ReplaceBlockInPlace) | ||
{ | ||
if (hitInfo.normal.x > 0 || hitInfo.normal.y > 0 || hitInfo.normal.z > 0) | ||
blockPos -= hitInfo.normal; | ||
} | ||
else | ||
{ | ||
|
||
if (hitInfo.normal.x < 0 || hitInfo.normal.y < 0 || hitInfo.normal.z < 0) | ||
blockPos += hitInfo.normal; | ||
} | ||
return true; | ||
} | ||
} | ||
ChunkPos = Vector3.zero; | ||
blockPos = Vector3.zero; | ||
return false; | ||
} | ||
|
||
public enum ToolMode | ||
{ | ||
Single, | ||
Continuous | ||
} | ||
public enum ToolType | ||
{ | ||
SingleBlock, | ||
Radius | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Assets/VoxelProjectSeries/Scripts/Camera/TerrainInteractor.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
Oops, something went wrong.