added water area scene, closes #10

This commit is contained in:
Lilith Schier 2026-08-30 15:08:50 +02:00
parent 9eb3951361
commit a5093e62e4
7 changed files with 140 additions and 48 deletions

78
src/WaterArea.cs Normal file
View file

@ -0,0 +1,78 @@
using System;
using System.Linq;
namespace Dancer;
[Tool]
public sealed partial class WaterArea : Polygon2D
{
private CollisionShape2D? _collisionShape2D;
[Export(PropertyHint.Range, "0,1")]
public float FillLevel
{
get;
set
{
field = value;
UpdateFillLevel();
}
} = 1f;
public override void _Ready()
{
var area = new Area2D
{
Monitorable = true,
Monitoring = false,
CollisionLayer = 1 << 5,
CollisionMask = 0,
Visible = false,
};
AddChild(area, @internal: InternalMode.Front);
_collisionShape2D = new()
{
DebugColor = Colors.Cyan with { A = 0.3f },
};
area.AddChild(_collisionShape2D, @internal: InternalMode.Front);
_collisionShape2D.Shape = new ConvexPolygonShape2D();
UpdateFillLevel();
}
private void UpdateFillLevel()
{
var boundingRect =
Polygon.Skip(1)
.Aggregate(
new Rect2(Polygon[0], Vector2.Zero),
(current, p) => current.Expand(p)
);
var topBorder = Mathf.Lerp(boundingRect.End.Y, boundingRect.Position.Y, FillLevel);
boundingRect.Position = boundingRect.Position with { Y = topBorder };
boundingRect.Size = boundingRect.Size with { Y = boundingRect.Size.Y * FillLevel };
var material = (ShaderMaterial)Material;
material.SetShaderParameter("top_border", ToGlobal(boundingRect.Position).Y);
if (_collisionShape2D?.Shape is not ConvexPolygonShape2D convexPolygonShape) return;
ReadOnlySpan<Vector2> filledPolygon =
[
boundingRect.Position,
boundingRect.Position + new Vector2(boundingRect.Size.X, 0),
boundingRect.End,
boundingRect.Position + new Vector2(0, boundingRect.Size.Y),
];
convexPolygonShape.Points =
Geometry2D.IntersectPolygons(Polygon, filledPolygon) is [var polygon]
? polygon
: [];
}
public override void _Draw()
{
UpdateFillLevel();
}
}

1
src/WaterArea.cs.uid Normal file
View file

@ -0,0 +1 @@
uid://65jqru6xr5bo

File diff suppressed because one or more lines are too long

View file

@ -2,6 +2,7 @@ shader_type canvas_item;
uniform vec4 water : source_color; uniform vec4 water : source_color;
uniform vec3 highlights : source_color; uniform vec3 highlights : source_color;
uniform float top_border;
varying vec2 world_position; varying vec2 world_position;
@ -28,13 +29,17 @@ void fragment() {
if (x > 0.8) x = 1.0; else x = 0.0; if (x > 0.8) x = 1.0; else x = 0.0;
if (int(world_position.y) % 2 == 1) x = 0.0; if (int(world_position.y) % 2 == 1) x = 0.0;
vec3 raw_color = mix(water.rgb, highlights, x); vec3 raw_color = mix(water.rgb, highlights, x);
float depth = UV.y / TEXTURE_PIXEL_SIZE.y; float depth = world_position.y - top_border;
if (depth < 0.0) {
COLOR = vec4(0.0);
} else {
float top_ribbon_depth = 6.0; float top_ribbon_depth = 6.0;
float depth_grad = (top_ribbon_depth - depth) / top_ribbon_depth; float depth_grad = (top_ribbon_depth - depth) / top_ribbon_depth;
float top_light = depth_grad > 0.0 ? max(0.0, depth_grad*depth_grad) : 0.0; float top_light = depth_grad > 0.0 ? max(0.0, depth_grad*depth_grad) : 0.0;
raw_color += vec3(top_light); raw_color += vec3(top_light);
COLOR = vec4(raw_color, water.a); COLOR = vec4(raw_color, water.a);
} }
}
//void light() { //void light() {
// // Called for every pixel for every light affecting the CanvasItem. // // Called for every pixel for every light affecting the CanvasItem.

16
src/level/water_area.tscn Normal file
View file

