Compare commits

..

8 Commits

Author SHA1 Message Date
LittleSheep
6d91d4efb8 💫 Better build effects 2025-08-28 02:06:00 +08:00
LittleSheep
4ab6271e16 Player zoom 2025-08-28 01:12:00 +08:00
LittleSheep
359e1532d2 Natural resource generator 2025-08-27 23:20:29 +08:00
LittleSheep
3fd655a3e5 Add more tiles 2025-08-27 22:56:26 +08:00
LittleSheep
d3b19aa104 Better placement and data modeling 2025-08-27 22:09:38 +08:00
LittleSheep
e54215423d Grid layer system 2025-08-27 17:52:53 +08:00
LittleSheep
84b908ef51 Rotate building tiles 2025-08-27 17:37:31 +08:00
LittleSheep
c078204e60 Player movement with sprints and optimzation 2025-08-27 17:37:04 +08:00
25 changed files with 917 additions and 67 deletions

View File

@@ -5,7 +5,9 @@
"stone": 5
},
"durability": 100,
"buildTime": 1.5
"buildTime": 1.5,
"allowedRotations": [0],
"layer": 1
},
"miner": {
"scene": "res://Scenes/Tiles/MinerTile.tscn",
@@ -14,6 +16,32 @@
"stone": 3
},
"durability": 200,
"buildTime": 3.0
"buildTime": 3.0,
"allowedRotations": [0],
"layer": 1
},
"ground": {
"scene": "res://Scenes/Tiles/GroundTile.tscn",
"cost": {},
"durability": 200,
"buildTime": 0.0,
"allowedRotations": [0],
"layer": 0
},
"stone": {
"scene": "res://Scenes/Tiles/StoneTile.tscn",
"cost": {},
"durability": 200,
"buildTime": 0.0,
"allowedRotations": [0],
"layer": 0
},
"stone_iron": {
"scene": "res://Scenes/Tiles/StoneIronTile.tscn",
"cost": {},
"durability": 200,
"buildTime": 0.0,
"allowedRotations": [0],
"layer": 0
}
}

View File

@@ -1,7 +1,8 @@
[gd_scene load_steps=5 format=3 uid="uid://c22aprj452aha"]
[gd_scene load_steps=6 format=3 uid="uid://c22aprj452aha"]
[ext_resource type="Script" uid="uid://cudpc3w17mbsw" path="res://Scripts/System/GridManager.cs" id="1_knkkn"]
[ext_resource type="Script" uid="uid://cfbj72nm0eovg" path="res://Scripts/System/BuildingRegistry.cs" id="1_sxhdm"]
[ext_resource type="Script" uid="uid://cugfbvw70clgd" path="res://Scripts/System/NaturalResourceGenerator.cs" id="2_oss8w"]
[ext_resource type="Script" uid="uid://bx1wj7gn6vrqe" path="res://Scripts/System/PlacementManager.cs" id="2_sxhdm"]
[ext_resource type="PackedScene" uid="uid://doxy60afddg1m" path="res://Scenes/Entities/Player.tscn" id="3_oss8w"]
@@ -10,6 +11,11 @@
[node name="BuildingRegistry" type="Node" parent="."]
script = ExtResource("1_sxhdm")
[node name="NaturalResourceGenerator" type="Node2D" parent="." node_paths=PackedStringArray("Grid", "Registry")]
script = ExtResource("2_oss8w")
Grid = NodePath("../GridSystem")
Registry = NodePath("../BuildingRegistry")
[node name="GridSystem" type="Node2D" parent="."]
script = ExtResource("1_knkkn")
@@ -19,5 +25,4 @@ Grid = NodePath("../GridSystem")
Registry = NodePath("../BuildingRegistry")
[node name="Player" parent="." instance=ExtResource("3_oss8w")]
position = Vector2(602, 324)
scale = Vector2(0.35, 0.35)

BIN
Scenes/Tiles/GroundTile.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 486 KiB

View File

