oecs-sharp/TicTacToe/CsvLoader.cs

46 lines
1.3 KiB
C#

using OECS;
namespace TicTacToe;
/// <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;
}
}