Building duartion

This commit is contained in:
2025-08-27 02:42:13 +08:00
parent 3d11cecd5e
commit e9b070ff35
6 changed files with 178 additions and 48 deletions

View File

@@ -1,45 +1,75 @@
using System.Numerics;
using AceFieldNewHorizon.Scripts.System;
using Godot;
using Vector2 = Godot.Vector2;
namespace AceFieldNewHorizon.Scripts.Tiles
namespace AceFieldNewHorizon.Scripts.Tiles;
public partial class BaseTile : Node2D
{
public partial class BaseTile : Node2D
private CollisionShape2D _collisionShape;
private Sprite2D _sprite;
private ColorRect _progressOverlay;
public override void _Ready()
{
private CollisionShape2D _collisionShape;
private Sprite2D _sprite;
_collisionShape = GetNodeOrNull<CollisionShape2D>("CollisionShape2D");
_sprite = GetNodeOrNull<Sprite2D>("Sprite2D");
_progressOverlay = GetNodeOrNull<ColorRect>("ProgressOverlay");
if (_progressOverlay != null)
_progressOverlay.Visible = false;
}
public override void _Ready()
public void SetGhostMode(bool canPlace)
{
if (_collisionShape != null)
_collisionShape.Disabled = true;
if (_sprite != null)
_sprite.Modulate = canPlace
? new Color(0, 1, 0, 0.5f)
: new Color(1, 0, 0, 0.5f);
}
public void FinalizePlacement()
{
if (_collisionShape != null)
_collisionShape.Disabled = false;
if (_sprite != null)
_sprite.Modulate = Colors.White;
}
// Building progress visualization
public void StartConstruction(float buildTime)
{
if (_progressOverlay == null || _sprite?.Texture == null) return;
var texSize = new Vector2(GridUtils.TileSize, GridUtils.TileSize);
_progressOverlay.Visible = true;
_progressOverlay.Color = new Color(0, 0, 1, 0.4f); // semi-transparent blue
async void RunProgress()
{
_collisionShape = GetNodeOrNull<CollisionShape2D>("CollisionShape2D");
_sprite = GetNodeOrNull<Sprite2D>("Sprite2D");
}
/// <summary>
/// Switch between ghost and placed mode.
/// </summary>
/// <param name="canPlace">If in ghost mode, true = green, false = red. If placed, always white.</param>
public void SetGhostMode(bool canPlace)
{
if (_collisionShape != null)
_collisionShape.Disabled = true; // always disabled in ghost mode
if (_sprite != null)
var elapsed = 0f;
while (elapsed < buildTime)
{
// Ghost preview coloring
_sprite.Modulate = canPlace ? new Color(1, 1, 1, 0.2f) : // white semi-transparent
new Color(1, 0, 0, 0.2f); // red semi-transparent
await ToSignal(GetTree(), SceneTree.SignalName.ProcessFrame);
elapsed += (float)GetProcessDeltaTime();
var ratio = Mathf.Clamp(elapsed / buildTime, 0, 1);
// Fill from bottom to top, aligned with sprite center pivot
_progressOverlay.Size = new Vector2(texSize.X, texSize.Y * ratio);
_progressOverlay.Position = new Vector2(
-texSize.X / 2, // align left edge
texSize.Y / 2 - _progressOverlay.Size.Y // move upward
);
}
_progressOverlay.Visible = false;
}
/// <summary>
/// Call when placement is finalized.
/// </summary>
public void FinalizePlacement()
{
if (_collisionShape != null)
_collisionShape.Disabled = false;
if (_sprite != null)
_sprite.Modulate = Colors.White; // reset to normal
}
RunProgress();
}
}