Player-field manipulations

This commit is contained in:
Фёдор Веселов 2024-09-15 19:19:59 +05:00
commit 74759e9e16
21 changed files with 270 additions and 54 deletions

View file

@ -1,12 +1,19 @@
using Godot;
using System;
public partial class PlantSlot : Node
public partial class PlantSlot : TextureButton
{
[Export] private PlantResource _resource;
[Export] private Label _cost;
[Export] private TextureRect _icon;
[Export] private Timer _timer;
public override void _Ready()
{
if (_resource != null)
UpdateContents();
}
public void SetPlantResource( PlantResource resource )
{
_resource = resource;
@ -18,5 +25,35 @@ public partial class PlantSlot : Node
{
_cost.Text = _resource.Cost.ToString();
_icon.Texture = _resource.Preview;
_timer.WaitTime = _resource.ReloadTime;
}
private void OnPressed()
{
if (_timer.TimeLeft == 0)
{
LevelController.Instance.PlantField.SetPlant(this, _resource);
}
}
private void OnFocusExited()
{
LevelController.Instance.PlantField.SetPlant(null, null);
}
public void Recharge()
{
Disabled = true;
FocusMode = FocusModeEnum.None;
ReleaseFocus();
_timer.Start();
}
private void Timeout()
{
Disabled = false;
FocusMode = FocusModeEnum.All;
}
}

View file

@ -0,0 +1,12 @@
using Godot;
using System;
public partial class VeilResizer : TextureProgressBar
{
[Export] private Timer _referenceTimer;
public override void _Process(double delta)
{
Value = (_referenceTimer.TimeLeft/_referenceTimer.WaitTime);
}
}

View file

@ -4,15 +4,21 @@ 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;
private PlantResource _resource;
private PlantSlot _slot;
public override void _Ready()
{
LevelController.Instance.PlantField = this;
_plantSetter = GetChild<Sprite2D>(0);
}
public void SetPlant(PlantSlot slot, PlantResource plant)
{
_resource = plant;
_slot = slot;
}
public override void _Process(double delta)
{
// Getting and storing global mouse position, setting plant-poiner to it
@ -24,29 +30,51 @@ public partial class PlantField : Node2D
// 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;
bool _canPlace = _resource != null
&& inBoundary
&& LevelController.Instance.Pools.EntityField[_resource.Layer].ContainsKey(expected_pos) == false
&& LevelController.Instance.LevelData.CheckSpendSun(_resource.Cost);
// Setting visuals
if (_plant != null && inBoundary)
if (_canPlace)
{
_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);
}
_plantSetter.Texture = _resource == null ? null : _resource.Preview;
// Spawning plant
if (Input.IsMouseButtonPressed(MouseButton.Left) && _canPlace)
if (Input.IsMouseButtonPressed(MouseButton.Left)
&& _canPlace )
{
var plant = _plant.Scene.Instantiate<Node2D>();
var plant = _resource.Scene.Instantiate<RuntimePlantData>();
LevelController.Instance.Pools.Plants.AddChild(plant);
plant.GlobalPosition = expected_pos;
LevelController.Instance.Pools.EntityField[_resource.Layer].Add(expected_pos, plant as IEntity);
LevelController.Instance.LevelData.SpendSun(_resource.Cost);
// Unfocusing and recharging slot
_slot.Recharge();
}
}
public override void _Input(InputEvent @event)
{
if (@event.IsActionPressed("cancel_plant"))
{
_slot.ReleaseFocus();
}
}
}

View file

@ -1,5 +1,5 @@
using Godot;
using System;
using System.Collections.Generic;
//
// PoolContainer contains nodes that contain different elemnts that generate during runtime
@ -17,6 +17,7 @@ public partial class PoolContainer : Node
[Export]
public Node Structures { get; private set; }
public Dictionary<Vector2, IEntity>[] EntityField = { new Dictionary<Vector2, IEntity>(), new Dictionary<Vector2, IEntity>(), new Dictionary<Vector2, IEntity>() };
public override void _Ready()
{
LevelController.Instance.Pools = this;

View file

@ -4,6 +4,7 @@ using System;
public partial class RuntimeLevelData : Node
{
[Export]
public int SunCount { get; private set; } = 0;
public override void _Ready()
@ -15,13 +16,15 @@ public partial class RuntimeLevelData : Node
{
SunCount += amount;
}
public void SpendSun(int amount)
{
SunCount -= amount;
}
public bool TrySpendSun(int amount)
public bool CheckSpendSun(int amount)
{
if (SunCount - amount < 0) return false;
SunCount -= amount;
return true;
}

View file

@ -15,6 +15,7 @@ public partial class RuntimePlantData : Node2D, IEntity
public int Hp => _hp;
public int MaxHp => _maxHP;
public int Line => _line;
public int Layer;
public override void _Ready()
{
@ -37,6 +38,7 @@ public partial class RuntimePlantData : Node2D, IEntity
if (_hp <= 0)
{
LevelController.Instance.Pools.EntityField[Layer].Remove(GlobalPosition);
QueueFree();
}
}

View file

@ -14,4 +14,5 @@ public partial class PlantResource : Resource
public float StartReloadTime;
[Export]
public Texture2D Preview;
[Export] public int Layer = 1;
}