✨ Placement check with collision
This commit is contained in:
6
.idea/.idea.AceField New Horizon/.idea/vcs.xml
generated
Normal file
6
.idea/.idea.AceField New Horizon/.idea/vcs.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
@@ -19,4 +19,4 @@ Grid = NodePath("../GridSystem")
|
||||
|
||||
[node name="Player" parent="." instance=ExtResource("3_oss8w")]
|
||||
position = Vector2(602, 324)
|
||||
scale = Vector2(0.5, 0.5)
|
||||
scale = Vector2(0.35, 0.35)
|
||||
|
@@ -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();
|
||||
|
@@ -1,30 +1,45 @@
|
||||
using Godot;
|
||||
|
||||
namespace AceFieldNewHorizon.Scripts.Tiles;
|
||||
|
||||
public partial class BaseTile : Node2D
|
||||
namespace AceFieldNewHorizon.Scripts.Tiles
|
||||
{
|
||||
protected CollisionShape2D CollisionShape;
|
||||
protected Sprite2D Sprite;
|
||||
|
||||
public override void _Ready()
|
||||
public partial class BaseTile : Node2D
|
||||
{
|
||||
// Get references (optional: you can also Export and assign in editor)
|
||||
CollisionShape = GetNode<CollisionShape2D>("CollisionShape2D");
|
||||
Sprite = GetNode<Sprite2D>("Sprite2D");
|
||||
}
|
||||
private CollisionShape2D _collisionShape;
|
||||
private Sprite2D _sprite;
|
||||
|
||||
public void SetGhostMode(bool ghost)
|
||||
{
|
||||
if (CollisionShape != null)
|
||||
CollisionShape.Disabled = ghost;
|
||||
|
||||
if (Sprite != null)
|
||||
public override void _Ready()
|
||||
{
|
||||
if (ghost)
|
||||
Sprite.Modulate = new Color(1, 1, 1, 0.5f); // semi-transparent
|
||||
else
|
||||
Sprite.Modulate = Colors.White;
|
||||
_collisionShape = GetNodeOrNull<CollisionShape2D>("CollisionShape2D");
|
||||
_sprite = GetNodeOrNull<Sprite2D>("Sprite2D");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Switch between ghost and placed mode.
|
||||
/// </summary>
|
||||
/// <param name="canPlace">If in ghost mode, true = green, false = red. If placed, always white.</param>
|
||||
public void SetGhostMode(bool canPlace)
|
||||
{
|
||||
if (_collisionShape != null)
|
||||
_collisionShape.Disabled = true; // always disabled in ghost mode
|
||||
|
||||
if (_sprite != null)
|
||||
{
|
||||
// Ghost preview coloring
|
||||
_sprite.Modulate = canPlace ? new Color(1, 1, 1, 0.2f) : // white semi-transparent
|
||||
new Color(1, 0, 0, 0.2f); // red semi-transparent
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Call when placement is finalized.
|
||||
/// </summary>
|
||||
public void FinalizePlacement()
|
||||
{
|
||||
if (_collisionShape != null)
|
||||
_collisionShape.Disabled = false;
|
||||
|
||||
if (_sprite != null)
|
||||
_sprite.Modulate = Colors.White; // reset to normal
|
||||
}
|
||||
}
|
||||
}
|
@@ -15,6 +15,10 @@ run/main_scene="uid://c22aprj452aha"
|
||||
config/features=PackedStringArray("4.4", "C#", "GL Compatibility")
|
||||
config/icon="res://icon.svg"
|
||||
|
||||
[display]
|
||||
|
||||
window/stretch/mode="viewport"
|
||||
|
||||
[dotnet]
|
||||
|
||||
project/assembly_name="AceField New Horizon"
|
||||
|
Reference in New Issue
Block a user