From 9ced321538cb99d5035292525f48ecf11dae9634 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A4=D1=91=D0=B4=D0=BE=D1=80=20=D0=92=D0=B5=D1=81=D0=B5?= =?UTF-8?q?=D0=BB=D0=BE=D0=B2?= Date: Sun, 15 Sep 2024 16:22:55 +0500 Subject: [PATCH] Entity field with three layers --- scripts/components/level/PlantField.cs | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/scripts/components/level/PlantField.cs b/scripts/components/level/PlantField.cs index 943096a..dceefe9 100644 --- a/scripts/components/level/PlantField.cs +++ b/scripts/components/level/PlantField.cs @@ -2,22 +2,31 @@ using Godot; public partial class PlantField : Node2D { - private Area2D _plantSetter; + 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(0); + _plantSetter = GetChild(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); - if (expected_pos.X > Utility.LeftFieldBoundary.X && expected_pos.X < Utility.RightFieldBoundary.X && expected_pos.Y > Utility.LeftFieldBoundary.Y && expected_pos.Y < Utility.RightFieldBoundary.Y) + + // 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); @@ -30,5 +39,14 @@ public partial class PlantField : Node2D Cursor.Instance.SetDefaultCursor(); Material.Set("shader_parameter/amount", 1); } + + // Spawning plant + if (Input.IsMouseButtonPressed(MouseButton.Left) && _canPlace) + { + + var plant = _plant.Scene.Instantiate(); + LevelController.Instance.Pools.Plants.AddChild(plant); + plant.GlobalPosition = expected_pos; + } } }