Skip to content

Commit

Permalink
fix placing blocks inside player or zombies
Browse files Browse the repository at this point in the history
  • Loading branch information
danilwhale committed Jul 17, 2024
1 parent 07bf494 commit e1c8e98
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
5 changes: 5 additions & 0 deletions SharpCraft/Entities/EntitySystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 9 additions & 1 deletion SharpCraft/Entities/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,22 @@ 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;

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;
Expand Down
5 changes: 5 additions & 0 deletions SharpCraft/Tiles/Tile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

0 comments on commit e1c8e98

Please sign in to comment.