|
11 | 11 | import java.awt.Point; |
12 | 12 | import java.util.List; |
13 | 13 | import java.util.Map; |
| 14 | +import java.util.Set; |
14 | 15 |
|
15 | 16 | /** |
16 | 17 | * Central coordinator for all fence building operations. |
@@ -133,9 +134,9 @@ public void update(float deltaTime) { |
133 | 134 | enterBuildingMode(); |
134 | 135 | } |
135 | 136 |
|
136 | | - // Handle clear all fences (C key) when in building mode |
| 137 | + // Handle clear nearest enclosure (C key) when in building mode |
137 | 138 | if (buildingModeActive && Gdx.input.isKeyJustPressed(Input.Keys.C)) { |
138 | | - clearAllFences(); |
| 139 | + clearNearestEnclosure(); |
139 | 140 | } |
140 | 141 |
|
141 | 142 | // Handle rebuild collision boundaries (R key) when in building mode |
@@ -860,6 +861,76 @@ private void updateCollisionBoundaries(Point gridPos, boolean placed) { |
860 | 861 | } |
861 | 862 | } |
862 | 863 |
|
| 864 | + /** |
| 865 | + * Clears the fence enclosure nearest to the targeting cursor. |
| 866 | + * Finds the closest fence piece, identifies all connected pieces, and removes them. |
| 867 | + * Returns materials to inventory and plays a single removal sound. |
| 868 | + */ |
| 869 | + public void clearNearestEnclosure() { |
| 870 | + if (structureManager == null) return; |
| 871 | + |
| 872 | + // Get mouse position in world coordinates |
| 873 | + Vector3 mousePos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0); |
| 874 | + camera.unproject(mousePos); |
| 875 | + |
| 876 | + // Find closest fence piece |
| 877 | + Map<Point, FencePiece> allPieces = structureManager.getAllFencePieces(); |
| 878 | + if (allPieces.isEmpty()) { |
| 879 | + return; |
| 880 | + } |
| 881 | + |
| 882 | + Point closestPos = null; |
| 883 | + float minDistanceSq = Float.MAX_VALUE; |
| 884 | + |
| 885 | + for (Map.Entry<Point, FencePiece> entry : allPieces.entrySet()) { |
| 886 | + FencePiece piece = entry.getValue(); |
| 887 | + float dx = piece.getX() - mousePos.x; |
| 888 | + float dy = piece.getY() - mousePos.y; |
| 889 | + float distSq = dx*dx + dy*dy; |
| 890 | + |
| 891 | + if (distSq < minDistanceSq) { |
| 892 | + minDistanceSq = distSq; |
| 893 | + closestPos = entry.getKey(); |
| 894 | + } |
| 895 | + } |
| 896 | + |
| 897 | + if (closestPos != null) { |
| 898 | + // Get connected pieces (the enclosure) |
| 899 | + Set<Point> connectedPoints = structureManager.findConnectedPieces(closestPos); |
| 900 | + |
| 901 | + if (!connectedPoints.isEmpty()) { |
| 902 | + System.out.println("[FenceBuildingManager] Clearing enclosure of " + connectedPoints.size() + " pieces nearest to " + closestPos); |
| 903 | + |
| 904 | + int removedCount = 0; |
| 905 | + |
| 906 | + // Remove each piece |
| 907 | + for (Point pos : connectedPoints) { |
| 908 | + FencePiece removedPiece = structureManager.removeFencePiece(pos); |
| 909 | + if (removedPiece != null) { |
| 910 | + updateCollisionBoundaries(pos, false); |
| 911 | + removedCount++; |
| 912 | + // Trigger visual effect for each piece |
| 913 | + if (visualEffectsManager != null) { |
| 914 | + visualEffectsManager.triggerRemovalAnimation(pos, selectedMaterialType); |
| 915 | + } |
| 916 | + } |
| 917 | + } |
| 918 | + |
| 919 | + // Return materials |
| 920 | + FenceMaterialProvider materialProvider = validator.getMaterialProvider(); |
| 921 | + if (materialProvider != null && removedCount > 0) { |
| 922 | + materialProvider.returnMaterials(selectedMaterialType, removedCount); |
| 923 | + System.out.println("Returned " + removedCount + " " + selectedMaterialType.getDisplayName() + " materials"); |
| 924 | + } |
| 925 | + |
| 926 | + // Play sound once |
| 927 | + if (soundManager != null) { |
| 928 | + soundManager.playRemovalSound(); |
| 929 | + } |
| 930 | + } |
| 931 | + } |
| 932 | + } |
| 933 | + |
863 | 934 | /** |
864 | 935 | * Clears all fence pieces from the world. |
865 | 936 | * Useful for debugging and testing. |
|
0 commit comments