Compare commits

..

3 Commits

Author SHA1 Message Date
LittleSheep
e9b070ff35 Building duartion 2025-08-27 02:42:13 +08:00
LittleSheep
3d11cecd5e Miner tile 2025-08-26 23:12:46 +08:00
LittleSheep
6f2969d880 💄 Optimize movement 2025-08-26 23:12:39 +08:00
12 changed files with 248 additions and 54 deletions

View File

@@ -1,5 +1,19 @@
{ {
"wall": "res://Scenes/Tiles/WallTile.tscn", "wall": {
"farm": "res://Scenes/Tiles/FarmTile.tscn", "scene": "res://Scenes/Tiles/WallTile.tscn",
"tower": "res://Scenes/Tiles/TowerTile.tscn" "cost": {
"stone": 5
},
"durability": 100,
"buildTime": 1.5
},
"miner": {
"scene": "res://Scenes/Tiles/MinerTile.tscn",
"cost": {
"wood": 10,
"stone": 3
},
"durability": 200,
"buildTime": 3.0
}
} }

View File

@@ -7,11 +7,13 @@
script = ExtResource("1_08t41") script = ExtResource("1_08t41")
[node name="Sprite2D" type="Sprite2D" parent="."] [node name="Sprite2D" type="Sprite2D" parent="."]
rotation = 1.5708
scale = Vector2(0.1, 0.1) scale = Vector2(0.1, 0.1)
texture = ExtResource("1_ucweq") texture = ExtResource("1_ucweq")
[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="."] [node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="."]
position = Vector2(1, 2.5) position = Vector2(1, 2.5)
rotation = 1.5708
polygon = PackedVector2Array(54, 87.5, 62, 76.5, 7, -89.5, -1, -92.5, -10, -89.5, -64, 76.5, -55, 87.5) polygon = PackedVector2Array(54, 87.5, 62, 76.5, 7, -89.5, -1, -92.5, -10, -89.5, -64, 76.5, -55, 87.5)
[node name="Camera2D" type="Camera2D" parent="."] [node name="Camera2D" type="Camera2D" parent="."]

BIN
Scenes/Tiles/MinerTile.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 438 KiB

View File

@@ -0,0 +1,34 @@
[remap]
importer="texture"
type="CompressedTexture2D"
uid="uid://bn80thu20eaia"
path="res://.godot/imported/MinerTile.png-a6c5eba73cdda5685afda8ced0234fec.ctex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://Scenes/Tiles/MinerTile.png"
dest_files=["res://.godot/imported/MinerTile.png-a6c5eba73cdda5685afda8ced0234fec.ctex"]
[params]
compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1

View File