@ -0,0 +1,16 @@
[gd_scene format=3 uid="uid://cbr6glix407xt"]
[ext_resource type="Shader" uid="uid://dxyevul4ilojm" path="res://src/level/water.gdshader" id="1_vkr3x"]
[ext_resource type="Script" uid="uid://65jqru6xr5bo" path="res://src/WaterArea.cs" id="2_hfx2r"]
[sub_resource type="ShaderMaterial" id="ShaderMaterial_7iiff"]
resource_local_to_scene = true
shader = ExtResource("1_vkr3x")
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
[node name="WaterArea" type="Polygon2D" unique_id=469119063]
material = SubResource("ShaderMaterial_7iiff")
polygon = PackedVector2Array(0, 0, 64, 0, 64, 32, 0, 32)
script = ExtResource("2_hfx2r")

View file

@ -258,10 +258,15 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
if (IsOnCeiling()) JumpTimer.Stop(); if (IsOnCeiling()) JumpTimer.Stop();
} }
public void OnEnteredWater(Node2D body) => IsInWater = true; public void OnEnteredWater(Node2D body)
{
GD.Print("Entered water");
IsInWater = true;
}
public void OnLeftWater(Node2D body) public void OnLeftWater(Node2D body)
{ {
GD.Print("Left water");
if (!IsInWater || SubmersedSensor.HasOverlappingBodies()) return; if (!IsInWater || SubmersedSensor.HasOverlappingBodies()) return;
IsInWater = false; IsInWater = false;
WaterJumpAllowedTimer.Start(); WaterJumpAllowedTimer.Start();

View file

@ -8,9 +8,6 @@
[sub_resource type="RectangleShape2D" id="RectangleShape2D_jiejy"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_jiejy"]
size = Vector2(8, 28) size = Vector2(8, 28)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_o822w"]
size = Vector2(8, 12)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_qi477"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_qi477"]
size = Vector2(4, 24) size = Vector2(4, 24)
@ -20,6 +17,9 @@ size = Vector2(14, 32)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_5f2sf"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_5f2sf"]
size = Vector2(13.59375, 20) size = Vector2(13.59375, 20)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_o822w"]
size = Vector2(8, 12)
[sub_resource type="Animation" id="Animation_yw30f"] [sub_resource type="Animation" id="Animation_yw30f"]
resource_name = "RESET" resource_name = "RESET"
length = 0.001 length = 0.001
@ -356,17 +356,6 @@ visible = false
position = Vector2(0, -14) position = Vector2(0, -14)
shape = SubResource("RectangleShape2D_jiejy") shape = SubResource("RectangleShape2D_jiejy")
[node name="SubmersedSensor" type="Area2D" parent="." unique_id=1595680866]
unique_name_in_owner = true
visible = false
collision_layer = 0
collision_mask = 32
[node name="CollisionShape2D" type="CollisionShape2D" parent="SubmersedSensor" unique_id=1538343174]
position = Vector2(0, -21)
shape = SubResource("RectangleShape2D_o822w")
debug_color = Color(0, 0.5282814, 0.99723786, 0.41960785)
[node name="Camera2D" type="Camera2D" parent="." unique_id=1975474809] [node name="Camera2D" type="Camera2D" parent="." unique_id=1975474809]
position = Vector2(0, -16) position = Vector2(0, -16)
process_callback = 0 process_callback = 0
@ -409,6 +398,7 @@ metadata/_aseprite_wizard_config_ = {
metadata/_aseprite_wizard_source_file_hash_ = "61168df4a163c039cfaab97f4fccb905" metadata/_aseprite_wizard_source_file_hash_ = "61168df4a163c039cfaab97f4fccb905"
[node name="Hurtbox" type="Area2D" parent="Body" unique_id=1918418417] [node name="Hurtbox" type="Area2D" parent="Body" unique_id=1918418417]
visible = false
collision_layer = 2 collision_layer = 2
collision_mask = 0 collision_mask = 0
monitoring = false monitoring = false
@ -455,6 +445,16 @@ position = Vector2(5, 0)
shape = SubResource("RectangleShape2D_244u8") shape = SubResource("RectangleShape2D_244u8")
debug_color = Color(0.99430805, 0, 0.2314094, 0.41960785) debug_color = Color(0.99430805, 0, 0.2314094, 0.41960785)
[node name="SubmersedSensor" type="Area2D" parent="." unique_id=1595680866]
unique_name_in_owner = true
collision_layer = 0
collision_mask = 32
[node name="CollisionShape2D" type="CollisionShape2D" parent="SubmersedSensor" unique_id=1538343174]
position = Vector2(0, -21)
shape = SubResource("RectangleShape2D_o822w")
debug_color = Color(0, 0.5282814, 0.99723786, 0.41960785)
[node name="AnimationPlayer" type="AnimationPlayer" parent="." unique_id=1251324400] [node name="AnimationPlayer" type="AnimationPlayer" parent="." unique_id=1251324400]
libraries/ = SubResource("AnimationLibrary_rgyib") libraries/ = SubResource("AnimationLibrary_rgyib")