sprite trails!

This commit is contained in:
Lilith Schier 2026-09-07 03:28:22 +02:00
parent ee8cbaa1dc
commit b8d06ff5c3
5 changed files with 182 additions and 32 deletions

View file

@ -218,12 +218,16 @@ tile_set = SubResource("TileSet_mwb40")
collision_visibility_mode = 2
[node name="Entities" type="Node2D" parent="." unique_id=1592804921]
z_index = 10
z_as_relative = false
[node name="Player" parent="Entities" unique_id=500011307 instance=ExtResource("2_feb5d")]
position = Vector2(-320, -80)
[node name="Overlay" type="Node2D" parent="." unique_id=59734162]
visibility_layer = 3
z_index = 15
z_as_relative = false
[node name="Rain4" type="Sprite2D" parent="Overlay" unique_id=940148613]
physics_interpolation_mode = 2
@ -300,6 +304,7 @@ process_callback = 0
script = ExtResource("9_qiocs")
[node name="BackBufferCopy" type="BackBufferCopy" parent="Static/SubViewportContainer/SubViewport" unique_id=118401361]
z_index = 18
copy_mode = 2
[node name="CanvasLayer" type="CanvasLayer" parent="Static/SubViewportContainer/SubViewport" unique_id=1370865983]

View file

@ -21,13 +21,13 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
public float GroundedSpeedStopDecay { get; set; } = 0.005f;
[Export(PropertyHint.None, "suffix:px/s")]
public float WalkMaxSpeed { get; set; } = 150.0f;
public float WalkMaxSpeed { get; set; } = 140.0f;
[Export(PropertyHint.None, "suffix:px/s")]
public float AirMaxSpeed { get; set; } = 150.0f;
public float AirMaxSpeed { get; set; } = 140.0f;
[Export(PropertyHint.None, "suffix:px/s")]
public float WaterMaxSpeed { get; set; } = 190.0f;
public float WaterMaxSpeed { get; set; } = 220.0f;
[Export(PropertyHint.None, "suffix:px/s²")]
public float WalkAccel { get; set; } = 800.0f;
@ -39,13 +39,13 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
public float WaterAccel { get; set; } = 1200.0f;
[Export(PropertyHint.None, "suffix:px/s")]
public float JumpAscentSpeed { get; set; } = 200.0f;
public float JumpAscentSpeed { get; set; } = 190.0f;
[Export(PropertyHint.None, "suffix:px/s")]
public float SlashSpeedBoost { get; set; } = 90.0f;
[Export(PropertyHint.None, "suffix:px/s")]
public float DashSpeed { get; set; } = 400.0f;
public float DashSpeed { get; set; } = 800.0f;
/// <summary>The angle difference from perfect left/right that still counts.</summary>
[Export(PropertyHint.Range, "0,360,0.1,radians_as_degrees")]
@ -96,6 +96,7 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
[Node] private Timer GlobalDamageImmunityTimer { get; set; } = null!;
[Node] private Timer PlatformIgnoreTimer { get; set; } = null!;
[Node] private Timer ParryTimer { get; set; } = null!;
[Node] private SpriteTrail SpriteTrail { get; set; } = null!;
public void OnReady()
{
@ -167,7 +168,9 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
{
if (!DashDirectionChangeTimer.IsStopped())
{
DashDirection = _currentDirection;
DashDirection = _currentDirection.IsZeroApprox()
? FacingVector
: _currentDirection.Normalized();
}
velocity = DashDirection * DashSpeed;
@ -182,18 +185,17 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
inputDirection.Y = -1f;
}
if (!velocityWasTooLarge || velocity.Dot(inputDirection) < 0f)
{
velocity += inputDirection.Normalized() * WaterAccel * deltaF;
}
var velocityIsTooLarge = velocity.Length() > WaterMaxSpeed;
if (velocityWasTooLarge && velocityIsTooLarge)
{
dragAmount = 0.015f;
}
if (!velocityWasTooLarge && velocityIsTooLarge)
{
velocity = velocity.LimitLength(WaterMaxSpeed);
}
if (!velocityIsTooLarge && inputDirection.IsZeroApprox())
{
dragAmount = 0.1f;
@ -365,8 +367,10 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
AnimationTree.Set("parameters/holding_wall/blend_amount", IsHoldingWall ? 1f : 0f);
AnimationTree.Set("parameters/ground/blend_position", Mathf.Abs(Velocity.X));
AnimationTree.Set("parameters/air/blend_position", Velocity.Y);
AnimationTree.Set("parameters/dive/blend_position", new Vector2(Mathf.Abs(Velocity.X), Velocity.Y));
AnimationTree.Set("parameters/dash_sm/dash/blend_position", new Vector2(Mathf.Abs(Velocity.X), Velocity.Y));
AnimationTree.Set("parameters/dive/blend_position",
new Vector2(Mathf.Abs(Velocity.X), Velocity.Y));
AnimationTree.Set("parameters/dash_sm/dash/blend_position",
new Vector2(Mathf.Abs(Velocity.X), Velocity.Y));
}
public readonly struct BufferedInput
@ -535,16 +539,21 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
DashTimer.Start();
DashDirectionChangeTimer.Start();
DashCooldownTimer.Start();
CreateTween()
.TweenProperty(Engine.Singleton, "time_scale", 1.0f, 0.05f)
.From(0.01)
.SetEase(Tween.EaseType.InOut);
DashDirection = (_currentDirection.IsZeroApprox()
? FacingVector
: _currentDirection
).Normalized();
DashHitbox.StartAttack();
AnimationTree.Set("parameters/dash/request", (int)AnimationNodeOneShot.OneShotRequest.Fire);
Engine.Singleton.TimeScale = 0.1f;
SpriteTrail.CreateAfterImages = true;
var tween = CreateTween().SetEase(Tween.EaseType.InOut);
tween.TweenProperty(Engine.Singleton, "time_scale", 0.2f, 0.02);
tween.TweenProperty(Engine.Singleton, "time_scale", 0.5f, 0.01);
tween.TweenProperty(Engine.Singleton, "time_scale", 1.0f, 0.01).SetDelay(0.07);
tween.TweenCallback(Callable.From(() => SpriteTrail.CreateAfterImages = false)).SetDelay(0.05);
}
private void Parry() => ParryTimer.Start();

