Add Godot conversion extensions and update related code for integration

This commit is contained in:
2025-08-08 16:03:25 +02:00
parent cabf13d164
commit ba366157fd
15 changed files with 50 additions and 23 deletions

View File

@@ -8,6 +8,9 @@ public readonly struct Vec2I(int x, int y) : IEquatable<Vec2I>
public static Vec2I operator +(Vec2I a, Vec2I b) => new(a.X + b.X, a.Y + b.Y); public static Vec2I operator +(Vec2I a, Vec2I b) => new(a.X + b.X, a.Y + b.Y);
public static Vec2I operator -(Vec2I a, Vec2I b) => new(a.X - b.X, a.Y - b.Y); public static Vec2I operator -(Vec2I a, Vec2I b) => new(a.X - b.X, a.Y - b.Y);
public static bool operator ==(Vec2I left, Vec2I right) => left.Equals(right);
public static bool operator !=(Vec2I left, Vec2I right) => !left.Equals(right);
public override string ToString() => $"({X}, {Y})"; public override string ToString() => $"({X}, {Y})";
public bool Equals(Vec2I other) => X == other.X && Y == other.Y; public bool Equals(Vec2I other) => X == other.X && Y == other.Y;
public override bool Equals(object? obj) => obj is Vec2I other && Equals(other); public override bool Equals(object? obj) => obj is Vec2I other && Equals(other);

5
global.json Normal file
View File

@@ -0,0 +1,5 @@
{
"sdk": {
"version": "8.0.100"
}
}

View File

@@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using Civilization.Core.Game; using Civilization.Core.Game;
using Civilization.GodotIntegration.Utils;
using Godot; using Godot;
namespace Civilization.GodotIntegration; namespace Civilization.GodotIntegration;
@@ -20,7 +21,7 @@ public partial class CityRenderer : Node2D
foreach (var city in state.Cities) foreach (var city in state.Cities)
{ {
var cityNode = CityScene.Instantiate<Node2D>(); var cityNode = CityScene.Instantiate<Node2D>();
cityNode.Position = MapRenderer.MapToWorld(city.Position); cityNode.Position = MapRenderer.MapToWorld(city.Position.ToGodot());
AddChild(cityNode); AddChild(cityNode);
_cityViews[city.Id] = cityNode; _cityViews[city.Id] = cityNode;
} }

View File

@@ -1,4 +1,4 @@
<Project Sdk="Godot.NET.Sdk/4.2.0"> <Project Sdk="Godot.NET.Sdk/4.4.1">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net8.0</TargetFramework> <TargetFramework>net8.0</TargetFramework>
@@ -9,5 +9,4 @@
<ProjectReference Include="../../Lib/Civilization.Core/Civilization.Core.csproj" /> <ProjectReference Include="../../Lib/Civilization.Core/Civilization.Core.csproj" />
<ProjectReference Include="../../Lib/Civilization.Shared/Civilization.Shared.csproj" /> <ProjectReference Include="../../Lib/Civilization.Shared/Civilization.Shared.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@@ -3,6 +3,7 @@ using Civilization.Core;
using Civilization.Core.Game; using Civilization.Core.Game;
using Civilization.Core.Grid; using Civilization.Core.Grid;
using Civilization.Core.Units; using Civilization.Core.Units;
using Civilization.GodotIntegration.Utils;
using Godot; using Godot;
namespace Civilization.GodotIntegration; namespace Civilization.GodotIntegration;
@@ -26,8 +27,8 @@ public partial class GameController : Node
var players = new List<Player> var players = new List<Player>
{ {
new Player(0, "Player 1", Colors.Red), new Player(0, "Player 1", Colors.Red.ToCore()),
new Player(1, "Player 2", Colors.Blue) new Player(1, "Player 2", Colors.Blue.ToCore())
}; };
var gameState = new GameState(gameMap, players); var gameState = new GameState(gameMap, players);
@@ -38,7 +39,7 @@ public partial class GameController : Node
InputSystem.OnStateChanged = Redraw; InputSystem.OnStateChanged = Redraw;
// Add one settler to start // Add one settler to start
var settler = new Unit(0, UnitType.Settler, new Vector2I(2, 2)); var settler = new Unit(0, UnitType.Settler, new Vector2I(2, 2).ToCore());
gameState.AddUnit(settler); gameState.AddUnit(settler);
GD.Print($"Added settler unit at {settler.Position}"); GD.Print($"Added settler unit at {settler.Position}");

View File

