2026-07-18 20:43:13 +08:00
|
|
|
using OECS;
|
|
|
|
|
|
2026-07-20 16:41:10 +08:00
|
|
|
namespace Game.TicTacToe;
|
2026-07-18 20:43:13 +08:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Minimal CSV loader that creates entities from a CSV file.
|
|
|
|
|
///
|
|
|
|
|
/// The first row is a header. Each subsequent row creates one entity.
|
|
|
|
|
/// Column names are mapped to component fields by convention:
|
|
|
|
|
/// a column named "Row" sets Cell.Row, "Col" sets Cell.Col, etc.
|
|
|
|
|
///
|
|
|
|
|
/// Currently hardcoded for the Cell component. Extend as needed.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static class CsvLoader
|
|
|
|
|
{
|
|
|
|
|
public static List<Entity> LoadCells(World world, string filePath)
|
|
|
|
|
{
|
|
|
|
|
var entities = new List<Entity>();
|
|
|
|
|
var lines = File.ReadAllLines(filePath);
|
|
|
|
|
|
|
|
|
|
if (lines.Length < 2)
|
|
|
|
|
return entities;
|
|
|
|
|
|
|
|
|
|
// Parse header to get column indices.
|
|
|
|
|
var headers = lines[0].Split(',');
|
|
|
|
|
int rowIdx = Array.IndexOf(headers, "Row");
|
|
|
|
|
int colIdx = Array.IndexOf(headers, "Col");
|
|
|
|
|
|
|
|
|
|
for (int i = 1; i < lines.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
var values = lines[i].Split(',');
|
|
|
|
|
if (values.Length < 2) continue;
|
|
|
|
|
|
|
|
|
|
var entity = world.CreateEntity();
|
|
|
|
|
world.AddComponent(entity, new Cell
|
|
|
|
|
{
|
|
|
|
|
Row = int.Parse(values[rowIdx]),
|
|
|
|
|
Col = int.Parse(values[colIdx])
|
|
|
|
|
});
|
|
|
|
|
entities.Add(entity);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return entities;
|
|
|
|
|
}
|
|
|
|
|
}
|