Shape: one dirty flag invalidates Coordinates, AllCoordinates and CenterOfMass; Resize marks dirty
Resize keeps its fill (new cells on): the hull drawer's width grow relies on it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
e4f19df15e
commit
7a44e0c00c
1 changed files with 29 additions and 9 deletions
|
|
@ -60,6 +60,7 @@ public class Shape
|
|||
}
|
||||
|
||||
Cells = newCells;
|
||||
_dirty = true;
|
||||
}
|
||||
|
||||
private static int mod(int x, int m) {
|
||||
|
|
@ -85,11 +86,7 @@ public class Shape
|
|||
{
|
||||
get
|
||||
{
|
||||
if (_dirty)
|
||||
{
|
||||
_cachedShapeCoordinates = EnumerateShapeCoordinates().ToArray();
|
||||
_dirty = false;
|
||||
}
|
||||
Refresh();
|
||||
return _cachedShapeCoordinates;
|
||||
}
|
||||
}
|
||||
|
|
@ -108,7 +105,14 @@ public class Shape
|
|||
private int2[] _cachedAllShapeCoordinates;
|
||||
|
||||
[IgnoreMember]
|
||||
public int2[] AllCoordinates => _cachedAllShapeCoordinates ?? (_cachedAllShapeCoordinates = EnumerateAllShapeCoordinates().ToArray());
|
||||
public int2[] AllCoordinates
|
||||
{
|
||||
get
|
||||
{
|
||||
Refresh();
|
||||
return _cachedAllShapeCoordinates;
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerable<int2> EnumerateAllShapeCoordinates()
|
||||
{
|
||||
|
|
@ -121,11 +125,27 @@ public class Shape
|
|||
}
|
||||
}
|
||||
|
||||
private float2? _centerOfMass;
|
||||
private float2 _centerOfMass;
|
||||
|
||||
[IgnoreMember]
|
||||
public float2 CenterOfMass => _centerOfMass ?? (_centerOfMass = Coordinates
|
||||
.Aggregate(float2.zero, (total, coord) => total + coord) / Coordinates.Length).Value;
|
||||
public float2 CenterOfMass
|
||||
{
|
||||
get
|
||||
{
|
||||
Refresh();
|
||||
return _centerOfMass;
|
||||
}
|
||||
}
|
||||
|
||||
// One dirty flag: Coordinates, AllCoordinates and CenterOfMass are caches of Cells and recompute together.
|
||||
private void Refresh()
|
||||
{
|
||||
if (!_dirty) return;
|
||||
_cachedShapeCoordinates = EnumerateShapeCoordinates().ToArray();
|
||||
_cachedAllShapeCoordinates = EnumerateAllShapeCoordinates().ToArray();
|
||||
_centerOfMass = _cachedShapeCoordinates.Aggregate(float2.zero, (total, coord) => total + coord) / _cachedShapeCoordinates.Length;
|
||||
_dirty = false;
|
||||
}
|
||||
|
||||
public bool this[int2 pos] {
|
||||
get { return pos.x >= 0 && pos.y >= 0 && pos.x < Width && pos.y < Height && Cells[pos.x, pos.y]; }
|
||||
|
|
|
|||
Loading…
Reference in a new issue