@@ -0,0 +1,34 @@
[remap]
importer="texture"
type="CompressedTexture2D"
uid="uid://cww1edmqcmb8b"
path="res://.godot/imported/GroundTile.png-b193d222fbf869e7606f01318fb918f7.ctex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://Scenes/Tiles/GroundTile.png"
dest_files=["res://.godot/imported/GroundTile.png-b193d222fbf869e7606f01318fb918f7.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,25 @@
[gd_scene load_steps=4 format=3 uid="uid://7735luf8htft"]
[ext_resource type="Script" uid="uid://dh0jdeplrigxu" path="res://Scripts/Tiles/GroundTile.cs" id="1_mqsaf"]
[ext_resource type="Texture2D" uid="uid://cww1edmqcmb8b" path="res://Scenes/Tiles/GroundTile.png" id="2_tow7r"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_8o613"]
size = Vector2(54, 54)
[node name="GroundTile" type="StaticBody2D"]
collision_layer = 0
script = ExtResource("1_mqsaf")
TileId = "ground"
[node name="Sprite2D" type="Sprite2D" parent="."]
scale = Vector2(0.1, 0.1)
texture = ExtResource("2_tow7r")
[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

@@ -8,6 +8,7 @@ size = Vector2(54, 54)
[node name="MinerTile" type="StaticBody2D"]
script = ExtResource("1_mecoy")
TileId = "miner"
[node name="Sprite2D" type="Sprite2D" parent="."]
scale = Vector2(0.1, 0.1)

Binary file not shown.

After

Width:  |  Height:  |  Size: 474 KiB

View File

@@ -0,0 +1,34 @@
[remap]
importer="texture"
type="CompressedTexture2D"
uid="uid://c3qdue55e6blv"
path="res://.godot/imported/StoneIronTile.png-a3d6be6bb8b8b8a0f32242e23f1c00ca.ctex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://Scenes/Tiles/StoneIronTile.png"
dest_files=["res://.godot/imported/StoneIronTile.png-a3d6be6bb8b8b8a0f32242e23f1c00ca.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,25 @@
[gd_scene load_steps=4 format=3 uid="uid://dnkcl5ucmip40"]
[ext_resource type="Script" uid="uid://dh0jdeplrigxu" path="res://Scripts/Tiles/GroundTile.cs" id="1_ewklp"]
[ext_resource type="Texture2D" uid="uid://c3qdue55e6blv" path="res://Scenes/Tiles/StoneIronTile.png" id="2_ewklp"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_8o613"]
size = Vector2(54, 54)
[node name="StoneIronTile" type="StaticBody2D"]
collision_layer = 0
script = ExtResource("1_ewklp")
TileId = "stone_iron"
[node name="Sprite2D" type="Sprite2D" parent="."]
scale = Vector2(0.1, 0.1)
texture = ExtResource("2_ewklp")
[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

BIN
Scenes/Tiles/StoneTile.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 590 KiB

View File

@@ -0,0 +1,34 @@
[remap]
importer="texture"
type="CompressedTexture2D"
uid="uid://ca0qwr6hngho2"
path="res://.godot/imported/StoneTile.png-20e063912018fbf7fd000e734fc2097f.ctex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://Scenes/Tiles/StoneTile.png"
dest_files=["res://.godot/imported/StoneTile.png-20e063912018fbf7fd000e734fc2097f.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,25 @@
[gd_scene load_steps=4 format=3 uid="uid://bafvcatjix3mx"]
[ext_resource type="Script" uid="uid://dh0jdeplrigxu" path="res://Scripts/Tiles/GroundTile.cs" id="1_rndy8"]
[ext_resource type="Texture2D" uid="uid://ca0qwr6hngho2" path="res://Scenes/Tiles/StoneTile.png" id="2_rndy8"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_8o613"]
size = Vector2(54, 54)
[node name="StoneTile" type="StaticBody2D"]
collision_layer = 0
script = ExtResource("1_rndy8")
TileId = "stone"
[node name="Sprite2D" type="Sprite2D" parent="."]
scale = Vector2(0.1, 0.1)
texture = ExtResource("2_rndy8")
[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

@@ -8,6 +8,7 @@ size = Vector2(54, 54)
[node name="WallTile" type="StaticBody2D"]
script = ExtResource("1_ph7y3")
TileId = "wall"
[node name="Sprite2D" type="Sprite2D" parent="."]
scale = Vector2(0.05, 0.05)

View File

@@ -5,8 +5,74 @@ namespace AceFieldNewHorizon.Scripts.Entities;
public partial class Player : CharacterBody2D
{
[Export] public float Speed = 400.0f;
[Export] public float MaxSpeed = 400.0f;
[Export] public float SprintMultiplier = 1.8f; // 80% faster when sprinting
[Export] public float Acceleration = 1500.0f;
[Export] public float SprintAcceleration = 1800.0f; // Slightly faster acceleration when sprinting
[Export] public float Deceleration = 1200.0f;
[Export] public float RotationSpeed = 3.0f;
[Export] public float MinZoom = 0.5f;
[Export] public float MaxZoom = 2.0f;
[Export] public float BaseZoomSpeed = 0.1f;
[Export] public float MaxZoomSpeed = 0.5f;
[Export] public float ZoomAcceleration = 0.05f;
[Export] public float ZoomDecay = 0.9f;
[Export] public float ZoomSmoothing = 10.0f;
private Camera2D _camera;
private Vector2 _cameraTargetZoom = Vector2.One;
private float _currentZoomSpeed;
private int _lastZoomDirection;
private double _lastZoomTime;
public override void _Ready()
{
_camera = GetNode<Camera2D>("Camera2D");
_cameraTargetZoom = _camera.Zoom;
}
public override void _Input(InputEvent @event)
{
// Handle mouse wheel zoom
if (@event is InputEventMouseButton mouseEvent)
{
switch (mouseEvent.ButtonIndex)
{
case MouseButton.WheelDown when mouseEvent.Pressed:
HandleZoomInput(1);
break;
case MouseButton.WheelUp when mouseEvent.Pressed:
HandleZoomInput(-1);
break;
}
}
}
private void HandleZoomInput(int direction)
{
var currentTime = Time.GetTicksMsec();
// If same direction as last time, accelerate
if (direction == _lastZoomDirection && (currentTime - _lastZoomTime) < 300)
{
_currentZoomSpeed = Mathf.Min(_currentZoomSpeed + ZoomAcceleration, MaxZoomSpeed);
}
else
{
_currentZoomSpeed = BaseZoomSpeed;
}
_lastZoomDirection = direction;
_lastZoomTime = currentTime;
// Apply zoom with current speed
var zoomFactor = 1.0f + (_currentZoomSpeed * -direction);
_cameraTargetZoom = _camera.Zoom * zoomFactor;
// Clamp target zoom
_cameraTargetZoom.X = Mathf.Clamp(_cameraTargetZoom.X, MinZoom, MaxZoom);
_cameraTargetZoom.Y = Mathf.Clamp(_cameraTargetZoom.Y, MinZoom, MaxZoom);
}
public override void _Process(double delta)
{
@@ -14,16 +80,59 @@ public partial class Player : CharacterBody2D
var mousePos = GetGlobalMousePosition();
var direction = GlobalPosition.DirectionTo(mousePos);
Rotation = direction.Angle();
// Smoothly interpolate to target zoom
var deltaF = (float)delta;
_camera.Zoom = _camera.Zoom.Lerp(_cameraTargetZoom, ZoomSmoothing * deltaF);
// Decay zoom speed when not zooming
if ((Time.GetTicksMsec() - _lastZoomTime) > 300)
{
_currentZoomSpeed = Mathf.Max(_currentZoomSpeed * ZoomDecay, BaseZoomSpeed);
}
}
public override void _PhysicsProcess(double delta)
{
// Get movement input
var moveForward = Input.GetActionStrength("move_up") - Input.GetActionStrength("move_down");
var moveHorizontal = Input.GetActionStrength("move_right") - Input.GetActionStrength("move_left");
var isSprinting = Input.IsActionPressed("move_sprint");
// Calculate movement direction based on rotation
Velocity = Vector2.Right.Rotated(Rotation) * moveForward * Speed;
// Calculate movement parameters based on sprint state
var currentMaxSpeed = isSprinting ? MaxSpeed * SprintMultiplier : MaxSpeed;
var currentAcceleration = isSprinting ? SprintAcceleration : Acceleration;
// Calculate desired movement direction
var forwardVector = Vector2.Right.Rotated(Rotation);
var rightVector = new Vector2(-forwardVector.Y, forwardVector.X);
// Calculate target velocity based on input
var targetVelocity = (forwardVector * moveForward + rightVector * moveHorizontal).Normalized() * currentMaxSpeed;
// Apply acceleration or deceleration
var currentSpeed = Velocity.Length();
var isAccelerating = targetVelocity != Vector2.Zero;
if (isAccelerating)
{
// Accelerate towards target velocity
Velocity = Velocity.MoveToward(targetVelocity, (float)(currentAcceleration * delta));
}
else
{
// Apply deceleration when no input
if (currentSpeed > 0)
{
var decelAmount = (float)(Deceleration * delta);
if (currentSpeed <= decelAmount)
Velocity = Vector2.Zero;
else
Velocity = Velocity.Normalized() * (currentSpeed - decelAmount);
}
}
// Apply the movement
MoveAndSlide();
}
}

View File

@@ -1,17 +1,46 @@
using System.Collections.Generic;
using System.Linq;
using Godot;
namespace AceFieldNewHorizon.Scripts.System;
public enum RotationDirection
{
Up, // 0 degrees
Right, // 90 degrees
Down, // 180 degrees
Left // 270 degrees
}
public record BuildingData(
PackedScene Scene,
Dictionary<string, int> Cost,
int Durability,
GridLayer Layer,
float BuildTime,
Vector2I Size = default,
RotationDirection[] AllowedRotations = null
)
{
public Vector2I Size { get; } = Size == default ? Vector2I.One : Size;
public RotationDirection[] AllowedRotations { get; } =
AllowedRotations ?? [RotationDirection.Up, RotationDirection.Right, RotationDirection.Down, RotationDirection.Left
];
public bool IsRotationAllowed(float degrees)
{
var direction = (RotationDirection)((Mathf.RoundToInt(degrees / 90f) % 4 + 4) % 4);
return AllowedRotations.Contains(direction);
}
public Vector2I GetRotatedSize(float degrees)
{
return (Mathf.RoundToInt(degrees / 90f) % 2) == 0 ? Size : new Vector2I(Size.Y, Size.X);
}
}
public partial class BuildingRegistry : Node
{
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";
@@ -45,7 +74,7 @@ public partial class BuildingRegistry : Node
foreach (string key in dict.Keys)
{
// Each entry is a Dictionary with keys: "scene", "cost", "durability", "buildTime"
// Each entry is a Dictionary with keys: "scene", "cost", "durability", "buildTime", "size", "allowedRotations"
var buildingDict = dict[key].AsGodotDictionary();
// Parse scene
@@ -115,7 +144,36 @@ public partial class BuildingRegistry : Node
}
}
var buildingData = new BuildingData(scene, cost, durability, buildTime);
// Parse size
var size = Vector2I.One;
if (buildingDict.TryGetValue("size", out var sObj))
{
var sizeArray = sObj.AsGodotArray();
if (sizeArray.Count == 2)
{
size = new Vector2I((int)sizeArray[0].AsInt64(), (int)sizeArray[1].AsInt64());
}
}
// Parse allowedRotations
RotationDirection[] allowedRotations = null;
if (buildingDict.TryGetValue("allowedRotations", out var arObj))
{
var arArray = arObj.AsGodotArray();
allowedRotations = new RotationDirection[arArray.Count];
for (var i = 0; i < arArray.Count; i++)
{
allowedRotations[i] = (RotationDirection)arArray[i].AsInt32();
}
}
var layer = GridLayer.Building;
if (buildingDict.TryGetValue("layer", out var lObj))
{
layer = (GridLayer)lObj.AsInt32();
}
var buildingData = new BuildingData(scene, cost, durability, layer, buildTime, size, allowedRotations);
_registry[key] = buildingData;
GD.Print($"[BuildingRegistry] Loaded building '{key}' from {scenePath}");
}

View File

@@ -1,31 +1,107 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
using Godot;
namespace AceFieldNewHorizon.Scripts.System;
public enum GridLayer
{
Ground, // Base layer (e.g., terrain, floors)
Building, // Main building layer
Decoration // Additional layer for decorations, effects, etc.
}
public partial class GridManager : Node
{
private Dictionary<Vector2I, Node2D> _grid = new();
private Dictionary<GridLayer, Dictionary<Vector2I, (Node2D Building, Vector2I Size, float Rotation)>> _layers =
new();
public bool IsCellFree(Vector2I cell)
public GridManager()
{
return !_grid.ContainsKey(cell);
// Initialize all layers
foreach (GridLayer layer in Enum.GetValues(typeof(GridLayer)))
_layers[layer] = new Dictionary<Vector2I, (Node2D, Vector2I, float)>();
}
public void OccupyCell(Vector2I cell, Node2D building)
public bool IsAreaFree(Vector2I topLeft, Vector2I size, float rotation, GridLayer layer = GridLayer.Building)
{
_grid[cell] = building;
var occupiedCells = GridUtils.GetOccupiedCells(topLeft, size, rotation);
return occupiedCells.All(cell => !_layers[layer].ContainsKey(cell));
}
public void FreeCell(Vector2I cell)
public void OccupyArea(Vector2I topLeft, Node2D building, Vector2I size, float rotation,
GridLayer layer = GridLayer.Building)
{
_grid.Remove(cell);
var occupiedCells = GridUtils.GetOccupiedCells(topLeft, size, rotation);
foreach (var cell in occupiedCells)
{
_layers[layer][cell] = (building, size, rotation);
}
}
public Node2D? GetBuildingAtCell(Vector2I cell)
public void FreeArea(Vector2I topLeft, Vector2I size, float rotation, GridLayer layer = GridLayer.Building)
{
return _grid.GetValueOrDefault(cell);
var occupiedCells = GridUtils.GetOccupiedCells(topLeft, size, rotation);
foreach (var cell in occupiedCells)
{
if (_layers[layer].ContainsKey(cell))
_layers[layer].Remove(cell);
}
}
public Node2D? GetBuildingAtCell(Vector2I cell, GridLayer layer = GridLayer.Building)
{
return _layers[layer].TryGetValue(cell, out var data) ? data.Building : null;
}
public bool IsAnyCellOccupied(Vector2I cell, params GridLayer[] layers)
{
if (layers.Length == 0)
{
// Check all layers if none specified
foreach (var layer in _layers.Values)
{
if (layer.ContainsKey(cell)) return true;
}
return false;
}
foreach (var layer in layers)
{
if (_layers[layer].ContainsKey(cell)) return true;
}
return false;
}
public bool IsAreaOccupied(Vector2I topLeft, Vector2I size, float rotation, params GridLayer[] layers)
{
var occupiedCells = GridUtils.GetOccupiedCells(topLeft, size, rotation);
if (layers.Length == 0)
{
// Check all layers if none specified
foreach (var cell in occupiedCells)
{
foreach (var layer in _layers.Values)
if (layer.ContainsKey(cell))
return true;
}
return false;
}
foreach (var cell in occupiedCells)
{
foreach (var layer in layers)
if (_layers[layer].ContainsKey(cell))
return true;
}
return false;
}
}
@@ -48,4 +124,26 @@ public static class GridUtils
cell.Y * TileSize
);
}
public static Vector2 GetCenterOffset(Vector2I size, float rotation)
{
var rotatedSize = (Mathf.RoundToInt(rotation / 90f) % 2) == 0 ? size : new Vector2I(size.Y, size.X);
return new Vector2(rotatedSize.X * TileSize / 2f, rotatedSize.Y * TileSize / 2f);
}
public static List<Vector2I> GetOccupiedCells(Vector2I topLeft, Vector2I size, float rotation)
{
var occupiedCells = new List<Vector2I>();
var rotatedSize = (Mathf.RoundToInt(rotation / 90f) % 2) == 0 ? size : new Vector2I(size.Y, size.X);
for (int x = 0; x < rotatedSize.X; x++)
{
for (int y = 0; y < rotatedSize.Y; y++)
{
occupiedCells.Add(new Vector2I(topLeft.X + x, topLeft.Y + y));
}
}
return occupiedCells;
}
}

View File

@@ -0,0 +1,153 @@
using Godot;
using System.Collections.Generic;
using AceFieldNewHorizon.Scripts.Tiles;
namespace AceFieldNewHorizon.Scripts.System;
public partial class NaturalResourceGenerator : Node2D
{
[Export] public GridManager Grid { get; set; }
[Export] public BuildingRegistry Registry { get; set; }
[Export] public int MapWidth = 100;
[Export] public int MapHeight = 100;
[Export] public float StoneDensity = 0.1f; // 10% chance for stone
[Export] public float IronDensity = 0.03f; // 3% chance for iron (within stone)
[Export] public int MinStoneVeinSize = 1;
[Export] public int MaxStoneVeinSize = 5;
[Export] public int MinIronVeinSize = 1;
[Export] public int MaxIronVeinSize = 3;
[Export] public int Seed;
private RandomNumberGenerator _rng;
private readonly List<Vector2I> _groundTiles = [];
private readonly List<Vector2I> _stoneTiles = [];
private readonly List<Vector2I> _ironTiles = [];
public override void _Ready()
{
_rng = new RandomNumberGenerator();
_rng.Seed = (ulong)(Seed != 0 ? Seed : (int)GD.Randi());
GenerateTerrain();
PlaceResources();
}
private void GenerateTerrain()
{
// First pass: Generate base ground tiles
for (int x = 0; x < MapWidth; x++)
{
for (int y = 0; y < MapHeight; y++)
{
var cell = new Vector2I(x, y);
_groundTiles.Add(cell);
PlaceTile("ground", cell);
}
}
}
private void PlaceResources()
{
// Create a copy of ground tiles for iteration
var groundTilesToProcess = new List<Vector2I>(_groundTiles);
// Place stone veins
foreach (var cell in groundTilesToProcess)
{
if (_rng.Randf() < StoneDensity)
{
var veinSize = _rng.RandiRange(MinStoneVeinSize, MaxStoneVeinSize);
PlaceVein(cell, "stone", veinSize, _stoneTiles);
}
}
// Create a copy of stone tiles for iteration
var stoneTilesToProcess = new List<Vector2I>(_stoneTiles);
// Place iron veins within stone
foreach (var stoneCell in stoneTilesToProcess)
{
if (_rng.Randf() < IronDensity)
{
var veinSize = _rng.RandiRange(MinIronVeinSize, MaxIronVeinSize);
PlaceVein(stoneCell, "stone_iron", veinSize, _ironTiles);
}
}
}
private void PlaceVein(Vector2I startCell, string tileType, int maxVeinSize, ICollection<Vector2I> tileList)
{
var queue = new Queue<Vector2I>();
var placed = new HashSet<Vector2I>();
queue.Enqueue(startCell);
int placedCount = 0;
while (queue.Count > 0 && placedCount < maxVeinSize)
{
var cell = queue.Dequeue();
if (placed.Contains(cell)) continue;
if (!IsInBounds(cell)) continue;
switch (tileType)
{
// For iron, make sure we're placing on stone
case "stone_iron" when !_stoneTiles.Contains(cell):
continue;
// Remove from previous layer if needed
case "stone_iron" when _stoneTiles.Contains(cell):
_stoneTiles.Remove(cell);
break;
case "stone" when _groundTiles.Contains(cell):
_groundTiles.Remove(cell);
break;
}
PlaceTile(tileType, cell);
tileList.Add(cell);
placed.Add(cell);
placedCount++;
// Add adjacent cells to queue
for (var dx = -1; dx <= 1; dx++)
{
for (var dy = -1; dy <= 1; dy++)
{
if (dx == 0 && dy == 0) continue; // Skip self
var neighbor = new Vector2I(cell.X + dx, cell.Y + dy);
if (!placed.Contains(neighbor) && IsInBounds(neighbor))
{
queue.Enqueue(neighbor);
}
}
}
}
}
private bool IsInBounds(Vector2I cell)
{
return cell.X >= 0 && cell.X < MapWidth &&
cell.Y >= 0 && cell.Y < MapHeight;
}
private void PlaceTile(string tileType, Vector2I cell)
{
var building = Registry.GetBuilding(tileType);
if (building == null) return;
var scene = building.Scene;
var instance = (BaseTile)scene.Instantiate();
// Match PlacementManager's positioning logic
var rotatedSize = building.GetRotatedSize(0f);
var offset = GridUtils.GetCenterOffset(rotatedSize, 0f);
instance.Position = GridUtils.GridToWorld(cell) + offset;
instance.ZIndex = (int)building.Layer;
AddChild(instance);
// Make sure to use the building's size from the registry
Grid.OccupyArea(cell, instance, building.Size, 0f, building.Layer);
}
}

View File

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

View File

@@ -1,5 +1,7 @@
using System;
using AceFieldNewHorizon.Scripts.Tiles;
using Godot;
using System.Linq;
namespace AceFieldNewHorizon.Scripts.System;
@@ -8,82 +10,202 @@ public partial class PlacementManager : Node2D
[Export] public GridManager Grid { get; set; }
[Export] public BuildingRegistry Registry { get; set; }
private string _currentBuildingId = "miner";
private string _currentBuildingId = "wall";
private Vector2I _hoveredCell;
private BaseTile _ghostBuilding;
private float _currentRotation;
private Vector2I _currentBuildingSize = Vector2I.One;
public void SetCurrentBuilding(string buildingId)
{
_currentBuildingId = buildingId;
var buildingData = Registry.GetBuilding(buildingId);
if (buildingData != null)
{
_currentBuildingSize = buildingData.Size;
// Reset rotation to nearest allowed rotation
if (!buildingData.IsRotationAllowed(_currentRotation))
{
_currentRotation = (int)buildingData.AllowedRotations[0] * 90f;
}
}
// Replace ghost immediately
if (_ghostBuilding != null)
{
_ghostBuilding.QueueFree();
_ghostBuilding = null;
}
}
private void RotateGhost(bool reverse = false)
{
if (_ghostBuilding == null) return;
_ghostBuilding.QueueFree();
_ghostBuilding = null;
var buildingData = Registry.GetBuilding(_currentBuildingId);
if (buildingData == null) return;
// Calculate next rotation
var currentDirection = (RotationDirection)((Mathf.RoundToInt(_currentRotation / 90f) % 4 + 4) % 4);
var currentIndex = Array.IndexOf(buildingData.AllowedRotations.ToArray(), currentDirection);
if (reverse)
currentIndex = (currentIndex - 1 + buildingData.AllowedRotations.Length) %
buildingData.AllowedRotations.Length;
else
currentIndex = (currentIndex + 1) % buildingData.AllowedRotations.Length;
_currentRotation = (int)buildingData.AllowedRotations[currentIndex] * 90f;
_ghostBuilding.RotationDegrees = _currentRotation;
// Update ghost position to keep the same cell under cursor
UpdateGhostPosition();
}
private void UpdateGhostPosition()
{
if (_ghostBuilding == null) return;
var buildingData = Registry.GetBuilding(_currentBuildingId);
if (buildingData == null) return;
var rotatedSize = buildingData.GetRotatedSize(_currentRotation);
var offset = GridUtils.GetCenterOffset(rotatedSize, _currentRotation);
_ghostBuilding.Position = GridUtils.GridToWorld(_hoveredCell) + offset;
// Update occupied cells
GridUtils.GetOccupiedCells(_hoveredCell, rotatedSize, _currentRotation);
}
public override void _Process(double delta)
{
// Snap mouse to grid
var mousePos = GetGlobalMousePosition();
_hoveredCell = GridUtils.WorldToGrid(mousePos);
var newHoveredCell = GridUtils.WorldToGrid(mousePos);
// Only update if cell changed
if (newHoveredCell != _hoveredCell)
{
_hoveredCell = newHoveredCell;
UpdateGhostPosition();
}
if (Input.IsActionJustPressed("rotate_tile_reverse"))
RotateGhost(reverse: true);
else if (Input.IsActionJustPressed("rotate_tile"))
RotateGhost();
if (_ghostBuilding == null)
{
var scene = Registry.GetBuilding(_currentBuildingId)?.Scene;
if (scene == null) return;
var building = Registry.GetBuilding(_currentBuildingId);
if (building == null) return;
var scene = building.Scene;
_ghostBuilding = (BaseTile)scene.Instantiate();
_ghostBuilding.SetGhostMode(true);
_ghostBuilding.RotationDegrees = _currentRotation;
_ghostBuilding.ZAsRelative = false;
_ghostBuilding.ZIndex = (int)building.Layer;
UpdateGhostPosition();
AddChild(_ghostBuilding);
}
var placementPos = GridUtils.GridToWorld(_hoveredCell);
var spaceState = GetWorld2D().DirectSpaceState;
var centerPos = placementPos + new Vector2(GridUtils.TileSize / 2f, GridUtils.TileSize / 2f);
var query = new PhysicsPointQueryParameters2D
{
Position = centerPos,
CollideWithBodies = true,
CollideWithAreas = true,
CollisionMask = uint.MaxValue // check against all layers/masks
// Exclude = new Godot.Collections.Array<Rid>() // optional, see below
};
var collision = spaceState.IntersectPoint(query, 8);
var canPlace = Grid.IsCellFree(_hoveredCell) && collision.Count == 0;
_ghostBuilding.Position = placementPos;
var canPlace = CanPlaceBuilding();
_ghostBuilding.SetGhostMode(canPlace);
// Left click to place
if (Input.IsActionPressed("build_tile") && canPlace)
{
var scene = Registry.GetBuilding(_currentBuildingId)?.Scene;
if (scene == null) return;
_ghostBuilding.FinalizePlacement();
var building = Registry.GetBuilding(_currentBuildingId);
if (building == null) return;
var buildingData = Registry.GetBuilding(_currentBuildingId);
Grid.OccupyCell(_hoveredCell, _ghostBuilding);
var scene = building.Scene;
var buildingInstance = (BaseTile)scene.Instantiate();
buildingInstance.RotationDegrees = _currentRotation;
buildingInstance.ZIndex = (int)building.Layer;
buildingInstance.Position = _ghostBuilding.Position;
AddChild(buildingInstance);
if (buildingData is { BuildTime: > 0f })
_ghostBuilding.StartConstruction(buildingData.BuildTime);
Grid.OccupyArea(_hoveredCell, buildingInstance, building.Size, _currentRotation, building.Layer);
_ghostBuilding = (BaseTile)scene.Instantiate();
_ghostBuilding.SetGhostMode(true);
AddChild(_ghostBuilding);
if (building.BuildTime > 0f)
buildingInstance.StartConstruction(building.BuildTime);
}
if (Input.IsActionPressed("destroy_tile") && !Grid.IsCellFree(_hoveredCell))
if (Input.IsActionPressed("destroy_tile") &&
!Grid.IsAreaFree(_hoveredCell, Vector2I.One, 0f))
{
// Right click to destroy
// Right click to destroy from current layer
var building = Grid.GetBuildingAtCell(_hoveredCell);
if (building == null) return;
// Find all cells occupied by this building
var buildingInfo = Grid.GetBuildingInfoAtCell(_hoveredCell, GridLayer.Building);
if (buildingInfo == null) return;
building.QueueFree();
Grid.FreeCell(_hoveredCell);
Grid.FreeArea(buildingInfo.Value.Position, buildingInfo.Value.Size, buildingInfo.Value.Rotation);
}
}
private bool CanPlaceBuilding()
{
var buildingData = Registry.GetBuilding(_currentBuildingId);
if (buildingData == null) return false;
// Check if rotation is allowed
if (!buildingData.IsRotationAllowed(_currentRotation))
return false;
// Check if area is free
var rotatedSize = buildingData.GetRotatedSize(_currentRotation);
return !Grid.IsAreaOccupied(_hoveredCell, rotatedSize, _currentRotation, GetBlockingLayers(buildingData.Layer));
}
private GridLayer[] GetBlockingLayers(GridLayer layer)
{
return layer switch
{
GridLayer.Ground => [GridLayer.Ground],
GridLayer.Building => [GridLayer.Building],
GridLayer.Decoration => [GridLayer.Decoration],
_ => []
};
}
}
public static class GridManagerExtensions
{
public static (Vector2I Position, Vector2I Size, float Rotation)? GetBuildingInfoAtCell(this GridManager grid,
Vector2I cell, GridLayer layer)
{
if (grid.GetBuildingAtCell(cell, layer) is { } building)
{
// Find the top-left position of the building
for (int x = 0; x < 100; x++) // Arbitrary max size
{
for (int y = 0; y < 100; y++)
{
var checkCell = new Vector2I(cell.X - x, cell.Y - y);
if (grid.GetBuildingAtCell(checkCell, layer) == building)
{
// Found the top-left corner, now find the size
var size = Vector2I.One;
// Search right
while (grid.GetBuildingAtCell(new Vector2I(checkCell.X + size.X, checkCell.Y), layer) ==
building)
size.X++;
// Search down
while (grid.GetBuildingAtCell(new Vector2I(checkCell.X, checkCell.Y + size.Y), layer) ==
building)
size.Y++;
// Get rotation from the first cell
var rotation = 0f; // You'll need to store rotation in GridManager to make this work
return (checkCell, size, rotation);
}
}
}
}
return null;
}
}

View File

@@ -1,4 +1,4 @@
using System.Numerics;
using System.Threading.Tasks;
using AceFieldNewHorizon.Scripts.System;
using Godot;
using Vector2 = Godot.Vector2;
@@ -7,9 +7,14 @@ namespace AceFieldNewHorizon.Scripts.Tiles;
public partial class BaseTile : Node2D
{
[Export] public string TileId { get; set; }
public BuildingData TileData { get; set; }
private CollisionShape2D _collisionShape;
private Sprite2D _sprite;
private ColorRect _progressOverlay;
private AudioStreamPlayer _completionSound;
public override void _Ready()
{
@@ -18,6 +23,15 @@ public partial class BaseTile : Node2D
_progressOverlay = GetNodeOrNull<ColorRect>("ProgressOverlay");
if (_progressOverlay != null)
_progressOverlay.Visible = false;
// Setup audio player
_completionSound = new AudioStreamPlayer();
AddChild(_completionSound);
var sound = GD.Load<AudioStream>("res://Sounds/Events/ConstructionComplete.wav");
if (sound != null)
{
_completionSound.Stream = sound;
}
}
public void SetGhostMode(bool canPlace)
@@ -47,6 +61,7 @@ public partial class BaseTile : Node2D
var texSize = new Vector2(GridUtils.TileSize, GridUtils.TileSize);
_progressOverlay.Visible = true;
_progressOverlay.Modulate = Colors.White;
_progressOverlay.Color = new Color(0, 0, 1, 0.4f); // semi-transparent blue
async void RunProgress()
@@ -67,9 +82,33 @@ public partial class BaseTile : Node2D
);
}
_progressOverlay.Visible = false;
// Fade out the overlay instead of making it instantly disappear
await FadeOutOverlay(0.5f);
}
RunProgress();
}
private async Task FadeOutOverlay(float duration)
{
var elapsed = 0f;
var startAlpha = _progressOverlay.Modulate.A;
// Play completion sound
if (_completionSound?.Stream != null)
{
_completionSound.Play();
}
while (elapsed < duration)
{
await ToSignal(GetTree(), SceneTree.SignalName.ProcessFrame);
elapsed += (float)GetProcessDeltaTime();
var alpha = Mathf.Lerp(startAlpha, 0f, elapsed / duration);
_progressOverlay.Modulate = new Color(1, 1, 1, alpha);
}
_progressOverlay.Visible = false;
_progressOverlay.Modulate = Colors.White; // Reset alpha for next use
}
}

