initialize repo
This commit is contained in:
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++;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user