✨ Miner tile
This commit is contained in:
		| @@ -9,12 +9,15 @@ namespace AceFieldNewHorizon.Scripts.Tiles; | ||||
| public partial class BaseTile : Node2D | ||||
| { | ||||
|     [Export] public string TileId { get; set; } | ||||
|     [Export] public GridManager Grid { get; set; } | ||||
|  | ||||
|     private CollisionShape2D _collisionShape; | ||||
|     private Sprite2D _sprite; | ||||
|     private ColorRect _progressOverlay; | ||||
|     private Action _onConstructionComplete; | ||||
|     private bool _isConstructing = false; | ||||
|      | ||||
|     public bool IsConstructing; | ||||
|     public bool IsConstructed; | ||||
|  | ||||
|     public override void _Ready() | ||||
|     { | ||||
| @@ -28,7 +31,7 @@ public partial class BaseTile : Node2D | ||||
|     public void SetGhostMode(bool canPlace) | ||||
|     { | ||||
|         // Don't modify collision for constructing buildings | ||||
|         if (_isConstructing) return; | ||||
|         if (IsConstructing) return; | ||||
|              | ||||
|         if (_collisionShape != null) | ||||
|             _collisionShape.Disabled = true; | ||||
| @@ -50,13 +53,13 @@ public partial class BaseTile : Node2D | ||||
|     // Building progress visualization | ||||
|     public void StartConstruction(float buildTime, Action onComplete = null) | ||||
|     { | ||||
|         _isConstructing = true; | ||||
|         IsConstructing = true; | ||||
|         if (_collisionShape != null) | ||||
|             _collisionShape.Disabled = true; | ||||
|  | ||||
|         if (_progressOverlay == null || _sprite?.Texture == null)  | ||||
|         { | ||||
|             _isConstructing = false; | ||||
|             IsConstructing = false; | ||||
|             onComplete?.Invoke(); | ||||
|             return; | ||||
|         } | ||||
| @@ -97,11 +100,12 @@ public partial class BaseTile : Node2D | ||||
|             if (_sprite != null) | ||||
|                 _sprite.Modulate = Colors.White; | ||||
|                  | ||||
|             _isConstructing = false; | ||||
|             IsConstructing = false; | ||||
|             if (_collisionShape != null) | ||||
|                 _collisionShape.Disabled = false; | ||||
|                  | ||||
|             _onConstructionComplete?.Invoke(); | ||||
|             IsConstructed = true; | ||||
|         } | ||||
|  | ||||
|         RunProgress(); | ||||
|   | ||||
| @@ -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); | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user