85
src/player/SpriteTrail.cs Normal file
View file

@ -0,0 +1,85 @@
namespace Dancer;
[GlobalClass]
public partial class SpriteTrail : Node2D
{
private Sprite2D? _parentSprite;
private Timer? _timer;
[Export]
private float Interval
{
get;
set
{
field = value;
_timer?.WaitTime = field;
}
} = 0.05f;
[Export] private float Lifetime { get; set; } = 0.5f;
[Export] private Material? OverrideMaterial { get; set; }
[Export]
public bool CreateAfterImages
{
get;
set
{
if (value && !field) _timer?.Start();
if (!value && field) _timer?.Stop();
field = value;
}
} = true;
public override void _Ready()
{
if (GetParentOrNull<Sprite2D>() is not { } parentSprite)
{
GD.PushError("SpriteTrail was not child of a Sprite2D");
return;
}
_parentSprite = parentSprite;
_timer = new()
{
Autostart = CreateAfterImages,
OneShot = false,
WaitTime = Interval,
IgnoreTimeScale = false,
};
_timer.Timeout += CreateTrailSprite;
AddChild(_timer, @internal: InternalMode.Front);
}
private void CreateTrailSprite()
{
if (_parentSprite == null) return;
var sprite = new Sprite2D
{
TopLevel = true,
Visible = false,
GlobalTransform = _parentSprite.GlobalTransform,
Texture = _parentSprite.Texture,
Centered = _parentSprite.Centered,
FlipH = _parentSprite.FlipH,
FlipV = _parentSprite.FlipV,
Offset = _parentSprite.Offset,
Hframes = _parentSprite.Hframes,
Vframes = _parentSprite.Vframes,
Frame = _parentSprite.Frame,
RegionEnabled = _parentSprite.RegionEnabled,
RegionRect = _parentSprite.RegionRect,
RegionFilterClipEnabled = _parentSprite.RegionFilterClipEnabled,
ZIndex = 5,
Material = OverrideMaterial?.Duplicate() as Material,
PhysicsInterpolationMode = PhysicsInterpolationModeEnum.Off,
};
AddChild(sprite, @internal: InternalMode.Front);
var tween = sprite.CreateTween();
tween.TweenCallback(Callable.From(() => sprite.Visible = true)).SetDelay(Interval);
tween.TweenProperty(sprite.Material, "shader_parameter/age", 1f, Lifetime).From(0f);
tween.TweenCallback(Callable.From(sprite.QueueFree));
}
}

View file