@@ -0,0 +1,23 @@
[gd_scene load_steps=4 format=3 uid="uid://cbu81slklwq3u"]
[ext_resource type="Script" uid="uid://dyubkyqtpcg3a" path="res://Scripts/Tiles/MinerTile.cs" id="1_mecoy"]
[ext_resource type="Texture2D" uid="uid://bn80thu20eaia" path="res://Scenes/Tiles/MinerTile.png" id="2_mecoy"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_8o613"]
size = Vector2(54, 54)
[node name="MinerTile" type="StaticBody2D"]
script = ExtResource("1_mecoy")
[node name="Sprite2D" type="Sprite2D" parent="."]
scale = Vector2(0.1, 0.1)
texture = ExtResource("2_mecoy")
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource("RectangleShape2D_8o613")
[node name="ProgressOverlay" type="ColorRect" parent="."]
offset_left = -27.0
offset_top = -27.0
offset_right = 27.0
offset_bottom = 27.0

View File

@@ -15,3 +15,9 @@ texture = ExtResource("1_8o613")
[node name="CollisionShape2D" type="CollisionShape2D" parent="."] [node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource("RectangleShape2D_8o613") shape = SubResource("RectangleShape2D_8o613")
[node name="ProgressOverlay" type="ColorRect" parent="."]
offset_left = -27.0
offset_top = -27.0
offset_right = 27.0
offset_bottom = 27.0

View File

@@ -6,20 +6,23 @@ namespace AceFieldNewHorizon.Scripts.Entities;
public partial class Player : CharacterBody2D public partial class Player : CharacterBody2D
{ {
[Export] public float Speed = 400.0f; [Export] public float Speed = 400.0f;
[Export] public float RotationSpeed = 3.0f;
public override void _Process(double delta) public override void _Process(double delta)
{ {
// Get direction to mouse and calculate angle // Get direction to mouse and calculate angle
var mousePos = GetGlobalMousePosition(); var mousePos = GetGlobalMousePosition();
var direction = GlobalPosition.DirectionTo(mousePos); var direction = GlobalPosition.DirectionTo(mousePos);
Rotation = direction.Angle() + (float)Math.PI / 2; Rotation = direction.Angle();
} }
public override void _PhysicsProcess(double delta) public override void _PhysicsProcess(double delta)
{ {
// Get movement input // Get movement input
var inputDirection = Input.GetVector("move_left", "move_right", "move_up", "move_down"); var moveForward = Input.GetActionStrength("move_up") - Input.GetActionStrength("move_down");
Velocity = inputDirection * Speed;
// Calculate movement direction based on rotation
Velocity = Vector2.Right.Rotated(Rotation) * moveForward * Speed;
MoveAndSlide(); MoveAndSlide();
} }

View File

@@ -5,7 +5,14 @@ namespace AceFieldNewHorizon.Scripts.System;
public partial class BuildingRegistry : Node public partial class BuildingRegistry : Node
{ {
private Dictionary<string, PackedScene> _registry = new(); public record BuildingData(
PackedScene Scene,
Dictionary<string, int> Cost,
int Durability,
float BuildTime
);
private Dictionary<string, BuildingData> _registry = new();
[Export] public string JsonPath { get; set; } = "res://Data/Buildings.json"; [Export] public string JsonPath { get; set; } = "res://Data/Buildings.json";
@@ -38,26 +45,87 @@ public partial class BuildingRegistry : Node
foreach (string key in dict.Keys) foreach (string key in dict.Keys)
{ {
var scenePath = dict[key].AsString(); // Each entry is a Dictionary with keys: "scene", "cost", "durability", "buildTime"
var scene = GD.Load<PackedScene>(scenePath); var buildingDict = dict[key].AsGodotDictionary();
if (scene != null) // Parse scene
var scenePath = buildingDict.ContainsKey("scene") ? buildingDict["scene"].AsString() : null;
if (string.IsNullOrEmpty(scenePath))
{ {
_registry[key] = scene; GD.PrintErr($"[BuildingRegistry] No scene path for '{key}'");
GD.Print($"[BuildingRegistry] Loaded building '{key}' from {scenePath}"); continue;
} }
else
var scene = GD.Load<PackedScene>(scenePath);
if (scene == null)
{ {
GD.PrintErr($"[BuildingRegistry] Failed to load scene for '{key}' at {scenePath}"); GD.PrintErr($"[BuildingRegistry] Failed to load scene for '{key}' at {scenePath}");
continue;
} }
// Parse cost
Dictionary<string, int> cost = new();
if (buildingDict.TryGetValue("cost", out var value))
{
var costDict = value.AsGodotDictionary();
foreach (string mat in costDict.Keys)
{
int val;
var obj = costDict[mat];
if (obj.VariantType == Variant.Type.PackedInt64Array)
val = (int)obj.AsInt64();
else if (obj.VariantType == Variant.Type.PackedInt32Array)
val = obj.AsInt32();
else
int.TryParse(obj.ToString(), out val);
cost[mat] = val;
}
}
// Parse durability
var durability = 0;
if (buildingDict.TryGetValue("durability", out var dObj))
{
if (dObj.VariantType == Variant.Type.PackedInt64Array)
durability = (int)dObj.AsInt64();
else if (dObj.VariantType == Variant.Type.PackedInt32Array)
durability = dObj.AsInt32();
else
int.TryParse(dObj.ToString(), out durability);
}
// Parse buildTime
var buildTime = 0f;
if (buildingDict.TryGetValue("buildTime", out var bObj))
{
switch (bObj.VariantType)
{
case Variant.Type.PackedFloat32Array or Variant.Type.PackedFloat64Array:
buildTime = (float)bObj.AsDouble();
break;
case Variant.Type.PackedInt64Array:
buildTime = bObj.AsInt64();
break;
case Variant.Type.PackedInt32Array:
buildTime = bObj.AsInt32();
break;
default:
float.TryParse(bObj.ToString(), out buildTime);
break;
}
}
var buildingData = new BuildingData(scene, cost, durability, buildTime);
_registry[key] = buildingData;
GD.Print($"[BuildingRegistry] Loaded building '{key}' from {scenePath}");
} }
GD.Print($"[BuildingRegistry] Loaded {_registry.Count} buildings"); GD.Print($"[BuildingRegistry] Loaded {_registry.Count} buildings");
} }
public PackedScene GetScene(string id) public BuildingData GetBuilding(string id)
{ {
_registry.TryGetValue(id, out var scene); _registry.TryGetValue(id, out var data);
return scene; return data;
} }
} }

View File

@@ -8,7 +8,7 @@ public partial class PlacementManager : Node2D
[Export] public GridManager Grid { get; set; } [Export] public GridManager Grid { get; set; }
[Export] public BuildingRegistry Registry { get; set; } [Export] public BuildingRegistry Registry { get; set; }
private string _currentBuildingId = "wall"; private string _currentBuildingId = "miner";
private Vector2I _hoveredCell; private Vector2I _hoveredCell;
private BaseTile _ghostBuilding; private BaseTile _ghostBuilding;
@@ -31,7 +31,7 @@ public partial class PlacementManager : Node2D
if (_ghostBuilding == null) if (_ghostBuilding == null)
{ {
var scene = Registry.GetScene(_currentBuildingId); var scene = Registry.GetBuilding(_currentBuildingId)?.Scene;
if (scene == null) return; if (scene == null) return;
_ghostBuilding = (BaseTile)scene.Instantiate(); _ghostBuilding = (BaseTile)scene.Instantiate();
@@ -61,12 +61,17 @@ public partial class PlacementManager : Node2D
// Left click to place // Left click to place
if (Input.IsActionPressed("build_tile") && canPlace) if (Input.IsActionPressed("build_tile") && canPlace)
{ {
var scene = Registry.GetScene(_currentBuildingId); var scene = Registry.GetBuilding(_currentBuildingId)?.Scene;
if (scene == null) return; if (scene == null) return;
_ghostBuilding.FinalizePlacement(); _ghostBuilding.FinalizePlacement();
var buildingData = Registry.GetBuilding(_currentBuildingId);
Grid.OccupyCell(_hoveredCell, _ghostBuilding); Grid.OccupyCell(_hoveredCell, _ghostBuilding);
if (buildingData is { BuildTime: > 0f })
_ghostBuilding.StartConstruction(buildingData.BuildTime);
_ghostBuilding = (BaseTile)scene.Instantiate(); _ghostBuilding = (BaseTile)scene.Instantiate();
_ghostBuilding.SetGhostMode(true); _ghostBuilding.SetGhostMode(true);
AddChild(_ghostBuilding); AddChild(_ghostBuilding);

View File

@@ -1,45 +1,75 @@
using System.Numerics;
using AceFieldNewHorizon.Scripts.System;
using Godot; 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 CollisionShape2D _collisionShape;
private Sprite2D _sprite; private Sprite2D _sprite;
private ColorRect _progressOverlay;
public override void _Ready() public override void _Ready()
{ {
_collisionShape = GetNodeOrNull<CollisionShape2D>("CollisionShape2D"); _collisionShape = GetNodeOrNull<CollisionShape2D>("CollisionShape2D");
_sprite = GetNodeOrNull<Sprite2D>("Sprite2D"); _sprite = GetNodeOrNull<Sprite2D>("Sprite2D");
_progressOverlay = GetNodeOrNull<ColorRect>("ProgressOverlay");
if (_progressOverlay != null)
_progressOverlay.Visible = false;
} }
/// <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) public void SetGhostMode(bool canPlace)
{ {
if (_collisionShape != null) if (_collisionShape != null)
_collisionShape.Disabled = true; // always disabled in ghost mode _collisionShape.Disabled = true;
if (_sprite != null) if (_sprite != null)
{ _sprite.Modulate = canPlace
// Ghost preview coloring ? new Color(0, 1, 0, 0.5f)
_sprite.Modulate = canPlace ? new Color(1, 1, 1, 0.2f) : // white semi-transparent : new Color(1, 0, 0, 0.5f);
new Color(1, 0, 0, 0.2f); // red semi-transparent
}
} }
/// <summary>
/// Call when placement is finalized.
/// </summary>
public void FinalizePlacement() public void FinalizePlacement()
{ {
if (_collisionShape != null) if (_collisionShape != null)
_collisionShape.Disabled = false; _collisionShape.Disabled = false;
if (_sprite != null) if (_sprite != null)
_sprite.Modulate = Colors.White; // reset to normal _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()
{
var elapsed = 0f;
while (elapsed < buildTime)
{
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;
}
RunProgress();
} }
} }

View File

@@ -0,0 +1,8 @@
using Godot;
namespace AceFieldNewHorizon.Scripts.Tiles;
public partial class MinerTile : BaseTile
{
}

View File

@@ -0,0 +1 @@
uid://dyubkyqtpcg3a