40 lines
1.2 KiB
C#
40 lines
1.2 KiB
C#
using AceFieldNewHorizon.Scripts.Tiles;
|
|
using Godot;
|
|
|
|
namespace AceFieldNewHorizon.Scripts.System;
|
|
|
|
public partial class PlacementManager : Node2D
|
|
{
|
|
[Export] public PackedScene BuildingScene { get; set; }
|
|
[Export] public GridManager Grid { get; set; }
|
|
|
|
private Vector2I _hoveredCell;
|
|
private BaseTile _ghostBuilding;
|
|
|
|
public override void _Process(double delta)
|
|
{
|
|
// Snap mouse to grid
|
|
var mousePos = GetGlobalMousePosition();
|
|
_hoveredCell = GridUtils.WorldToGrid(mousePos);
|
|
|
|
if (_ghostBuilding == null)
|
|
{
|
|
_ghostBuilding = (BaseTile)BuildingScene.Instantiate();
|
|
_ghostBuilding.SetGhostMode(true);
|
|
AddChild(_ghostBuilding);
|
|
}
|
|
|
|
_ghostBuilding.Position = GridUtils.GridToWorld(_hoveredCell);
|
|
_ghostBuilding.SetGhostMode(true);
|
|
|
|
// Left click to place
|
|
if (!Input.IsActionPressed("build_tile") || !Grid.IsCellFree(_hoveredCell)) return;
|
|
|
|
_ghostBuilding.SetGhostMode(false);
|
|
Grid.OccupyCell(_hoveredCell, _ghostBuilding);
|
|
|
|
_ghostBuilding = (BaseTile)BuildingScene.Instantiate();
|
|
_ghostBuilding.SetGhostMode(true);
|
|
AddChild(_ghostBuilding);
|
|
}
|
|
} |