@ -0,0 +1 @@
uid://djrplcugoka77

View file

@ -4,6 +4,7 @@
[ext_resource type="Texture2D" uid="uid://b3ysuraousdts" path="res://src/player/player.png" id="2_mf2ua"]
[ext_resource type="Script" uid="uid://ce3atuwxo8fvb" path="res://src/combat/Hurtbox.cs" id="4_5f2sf"]
[ext_resource type="Script" uid="uid://b4k42r866lkut" path="res://src/combat/Hitbox.cs" id="4_244u8"]
[ext_resource type="Script" uid="uid://djrplcugoka77" path="res://src/player/SpriteTrail.cs" id="5_5f2sf"]
[sub_resource type="Gradient" id="Gradient_244u8"]
colors = PackedColorArray(1, 1, 1, 1, 0, 0, 0, 1)
@ -14,8 +15,9 @@ fill = 1
fill_from = Vector2(0.50213677, 0.50641024)
fill_to = Vector2(1, 0.5363248)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_jiejy"]
size = Vector2(8, 28)
[sub_resource type="CapsuleShape2D" id="CapsuleShape2D_5f2sf"]
radius = 4.0
height = 28.0
[sub_resource type="RectangleShape2D" id="RectangleShape2D_qi477"]
size = Vector2(4, 24)
@ -26,6 +28,34 @@ size = Vector2(14, 32)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_5f2sf"]
size = Vector2(13.59375, 20)
[sub_resource type="Shader" id="Shader_jiejy"]
code = "shader_type canvas_item;
render_mode blend_mix;
uniform float age;
uniform sampler2D color_ramp : source_color;
void fragment() {
vec4 color = texture(color_ramp, vec2(age, 0.0));
COLOR = vec4(color.rgb, min(COLOR.a, color.a));
}
"
[sub_resource type="Gradient" id="Gradient_5f2sf"]
interpolation_mode = 2
interpolation_color_space = 2
offsets = PackedFloat32Array(0, 0.1344697, 0.25189394, 0.39204547, 0.71022725, 1)
colors = PackedColorArray(1, 1, 1, 1, 1.0438489, 1.0438489, 0, 1, 0.3852666, 0.8676332, 0.49039757, 0.84042877, 0.27789554, 0.6560017, 0.6578235, 0.809, 0.07414777, 0.34175637, 0.5940871, 0.476, 0, 0, 0, 0)
[sub_resource type="GradientTexture1D" id="GradientTexture1D_5f2sf"]
gradient = SubResource("Gradient_5f2sf")
[sub_resource type="ShaderMaterial" id="ShaderMaterial_5f2sf"]
resource_local_to_scene = true
shader = SubResource("Shader_jiejy")
shader_parameter/age = 0.0
shader_parameter/color_ramp = SubResource("GradientTexture1D_5f2sf")
[sub_resource type="RectangleShape2D" id="RectangleShape2D_o822w"]
size = Vector2(8, 12)
@ -93,6 +123,18 @@ tracks/4/keys = {
"update": 1,
"values": [0]
}
tracks/5/type = "value"
tracks/5/imported = false
tracks/5/enabled = true
tracks/5/path = NodePath("Body/SpriteTrail:CreateAfterImages")
tracks/5/interp = 1
tracks/5/loop_wrap = true
tracks/5/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 1,
"values": [false]
}
[sub_resource type="Animation" id="Animation_ylhto"]
resource_name = "air_ascend"
@ -354,7 +396,7 @@ tracks/1/loop_wrap = true
tracks/1/keys = {
"times": PackedFloat32Array(0, 0.45),
"transitions": PackedFloat32Array(1, 1),
"update": 1,
"update": 0,
"values": [false, true]
}
tracks/2/type = "method"
@ -383,7 +425,7 @@ tracks/3/loop_wrap = true
tracks/3/keys = {
"times": PackedFloat32Array(0, 0.050000004, 0.28333336),
"transitions": PackedFloat32Array(1, 1, 1),
"update": 1,
"update": 0,
"values": [true, false, true]
}
tracks/4/type = "value"
@ -395,7 +437,7 @@ tracks/4/loop_wrap = true
tracks/4/keys = {
"times": PackedFloat32Array(0, 0.050000004, 0.20000002, 0.36666667),
"transitions": PackedFloat32Array(1, 1, 1, 1),
"update": 1,
"update": 0,
"values": [true, true, false, true]
}
@ -571,7 +613,7 @@ advance_mode = 2
states/dash/node = SubResource("AnimationNodeBlendSpace2D_244u8")
states/dash/position = Vector2(521.5, 100)
transitions = ["dash", "End", SubResource("AnimationNodeStateMachineTransition_4uy46"), "Start", "dash", SubResource("AnimationNodeStateMachineTransition_244u8")]
graph_offset = Vector2(-36.76097, 0)
graph_offset = Vector2(-33.76097, -41)
[sub_resource type="AnimationNodeAnimation" id="AnimationNodeAnimation_5f2sf"]
animation = &"dive_idle"
@ -653,8 +695,8 @@ input_2/break_loop_at_end = false
input_2/reset = true
[sub_resource type="AnimationNodeBlendTree" id="AnimationNodeBlendTree_2j2mp"]
graph_offset = Vector2(-734.2429, 163.1087)
nodes/output/position = Vector2(160, 180)
graph_offset = Vector2(-787.6652, 26.473724)
nodes/output/position = Vector2(280, 180)
nodes/state/node = SubResource("AnimationNodeTransition_5f2sf")
nodes/state/position = Vector2(-380, 180)
nodes/ground/node = SubResource("AnimationNodeBlendSpace1D_cwolq")
@ -664,13 +706,13 @@ nodes/air/position = Vector2(-600, 240)
nodes/dive/node = SubResource("AnimationNodeBlendSpace2D_rl62b")
nodes/dive/position = Vector2(-600, 440)
nodes/dash/node = SubResource("AnimationNodeOneShot_8ydov")
nodes/dash/position = Vector2(0, 180)
nodes/dash/position = Vector2(100, 180)
"nodes/Animation 3/node" = SubResource("AnimationNodeAnimation_8ydov")
"nodes/Animation 3/position" = Vector2(-340, 460)
nodes/holding_wall/node = SubResource("AnimationNodeBlend2_ylhto")
nodes/holding_wall/position = Vector2(-180, 180)
nodes/dash_sm/node = SubResource("AnimationNodeStateMachine_k60ra")
nodes/dash_sm/position = Vector2(-160, 460)
nodes/dash_sm/position = Vector2(-140, 440)
node_connections = [&"output", 0, &"dash", &"state", 0, &"ground", &"state", 1, &"air", &"state", 2, &"dive", &"dash", 0, &"holding_wall", &"dash", 1, &"dash_sm", &"holding_wall", 0, &"state", &"holding_wall", 1, &"Animation 3"]
[node name="Player" type="CharacterBody2D" unique_id=500011307]
@ -690,8 +732,9 @@ texture = SubResource("GradientTexture2D_2j2mp")
texture_scale = 3.37
[node name="CollisionShape2D" type="CollisionShape2D" parent="." unique_id=84142517]
visible = false
position = Vector2(0, -14)
shape = SubResource("RectangleShape2D_jiejy")
shape = SubResource("CapsuleShape2D_5f2sf")
[node name="Camera2D" type="Camera2D" parent="." unique_id=1975474809]
position = Vector2(0, -16)
@ -704,9 +747,8 @@ current = true
[node name="Body" type="Sprite2D" parent="." unique_id=567060128]
unique_name_in_owner = true
position = Vector2(0, 2)
texture = ExtResource("2_mf2ua")
offset = Vector2(0, -24)
offset = Vector2(0, -22)
hframes = 4
vframes = 12
metadata/_aseprite_wizard_interface_config_ = {
@ -789,6 +831,14 @@ position = Vector2(5, 0)
shape = SubResource("RectangleShape2D_244u8")
debug_color = Color(0.99430805, 0, 0.2314094, 0.41960785)
[node name="SpriteTrail" type="Node2D" parent="Body" unique_id=156229217]
unique_name_in_owner = true
script = ExtResource("5_5f2sf")
Interval = 0.03
Lifetime = 0.2
OverrideMaterial = SubResource("ShaderMaterial_5f2sf")
CreateAfterImages = false
[node name="SubmersedSensor" type="Area2D" parent="." unique_id=1595680866]
unique_name_in_owner = true
visible = false
@ -855,7 +905,7 @@ one_shot = true
[node name="DashTimer" type="Timer" parent="." unique_id=18391348]
unique_name_in_owner = true
process_callback = 0
wait_time = 0.2
wait_time = 0.1
one_shot = true
[node name="DashDirectionChangeTimer" type="Timer" parent="." unique_id=911466358]