View File

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

View File

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

Binary file not shown.

View File

@@ -0,0 +1,24 @@
[remap]
importer="wav"
type="AudioStreamWAV"
uid="uid://xmgj505csg5x"
path="res://.godot/imported/ConstructionComplete.wav-094dbaa5fa3f80e5fe8fc1583055a77d.sample"
[deps]
source_file="res://Sounds/Events/ConstructionComplete.wav"
dest_files=["res://.godot/imported/ConstructionComplete.wav-094dbaa5fa3f80e5fe8fc1583055a77d.sample"]
[params]
force/8_bit=false
force/mono=false
force/max_rate=false
force/max_rate_hz=44100
edit/trim=false
edit/normalize=false
edit/loop_mode=0
edit/loop_begin=0
edit/loop_end=-1
compress/mode=2

View File

@@ -55,6 +55,31 @@ destroy_tile={
"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":2,"position":Vector2(481, 42),"global_position":Vector2(500, 138),"factor":1.0,"button_index":2,"canceled":false,"pressed":true,"double_click":false,"script":null)
]
}
rotate_tile={
"deadzone": 0.2,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":79,"key_label":0,"unicode":111,"location":0,"echo":false,"script":null)
]
}
rotate_tile_reverse={
"deadzone": 0.2,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":true,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":79,"key_label":0,"unicode":79,"location":0,"echo":false,"script":null)
]
}
move_sprint={
"deadzone": 0.2,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194325,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
]
}
camera_zoom_in={
"deadzone": 0.2,
"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":8,"position":Vector2(437, 34),"global_position":Vector2(456, 130),"factor":0.0300018,"button_index":4,"canceled":false,"pressed":true,"double_click":false,"script":null)
]
}
camera_zoom_out={
"deadzone": 0.2,
"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":16,"position":Vector2(583, 50),"global_position":Vector2(602, 146),"factor":0.437677,"button_index":5,"canceled":false,"pressed":true,"double_click":false,"script":null)
]
}
[rendering]