@@ -1,5 +1,6 @@
using System; using System;
using Civilization.Core.Actions; using Civilization.Core.Actions;
using Civilization.GodotIntegration.Utils;
using Godot; using Godot;
namespace Civilization.GodotIntegration; namespace Civilization.GodotIntegration;
@@ -31,7 +32,7 @@ public partial class InputSystem : Node
if (selected != null) if (selected != null)
{ {
if (!isRightClick) return; if (!isRightClick) return;
var move = new MoveUnitAction(selected.Id, position); var move = new MoveUnitAction(selected.Id, position.ToCore());
if (!move.CanExecute(context)) return; if (!move.CanExecute(context)) return;

View File

@@ -1,4 +1,5 @@
using Civilization.Core; using Civilization.Core;
using Civilization.GodotIntegration.Utils;
using Godot; using Godot;
namespace Civilization.GodotIntegration; namespace Civilization.GodotIntegration;
@@ -33,7 +34,7 @@ public partial class MapRenderer : Node2D
var pos = tile.Position; var pos = tile.Position;
var tileId = (int)tile.Type + TileIndexOffset; var tileId = (int)tile.Type + TileIndexOffset;
var atlasCoords = tileSetSource.GetTileId(tileId); var atlasCoords = tileSetSource.GetTileId(tileId);
TileMapLayer.SetCell(pos, tileId, atlasCoords); TileMapLayer.SetCell(pos.ToGodot(), tileId, atlasCoords);
} }
} }

View File

