✨ Placement check with collision
This commit is contained in:
@@ -24,13 +24,29 @@ public partial class PlacementManager : Node2D
|
||||
AddChild(_ghostBuilding);
|
||||
}
|
||||
|
||||
_ghostBuilding.Position = GridUtils.GridToWorld(_hoveredCell);
|
||||
_ghostBuilding.SetGhostMode(true);
|
||||
var placementPos = GridUtils.GridToWorld(_hoveredCell);
|
||||
var spaceState = GetWorld2D().DirectSpaceState;
|
||||
var centerPos = placementPos + new Vector2(GridUtils.TileSize / 2f, GridUtils.TileSize / 2f);
|
||||
|
||||
var query = new PhysicsPointQueryParameters2D
|
||||
{
|
||||
Position = centerPos,
|
||||
CollideWithBodies = true,
|
||||
CollideWithAreas = true,
|
||||
CollisionMask = uint.MaxValue // check against all layers/masks
|
||||
// Exclude = new Godot.Collections.Array<Rid>() // optional, see below
|
||||
};
|
||||
|
||||
var collision = spaceState.IntersectPoint(query, 8);
|
||||
var canPlace = Grid.IsCellFree(_hoveredCell) && collision.Count == 0;
|
||||
|
||||
_ghostBuilding.Position = placementPos;
|
||||
_ghostBuilding.SetGhostMode(canPlace);
|
||||
|
||||
// Left click to place
|
||||
if (!Input.IsActionPressed("build_tile") || !Grid.IsCellFree(_hoveredCell)) return;
|
||||
if (!Input.IsActionPressed("build_tile") || !canPlace) return;
|
||||
|
||||
_ghostBuilding.SetGhostMode(false);
|
||||
_ghostBuilding.FinalizePlacement();
|
||||
Grid.OccupyCell(_hoveredCell, _ghostBuilding);
|
||||
|
||||
_ghostBuilding = (BaseTile)BuildingScene.Instantiate();
|
||||
|
Reference in New Issue
Block a user