Add Godot conversion extensions and update related code for integration
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Civilization.Core.Game;
|
||||
using Civilization.GodotIntegration.Utils;
|
||||
using Godot;
|
||||
|
||||
namespace Civilization.GodotIntegration;
|
||||
@@ -20,7 +21,7 @@ public partial class CityRenderer : Node2D
|
||||
foreach (var city in state.Cities)
|
||||
{
|
||||
var cityNode = CityScene.Instantiate<Node2D>();
|
||||
cityNode.Position = MapRenderer.MapToWorld(city.Position);
|
||||
cityNode.Position = MapRenderer.MapToWorld(city.Position.ToGodot());
|
||||
AddChild(cityNode);
|
||||
_cityViews[city.Id] = cityNode;
|
||||
}
|
||||
|
@@ -1,4 +1,4 @@
|
||||
<Project Sdk="Godot.NET.Sdk/4.2.0">
|
||||
<Project Sdk="Godot.NET.Sdk/4.4.1">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
@@ -9,5 +9,4 @@
|
||||
<ProjectReference Include="../../Lib/Civilization.Core/Civilization.Core.csproj" />
|
||||
<ProjectReference Include="../../Lib/Civilization.Shared/Civilization.Shared.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
@@ -3,6 +3,7 @@ using Civilization.Core;
|
||||
using Civilization.Core.Game;
|
||||
using Civilization.Core.Grid;
|
||||
using Civilization.Core.Units;
|
||||
using Civilization.GodotIntegration.Utils;
|
||||
using Godot;
|
||||
|
||||
namespace Civilization.GodotIntegration;
|
||||
@@ -26,8 +27,8 @@ public partial class GameController : Node
|
||||
|
||||
var players = new List<Player>
|
||||
{
|
||||
new Player(0, "Player 1", Colors.Red),
|
||||
new Player(1, "Player 2", Colors.Blue)
|
||||
new Player(0, "Player 1", Colors.Red.ToCore()),
|
||||
new Player(1, "Player 2", Colors.Blue.ToCore())
|
||||
};
|
||||
|
||||
var gameState = new GameState(gameMap, players);
|
||||
@@ -38,7 +39,7 @@ public partial class GameController : Node
|
||||
InputSystem.OnStateChanged = Redraw;
|
||||
|
||||
// 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);
|
||||
GD.Print($"Added settler unit at {settler.Position}");
|
||||
|
||||
|
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using Civilization.Core.Actions;
|
||||
using Civilization.GodotIntegration.Utils;
|
||||
using Godot;
|
||||
|
||||
namespace Civilization.GodotIntegration;
|
||||
@@ -31,7 +32,7 @@ public partial class InputSystem : Node
|
||||
if (selected != null)
|
||||
{
|
||||
if (!isRightClick) return;
|
||||
var move = new MoveUnitAction(selected.Id, position);
|
||||
var move = new MoveUnitAction(selected.Id, position.ToCore());
|
||||
|
||||
if (!move.CanExecute(context)) return;
|
||||
|
||||
|
@@ -1,4 +1,5 @@
|
||||
using Civilization.Core;
|
||||
using Civilization.GodotIntegration.Utils;
|
||||
using Godot;
|
||||
|
||||
namespace Civilization.GodotIntegration;
|
||||
@@ -33,7 +34,7 @@ public partial class MapRenderer : Node2D
|
||||
var pos = tile.Position;
|
||||
var tileId = (int)tile.Type + TileIndexOffset;
|
||||
var atlasCoords = tileSetSource.GetTileId(tileId);
|
||||
TileMapLayer.SetCell(pos, tileId, atlasCoords);
|
||||
TileMapLayer.SetCell(pos.ToGodot(), tileId, atlasCoords);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -6,21 +6,21 @@ namespace Civilization.GodotIntegration;
|
||||
|
||||
public partial class SelectedUnitPanel : Control
|
||||
{
|
||||
[Export] public Label UnitInfoLabel;
|
||||
[Export] public Button SettleButton;
|
||||
|
||||
public Action? OnSettleClicked;
|
||||
// [Export] public Label UnitInfoLabel;
|
||||
// [Export] public Button SettleButton;
|
||||
//
|
||||
// public Action? OnSettleClicked;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
SettleButton.Pressed += () => OnSettleClicked?.Invoke();
|
||||
// SettleButton.Pressed += () => OnSettleClicked?.Invoke();
|
||||
Hide();
|
||||
}
|
||||
|
||||
public void ShowFor(Unit unit)
|
||||
{
|
||||
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();
|
||||
}
|
||||
|
||||
|
@@ -1,6 +1,7 @@
|
||||
using System.Linq;
|
||||
using Civilization.Core.Game;
|
||||
using Civilization.Core.Units;
|
||||
using Civilization.GodotIntegration.Utils;
|
||||
using Godot;
|
||||
|
||||
namespace Civilization.GodotIntegration;
|
||||
@@ -13,7 +14,8 @@ public partial class SelectionSystem : Node2D
|
||||
|
||||
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;
|
||||
|
||||
SelectedUnit = unit;
|
||||
|
@@ -14,7 +14,7 @@ public partial class UiController : Node
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
UnitPanel.OnSettleClicked = TrySettleCity;
|
||||
// UnitPanel.OnSettleClicked = TrySettleCity;
|
||||
}
|
||||
|
||||
public override void _Process(double delta)
|
||||
|
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Civilization.Core.Game;
|
||||
using Civilization.GodotIntegration.Utils;
|
||||
using Godot;
|
||||
|
||||
namespace Civilization.GodotIntegration;
|
||||
@@ -20,7 +21,7 @@ public partial class UnitRenderer : Node2D
|
||||
foreach (var unit in state.Units)
|
||||
{
|
||||
var unitNode = UnitScene.Instantiate<Node2D>();
|
||||
unitNode.Position = MapRenderer.MapToWorld(unit.Position);
|
||||
unitNode.Position = MapRenderer.MapToWorld(unit.Position.ToGodot());
|
||||
AddChild(unitNode);
|
||||
_unitViews[unit.Id] = unitNode;
|
||||
}
|
||||
|
@@ -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));
|
||||
}
|
Reference in New Issue
Block a user