unlinq'd & refactored

This commit is contained in:
Lilith Schier 2026-08-30 16:02:24 +02:00
parent a5093e62e4
commit 6bdc9617b2
4 changed files with 38 additions and 24 deletions

View file

@ -1,12 +1,41 @@
using System.Runtime.InteropServices.Swift; using System;
using System.Diagnostics;
namespace Dancer; namespace Dancer;
public static class TileMapLayerExtensions public static class Extensions
{ {
extension(TileMapLayer self) extension(TileMapLayer self)
{ {
public Vector2I GlobalToMap(Vector2 globalPosition) => public Vector2I GlobalToMap(Vector2 globalPosition) =>
self.LocalToMap(self.ToLocal(globalPosition)); self.LocalToMap(self.ToLocal(globalPosition));
} }
extension(ReadOnlySpan<Vector2> polygon)
{
/// <summary>
/// Calculates the bounding rectangle that encompasses all points in the given polygon.
/// </summary>
/// <remarks>
/// The polygon must contain at least one point.
/// </remarks>
public Rect2 CalculateBoundingRect()
{
var result = new Rect2(polygon[0], Vector2.Zero);
foreach (var point in polygon[1..]) result = result.Expand(point);
return result;
}
}
extension(Rect2 rect)
{
public void WritePoints(Span<Vector2> poly)
{
Debug.Assert(poly.Length >= 4);
poly[0] = rect.Position;
poly[1] = rect.Position + new Vector2(rect.Size.X, 0);
poly[2] = rect.End;
poly[3] = rect.Position + new Vector2(0, rect.Size.Y);
}
}
} }

View file

@ -1,5 +1,4 @@
using System; using System;
using System.Linq;
namespace Dancer; namespace Dancer;
@ -41,12 +40,8 @@ public sealed partial class WaterArea : Polygon2D
private void UpdateFillLevel() private void UpdateFillLevel()
{ {
var boundingRect = if (Polygon.Length < 3) return;
Polygon.Skip(1) var boundingRect = Polygon.CalculateBoundingRect();
.Aggregate(
new Rect2(Polygon[0], Vector2.Zero),
(current, p) => current.Expand(p)
);
var topBorder = Mathf.Lerp(boundingRect.End.Y, boundingRect.Position.Y, FillLevel); var topBorder = Mathf.Lerp(boundingRect.End.Y, boundingRect.Position.Y, FillLevel);
boundingRect.Position = boundingRect.Position with { Y = topBorder }; boundingRect.Position = boundingRect.Position with { Y = topBorder };
@ -57,16 +52,11 @@ public sealed partial class WaterArea : Polygon2D
if (_collisionShape2D?.Shape is not ConvexPolygonShape2D convexPolygonShape) return; if (_collisionShape2D?.Shape is not ConvexPolygonShape2D convexPolygonShape) return;
ReadOnlySpan<Vector2> filledPolygon = Span<Vector2> boundingRectPolygon = stackalloc Vector2[4];
[ boundingRect.WritePoints(boundingRectPolygon);
boundingRect.Position,
boundingRect.Position + new Vector2(boundingRect.Size.X, 0),
boundingRect.End,
boundingRect.Position + new Vector2(0, boundingRect.Size.Y),
];
convexPolygonShape.Points = convexPolygonShape.Points =
Geometry2D.IntersectPolygons(Polygon, filledPolygon) is [var polygon] Geometry2D.IntersectPolygons(Polygon, boundingRectPolygon) is [var polygon]
? polygon ? polygon
: []; : [];
} }

View file

@ -146,7 +146,7 @@ resource_local_to_scene = true
shader = ExtResource("9_y2hwx") shader = ExtResource("9_y2hwx")
shader_parameter/water = Color(0.31764707, 0.5921569, 0.6431373, 0.64705884) shader_parameter/water = Color(0.31764707, 0.5921569, 0.6431373, 0.64705884)
shader_parameter/highlights = Color(0.51546186, 0.75253254, 0.7743172, 1) shader_parameter/highlights = Color(0.51546186, 0.75253254, 0.7743172, 1)
shader_parameter/top_border = 0.0 shader_parameter/top_border = 12.799999
[node name="Game" type="Node2D" unique_id=1186062666] [node name="Game" type="Node2D" unique_id=1186062666]
script = ExtResource("1_kc8ui") script = ExtResource("1_kc8ui")
@ -211,6 +211,7 @@ position = Vector2(16, 32)
material = SubResource("ShaderMaterial_y2hwx") material = SubResource("ShaderMaterial_y2hwx")
position = Vector2(-624, 0) position = Vector2(-624, 0)
polygon = PackedVector2Array(0, 0, 256, 0, 256, 32, 0, 32) polygon = PackedVector2Array(0, 0, 256, 0, 256, 32, 0, 32)
FillLevel = 0.6
[node name="Rain4" type="Sprite2D" parent="World/Overlay" unique_id=940148613] [node name="Rain4" type="Sprite2D" parent="World/Overlay" unique_id=940148613]
physics_interpolation_mode = 2 physics_interpolation_mode = 2

View file

@ -60,9 +60,3 @@ public static class Physics
public sealed record AnticipationResult(float Time, Vector2 Position); public sealed record AnticipationResult(float Time, Vector2 Position);
} }
public static class Extensions
{
extension(CharacterBody2D body)
{
}
}