diff --git a/SharpCraft/Entities/EntitySystem.cs b/SharpCraft/Entities/EntitySystem.cs index 9ed4a54..bf3b61e 100644 --- a/SharpCraft/Entities/EntitySystem.cs +++ b/SharpCraft/Entities/EntitySystem.cs @@ -49,6 +49,11 @@ public void Draw(float lastPartTicks, Frustum frustum, RenderLayer layer) } } + public bool IsAreaFree(BoundingBox box) + { + return !_entities.Any(e => CheckCollisionBoxes(e.Box, box)); + } + public void Dispose() { foreach (var entity in _entities) diff --git a/SharpCraft/Entities/Player.cs b/SharpCraft/Entities/Player.cs index 2e75658..46bd825 100644 --- a/SharpCraft/Entities/Player.cs +++ b/SharpCraft/Entities/Player.cs @@ -44,7 +44,8 @@ private void HandleMouseInput() var oldTile = entity.World.GetTile(hitPoint); if (entity.World.TrySetTile(hitPoint, 0)) { - Registries.Tiles.Registry[oldTile]?.Break(entity.World, hitPoint.X, hitPoint.Y, hitPoint.Z, particleSystem); + Registries.Tiles.Registry[oldTile]?.Break(entity.World, hitPoint.X, hitPoint.Y, hitPoint.Z, + particleSystem); } break; @@ -52,6 +53,13 @@ private void HandleMouseInput() case EditMode.Place: hitPoint = _rayCast.Point + _rayCast.Normal / 2; + var tile = Registries.Tiles.Registry[CurrentTile]; + var box = tile!.GetBox(hitPoint.X, hitPoint.Y, hitPoint.Z); + if (!entity.System?.IsAreaFree(box) ?? false) + { + break; + } + entity.World.TrySetTile(hitPoint, CurrentTile); break; diff --git a/SharpCraft/Tiles/Tile.cs b/SharpCraft/Tiles/Tile.cs index d6ae25e..43e83fa 100644 --- a/SharpCraft/Tiles/Tile.cs +++ b/SharpCraft/Tiles/Tile.cs @@ -95,4 +95,9 @@ public void Break(World.World world, int x, int y, int z, ParticleSystem particl } } } + + public BoundingBox GetBox(int x, int y, int z) + { + return new BoundingBox(new Vector3(x, y, z), new Vector3(x + 1, y + 1, z + 1)); + } } \ No newline at end of file