Chessistics/Scripts/Board/BoardView.cs
Samuel Bouchet e6eaae43ab Initial commit: Chessistics prototype v0.3
Black box sim engine (commands in, events out) with 3 piece types
(Rook, Bishop, Knight), cargo transfer system with social status
priority, collision detection, and victory/defeat conditions.

57 tests covering rules, simulation, loading, and solvability.
Godot 4 presentation layer scaffolding.
2026-04-10 14:58:03 +02:00

87 lines
2.3 KiB
C#

using Godot;
using System.Collections.Generic;
using Chessistics.Engine.Model;
namespace Chessistics.Scripts.Board;
public partial class BoardView : Node2D
{
public const int CellSize = 80;
private readonly Dictionary<Coords, CellView> _cells = new();
private int _width;
private int _height;
public void BuildBoard(LevelDef level)
{
// Clear existing children
foreach (var child in GetChildren())
child.QueueFree();
_cells.Clear();
_width = level.Width;
_height = level.Height;
var boardState = BoardState.FromLevel(level);
for (int col = 0; col < level.Width; col++)
{
for (int row = 0; row < level.Height; row++)
{
var coords = new Coords(col, row);
var cellView = new CellView();
cellView.Setup(coords, boardState.GetCell(coords), CellSize);
AddChild(cellView);
_cells[coords] = cellView;
}
}
// Label productions and demands
foreach (var prod in level.Productions)
{
if (_cells.TryGetValue(prod.Position, out var cell))
cell.SetLabel(prod.Name);
}
foreach (var demand in level.Demands)
{
if (_cells.TryGetValue(demand.Position, out var cell))
cell.SetLabel(demand.Name);
}
}
public Coords? PixelToCoords(Vector2 localPos)
{
int col = Mathf.FloorToInt(localPos.X / CellSize);
int row = Mathf.FloorToInt(-localPos.Y / CellSize);
var coords = new Coords(col, row);
return coords.IsOnBoard(_width, _height) ? coords : null;
}
public Vector2 CoordsToPixel(Coords coords)
{
return new Vector2(
coords.Col * CellSize + CellSize / 2f,
-coords.Row * CellSize + CellSize / 2f
);
}
public CellView? GetCellView(Coords coords)
=> _cells.GetValueOrDefault(coords);
public void ClearHighlights()
{
foreach (var cell in _cells.Values)
cell.SetHighlight(false);
}
public void HighlightCells(IEnumerable<Coords> cells, Color color)
{
foreach (var coords in cells)
{
if (_cells.TryGetValue(coords, out var cellView))
cellView.SetHighlightColor(color);
}
}
}