initialize repo
This commit is contained in:
28
godot_game/Civilization.GodotIntegration/CityRenderer.cs
Normal file
28
godot_game/Civilization.GodotIntegration/CityRenderer.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Civilization.Core.Game;
|
||||
using Godot;
|
||||
|
||||
namespace Civilization.GodotIntegration;
|
||||
|
||||
public partial class CityRenderer : Node2D
|
||||
{
|
||||
[Export] public PackedScene CityScene;
|
||||
[Export] public MapRenderer MapRenderer;
|
||||
|
||||
private readonly Dictionary<Guid, Node2D> _cityViews = new();
|
||||
|
||||
public void Render(GameState state)
|
||||
{
|
||||
foreach (var view in _cityViews.Values) view.QueueFree();
|
||||
_cityViews.Clear();
|
||||
|
||||
foreach (var city in state.Cities)
|
||||
{
|
||||
var cityNode = CityScene.Instantiate<Node2D>();
|
||||
cityNode.Position = MapRenderer.MapToWorld(city.Position);
|
||||
AddChild(cityNode);
|
||||
_cityViews[city.Id] = cityNode;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1 @@
|
||||
uid://byag1mawqiemf
|
@@ -0,0 +1,13 @@
|
||||
<Project Sdk="Godot.NET.Sdk/4.2.0">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<LangVersion>latest</LangVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="../../Lib/Civilization.Core/Civilization.Core.csproj" />
|
||||
<ProjectReference Include="../../Lib/Civilization.Shared/Civilization.Shared.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
61
godot_game/Civilization.GodotIntegration/GameController.cs
Normal file
61
godot_game/Civilization.GodotIntegration/GameController.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
using System.Collections.Generic;
|
||||
using Civilization.Core;
|
||||
using Civilization.Core.Game;
|
||||
using Civilization.Core.Grid;
|
||||
using Civilization.Core.Units;
|
||||
using Godot;
|
||||
|
||||
namespace Civilization.GodotIntegration;
|
||||
|
||||
public partial class GameController : Node
|
||||
{
|
||||
private const int DefaultWorldSize = 10;
|
||||
|
||||
[Export] public GameStateProvider StateProvider;
|
||||
[Export] public MapRenderer MapRenderer;
|
||||
[Export] public UnitRenderer UnitRenderer;
|
||||
[Export] public CityRenderer CityRenderer;
|
||||
[Export] public InputSystem InputSystem;
|
||||
[Export] public SelectionSystem SelectionSystem;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
// Setup initial game state
|
||||
var grid = new SquareGrid(DefaultWorldSize, DefaultWorldSize);
|
||||
var gameMap = new GameMap(grid);
|
||||
|
||||
var players = new List<Player>
|
||||
{
|
||||
new Player(0, "Player 1", Colors.Red),
|
||||
new Player(1, "Player 2", Colors.Blue)
|
||||
};
|
||||
|
||||
var gameState = new GameState(gameMap, players);
|
||||
StateProvider.Initialize(gameState);
|
||||
|
||||
// Setup UI systems
|
||||
MapRenderer.RenderMap(gameMap);
|
||||
InputSystem.OnStateChanged = Redraw;
|
||||
|
||||
// Add one settler to start
|
||||
var settler = new Unit(0, UnitType.Settler, new Vector2I(2, 2));
|
||||
gameState.AddUnit(settler);
|
||||
GD.Print($"Added settler unit at {settler.Position}");
|
||||
|
||||
Redraw();
|
||||
GD.Print($"Turn {gameState.TurnManager.TurnNumber}: {gameState.CurrentPlayer.Name}'s turn");
|
||||
}
|
||||
|
||||
public void OnEndTurnPressed()
|
||||
{
|
||||
StateProvider.GameState.NextTurn();
|
||||
Redraw();
|
||||
GD.Print($"Turn {StateProvider.GameState.TurnManager.TurnNumber}: {StateProvider.GameState.CurrentPlayer.Name}'s turn");
|
||||
}
|
||||
|
||||
public void Redraw()
|
||||
{
|
||||
UnitRenderer.Render(StateProvider.GameState);
|
||||
CityRenderer.Render(StateProvider.GameState);
|
||||
}
|
||||
}
|
@@ -0,0 +1 @@
|
||||
uid://c5dq2mw7228ya
|
@@ -0,0 +1,15 @@
|
||||
using Civilization.Core.Game;
|
||||
using Godot;
|
||||
|
||||
namespace Civilization.GodotIntegration;
|
||||
|
||||
public partial class GameStateProvider : Node
|
||||
{
|
||||
public GameState GameState { get; private set; }
|
||||
|
||||
public void Initialize(GameState gameState)
|
||||
{
|
||||
GameState = gameState;
|
||||
GD.Print("GameStateProvider initialized with game state.");
|
||||
}
|
||||
}
|
@@ -0,0 +1 @@
|
||||
uid://dyoapq2dkn0d6
|
49
godot_game/Civilization.GodotIntegration/InputSystem.cs
Normal file
49
godot_game/Civilization.GodotIntegration/InputSystem.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
using System;
|
||||
using Civilization.Core.Actions;
|
||||
using Godot;
|
||||
|
||||
namespace Civilization.GodotIntegration;
|
||||
|
||||
public partial class InputSystem : Node
|
||||
{
|
||||
[Export] public MapRenderer MapRenderer;
|
||||
[Export] public SelectionSystem SelectionSystem;
|
||||
[Export] public GameStateProvider StateProvider;
|
||||
|
||||
public Action? OnStateChanged;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
MapRenderer.TileClicked += HandleTileClick;
|
||||
}
|
||||
|
||||
public override void _ExitTree()
|
||||
{
|
||||
MapRenderer.TileClicked -= HandleTileClick;
|
||||
}
|
||||
|
||||
private void HandleTileClick(Vector2I position, bool isRightClick)
|
||||
{
|
||||
var state = StateProvider.GameState;
|
||||
var context = new GameActionContext(state);
|
||||
var selected = SelectionSystem.SelectedUnit;
|
||||
|
||||
if (selected != null)
|
||||
{
|
||||
if (!isRightClick) return;
|
||||
var move = new MoveUnitAction(selected.Id, position);
|
||||
|
||||
if (!move.CanExecute(context)) return;
|
||||
|
||||
state.ActionQueue.Enqueue(move);
|
||||
state.ActionQueue.ExecuteAll(context);
|
||||
OnStateChanged?.Invoke();
|
||||
return;
|
||||
}
|
||||
|
||||
if (SelectionSystem.TrySelectUnitAt(position, state))
|
||||
{
|
||||
GD.Print($"InputSystem: unit {selected?.Id} selected at {position}");
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1 @@
|
||||
uid://clke0qvrtll81
|
41
godot_game/Civilization.GodotIntegration/MapRenderer.cs
Normal file
41
godot_game/Civilization.GodotIntegration/MapRenderer.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using Civilization.Core;
|
||||
using Godot;
|
||||
|
||||
namespace Civilization.GodotIntegration;
|
||||
|
||||
public partial class MapRenderer : Node2D
|
||||
{
|
||||
private const int TileIndexOffset = 1;
|
||||
|
||||
[Export] public TileMapLayer TileMapLayer;
|
||||
[Export] public TileSet TileSet;
|
||||
|
||||
[Signal] public delegate void TileClickedEventHandler(Vector2I position, bool isRightClick);
|
||||
|
||||
public override void _Input(InputEvent @event)
|
||||
{
|
||||
if (@event is not InputEventMouseButton { Pressed: true }) return;
|
||||
|
||||
var worldPos = GetGlobalMousePosition();
|
||||
var localPos = TileMapLayer.ToLocal(worldPos);
|
||||
var tilePos = TileMapLayer.LocalToMap(localPos);
|
||||
EmitSignalTileClicked(tilePos, @event is InputEventMouseButton { ButtonIndex: MouseButton.Right });
|
||||
}
|
||||
|
||||
public void RenderMap(GameMap map)
|
||||
{
|
||||
TileMapLayer.SetTileSet(TileSet);
|
||||
var tileSetSource = TileSet.GetSource(1);
|
||||
TileMapLayer.Clear();
|
||||
|
||||
foreach (var tile in map.GetTiles())
|
||||
{
|
||||
var pos = tile.Position;
|
||||
var tileId = (int)tile.Type + TileIndexOffset;
|
||||
var atlasCoords = tileSetSource.GetTileId(tileId);
|
||||
TileMapLayer.SetCell(pos, tileId, atlasCoords);
|
||||
}
|
||||
}
|
||||
|
||||
public Vector2 MapToWorld(Vector2I position) => TileMapLayer.MapToLocal(position);
|
||||
}
|
@@ -0,0 +1 @@
|
||||
uid://c0i71sgyrp2xt
|
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using Civilization.Core.Units;
|
||||
using Godot;
|
||||
|
||||
namespace Civilization.GodotIntegration;
|
||||
|
||||
public partial class SelectedUnitPanel : Control
|
||||
{
|
||||
[Export] public Label UnitInfoLabel;
|
||||
[Export] public Button SettleButton;
|
||||
|
||||
public Action? OnSettleClicked;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
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)";
|
||||
Show();
|
||||
}
|
||||
|
||||
public void HidePanel() => Hide();
|
||||
}
|
@@ -0,0 +1 @@
|
||||
uid://qqlmdir1bdjd
|
31
godot_game/Civilization.GodotIntegration/SelectionSystem.cs
Normal file
31
godot_game/Civilization.GodotIntegration/SelectionSystem.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using System.Linq;
|
||||
using Civilization.Core.Game;
|
||||
using Civilization.Core.Units;
|
||||
using Godot;
|
||||
|
||||
namespace Civilization.GodotIntegration;
|
||||
|
||||
public partial class SelectionSystem : Node2D
|
||||
{
|
||||
public Unit? SelectedUnit { get; private set; }
|
||||
|
||||
[Export] public SelectedUnitPanel UnitPanel;
|
||||
|
||||
public bool TrySelectUnitAt(Vector2I tilePos, GameState state)
|
||||
{
|
||||
var unit = state.GetUnitsForPlayer(state.CurrentPlayer.Id).FirstOrDefault(u => u.Position == tilePos);
|
||||
if (unit == null) return false;
|
||||
|
||||
SelectedUnit = unit;
|
||||
GD.Print($"Selected unit {unit.Id} at {tilePos} ({unit.Type})");
|
||||
|
||||
UnitPanel.ShowFor(unit);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Deselect()
|
||||
{
|
||||
SelectedUnit = null;
|
||||
UnitPanel.HidePanel();
|
||||
}
|
||||
}
|
@@ -0,0 +1 @@
|
||||
uid://c2ovyn15v1rr4
|
43
godot_game/Civilization.GodotIntegration/UiController.cs
Normal file
43
godot_game/Civilization.GodotIntegration/UiController.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using Civilization.Core.Actions;
|
||||
using Civilization.Core.Game;
|
||||
using Godot;
|
||||
|
||||
namespace Civilization.GodotIntegration;
|
||||
|
||||
public partial class UiController : Node
|
||||
{
|
||||
[Export] public Label TurnLabel;
|
||||
[Export] public SelectedUnitPanel UnitPanel;
|
||||
[Export] public GameStateProvider StateProvider;
|
||||
[Export] public SelectionSystem SelectionSystem;
|
||||
[Export] public GameController GameController;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
UnitPanel.OnSettleClicked = TrySettleCity;
|
||||
}
|
||||
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
TurnLabel.Text = $"Turn: {StateProvider.GameState.TurnManager.TurnNumber} - {StateProvider.GameState.CurrentPlayer.Name}";
|
||||
}
|
||||
|
||||
private void TrySettleCity()
|
||||
{
|
||||
var selected = SelectionSystem.SelectedUnit;
|
||||
if (selected == null) return;
|
||||
|
||||
var context = new GameActionContext(StateProvider.GameState);
|
||||
var settle = new SettleCityAction(selected.Id);
|
||||
if (!settle.CanExecute(context))
|
||||
{
|
||||
GD.PrintErr($"Cannot settle city with unit {selected.Id} at {selected.Position}");
|
||||
return;
|
||||
}
|
||||
|
||||
StateProvider.GameState.ActionQueue.Enqueue(settle);
|
||||
StateProvider.GameState.ActionQueue.ExecuteAll(context);
|
||||
SelectionSystem.Deselect();
|
||||
GameController.Redraw();
|
||||
}
|
||||
}
|
@@ -0,0 +1 @@
|
||||
uid://bx7oa1veqfk35
|
28
godot_game/Civilization.GodotIntegration/UnitRenderer.cs
Normal file
28
godot_game/Civilization.GodotIntegration/UnitRenderer.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Civilization.Core.Game;
|
||||
using Godot;
|
||||
|
||||
namespace Civilization.GodotIntegration;
|
||||
|
||||
public partial class UnitRenderer : Node2D
|
||||
{
|
||||
[Export] public PackedScene UnitScene;
|
||||
[Export] public MapRenderer MapRenderer;
|
||||
|
||||
private readonly Dictionary<Guid, Node2D> _unitViews = new();
|
||||
|
||||
public void Render(GameState state)
|
||||
{
|
||||
foreach (var view in _unitViews.Values) view.QueueFree();
|
||||
_unitViews.Clear();
|
||||
|
||||
foreach (var unit in state.Units)
|
||||
{
|
||||
var unitNode = UnitScene.Instantiate<Node2D>();
|
||||
unitNode.Position = MapRenderer.MapToWorld(unit.Position);
|
||||
AddChild(unitNode);
|
||||
_unitViews[unit.Id] = unitNode;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1 @@
|
||||
uid://bqa3dsxjyij5m
|
Reference in New Issue
Block a user