Miner tile

This commit is contained in:
2025-08-29 16:59:59 +08:00
parent 7720e74a3d
commit 56cd4c2db2
9 changed files with 105 additions and 23 deletions

View File

@@ -1,8 +1,63 @@
using AceFieldNewHorizon.Scripts.System;
using Godot;
namespace AceFieldNewHorizon.Scripts.Tiles;
public partial class MinerTile : BaseTile
{
}
[Export] public PackedScene ItemPickup { get; set; }
[Export] public string ItemToMine { get; set; } = "OreIron";
[Export] public int MiningRate = 1; // Items per second
private Vector2I _gridPosition;
private float _timeSinceLastMine;
public override void _Ready()
{
base._Ready();
_gridPosition = GridUtils.WorldToGrid(Position);
}
public override void _Process(double delta)
{
// Don't mine if building is not completed
if (!IsConstructed || ItemPickup == null)
return;
_timeSinceLastMine += (float)delta;
if (!(_timeSinceLastMine >= 1f / MiningRate)) return;
_timeSinceLastMine = 0f;
SpawnItem();
}
private void SpawnItem()
{
var itemPickup = ItemPickup?.Instantiate<ItemPickup>();
if (itemPickup == null) return;
itemPickup.ItemId = ItemToMine;
itemPickup.Quantity = 1;
// Initial position (slightly below the spawn point)
var spawnPosition = GridUtils.GridToWorld(_gridPosition);
var targetY = spawnPosition.Y - 27f; // Target Y position
var targetX = spawnPosition.X + 27f + (GD.Randf() * 10f - 5f);
itemPickup.Position =
new Vector2(spawnPosition.X + 27f, spawnPosition.Y + 16); // Start below
itemPickup.Scale = Vector2.Zero; // Start invisible
// Add to the scene
GetTree().CurrentScene.AddChild(itemPickup);
// Create the pop-up animation
var tween = CreateTween().SetTrans(Tween.TransitionType.Elastic).SetEase(Tween.EaseType.Out);
// Animate the pop-up effect
tween.TweenProperty(itemPickup, "position:y", targetY, 0.6f);
tween.Parallel().TweenProperty(itemPickup, "scale", Vector2.One, 0.6f);
// Optional: Add a slight horizontal wobble
tween.Parallel().TweenProperty(itemPickup, "position:x", targetX, 0.6f);
}
}