52 lines
1.8 KiB
C#
52 lines
1.8 KiB
C#
using Godot;
|
|
|
|
public partial class PlantField : Node2D
|
|
{
|
|
private Sprite2D _plantSetter;
|
|
private readonly Vector2 tile = new Vector2(Utility.TileWidth, Utility.TileHeight);
|
|
private bool _canPlace;
|
|
[Export]
|
|
private PlantResource _plant;
|
|
public override void _Ready()
|
|
{
|
|
LevelController.Instance.PlantField = this;
|
|
_plantSetter = GetChild<Sprite2D>(0);
|
|
}
|
|
|
|
public override void _Process(double delta)
|
|
{
|
|
// Getting and storing global mouse position, setting plant-poiner to it
|
|
var mouse_pos = GetGlobalMousePosition();
|
|
_plantSetter.GlobalPosition = mouse_pos;
|
|
|
|
// Getting position in grid coordinates
|
|
var expected_pos = (_plantSetter.GlobalPosition / tile).Ceil() * tile - new Vector2(20, 14);
|
|
|
|
// Checking for boundaries
|
|
bool inBoundary = expected_pos.X > Utility.LeftFieldBoundary.X && expected_pos.X < Utility.RightFieldBoundary.X && expected_pos.Y > Utility.LeftFieldBoundary.Y && expected_pos.Y < Utility.RightFieldBoundary.Y;
|
|
|
|
// Setting visuals
|
|
if (_plant != null && inBoundary)
|
|
{
|
|
_canPlace = true;
|
|
Material.Set("shader_parameter/amount", 0);
|
|
Cursor.Instance.SetPlantCursor();
|
|
_plantSetter.GlobalPosition = expected_pos;
|
|
}
|
|
else
|
|
{
|
|
_canPlace = false;
|
|
Cursor.Instance.SetDefaultCursor();
|
|
Material.Set("shader_parameter/amount", 1);
|
|
}
|
|
|
|
// Spawning plant
|
|
if (Input.IsMouseButtonPressed(MouseButton.Left) && _canPlace)
|
|
{
|
|
|
|
var plant = _plant.Scene.Instantiate<Node2D>();
|
|
LevelController.Instance.Pools.Plants.AddChild(plant);
|
|
plant.GlobalPosition = expected_pos;
|
|
}
|
|
}
|
|
}
|