diff --git a/src/Util.cs b/src/Util.cs index 0f50972..a87c073 100644 --- a/src/Util.cs +++ b/src/Util.cs @@ -1,12 +1,41 @@ -using System.Runtime.InteropServices.Swift; +using System; +using System.Diagnostics; namespace Dancer; -public static class TileMapLayerExtensions +public static class Extensions { extension(TileMapLayer self) { public Vector2I GlobalToMap(Vector2 globalPosition) => self.LocalToMap(self.ToLocal(globalPosition)); } + + extension(ReadOnlySpan polygon) + { + /// + /// Calculates the bounding rectangle that encompasses all points in the given polygon. + /// + /// + /// The polygon must contain at least one point. + /// + 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 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); + } + } } diff --git a/src/WaterArea.cs b/src/WaterArea.cs index 78fe254..18a0532 100644 --- a/src/WaterArea.cs +++ b/src/WaterArea.cs @@ -1,5 +1,4 @@ using System; -using System.Linq; namespace Dancer; @@ -41,12 +40,8 @@ public sealed partial class WaterArea : Polygon2D private void UpdateFillLevel() { - var boundingRect = - Polygon.Skip(1) - .Aggregate( - new Rect2(Polygon[0], Vector2.Zero), - (current, p) => current.Expand(p) - ); + if (Polygon.Length < 3) return; + var boundingRect = Polygon.CalculateBoundingRect(); var topBorder = Mathf.Lerp(boundingRect.End.Y, boundingRect.Position.Y, FillLevel); 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; - ReadOnlySpan filledPolygon = - [ - boundingRect.Position, - boundingRect.Position + new Vector2(boundingRect.Size.X, 0), - boundingRect.End, - boundingRect.Position + new Vector2(0, boundingRect.Size.Y), - ]; + Span boundingRectPolygon = stackalloc Vector2[4]; + boundingRect.WritePoints(boundingRectPolygon); convexPolygonShape.Points = - Geometry2D.IntersectPolygons(Polygon, filledPolygon) is [var polygon] + Geometry2D.IntersectPolygons(Polygon, boundingRectPolygon) is [var polygon] ? polygon : []; } diff --git a/src/game.tscn b/src/game.tscn index 6788c6e..6a1a3e8 100644 --- a/src/game.tscn +++ b/src/game.tscn @@ -146,7 +146,7 @@ resource_local_to_scene = true shader = ExtResource("9_y2hwx") 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/top_border = 0.0 +shader_parameter/top_border = 12.799999 [node name="Game" type="Node2D" unique_id=1186062666] script = ExtResource("1_kc8ui") @@ -211,6 +211,7 @@ position = Vector2(16, 32) material = SubResource("ShaderMaterial_y2hwx") position = Vector2(-624, 0) polygon = PackedVector2Array(0, 0, 256, 0, 256, 32, 0, 32) +FillLevel = 0.6 [node name="Rain4" type="Sprite2D" parent="World/Overlay" unique_id=940148613] physics_interpolation_mode = 2 diff --git a/src/physics/Physics.cs b/src/physics/Physics.cs index 5681165..ae03c25 100644 --- a/src/physics/Physics.cs +++ b/src/physics/Physics.cs @@ -60,9 +60,3 @@ public static class Physics public sealed record AnticipationResult(float Time, Vector2 Position); } -public static class Extensions -{ - extension(CharacterBody2D body) - { - } -}