initialize repo
This commit is contained in:
1
Lib
1
Lib
Submodule Lib deleted from 67de04e3da
2
Lib/.gitignore
vendored
Normal file
2
Lib/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
.idea/
|
||||
civ_codebase.txt
|
133
Lib/Civilization.Core/.gitignore
vendored
Normal file
133
Lib/Civilization.Core/.gitignore
vendored
Normal file
@@ -0,0 +1,133 @@
|
||||
## Ignore Visual Studio temporary files, build results, and
|
||||
## files generated by popular Visual Studio add-ons.
|
||||
|
||||
# User-specific files
|
||||
*.suo
|
||||
*.user
|
||||
*.sln.docstates
|
||||
|
||||
# Build results
|
||||
|
||||
[Dd]ebug/
|
||||
[Rr]elease/
|
||||
x64/
|
||||
[Bb]in/
|
||||
[Oo]bj/
|
||||
|
||||
# MSTest test Results
|
||||
[Tt]est[Rr]esult*/
|
||||
[Bb]uild[Ll]og.*
|
||||
|
||||
*_i.c
|
||||
*_p.c
|
||||
*_i.h
|
||||
*.ilk
|
||||
*.meta
|
||||
*.obj
|
||||
*.pch
|
||||
*.pdb
|
||||
*.pgc
|
||||
*.pgd
|
||||
*.rsp
|
||||
*.sbr
|
||||
*.tlb
|
||||
*.tli
|
||||
*.tlh
|
||||
*.tmp
|
||||
*.tmp_proj
|
||||
*.log
|
||||
*.vspscc
|
||||
*.vssscc
|
||||
.builds
|
||||
*.pidb
|
||||
*.log
|
||||
*.svclog
|
||||
*.scc
|
||||
|
||||
# Visual C++ cache files
|
||||
ipch/
|
||||
*.aps
|
||||
*.ncb
|
||||
*.opensdf
|
||||
*.sdf
|
||||
*.cachefile
|
||||
|
||||
# Visual Studio profiler
|
||||
*.psess
|
||||
*.vsp
|
||||
*.vspx
|
||||
|
||||
# Guidance Automation Toolkit
|
||||
*.gpState
|
||||
|
||||
# ReSharper is a .NET coding add-in
|
||||
_ReSharper*/
|
||||
*.[Rr]e[Ss]harper
|
||||
*.DotSettings.user
|
||||
|
||||
# Click-Once directory
|
||||
publish/
|
||||
|
||||
# Publish Web Output
|
||||
*.Publish.xml
|
||||
*.pubxml
|
||||
*.azurePubxml
|
||||
|
||||
# NuGet Packages Directory
|
||||
## TODO: If you have NuGet Package Restore enabled, uncomment the next line
|
||||
packages/
|
||||
## TODO: If the tool you use requires repositories.config, also uncomment the next line
|
||||
!packages/repositories.config
|
||||
|
||||
# Windows Azure Build Output
|
||||
csx/
|
||||
*.build.csdef
|
||||
|
||||
# Windows Store app package directory
|
||||
AppPackages/
|
||||
|
||||
# Others
|
||||
sql/
|
||||
*.Cache
|
||||
ClientBin/
|
||||
[Ss]tyle[Cc]op.*
|
||||
![Ss]tyle[Cc]op.targets
|
||||
~$*
|
||||
*~
|
||||
*.dbmdl
|
||||
*.[Pp]ublish.xml
|
||||
|
||||
*.publishsettings
|
||||
|
||||
# RIA/Silverlight projects
|
||||
Generated_Code/
|
||||
|
||||
# Backup & report files from converting an old project file to a newer
|
||||
# Visual Studio version. Backup files are not needed, because we have git ;-)
|
||||
_UpgradeReport_Files/
|
||||
Backup*/
|
||||
UpgradeLog*.XML
|
||||
UpgradeLog*.htm
|
||||
|
||||
# SQL Server files
|
||||
App_Data/*.mdf
|
||||
App_Data/*.ldf
|
||||
|
||||
# =========================
|
||||
# Windows detritus
|
||||
# =========================
|
||||
|
||||
# Windows image file caches
|
||||
Thumbs.db
|
||||
ehthumbs.db
|
||||
|
||||
# Folder config file
|
||||
Desktop.ini
|
||||
|
||||
# Recycle Bin used on file shares
|
||||
$RECYCLE.BIN/
|
||||
|
||||
# Mac desktop service store files
|
||||
.DS_Store
|
||||
|
||||
_NCrunch*
|
26
Lib/Civilization.Core/Actions/ActionQueue.cs
Normal file
26
Lib/Civilization.Core/Actions/ActionQueue.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using Civilization.Core.Interfaces;
|
||||
|
||||
namespace Civilization.Core.Actions;
|
||||
|
||||
public class ActionQueue
|
||||
{
|
||||
private readonly Queue<IGameAction> _pending = new();
|
||||
private readonly Stack<ExecutedAction> _history = new();
|
||||
|
||||
public void Enqueue(IGameAction action)
|
||||
{
|
||||
_pending.Enqueue(action);
|
||||
}
|
||||
|
||||
public void ExecuteAll(GameActionContext context)
|
||||
{
|
||||
while (_pending.Count > 0)
|
||||
{
|
||||
var action = _pending.Dequeue();
|
||||
if (!action.CanExecute(context)) continue;
|
||||
|
||||
action.Execute(context);
|
||||
_history.Push(new ExecutedAction(action, context));
|
||||
}
|
||||
}
|
||||
}
|
15
Lib/Civilization.Core/Actions/ExecutedAction.cs
Normal file
15
Lib/Civilization.Core/Actions/ExecutedAction.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using Civilization.Core.Interfaces;
|
||||
|
||||
namespace Civilization.Core.Actions;
|
||||
|
||||
public class ExecutedAction
|
||||
{
|
||||
public IGameAction Action { get; }
|
||||
public GameActionContext ContextSnapshot { get; }
|
||||
|
||||
public ExecutedAction(IGameAction action, GameActionContext snapshot)
|
||||
{
|
||||
Action = action;
|
||||
ContextSnapshot = snapshot;
|
||||
}
|
||||
}
|
39
Lib/Civilization.Core/Actions/ExpandTerritoryAction.cs
Normal file
39
Lib/Civilization.Core/Actions/ExpandTerritoryAction.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using Civilization.Core.Interfaces;
|
||||
|
||||
|
||||
namespace Civilization.Core.Actions;
|
||||
|
||||
public class ExpandTerritoryAction(Guid cityId, Vec2I targetTile) : IGameAction
|
||||
{
|
||||
public Guid CityId { get; } = cityId;
|
||||
public Vec2I TargetTile { get; } = targetTile;
|
||||
|
||||
public bool CanExecute(GameActionContext context)
|
||||
{
|
||||
var city = context.State.FindCity(CityId);
|
||||
if (city == null || city.ActionPoints < 1) return false;
|
||||
|
||||
if (!context.Map.Grid.IsValidPosition(TargetTile)) return false;
|
||||
|
||||
var tile = context.Map.GetTile(TargetTile);
|
||||
if (tile is not { OwnerId: null }) return false;
|
||||
|
||||
return city.Territory.Any(t =>
|
||||
{
|
||||
var neighbors = context.Map.GetNeighbors(t);
|
||||
return neighbors.Contains(tile);
|
||||
});
|
||||
}
|
||||
|
||||
public void Execute(GameActionContext context)
|
||||
{
|
||||
var city = context.State.FindCity(CityId)!;
|
||||
city.ClaimTile(context.Map, TargetTile);
|
||||
city.SpendActionPoint();
|
||||
}
|
||||
|
||||
public void Undo(GameActionContext context)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
19
Lib/Civilization.Core/Actions/GameActionContext.cs
Normal file
19
Lib/Civilization.Core/Actions/GameActionContext.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using Civilization.Core.Game;
|
||||
|
||||
namespace Civilization.Core.Actions;
|
||||
|
||||
public class GameActionContext
|
||||
{
|
||||
public GameMap Map { get; }
|
||||
public List<Player> Players { get; }
|
||||
public GameState State { get; }
|
||||
|
||||
public GameActionContext(GameState gameState)
|
||||
{
|
||||
State = gameState;
|
||||
Map = gameState.Map;
|
||||
Players = gameState.Players;
|
||||
}
|
||||
|
||||
public Player CurrentPlayer => State.CurrentPlayer;
|
||||
}
|
31
Lib/Civilization.Core/Actions/MoveUnitAction.cs
Normal file
31
Lib/Civilization.Core/Actions/MoveUnitAction.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using Civilization.Core.Interfaces;
|
||||
|
||||
|
||||
namespace Civilization.Core.Actions;
|
||||
|
||||
public class MoveUnitAction(Guid unitId, Vec2I targetPosition) : IGameAction
|
||||
{
|
||||
public Guid UnitId { get; } = unitId;
|
||||
public Vec2I TargetPosition { get; } = targetPosition;
|
||||
|
||||
public bool CanExecute(GameActionContext context)
|
||||
{
|
||||
var unit = context.State.FindUnit(UnitId);
|
||||
if (unit == null || unit.OwnerId != context.CurrentPlayer.Id) return false;
|
||||
|
||||
return context.Map.Grid.IsValidPosition(TargetPosition) && unit.CanMoveTo(TargetPosition, context.Map);
|
||||
}
|
||||
|
||||
public void Execute(GameActionContext context)
|
||||
{
|
||||
var unit = context.State.FindUnit(UnitId);
|
||||
if (unit == null) return;
|
||||
unit.Position = TargetPosition;
|
||||
unit.ActionPoints -= 1;
|
||||
}
|
||||
|
||||
public void Undo(GameActionContext context)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
45
Lib/Civilization.Core/Actions/SettleCityAction.cs
Normal file
45
Lib/Civilization.Core/Actions/SettleCityAction.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using Civilization.Core.Game;
|
||||
using Civilization.Core.Interfaces;
|
||||
using Civilization.Core.Units;
|
||||
|
||||
namespace Civilization.Core.Actions;
|
||||
|
||||
public class SettleCityAction(Guid unitId) : IGameAction
|
||||
{
|
||||
private const int CityNearbyRange = 4;
|
||||
public Guid UnitId { get; } = unitId;
|
||||
|
||||
public bool CanExecute(GameActionContext context)
|
||||
{
|
||||
var unit = context.State.FindUnit(UnitId);
|
||||
|
||||
if (unit == null || !unit.HasTag(UnitTag.Settle)) return false;
|
||||
if (unit.OwnerId != context.CurrentPlayer.Id) return false;
|
||||
if (unit.ActionPoints < 1) return false;
|
||||
|
||||
var tile = context.Map.GetTile(unit.Position);
|
||||
if (tile is not { OwnerId: null }) return false;
|
||||
|
||||
// Later we could also check if there is city nearby
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Execute(GameActionContext context)
|
||||
{
|
||||
var unit = context.State.FindUnit(UnitId)!;
|
||||
var position = unit.Position;
|
||||
|
||||
context.State.RemoveUnit(UnitId);
|
||||
|
||||
var city = new City(unit.OwnerId, position);
|
||||
city.ClaimTile(context.Map, position);
|
||||
|
||||
context.State.AddCity(city);
|
||||
}
|
||||
|
||||
public void Undo(GameActionContext context)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
11
Lib/Civilization.Core/Civilization.Core.csproj
Normal file
11
Lib/Civilization.Core/Civilization.Core.csproj
Normal file
@@ -0,0 +1,11 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
|
||||
<GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
14
Lib/Civilization.Core/ColorRGBA.cs
Normal file
14
Lib/Civilization.Core/ColorRGBA.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
namespace Civilization.Core;
|
||||
|
||||
public readonly struct ColorRGBA(byte r, byte g, byte b, byte a = 255)
|
||||
{
|
||||
public byte R { get; } = r;
|
||||
public byte G { get; } = g;
|
||||
public byte B { get; } = b;
|
||||
public byte A { get; } = a;
|
||||
|
||||
public string ToHex()
|
||||
{
|
||||
return $"#{R:X2}{G:X2}{B:X2}{A:X2}";
|
||||
}
|
||||
}
|
71
Lib/Civilization.Core/Game/City.cs
Normal file
71
Lib/Civilization.Core/Game/City.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
using Civilization.Core.Actions;
|
||||
using Civilization.Core.Interfaces;
|
||||
|
||||
|
||||
namespace Civilization.Core.Game;
|
||||
|
||||
public class City : IOnTurnListener, IEntity
|
||||
{
|
||||
public Guid Id { get; } = Guid.NewGuid();
|
||||
public int OwnerId { get; }
|
||||
public string Name { get; set; }
|
||||
|
||||
public Vec2I Position { get; }
|
||||
public HashSet<Vec2I> Territory { get; } = [];
|
||||
|
||||
public int MaxActionPoints { get; private set; } = 1;
|
||||
public int ActionPoints { get; set; }
|
||||
|
||||
public City(int ownerId, Vec2I position, string name = "New City")
|
||||
{
|
||||
OwnerId = ownerId;
|
||||
Position = position;
|
||||
Name = name;
|
||||
Territory.Add(position);
|
||||
ActionPoints = MaxActionPoints;
|
||||
}
|
||||
|
||||
public void ExpandTo(Vec2I newTile) => Territory.Add(newTile);
|
||||
|
||||
public void ResetActionPoints() => ActionPoints = MaxActionPoints;
|
||||
|
||||
public bool CanProduce() => ActionPoints > 0;
|
||||
|
||||
public void SpendActionPoint()
|
||||
{
|
||||
if (ActionPoints > 0) ActionPoints--;
|
||||
}
|
||||
|
||||
public void ClaimTile(GameMap map, Vec2I tilePos)
|
||||
{
|
||||
var tile = map.GetTile(tilePos);
|
||||
if (tile == null) return;
|
||||
|
||||
tile.OwnerId = OwnerId;
|
||||
Territory.Add(tilePos);
|
||||
}
|
||||
|
||||
|
||||
public void OnTurnStart(GameState state)
|
||||
{
|
||||
ResetActionPoints();
|
||||
|
||||
var map = state.Map;
|
||||
var neighbors = Territory.SelectMany(pos => map.Grid.GetNeighbors(pos))
|
||||
.Distinct()
|
||||
.Where(p => map.GetTile(p)?.OwnerId == null)
|
||||
.ToList();
|
||||
|
||||
if (neighbors.Count > 0)
|
||||
{
|
||||
var rng = new Random();
|
||||
var choice = neighbors[rng.Next(neighbors.Count)];
|
||||
state.ActionQueue.Enqueue(new ExpandTerritoryAction(Id, choice));
|
||||
}
|
||||
}
|
||||
|
||||
public void OnTurnEnd(GameState state)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
56
Lib/Civilization.Core/Game/GameState.cs
Normal file
56
Lib/Civilization.Core/Game/GameState.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
using Civilization.Core.Actions;
|
||||
using Civilization.Core.Interfaces;
|
||||
using Civilization.Core.Units;
|
||||
|
||||
namespace Civilization.Core.Game;
|
||||
|
||||
public class GameState(GameMap map, List<Player> players)
|
||||
{
|
||||
private readonly Dictionary<Guid, Unit> _units = new();
|
||||
private readonly Dictionary<Guid, City> _cities = new();
|
||||
|
||||
public GameMap Map { get; } = map;
|
||||
public List<Player> Players { get; } = players;
|
||||
public TurnManager TurnManager { get; } = new(players);
|
||||
public ActionQueue ActionQueue { get; } = new();
|
||||
|
||||
public IEnumerable<Unit> Units => _units.Values;
|
||||
public Player CurrentPlayer => TurnManager.CurrentPlayer;
|
||||
|
||||
public IEnumerable<City> Cities => _cities.Values;
|
||||
|
||||
public void NextTurn()
|
||||
{
|
||||
foreach (var listener in GetAllTurnListeners(CurrentPlayer.Id)) listener.OnTurnEnd(this);
|
||||
|
||||
TurnManager.AdvanceTurn();
|
||||
|
||||
foreach (var listener in GetAllTurnListeners(CurrentPlayer.Id)) listener.OnTurnStart(this);
|
||||
|
||||
ActionQueue.ExecuteAll(new GameActionContext(this));
|
||||
}
|
||||
|
||||
public void AddUnit(Unit unit) => _units.Add(unit.Id, unit);
|
||||
|
||||
public Unit? FindUnit(Guid id) => _units.GetValueOrDefault(id);
|
||||
|
||||
public IEnumerable<Unit> GetUnitsForPlayer(int playerId) => _units.Values.Where(u => u.OwnerId == playerId);
|
||||
|
||||
public void RemoveUnit(Guid id) {
|
||||
// Later, let's check if player can remove this unit (e.g. his turn, unit is not in combat, etc.)
|
||||
_units.Remove(id);
|
||||
}
|
||||
|
||||
public void AddCity(City city) => _cities.Add(city.Id, city);
|
||||
|
||||
public City? FindCity(Guid id) => _cities.GetValueOrDefault(id);
|
||||
|
||||
public IEnumerable<City> GetCitiesForPlayer(int playerId)
|
||||
=> _cities.Values.Where(c => c.OwnerId == playerId);
|
||||
|
||||
private IEnumerable<IOnTurnListener> GetAllTurnListeners(int playerId)
|
||||
{
|
||||
foreach (var unit in GetUnitsForPlayer(playerId)) yield return unit;
|
||||
foreach (var city in GetCitiesForPlayer(playerId)) yield return city;
|
||||
}
|
||||
}
|
11
Lib/Civilization.Core/Game/Player.cs
Normal file
11
Lib/Civilization.Core/Game/Player.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
|
||||
|
||||
namespace Civilization.Core.Game;
|
||||
|
||||
public class Player(int id, string name, ColorRGBA color, bool isHuman = true)
|
||||
{
|
||||
public int Id { get; } = id;
|
||||
public string Name { get; } = name;
|
||||
public ColorRGBA Color { get; } = color;
|
||||
public bool IsHuman { get; } = isHuman;
|
||||
}
|
24
Lib/Civilization.Core/Game/TurnManager.cs
Normal file
24
Lib/Civilization.Core/Game/TurnManager.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
namespace Civilization.Core.Game;
|
||||
|
||||
public class TurnManager
|
||||
{
|
||||
private readonly List<Player> _players = [];
|
||||
private int _currentIndex = 0;
|
||||
|
||||
public int TurnNumber { get; private set; } = 1;
|
||||
public Player CurrentPlayer => _players[_currentIndex];
|
||||
|
||||
public TurnManager(IEnumerable<Player> players)
|
||||
{
|
||||
_players = players.ToList();
|
||||
}
|
||||
|
||||
public void AdvanceTurn()
|
||||
{
|
||||
_currentIndex++;
|
||||
if (_currentIndex < _players.Count) return;
|
||||
|
||||
_currentIndex = 0;
|
||||
TurnNumber++;
|
||||
}
|
||||
}
|
33
Lib/Civilization.Core/GameMap.cs
Normal file
33
Lib/Civilization.Core/GameMap.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using Civilization.Core.Interfaces;
|
||||
|
||||
|
||||
namespace Civilization.Core;
|
||||
|
||||
public class GameMap
|
||||
{
|
||||
private readonly Dictionary<Vec2I, Tile.Tile> _tiles = new();
|
||||
|
||||
public ITileGrid Grid { get; private set; }
|
||||
|
||||
public GameMap(ITileGrid grid)
|
||||
{
|
||||
Grid = grid;
|
||||
|
||||
for (var x = 0; x < ((IGridSize)grid).Width; x++)
|
||||
for (var y = 0; y < ((IGridSize)grid).Height; y++)
|
||||
{
|
||||
var pos = new Vec2I(x, y);
|
||||
_tiles[pos] = new Tile.Tile(pos);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public IEnumerable<Tile.Tile> GetTiles() => _tiles.Values;
|
||||
|
||||
public IEnumerable<Tile.Tile> GetNeighbors(Vec2I position)
|
||||
{
|
||||
return Grid.GetNeighbors(position).Select(GetTile).Where(t => t != null)!;
|
||||
}
|
||||
|
||||
public Tile.Tile? GetTile(Vec2I position) => _tiles.GetValueOrDefault(position);
|
||||
}
|
46
Lib/Civilization.Core/Grid/SquareGrid.cs
Normal file
46
Lib/Civilization.Core/Grid/SquareGrid.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using Civilization.Core.Interfaces;
|
||||
|
||||
|
||||
namespace Civilization.Core.Grid;
|
||||
|
||||
public class SquareGrid : ITileGrid, IGridSize
|
||||
{
|
||||
public int Width { get; }
|
||||
public int Height { get; }
|
||||
|
||||
private static readonly Vec2I[] NeighborOffsets =
|
||||
{
|
||||
new Vec2I(-1, 0), // Left
|
||||
new Vec2I(1, 0), // Right
|
||||
new Vec2I(0, -1), // Up
|
||||
new Vec2I(0, 1) // Down
|
||||
};
|
||||
|
||||
public SquareGrid(int width, int height)
|
||||
{
|
||||
Width = width;
|
||||
Height = height;
|
||||
}
|
||||
|
||||
public IEnumerable<Vec2I> GetNeighbors(Vec2I tilePosition)
|
||||
{
|
||||
foreach (var offset in NeighborOffsets)
|
||||
{
|
||||
var neighbor = tilePosition + offset;
|
||||
if (IsValidPosition(neighbor))
|
||||
yield return neighbor;
|
||||
}
|
||||
}
|
||||
|
||||
public Vec2I ClampPosition(Vec2I position)
|
||||
{
|
||||
var x = Math.Clamp(position.X, 0, Width - 1);
|
||||
var y = Math.Clamp(position.Y, 0, Height - 1);
|
||||
return new Vec2I(x, y);
|
||||
}
|
||||
|
||||
public bool IsValidPosition(Vec2I position)
|
||||
{
|
||||
return position.X >= 0 && position.X < Width && position.Y >= 0 && position.Y < Height;
|
||||
}
|
||||
}
|
7
Lib/Civilization.Core/Interfaces/IEntity.cs
Normal file
7
Lib/Civilization.Core/Interfaces/IEntity.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace Civilization.Core.Interfaces;
|
||||
|
||||
public interface IEntity
|
||||
{
|
||||
Guid Id { get; }
|
||||
int OwnerId { get; }
|
||||
}
|
10
Lib/Civilization.Core/Interfaces/IGameAction.cs
Normal file
10
Lib/Civilization.Core/Interfaces/IGameAction.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using Civilization.Core.Actions;
|
||||
|
||||
namespace Civilization.Core.Interfaces;
|
||||
|
||||
public interface IGameAction
|
||||
{
|
||||
bool CanExecute(GameActionContext context);
|
||||
void Execute(GameActionContext context);
|
||||
void Undo(GameActionContext context);
|
||||
}
|
7
Lib/Civilization.Core/Interfaces/IGridSize.cs
Normal file
7
Lib/Civilization.Core/Interfaces/IGridSize.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace Civilization.Core.Interfaces;
|
||||
|
||||
public interface IGridSize
|
||||
{
|
||||
public int Width { get; }
|
||||
public int Height { get; }
|
||||
}
|
6
Lib/Civilization.Core/Interfaces/ILogger.cs
Normal file
6
Lib/Civilization.Core/Interfaces/ILogger.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace Civilization.Core.Interfaces;
|
||||
|
||||
public interface ILogger
|
||||
{
|
||||
void Log(string message);
|
||||
}
|
9
Lib/Civilization.Core/Interfaces/IOnTurnListener.cs
Normal file
9
Lib/Civilization.Core/Interfaces/IOnTurnListener.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using Civilization.Core.Game;
|
||||
|
||||
namespace Civilization.Core.Interfaces;
|
||||
|
||||
public interface IOnTurnListener
|
||||
{
|
||||
void OnTurnStart(GameState state);
|
||||
void OnTurnEnd(GameState state);
|
||||
}
|
10
Lib/Civilization.Core/Interfaces/ITileGrid.cs
Normal file
10
Lib/Civilization.Core/Interfaces/ITileGrid.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
|
||||
|
||||
namespace Civilization.Core.Interfaces;
|
||||
|
||||
public interface ITileGrid
|
||||
{
|
||||
IEnumerable<Vec2I> GetNeighbors(Vec2I tilePosition);
|
||||
Vec2I ClampPosition(Vec2I position);
|
||||
bool IsValidPosition(Vec2I position);
|
||||
}
|
16
Lib/Civilization.Core/Tile/Tile.cs
Normal file
16
Lib/Civilization.Core/Tile/Tile.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
|
||||
|
||||
namespace Civilization.Core.Tile;
|
||||
|
||||
public class Tile
|
||||
{
|
||||
public Vec2I Position { get; }
|
||||
public TileType Type { get; set; } = TileType.Plain;
|
||||
public int? OwnerId { get; set; } = null;
|
||||
|
||||
public Tile(Vec2I position)
|
||||
{
|
||||
Position = position;
|
||||
}
|
||||
|
||||
}
|
16
Lib/Civilization.Core/Tile/TileType.cs
Normal file
16
Lib/Civilization.Core/Tile/TileType.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
namespace Civilization.Core.Tile;
|
||||
|
||||
public enum TileType
|
||||
{
|
||||
Plain = 0,
|
||||
Forest = 1,
|
||||
Mountain = 2,
|
||||
Water = 3,
|
||||
Desert = 4,
|
||||
Grassland = 5,
|
||||
Hills = 6,
|
||||
Ocean = 7,
|
||||
Tundra = 8,
|
||||
Snow = 9,
|
||||
Swamp = 10,
|
||||
}
|
56
Lib/Civilization.Core/Units/Unit.cs
Normal file
56
Lib/Civilization.Core/Units/Unit.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
using Civilization.Core.Game;
|
||||
using Civilization.Core.Interfaces;
|
||||
|
||||
|
||||
namespace Civilization.Core.Units;
|
||||
|
||||
public class Unit : IOnTurnListener, IEntity
|
||||
{
|
||||
public Guid Id { get; } = Guid.NewGuid();
|
||||
public int OwnerId { get; }
|
||||
public UnitType Type { get; }
|
||||
public UnitData Data { get; }
|
||||
|
||||
public Vec2I Position { get; set; }
|
||||
public int MaxActionPoints { get; }
|
||||
public int ActionPoints { get; set; }
|
||||
|
||||
public Unit(int ownerId, UnitType type, Vec2I position)
|
||||
{
|
||||
Id = Guid.NewGuid();
|
||||
OwnerId = ownerId;
|
||||
Type = type;
|
||||
Position = position;
|
||||
|
||||
Data = UnitDataRegistry.Get(Type);
|
||||
MaxActionPoints = Data.MaxActionPoints;
|
||||
ActionPoints = MaxActionPoints;
|
||||
}
|
||||
|
||||
public void ResetActionPoints()
|
||||
{
|
||||
ActionPoints = MaxActionPoints;
|
||||
}
|
||||
|
||||
public bool CanMoveTo(Vec2I destination, GameMap map)
|
||||
{
|
||||
if (ActionPoints <= 0) return false;
|
||||
|
||||
if (!map.Grid.IsValidPosition(destination)) return false;
|
||||
|
||||
var distance = Math.Abs(Position.X - destination.X) + Math.Abs(Position.Y - destination.Y);
|
||||
return distance <= Data.MoveRange;
|
||||
}
|
||||
|
||||
public bool HasTag(UnitTag tag) => Data.HasTag(tag);
|
||||
|
||||
public void OnTurnStart(GameState state)
|
||||
{
|
||||
ResetActionPoints();
|
||||
}
|
||||
|
||||
public void OnTurnEnd(GameState state)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
11
Lib/Civilization.Core/Units/UnitData.cs
Normal file
11
Lib/Civilization.Core/Units/UnitData.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
namespace Civilization.Core.Units;
|
||||
|
||||
public class UnitData
|
||||
{
|
||||
public string Name { get; init; }
|
||||
public int MaxActionPoints { get; init; }
|
||||
public int MoveRange { get; init; } = 1;
|
||||
public HashSet<UnitTag> Tags { get; init; } = [];
|
||||
|
||||
public bool HasTag(UnitTag tag) => Tags.Contains(tag);
|
||||
}
|
17
Lib/Civilization.Core/Units/UnitDataRegistry.cs
Normal file
17
Lib/Civilization.Core/Units/UnitDataRegistry.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
namespace Civilization.Core.Units;
|
||||
|
||||
public class UnitDataRegistry
|
||||
{
|
||||
private static readonly Dictionary<UnitType, UnitData> _registry = new()
|
||||
{
|
||||
[UnitType.Settler] = new UnitData
|
||||
{
|
||||
Name = "Settler",
|
||||
MaxActionPoints = 1,
|
||||
MoveRange = 1,
|
||||
Tags = new() { UnitTag.Settle, }
|
||||
}
|
||||
};
|
||||
|
||||
public static UnitData Get(UnitType unitType) => _registry[unitType];
|
||||
}
|
6
Lib/Civilization.Core/Units/UnitTag.cs
Normal file
6
Lib/Civilization.Core/Units/UnitTag.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace Civilization.Core.Units;
|
||||
|
||||
public enum UnitTag
|
||||
{
|
||||
Settle,
|
||||
}
|
6
Lib/Civilization.Core/Units/UnitType.cs
Normal file
6
Lib/Civilization.Core/Units/UnitType.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace Civilization.Core.Units;
|
||||
|
||||
public enum UnitType
|
||||
{
|
||||
Settler,
|
||||
}
|
15
Lib/Civilization.Core/Vec2i.cs
Normal file
15
Lib/Civilization.Core/Vec2i.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
namespace Civilization.Core;
|
||||
|
||||
public readonly struct Vec2I(int x, int y) : IEquatable<Vec2I>
|
||||
{
|
||||
public int X { get; } = x;
|
||||
public int Y { get; } = 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 override string ToString() => $"({X}, {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 int GetHashCode() => HashCode.Combine(X, Y);
|
||||
}
|
133
Lib/Civilization.Server/.gitignore
vendored
Normal file
133
Lib/Civilization.Server/.gitignore
vendored
Normal file
@@ -0,0 +1,133 @@
|
||||
## Ignore Visual Studio temporary files, build results, and
|
||||
## files generated by popular Visual Studio add-ons.
|
||||
|
||||
# User-specific files
|
||||
*.suo
|
||||
*.user
|
||||
*.sln.docstates
|
||||
|
||||
# Build results
|
||||
|
||||
[Dd]ebug/
|
||||
[Rr]elease/
|
||||
x64/
|
||||
[Bb]in/
|
||||
[Oo]bj/
|
||||
|
||||
# MSTest test Results
|
||||
[Tt]est[Rr]esult*/
|
||||
[Bb]uild[Ll]og.*
|
||||
|
||||
*_i.c
|
||||
*_p.c
|
||||
*_i.h
|
||||
*.ilk
|
||||
*.meta
|
||||
*.obj
|
||||
*.pch
|
||||
*.pdb
|
||||
*.pgc
|
||||
*.pgd
|
||||
*.rsp
|
||||
*.sbr
|
||||
*.tlb
|
||||
*.tli
|
||||
*.tlh
|
||||
*.tmp
|
||||
*.tmp_proj
|
||||
*.log
|
||||
*.vspscc
|
||||
*.vssscc
|
||||
.builds
|
||||
*.pidb
|
||||
*.log
|
||||
*.svclog
|
||||
*.scc
|
||||
|
||||
# Visual C++ cache files
|
||||
ipch/
|
||||
*.aps
|
||||
*.ncb
|
||||
*.opensdf
|
||||
*.sdf
|
||||
*.cachefile
|
||||
|
||||
# Visual Studio profiler
|
||||
*.psess
|
||||
*.vsp
|
||||
*.vspx
|
||||
|
||||
# Guidance Automation Toolkit
|
||||
*.gpState
|
||||
|
||||
# ReSharper is a .NET coding add-in
|
||||
_ReSharper*/
|
||||
*.[Rr]e[Ss]harper
|
||||
*.DotSettings.user
|
||||
|
||||
# Click-Once directory
|
||||
publish/
|
||||
|
||||
# Publish Web Output
|
||||
*.Publish.xml
|
||||
*.pubxml
|
||||
*.azurePubxml
|
||||
|
||||
# NuGet Packages Directory
|
||||
## TODO: If you have NuGet Package Restore enabled, uncomment the next line
|
||||
packages/
|
||||
## TODO: If the tool you use requires repositories.config, also uncomment the next line
|
||||
!packages/repositories.config
|
||||
|
||||
# Windows Azure Build Output
|
||||
csx/
|
||||
*.build.csdef
|
||||
|
||||
# Windows Store app package directory
|
||||
AppPackages/
|
||||
|
||||
# Others
|
||||
sql/
|
||||
*.Cache
|
||||
ClientBin/
|
||||
[Ss]tyle[Cc]op.*
|
||||
![Ss]tyle[Cc]op.targets
|
||||
~$*
|
||||
*~
|
||||
*.dbmdl
|
||||
*.[Pp]ublish.xml
|
||||
|
||||
*.publishsettings
|
||||
|
||||
# RIA/Silverlight projects
|
||||
Generated_Code/
|
||||
|
||||
# Backup & report files from converting an old project file to a newer
|
||||
# Visual Studio version. Backup files are not needed, because we have git ;-)
|
||||
_UpgradeReport_Files/
|
||||
Backup*/
|
||||
UpgradeLog*.XML
|
||||
UpgradeLog*.htm
|
||||
|
||||
# SQL Server files
|
||||
App_Data/*.mdf
|
||||
App_Data/*.ldf
|
||||
|
||||
# =========================
|
||||
# Windows detritus
|
||||
# =========================
|
||||
|
||||
# Windows image file caches
|
||||
Thumbs.db
|
||||
ehthumbs.db
|
||||
|
||||
# Folder config file
|
||||
Desktop.ini
|
||||
|
||||
# Recycle Bin used on file shares
|
||||
$RECYCLE.BIN/
|
||||
|
||||
# Mac desktop service store files
|
||||
.DS_Store
|
||||
|
||||
_NCrunch*
|
15
Lib/Civilization.Server/Civilization.Server.csproj
Normal file
15
Lib/Civilization.Server/Civilization.Server.csproj
Normal file
@@ -0,0 +1,15 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Civilization.Core\Civilization.Core.csproj" />
|
||||
<ProjectReference Include="..\Civilization.Shared\Civilization.Shared.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
68
Lib/Civilization.Server/Game/GameSession.cs
Normal file
68
Lib/Civilization.Server/Game/GameSession.cs
Normal file
@@ -0,0 +1,68 @@
|
||||
using Civilization.Core;
|
||||
using Civilization.Core.Actions;
|
||||
using Civilization.Core.Game;
|
||||
using Civilization.Core.Grid;
|
||||
using Civilization.Core.Interfaces;
|
||||
using Civilization.Core.Units;
|
||||
using Civilization.Shared.Commands;
|
||||
using Civilization.Shared.Packets;
|
||||
using Civilization.Shared.Packets.ServerMessages;
|
||||
|
||||
namespace Civilization.Server.Game;
|
||||
|
||||
public class GameSession
|
||||
{
|
||||
private const int DefaultSize = 10;
|
||||
private readonly GameState _state;
|
||||
private readonly GameSessionCoordinator _coordinator;
|
||||
|
||||
public GameState State => _state;
|
||||
|
||||
public GameSession(GameSessionCoordinator coordinator)
|
||||
{
|
||||
_coordinator = coordinator;
|
||||
var grid = new SquareGrid(DefaultSize, DefaultSize);
|
||||
var map = new GameMap(grid);
|
||||
|
||||
var players = new List<Player>
|
||||
{
|
||||
new(0, "Player 1", new ColorRGBA(255, 0, 0)),
|
||||
new(1, "Player 2", new ColorRGBA(0, 0, 255)),
|
||||
};
|
||||
|
||||
_state = new GameState(map, players);
|
||||
|
||||
_state.AddUnit(new Unit(0, UnitType.Settler, new Vec2I(2, 2)));
|
||||
}
|
||||
|
||||
public Task ProcessCommand(ClientMessage msg)
|
||||
{
|
||||
var context = new GameActionContext(_state);
|
||||
|
||||
switch (msg.Command)
|
||||
{
|
||||
case MoveUnitCommand move:
|
||||
EnqueueAndExecute(new MoveUnitAction(move.UnitId, move.TargetPosition), context);
|
||||
break;
|
||||
case SettleCityCommand settle:
|
||||
EnqueueAndExecute(new SettleCityAction(settle.UnitId), context);
|
||||
break;
|
||||
default:
|
||||
throw new NotSupportedException($"Command type {msg.Command.GetType()} is not supported.");
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private void EnqueueAndExecute(IGameAction action, GameActionContext context)
|
||||
{
|
||||
if (!action.CanExecute(context))
|
||||
{
|
||||
Console.WriteLine($"Rejected invalid command from player {context.CurrentPlayer.Id}");
|
||||
return;
|
||||
}
|
||||
|
||||
_state.ActionQueue.Enqueue(action);
|
||||
_state.ActionQueue.ExecuteAll(context);
|
||||
}
|
||||
}
|
115
Lib/Civilization.Server/Game/GameSessionCoordinator.cs
Normal file
115
Lib/Civilization.Server/Game/GameSessionCoordinator.cs
Normal file
@@ -0,0 +1,115 @@
|
||||
using Civilization.Core.Interfaces;
|
||||
using Civilization.Server.Networking.Interfaces;
|
||||
using Civilization.Shared.Commands;
|
||||
using Civilization.Shared.Packets;
|
||||
using Civilization.Shared.Packets.ServerMessages;
|
||||
|
||||
namespace Civilization.Server.Game;
|
||||
|
||||
public class GameSessionCoordinator
|
||||
{
|
||||
private readonly GameSession _session;
|
||||
private readonly Dictionary<int, bool> _connectedPlayers = new();
|
||||
private readonly Dictionary<int, IClientConnection> _connections = new();
|
||||
|
||||
public GameSessionCoordinator()
|
||||
{
|
||||
_session = new GameSession(this);
|
||||
}
|
||||
|
||||
public Task RegisterPlayerAsync(int playerId, IClientConnection connection)
|
||||
{
|
||||
_connectedPlayers[playerId] = true;
|
||||
_connections[playerId] = connection;
|
||||
|
||||
Console.WriteLine($"Player {playerId} registered.");
|
||||
|
||||
// Send initial log message
|
||||
return connection.SendAsync(new LogMessage("Welcome to the game!"));
|
||||
}
|
||||
|
||||
public async Task ReceiveCommandAsync(ClientMessage message)
|
||||
{
|
||||
if (!_connectedPlayers.ContainsKey(message.PlayerId))
|
||||
{
|
||||
Console.WriteLine($"Rejected command from unregistered player {message.PlayerId}.");
|
||||
return;
|
||||
}
|
||||
|
||||
switch (message.Command)
|
||||
{
|
||||
case EndTurnCommand:
|
||||
await HandleEndTurnAsync(message.PlayerId);
|
||||
break;
|
||||
|
||||
default:
|
||||
try
|
||||
{
|
||||
await _session.ProcessCommand(message);
|
||||
var state = _session.State;
|
||||
var currentPlayerInfo = new PlayerInfo(state.CurrentPlayer.Id, state.CurrentPlayer.Name, state.CurrentPlayer.Color.ToHex());
|
||||
await BroadcastAsync(new StateUpdateMessage(_session.State, currentPlayerInfo));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"[Player {message.PlayerId}] Error: {ex.Message}");
|
||||
await SendToPlayerAsync(message.PlayerId, new ErrorMessage(ex.Message));
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task SendToPlayerAsync(int playerId, BaseServerMessage message)
|
||||
{
|
||||
if (_connections.TryGetValue(playerId, out var conn))
|
||||
{
|
||||
await conn.SendAsync(message);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task BroadcastAsync(BaseServerMessage message)
|
||||
{
|
||||
foreach (var conn in _connections.Values)
|
||||
{
|
||||
await conn.SendAsync(message);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task BroadcastStateUpdateAsync()
|
||||
{
|
||||
var state = _session.State;
|
||||
var currentPlayerInfo = new PlayerInfo(state.CurrentPlayer.Id, state.CurrentPlayer.Name, state.CurrentPlayer.Color.ToHex());
|
||||
var msg = new StateUpdateMessage(state, currentPlayerInfo);
|
||||
await BroadcastAsync(msg);
|
||||
}
|
||||
|
||||
public async Task OnTurnAdvancedAsync()
|
||||
{
|
||||
var state = _session.State;
|
||||
var currentPlayerInfo = new PlayerInfo(state.CurrentPlayer.Id, state.CurrentPlayer.Name, state.CurrentPlayer.Color.ToHex());
|
||||
var message = new StateUpdateMessage(_session.State, currentPlayerInfo);
|
||||
await BroadcastAsync(message);
|
||||
}
|
||||
|
||||
public async Task HandleEndTurnAsync(int playerId)
|
||||
{
|
||||
if (playerId != _session.State.CurrentPlayer.Id)
|
||||
{
|
||||
Console.WriteLine($"[Turn] Player {playerId} attempted to end turn out of order.");
|
||||
await SendToPlayerAsync(playerId, new ErrorMessage("Not your turn."));
|
||||
return;
|
||||
}
|
||||
|
||||
_session.State.NextTurn();
|
||||
|
||||
Console.WriteLine($"[Turn] Player {playerId} ended their turn. Now it's Player {_session.State.CurrentPlayer.Id}'s turn.");
|
||||
|
||||
await BroadcastAsync(new LogMessage($"Player {playerId} ended their turn."));
|
||||
await OnTurnAdvancedAsync();
|
||||
}
|
||||
|
||||
// Later: Add methods like:
|
||||
// - BroadcastGameState()
|
||||
// - GetStateForPlayer(int playerId)
|
||||
// - NotifyTurnAdvance()
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
using Civilization.Shared.Packets.ServerMessages;
|
||||
|
||||
namespace Civilization.Server.Networking.Interfaces;
|
||||
|
||||
public interface IClientConnection
|
||||
{
|
||||
int PlayerId { get; }
|
||||
Task SendAsync(BaseServerMessage message);
|
||||
}
|
@@ -0,0 +1,8 @@
|
||||
using Civilization.Server.Game;
|
||||
|
||||
namespace Civilization.Server.Networking.Interfaces;
|
||||
|
||||
public interface ITransport
|
||||
{
|
||||
Task StartAsync(GameSessionCoordinator coordinator, CancellationToken cancellationToken = default);
|
||||
}
|
25
Lib/Civilization.Server/Networking/TcpClientConnection.cs
Normal file
25
Lib/Civilization.Server/Networking/TcpClientConnection.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using System.Text.Json;
|
||||
using Civilization.Server.Networking.Interfaces;
|
||||
using Civilization.Shared;
|
||||
using Civilization.Shared.Packets.ServerMessages;
|
||||
|
||||
namespace Civilization.Server.Networking;
|
||||
|
||||
public class TcpClientConnection : IClientConnection
|
||||
{
|
||||
private readonly StreamWriter _writer;
|
||||
|
||||
public int PlayerId { get; }
|
||||
|
||||
public TcpClientConnection(int playerId, Stream stream)
|
||||
{
|
||||
PlayerId = playerId;
|
||||
_writer = new StreamWriter(stream) { AutoFlush = true };
|
||||
}
|
||||
|
||||
public Task SendAsync(BaseServerMessage message)
|
||||
{
|
||||
var json = JsonSerializer.Serialize(message, SharedJson.Options);
|
||||
return _writer.WriteLineAsync(json);
|
||||
}
|
||||
}
|
@@ -0,0 +1,74 @@
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Text.Json;
|
||||
using Civilization.Server.Game;
|
||||
using Civilization.Server.Networking.Interfaces;
|
||||
using Civilization.Shared;
|
||||
using Civilization.Shared.Packets;
|
||||
|
||||
namespace Civilization.Server.Networking.Transports;
|
||||
|
||||
public class TcpTransport : ITransport
|
||||
{
|
||||
private readonly int _port;
|
||||
|
||||
public TcpTransport(int port = 9000)
|
||||
{
|
||||
_port = port;
|
||||
}
|
||||
|
||||
public async Task StartAsync(GameSessionCoordinator coordinator, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var listener = new TcpListener(IPAddress.Any, _port);
|
||||
listener.Start();
|
||||
Console.WriteLine($"TCP transport listening on port {_port}");
|
||||
|
||||
var nextPlayerId = 0;
|
||||
|
||||
while (!cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
var client = await listener.AcceptTcpClientAsync(cancellationToken);
|
||||
_ = HandleClientAsync(client, nextPlayerId++, coordinator, cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task HandleClientAsync(
|
||||
TcpClient client,
|
||||
int playerId,
|
||||
GameSessionCoordinator coordinator,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
Console.WriteLine($"Client {playerId} connected.");
|
||||
var stream = client.GetStream();
|
||||
var reader = new StreamReader(stream);
|
||||
var writer = new StreamWriter(stream) { AutoFlush = true };
|
||||
|
||||
await writer.WriteLineAsync($"{{\"info\": \"You are Player {playerId}\"}}");
|
||||
|
||||
var connection = new TcpClientConnection(playerId, stream);
|
||||
|
||||
await coordinator.RegisterPlayerAsync(playerId, connection);
|
||||
|
||||
while (!cancellationToken.IsCancellationRequested && client.Connected)
|
||||
{
|
||||
var line = await reader.ReadLineAsync(cancellationToken);
|
||||
if (line is null)
|
||||
break;
|
||||
|
||||
try
|
||||
{
|
||||
var message = JsonSerializer.Deserialize<ClientMessage>(line, SharedJson.Options);
|
||||
if (message is not null)
|
||||
{
|
||||
await coordinator.ReceiveCommandAsync(message);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"[Player {playerId}] Failed to process message: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine($"Client {playerId} disconnected.");
|
||||
}
|
||||
}
|
7
Lib/Civilization.Server/Program.cs
Normal file
7
Lib/Civilization.Server/Program.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
using Civilization.Server.Game;
|
||||
using Civilization.Server.Networking.Transports;
|
||||
|
||||
var transport = new TcpTransport();
|
||||
var coordinator = new GameSessionCoordinator();
|
||||
|
||||
await transport.StartAsync(coordinator);
|
133
Lib/Civilization.Shared/.gitignore
vendored
Normal file
133
Lib/Civilization.Shared/.gitignore
vendored
Normal file
@@ -0,0 +1,133 @@
|
||||
## Ignore Visual Studio temporary files, build results, and
|
||||
## files generated by popular Visual Studio add-ons.
|
||||
|
||||
# User-specific files
|
||||
*.suo
|
||||
*.user
|
||||
*.sln.docstates
|
||||
|
||||
# Build results
|
||||
|
||||
[Dd]ebug/
|
||||
[Rr]elease/
|
||||
x64/
|
||||
[Bb]in/
|
||||
[Oo]bj/
|
||||
|
||||
# MSTest test Results
|
||||
[Tt]est[Rr]esult*/
|
||||
[Bb]uild[Ll]og.*
|
||||
|
||||
*_i.c
|
||||
*_p.c
|
||||
*_i.h
|
||||
*.ilk
|
||||
*.meta
|
||||
*.obj
|
||||
*.pch
|
||||
*.pdb
|
||||
*.pgc
|
||||
*.pgd
|
||||
*.rsp
|
||||
*.sbr
|
||||
*.tlb
|
||||
*.tli
|
||||
*.tlh
|
||||
*.tmp
|
||||
*.tmp_proj
|
||||
*.log
|
||||
*.vspscc
|
||||
*.vssscc
|
||||
.builds
|
||||
*.pidb
|
||||
*.log
|
||||
*.svclog
|
||||
*.scc
|
||||
|
||||
# Visual C++ cache files
|
||||
ipch/
|
||||
*.aps
|
||||
*.ncb
|
||||
*.opensdf
|
||||
*.sdf
|
||||
*.cachefile
|
||||
|
||||
# Visual Studio profiler
|
||||
*.psess
|
||||
*.vsp
|
||||
*.vspx
|
||||
|
||||
# Guidance Automation Toolkit
|
||||
*.gpState
|
||||
|
||||
# ReSharper is a .NET coding add-in
|
||||
_ReSharper*/
|
||||
*.[Rr]e[Ss]harper
|
||||
*.DotSettings.user
|
||||
|
||||
# Click-Once directory
|
||||
publish/
|
||||
|
||||
# Publish Web Output
|
||||
*.Publish.xml
|
||||
*.pubxml
|
||||
*.azurePubxml
|
||||
|
||||
# NuGet Packages Directory
|
||||
## TODO: If you have NuGet Package Restore enabled, uncomment the next line
|
||||
packages/
|
||||
## TODO: If the tool you use requires repositories.config, also uncomment the next line
|
||||
!packages/repositories.config
|
||||
|
||||
# Windows Azure Build Output
|
||||
csx/
|
||||
*.build.csdef
|
||||
|
||||
# Windows Store app package directory
|
||||
AppPackages/
|
||||
|
||||
# Others
|
||||
sql/
|
||||
*.Cache
|
||||
ClientBin/
|
||||
[Ss]tyle[Cc]op.*
|
||||
![Ss]tyle[Cc]op.targets
|
||||
~$*
|
||||
*~
|
||||
*.dbmdl
|
||||
*.[Pp]ublish.xml
|
||||
|
||||
*.publishsettings
|
||||
|
||||
# RIA/Silverlight projects
|
||||
Generated_Code/
|
||||
|
||||
# Backup & report files from converting an old project file to a newer
|
||||
# Visual Studio version. Backup files are not needed, because we have git ;-)
|
||||
_UpgradeReport_Files/
|
||||
Backup*/
|
||||
UpgradeLog*.XML
|
||||
UpgradeLog*.htm
|
||||
|
||||
# SQL Server files
|
||||
App_Data/*.mdf
|
||||
App_Data/*.ldf
|
||||
|
||||
# =========================
|
||||
# Windows detritus
|
||||
# =========================
|
||||
|
||||
# Windows image file caches
|
||||
Thumbs.db
|
||||
ehthumbs.db
|
||||
|
||||
# Folder config file
|
||||
Desktop.ini
|
||||
|
||||
# Recycle Bin used on file shares
|
||||
$RECYCLE.BIN/
|
||||
|
||||
# Mac desktop service store files
|
||||
.DS_Store
|
||||
|
||||
_NCrunch*
|
13
Lib/Civilization.Shared/Civilization.Shared.csproj
Normal file
13
Lib/Civilization.Shared/Civilization.Shared.csproj
Normal file
@@ -0,0 +1,13 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Civilization.Core\Civilization.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
3
Lib/Civilization.Shared/Commands/BaseCommand.cs
Normal file
3
Lib/Civilization.Shared/Commands/BaseCommand.cs
Normal file
@@ -0,0 +1,3 @@
|
||||
namespace Civilization.Shared.Commands;
|
||||
|
||||
public abstract record BaseCommand;
|
3
Lib/Civilization.Shared/Commands/EndTurnCommand.cs
Normal file
3
Lib/Civilization.Shared/Commands/EndTurnCommand.cs
Normal file
@@ -0,0 +1,3 @@
|
||||
namespace Civilization.Shared.Commands;
|
||||
|
||||
public record EndTurnCommand() : BaseCommand;
|
5
Lib/Civilization.Shared/Commands/MoveUnitCommand.cs
Normal file
5
Lib/Civilization.Shared/Commands/MoveUnitCommand.cs
Normal file
@@ -0,0 +1,5 @@
|
||||
using Civilization.Core;
|
||||
|
||||
namespace Civilization.Shared.Commands;
|
||||
|
||||
public record MoveUnitCommand(Guid UnitId, Vec2I TargetPosition) : BaseCommand;
|
3
Lib/Civilization.Shared/Commands/SettleCityCommand.cs
Normal file
3
Lib/Civilization.Shared/Commands/SettleCityCommand.cs
Normal file
@@ -0,0 +1,3 @@
|
||||
namespace Civilization.Shared.Commands;
|
||||
|
||||
public record SettleCityCommand(Guid UnitId) : BaseCommand;
|
46
Lib/Civilization.Shared/JsonPolymorphicConverter.cs
Normal file
46
Lib/Civilization.Shared/JsonPolymorphicConverter.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Civilization.Shared;
|
||||
|
||||
public class JsonPolymorphicConverter<TBase> : JsonConverter<TBase>
|
||||
{
|
||||
private readonly Dictionary<string, Type> _typeMap;
|
||||
|
||||
public JsonPolymorphicConverter()
|
||||
{
|
||||
_typeMap = AppDomain.CurrentDomain.GetAssemblies()
|
||||
.SelectMany(a => a.GetTypes())
|
||||
.Where(t => typeof(TBase).IsAssignableFrom(t) && !t.IsAbstract && !t.IsInterface)
|
||||
.ToDictionary(t => t.Name, t => t);
|
||||
}
|
||||
|
||||
public override TBase? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
using var doc = JsonDocument.ParseValue(ref reader);
|
||||
if (!doc.RootElement.TryGetProperty("type", out var typeProp))
|
||||
throw new JsonException("Missing 'type' field for polymorphic deserialization");
|
||||
|
||||
var typeName = typeProp.GetString();
|
||||
if (typeName is null || !_typeMap.TryGetValue(typeName, out var derivedType))
|
||||
throw new JsonException($"Unknown type discriminator: '{typeName}'");
|
||||
|
||||
var json = doc.RootElement.GetRawText();
|
||||
return (TBase?)JsonSerializer.Deserialize(json, derivedType, options);
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, TBase value, JsonSerializerOptions options)
|
||||
{
|
||||
using var jsonDoc = JsonDocument.Parse(JsonSerializer.Serialize(value, value.GetType(), options));
|
||||
|
||||
writer.WriteStartObject();
|
||||
writer.WriteString("type", value.GetType().Name);
|
||||
|
||||
foreach (var prop in jsonDoc.RootElement.EnumerateObject())
|
||||
{
|
||||
prop.WriteTo(writer);
|
||||
}
|
||||
|
||||
writer.WriteEndObject();
|
||||
}
|
||||
}
|
5
Lib/Civilization.Shared/Packets/ClientMessage.cs
Normal file
5
Lib/Civilization.Shared/Packets/ClientMessage.cs
Normal file
@@ -0,0 +1,5 @@
|
||||
using Civilization.Shared.Commands;
|
||||
|
||||
namespace Civilization.Shared.Packets;
|
||||
|
||||
public record ClientMessage(int PlayerId, BaseCommand Command);
|
3
Lib/Civilization.Shared/Packets/PlayerInfo.cs
Normal file
3
Lib/Civilization.Shared/Packets/PlayerInfo.cs
Normal file
@@ -0,0 +1,3 @@
|
||||
namespace Civilization.Shared.Packets;
|
||||
|
||||
public record PlayerInfo(int Id, string Name, string ColorHex);
|
@@ -0,0 +1,3 @@
|
||||
namespace Civilization.Shared.Packets.ServerMessages;
|
||||
|
||||
public abstract record BaseServerMessage;
|
@@ -0,0 +1,3 @@
|
||||
namespace Civilization.Shared.Packets.ServerMessages;
|
||||
|
||||
public record ErrorMessage(string Reason) : BaseServerMessage;
|
@@ -0,0 +1,3 @@
|
||||
namespace Civilization.Shared.Packets.ServerMessages;
|
||||
|
||||
public record LogMessage(string Message) : BaseServerMessage;
|
@@ -0,0 +1,5 @@
|
||||
using Civilization.Core.Game;
|
||||
|
||||
namespace Civilization.Shared.Packets.ServerMessages;
|
||||
|
||||
public record StateUpdateMessage(GameState GameState, PlayerInfo CurrentPlayer) : BaseServerMessage;
|
21
Lib/Civilization.Shared/SharedJson.cs
Normal file
21
Lib/Civilization.Shared/SharedJson.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using Civilization.Shared.Commands;
|
||||
using Civilization.Shared.Packets.ServerMessages;
|
||||
|
||||
namespace Civilization.Shared;
|
||||
|
||||
public static class SharedJson
|
||||
{
|
||||
public static readonly JsonSerializerOptions Options = new()
|
||||
{
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
Converters =
|
||||
{
|
||||
new JsonStringEnumConverter(JsonNamingPolicy.CamelCase),
|
||||
new JsonPolymorphicConverter<BaseCommand>(),
|
||||
new JsonPolymorphicConverter<BaseServerMessage>(),
|
||||
},
|
||||
WriteIndented = false
|
||||
};
|
||||
}
|
1
Lib/codebase_cmd.txt
Normal file
1
Lib/codebase_cmd.txt
Normal file
@@ -0,0 +1 @@
|
||||
uvx files-to-prompt Civilization.Core GodotIntegration -o civ_codebase.txt --ignore bin* --ignore *.dll --ignore *.cache --ignore *.pdb --ignore *.uid --ignore obj*
|
Submodule godot_game deleted from 3d8e0f2828
4
godot_game/.editorconfig
Normal file
4
godot_game/.editorconfig
Normal file
@@ -0,0 +1,4 @@
|
||||
root = true
|
||||
|
||||
[*]
|
||||
charset = utf-8
|
2
godot_game/.gitattributes
vendored
Normal file
2
godot_game/.gitattributes
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
# Normalize EOL for all files that Git considers text files.
|
||||
* text=auto eol=lf
|
6
godot_game/.gitignore
vendored
Normal file
6
godot_game/.gitignore
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
# Godot 4+ specific ignores
|
||||
.godot/
|
||||
/android/
|
||||
civ_codebase.txt
|
||||
.idea/
|
||||
./civilization/.idea/
|
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
|
9
godot_game/Entities/city_view.tscn
Normal file
9
godot_game/Entities/city_view.tscn
Normal file
@@ -0,0 +1,9 @@
|
||||
[gd_scene load_steps=2 format=3 uid="uid://dtymuhj7dn87s"]
|
||||
|
||||
[ext_resource type="Texture2D" uid="uid://c8ukthe4bm6ui" path="res://Sprites/city.png" id="1_8or66"]
|
||||
|
||||
[node name="CityView" type="Node2D"]
|
||||
|
||||
[node name="Sprite2D" type="Sprite2D" parent="."]
|
||||
scale = Vector2(0.005, 0.005)
|
||||
texture = ExtResource("1_8or66")
|
9
godot_game/Entities/unit_view.tscn
Normal file
9
godot_game/Entities/unit_view.tscn
Normal file
@@ -0,0 +1,9 @@
|
||||
[gd_scene load_steps=2 format=3 uid="uid://cty4sa1bq3obk"]
|
||||
|
||||
[ext_resource type="Texture2D" uid="uid://chkaihrs4nd65" path="res://Sprites/settler.webp" id="1_7g07s"]
|
||||
|
||||
[node name="UnitView" type="Node2D"]
|
||||
|
||||
[node name="Sprite2D" type="Sprite2D" parent="."]
|
||||
scale = Vector2(0.005, 0.005)
|
||||
texture = ExtResource("1_7g07s")
|
99
godot_game/Scenes/world.tscn
Normal file
99
godot_game/Scenes/world.tscn
Normal file
File diff suppressed because one or more lines are too long
BIN
godot_game/Sprites/city.png
Normal file
BIN
godot_game/Sprites/city.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 20 KiB |
34
godot_game/Sprites/city.png.import
Normal file
34
godot_game/Sprites/city.png.import
Normal file
@@ -0,0 +1,34 @@
|
||||
[remap]
|
||||
|
||||
importer="texture"
|
||||
type="CompressedTexture2D"
|
||||
uid="uid://c8ukthe4bm6ui"
|
||||
path="res://.godot/imported/city.png-42096f3d2559488a3d64d50a417bd5ff.ctex"
|
||||
metadata={
|
||||
"vram_texture": false
|
||||
}
|
||||
|
||||
[deps]
|
||||
|
||||
source_file="res://Sprites/city.png"
|
||||
dest_files=["res://.godot/imported/city.png-42096f3d2559488a3d64d50a417bd5ff.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
|
BIN
godot_game/Sprites/settler.webp
Normal file
BIN
godot_game/Sprites/settler.webp
Normal file
Binary file not shown.
After Width: | Height: | Size: 14 KiB |
34
godot_game/Sprites/settler.webp.import
Normal file
34
godot_game/Sprites/settler.webp.import
Normal file
@@ -0,0 +1,34 @@
|
||||
[remap]
|
||||
|
||||
importer="texture"
|
||||
type="CompressedTexture2D"
|
||||
uid="uid://chkaihrs4nd65"
|
||||
path="res://.godot/imported/settler.webp-a74f7464a102bfcbf0634a3635027a21.ctex"
|
||||
metadata={
|
||||
"vram_texture": false
|
||||
}
|
||||
|
||||
[deps]
|
||||
|
||||
source_file="res://Sprites/settler.webp"
|
||||
dest_files=["res://.godot/imported/settler.webp-a74f7464a102bfcbf0634a3635027a21.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
|
BIN
godot_game/Sprites/tiny_terrain.png
Normal file
BIN
godot_game/Sprites/tiny_terrain.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 337 B |
34
godot_game/Sprites/tiny_terrain.png.import
Normal file
34
godot_game/Sprites/tiny_terrain.png.import
Normal file
@@ -0,0 +1,34 @@
|
||||
[remap]
|
||||
|
||||
importer="texture"
|
||||
type="CompressedTexture2D"
|
||||
uid="uid://syk5syuba1rw"
|
||||
path="res://.godot/imported/tiny_terrain.png-83221922c05e6d1fda6dd63ec35b2d2c.ctex"
|
||||
metadata={
|
||||
"vram_texture": false
|
||||
}
|
||||
|
||||
[deps]
|
||||
|
||||
source_file="res://Sprites/tiny_terrain.png"
|
||||
dest_files=["res://.godot/imported/tiny_terrain.png-83221922c05e6d1fda6dd63ec35b2d2c.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
|
21
godot_game/Tilesets/world.tres
Normal file
21
godot_game/Tilesets/world.tres
Normal file
@@ -0,0 +1,21 @@
|
||||
[gd_resource type="TileSet" load_steps=3 format=3 uid="uid://cmtqrdho2188u"]
|
||||
|
||||
[ext_resource type="Texture2D" uid="uid://syk5syuba1rw" path="res://Sprites/tiny_terrain.png" id="1_587l3"]
|
||||
|
||||
[sub_resource type="TileSetAtlasSource" id="TileSetAtlasSource_5a7ea"]
|
||||
texture = ExtResource("1_587l3")
|
||||
texture_region_size = Vector2i(1, 1)
|
||||
0:0/0 = 0
|
||||
1:0/0 = 0
|
||||
2:0/0 = 0
|
||||
3:0/0 = 0
|
||||
4:0/0 = 0
|
||||
5:0/0 = 0
|
||||
6:0/0 = 0
|
||||
7:0/0 = 0
|
||||
8:0/0 = 0
|
||||
9:0/0 = 0
|
||||
|
||||
[resource]
|
||||
tile_size = Vector2i(1, 1)
|
||||
sources/1 = SubResource("TileSetAtlasSource_5a7ea")
|
27
godot_game/UI/selected_unit_panel.tscn
Normal file
27
godot_game/UI/selected_unit_panel.tscn
Normal file
@@ -0,0 +1,27 @@
|
||||
[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"]
|
||||
|
||||
[node name="SelectedUnitPanel" type="MarginContainer" node_paths=PackedStringArray("UnitInfoLabel", "SettleButton")]
|
||||
anchors_preset = 15
|
||||
anchor_right = 1.0
|
||||
anchor_bottom = 1.0
|
||||
grow_horizontal = 2
|
||||
grow_vertical = 2
|
||||
theme_override_constants/margin_left = 8
|
||||
theme_override_constants/margin_top = 8
|
||||
theme_override_constants/margin_right = 8
|
||||
theme_override_constants/margin_bottom = 8
|
||||
script = ExtResource("1_sosfk")
|
||||
UnitInfoLabel = NodePath("UnitInfoLabel")
|
||||
SettleButton = NodePath("SettleButton")
|
||||
|
||||
[node name="UnitInfoLabel" type="Label" parent="."]
|
||||
layout_mode = 2
|
||||
size_flags_vertical = 0
|
||||
text = "Unit name"
|
||||
|
||||
[node name="SettleButton" type="Button" parent="."]
|
||||
layout_mode = 2
|
||||
size_flags_vertical = 8
|
||||
text = "Settle city"
|
1
godot_game/icon.svg
Normal file
1
godot_game/icon.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="128" height="128"><rect width="124" height="124" x="2" y="2" fill="#363d52" stroke="#212532" stroke-width="4" rx="14"/><g fill="#fff" transform="translate(12.322 12.322)scale(.101)"><path d="M105 673v33q407 354 814 0v-33z"/><path fill="#478cbf" d="m105 673 152 14q12 1 15 14l4 67 132 10 8-61q2-11 15-15h162q13 4 15 15l8 61 132-10 4-67q3-13 15-14l152-14V427q30-39 56-81-35-59-83-108-43 20-82 47-40-37-88-64 7-51 8-102-59-28-123-42-26 43-46 89-49-7-98 0-20-46-46-89-64 14-123 42 1 51 8 102-48 27-88 64-39-27-82-47-48 49-83 108 26 42 56 81zm0 33v39c0 276 813 276 814 0v-39l-134 12-5 69q-2 10-14 13l-162 11q-12 0-16-11l-10-65H446l-10 65q-4 11-16 11l-162-11q-12-3-14-13l-5-69z"/><path d="M483 600c0 34 58 34 58 0v-86c0-34-58-34-58 0z"/><circle cx="725" cy="526" r="90"/><circle cx="299" cy="526" r="90"/></g><g fill="#414042" transform="translate(12.322 12.322)scale(.101)"><circle cx="307" cy="532" r="60"/><circle cx="717" cy="532" r="60"/></g></svg>
|
After Width: | Height: | Size: 994 B |
37
godot_game/icon.svg.import
Normal file
37
godot_game/icon.svg.import
Normal file
@@ -0,0 +1,37 @@
|
||||
[remap]
|
||||
|
||||
importer="texture"
|
||||
type="CompressedTexture2D"
|
||||
uid="uid://cudqpxk1k2gk8"
|
||||
path="res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.ctex"
|
||||
metadata={
|
||||
"vram_texture": false
|
||||
}
|
||||
|
||||
[deps]
|
||||
|
||||
source_file="res://icon.svg"
|
||||
dest_files=["res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.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
|
||||
svg/scale=1.0
|
||||
editor/scale_with_editor_scale=false
|
||||
editor/convert_colors_with_editor_theme=false
|
25
godot_game/project.godot
Normal file
25
godot_game/project.godot
Normal file
@@ -0,0 +1,25 @@
|
||||
; Engine configuration file.
|
||||
; It's best edited using the editor UI and not directly,
|
||||
; since the parameters that go here are not all obvious.
|
||||
;
|
||||
; Format:
|
||||
; [section] ; section goes between []
|
||||
; param=value ; assign values to parameters
|
||||
|
||||
config_version=5
|
||||
|
||||
[application]
|
||||
|
||||
config/name="civilization"
|
||||
run/main_scene="uid://dy20m1dgo6mqq"
|
||||
config/features=PackedStringArray("4.4", "GL Compatibility")
|
||||
config/icon="res://icon.svg"
|
||||
|
||||
[dotnet]
|
||||
|
||||
project/assembly_name="civilization"
|
||||
|
||||
[rendering]
|
||||
|
||||
renderer/rendering_method="gl_compatibility"
|
||||
renderer/rendering_method.mobile="gl_compatibility"
|
Reference in New Issue
Block a user