@@ -6,21 +6,21 @@ namespace Civilization.GodotIntegration;
public partial class SelectedUnitPanel : Control public partial class SelectedUnitPanel : Control
{ {
[Export] public Label UnitInfoLabel; // [Export] public Label UnitInfoLabel;
[Export] public Button SettleButton; // [Export] public Button SettleButton;
//
public Action? OnSettleClicked; // public Action? OnSettleClicked;
public override void _Ready() public override void _Ready()
{ {
SettleButton.Pressed += () => OnSettleClicked?.Invoke(); // SettleButton.Pressed += () => OnSettleClicked?.Invoke();
Hide(); Hide();
} }
public void ShowFor(Unit unit) public void ShowFor(Unit unit)
{ {
GD.Print($"Showing unit panel for {unit.Id} at {unit.Position} ({unit.Type})"); GD.Print($"Showing unit panel for {unit.Id} at {unit.Position} ({unit.Type})");
UnitInfoLabel.Text = $"{unit.Type} at {unit.Position} ({unit.ActionPoints} AP)"; // UnitInfoLabel.Text = $"{unit.Type} at {unit.Position} ({unit.ActionPoints} AP)";
Show(); Show();
} }

View File

@@ -1,6 +1,7 @@
using System.Linq; using System.Linq;
using Civilization.Core.Game; using Civilization.Core.Game;
using Civilization.Core.Units; using Civilization.Core.Units;
using Civilization.GodotIntegration.Utils;
using Godot; using Godot;
namespace Civilization.GodotIntegration; namespace Civilization.GodotIntegration;
@@ -13,7 +14,8 @@ public partial class SelectionSystem : Node2D
public bool TrySelectUnitAt(Vector2I tilePos, GameState state) public bool TrySelectUnitAt(Vector2I tilePos, GameState state)
{ {
var unit = state.GetUnitsForPlayer(state.CurrentPlayer.Id).FirstOrDefault(u => u.Position == tilePos); var coreTilePos = tilePos.ToCore();
var unit = state.GetUnitsForPlayer(state.CurrentPlayer.Id).FirstOrDefault(u => u.Position == coreTilePos);
if (unit == null) return false; if (unit == null) return false;
SelectedUnit = unit; SelectedUnit = unit;

View File

@@ -14,7 +14,7 @@ public partial class UiController : Node
public override void _Ready() public override void _Ready()
{ {
UnitPanel.OnSettleClicked = TrySettleCity; // UnitPanel.OnSettleClicked = TrySettleCity;
} }
public override void _Process(double delta) public override void _Process(double delta)

View File

@@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using Civilization.Core.Game; using Civilization.Core.Game;
using Civilization.GodotIntegration.Utils;
using Godot; using Godot;
namespace Civilization.GodotIntegration; namespace Civilization.GodotIntegration;
@@ -20,7 +21,7 @@ public partial class UnitRenderer : Node2D
foreach (var unit in state.Units) foreach (var unit in state.Units)
{ {
var unitNode = UnitScene.Instantiate<Node2D>(); var unitNode = UnitScene.Instantiate<Node2D>();
unitNode.Position = MapRenderer.MapToWorld(unit.Position); unitNode.Position = MapRenderer.MapToWorld(unit.Position.ToGodot());
AddChild(unitNode); AddChild(unitNode);
_unitViews[unit.Id] = unitNode; _unitViews[unit.Id] = unitNode;
} }

View File

@@ -0,0 +1,13 @@
using Civilization.Core;
using Godot;
namespace Civilization.GodotIntegration.Utils;
public static class GodotConversionExtensions
{
public static Vector2I ToGodot(this Vec2I v) => new(v.X, v.Y);
public static Vec2I ToCore(this Vector2I v) => new(v.X, v.Y);
public static Color ToGodot(this ColorRGBA c) => new(c.R / 255f, c.G / 255f, c.B / 255f, c.A / 255f);
public static ColorRGBA ToCore(this Color c) => new((byte)(c.R * 255), (byte)(c.G * 255), (byte)(c.B * 255), (byte)(c.A * 255));
}

View File

@@ -27,8 +27,8 @@ tile_map_data = PackedByteArray("AADj/+j/AQAAAAAAAADj/+n/AQAAAAAAAADj/+r/AQAAAAA
[node name="MapRenderer" type="Node2D" parent="." node_paths=PackedStringArray("TileMapLayer")] [node name="MapRenderer" type="Node2D" parent="." node_paths=PackedStringArray("TileMapLayer")]
script = ExtResource("2_5a7ea") script = ExtResource("2_5a7ea")
TileMapLayer = NodePath("../TileMapLayer")
TileSet = ExtResource("2_8j60k") TileSet = ExtResource("2_8j60k")
TileMapLayer = NodePath("../TileMapLayer")
[node name="Camera2D" type="Camera2D" parent="."] [node name="Camera2D" type="Camera2D" parent="."]
zoom = Vector2(30, 30) zoom = Vector2(30, 30)
@@ -64,6 +64,7 @@ text = "End turn"
offset_top = 482.0 offset_top = 482.0
offset_right = -480.0 offset_right = -480.0
offset_bottom = 1.0 offset_bottom = 1.0
script = null
[node name="UIController" type="Node" parent="." node_paths=PackedStringArray("TurnLabel", "UnitPanel", "StateProvider", "SelectionSystem", "GameController")] [node name="UIController" type="Node" parent="." node_paths=PackedStringArray("TurnLabel", "UnitPanel", "StateProvider", "SelectionSystem", "GameController")]
script = ExtResource("6_5ukr8") script = ExtResource("6_5ukr8")

View File

@@ -1,8 +1,8 @@
[gd_scene load_steps=2 format=3 uid="uid://na1o6j7stseb"] [gd_scene load_steps=2 format=3 uid="uid://na1o6j7stseb"]
[ext_resource type="Script" uid="uid://c3mt3skudb7ky" path="res://GodotIntegration/SelectedUnitPanel.cs" id="1_sosfk"] [ext_resource type="Script" uid="uid://qqlmdir1bdjd" path="res://Civilization.GodotIntegration/SelectedUnitPanel.cs" id="1_sosfk"]
[node name="SelectedUnitPanel" type="MarginContainer" node_paths=PackedStringArray("UnitInfoLabel", "SettleButton")] [node name="SelectedUnitPanel" type="MarginContainer"]
anchors_preset = 15 anchors_preset = 15
anchor_right = 1.0 anchor_right = 1.0
anchor_bottom = 1.0 anchor_bottom = 1.0
@@ -13,8 +13,6 @@ theme_override_constants/margin_top = 8
theme_override_constants/margin_right = 8 theme_override_constants/margin_right = 8
theme_override_constants/margin_bottom = 8 theme_override_constants/margin_bottom = 8
script = ExtResource("1_sosfk") script = ExtResource("1_sosfk")
UnitInfoLabel = NodePath("UnitInfoLabel")
SettleButton = NodePath("SettleButton")
[node name="UnitInfoLabel" type="Label" parent="."] [node name="UnitInfoLabel" type="Label" parent="."]
layout_mode = 2 layout_mode = 2

View File

@@ -17,7 +17,8 @@ config/icon="res://icon.svg"
[dotnet] [dotnet]
project/assembly_name="civilization" project/assembly_name="Civilization.GodotIntegration"
project/solution_directory="res://../"
[rendering] [rendering]