refactor: update iterator access and simplify loops
Refactor code to use `Val1` and `Val2` instead of `Item1` and `Item2` for iterator access. Simplify `PlaceMarkCommand` and `WinCheckSystem` by replacing `while` loops with `foreach` and removing redundant comments.
This commit is contained in:
@@ -15,34 +15,24 @@ public struct PlaceMarkCommand : ICommand
|
||||
|
||||
public void Execute(World world)
|
||||
{
|
||||
// Read-only check before iteration — never auto-marks.
|
||||
if (world.ReadSingleton<GameState>().Status != GameStatus.Playing)
|
||||
return;
|
||||
|
||||
// Find the cell entity at (Row, Col) that has no Mark.
|
||||
var query = new Query<Cell>().Without<Mark>();
|
||||
Entity? target = null;
|
||||
|
||||
// Fetch singleton ref inside iteration so mutations are auto-marked.
|
||||
using var iter = world.Select(query);
|
||||
ref var state = ref world.GetSingleton<GameState>();
|
||||
|
||||
while (iter.MoveNext())
|
||||
{
|
||||
if (iter.Item1.Row == Row && iter.Item1.Col == Col)
|
||||
{
|
||||
target = iter.Entity;
|
||||
break;
|
||||
}
|
||||
foreach(var iter in world.Select(new Query<Cell>().Without<Mark>())){
|
||||
if (iter.Val1.Row != Row || iter.Val1.Col != Col) continue;
|
||||
target = iter.Entity;
|
||||
break;
|
||||
}
|
||||
|
||||
if (target == null)
|
||||
return; // Cell already occupied or invalid position.
|
||||
return;
|
||||
|
||||
// Place the mark.
|
||||
world.AddComponent(target.Value, new Mark { Player = state.CurrentPlayer });
|
||||
|
||||
// Advance turn. Mutations auto-marked via EndIteration — no MarkModified needed.
|
||||
state.MoveCount++;
|
||||
state.CurrentPlayer = state.CurrentPlayer == Player.X ? Player.O : Player.X;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user