setup demo level

This commit is contained in:
Lilith Schier 2026-10-14 20:25:51 +02:00
parent e6909833e9
commit 79201c8d6a
15 changed files with 17036 additions and 1408 deletions

View file

@ -1,3 +1,5 @@
using System;
namespace Dancer; namespace Dancer;
public static class ActionName public static class ActionName
@ -11,3 +13,30 @@ public static class ActionName
public static readonly StringName Dash = "dash"; public static readonly StringName Dash = "dash";
public static readonly StringName Parry = "parry"; public static readonly StringName Parry = "parry";
} }
public enum PhysicsLayer
{
Environment = 1,
PlayerHurtbox = 2,
EnemyHurtbox = 3,
PlayerHitbox = 4,
EnemyHitbox = 5,
Water = 6,
Platform = 7,
Player = 8,
Enemy = 9,
}
[Flags]
public enum PhysicsLayerBit
{
Environment = 1 << 0,
PlayerHurtbox = 1 << 1,
EnemyHurtbox = 1 << 2,
PlayerHitbox = 1 << 3,
EnemyHitbox = 1 << 4,
Water = 1 << 5,
Platform = 1 << 6,
Player = 1 << 7,
Enemy = 1 << 8,
}

307
src/Spring.cs Normal file
View file

@ -0,0 +1,307 @@
using System;
using System.Diagnostics.Contracts;
namespace Dancer;
// See: https://theorangeduck.com/page/spring-roll-call
public sealed class Spring {
public float Position { get; set; }
public float Velocity { get; set; }
public float GoalPosition { get; set; }
public float GoalVelocity { get; set; }
public float Stiffness { get; set; } = 10;
public float Damping { get; set; } = 1;
public float Epsilon { get; set; } = 1e-6f;
public float HalfLife {
get => SpringMath.DampingToHalfLife(Damping, Epsilon);
set => Damping = SpringMath.HalfLifeToDamping(value, Epsilon);
}
public float Frequency {
get => SpringMath.StiffnessToFrequency(Stiffness);
set => Stiffness = SpringMath.FrequencyToStiffness(value);
}
/// <summary>
/// Setting this property will set the spring stiffness based on the desired damping ratio.
/// A ratio of 1 results in a critically damped spring, less than 1 makes it under-damped,
/// and more than 1 makes it over-damped.
/// </summary>
public float DampingRatio {
get => SpringMath.DampingRatio(Stiffness, Damping);
set => Stiffness = SpringMath.DampingRatioToStiffness(value, Damping);
}
public void Update(float Δt) =>
(Position, Velocity) = SpringMath.Spring(
Position, Velocity, GoalPosition, GoalVelocity, Stiffness, Damping, Δt, Epsilon
);
}
public sealed class CriticalSpring {
public float Position { get; set; }
public float Velocity { get; set; }
public float GoalPosition { get; set; }
public float GoalVelocity { get; set; }
public float Damping { get; set; } = 1;
public float Epsilon { get; set; } = 1e-6f;
public float Stiffness {
get => SpringMath.CriticalStiffness(Damping);
set => Damping = SpringMath.CriticalDamping(value);
}
public float HalfLife {
get => SpringMath.DampingToHalfLife(Damping, Epsilon);
set => Damping = SpringMath.HalfLifeToDamping(value, Epsilon);
}
public float Frequency {
get => SpringMath.StiffnessToFrequency(Stiffness);
set => Stiffness = SpringMath.FrequencyToStiffness(value);
}
public void Update(float Δt) =>
(Position, Velocity) = SpringMath.CriticalSpring(
Position, Velocity, GoalPosition, GoalVelocity, Damping, Δt
);
}
public sealed class Spring2 {
public Vector2 Position { get; set; }
public Vector2 Velocity { get; set; }
public Vector2 GoalPosition { get; set; }
public Vector2 GoalVelocity { get; set; }
public float Stiffness { get; set; } = 10;
public float Damping { get; set; } = 1;
public float Epsilon { get; set; } = 1e-6f;
public float HalfLife {
get => SpringMath.DampingToHalfLife(Damping, Epsilon);
set => Damping = SpringMath.HalfLifeToDamping(value, Epsilon);
}
public float Frequency {
get => SpringMath.StiffnessToFrequency(Stiffness);
set => Stiffness = SpringMath.FrequencyToStiffness(value);
}
/// <summary>
/// Setting this property will set the spring stiffness based on the desired damping ratio.
/// A ratio of 1 results in a critically damped spring, less than 1 makes it under-damped,
/// and more than 1 makes it over-damped.
/// </summary>
public float DampingRatio {
get => SpringMath.DampingRatio(Stiffness, Damping);
set => Stiffness = SpringMath.DampingRatioToStiffness(value, Damping);
}
public void Update(float Δt) {
var (x, vx) = SpringMath.Spring(
Position.X, Velocity.X, GoalPosition.X, GoalVelocity.X, Stiffness, Damping, Δt, Epsilon
);
var (y, vy) = SpringMath.Spring(
Position.Y, Velocity.Y, GoalPosition.Y, GoalVelocity.Y, Stiffness, Damping, Δt, Epsilon
);
Position = new(x, y);
Velocity = new(vx, vy);
}
}
public sealed class CriticalSpring2 {
public Vector2 Position { get; set; }
public Vector2 Velocity { get; set; }
public Vector2 GoalPosition { get; set; }
public Vector2 GoalVelocity { get; set; }
public float Damping { get; set; } = 1;
public float Epsilon { get; set; } = 1e-6f;
public float Stiffness {
get => SpringMath.CriticalStiffness(Damping);
set => Damping = SpringMath.CriticalDamping(value);
}
public float HalfLife {
get => SpringMath.DampingToHalfLife(Damping, Epsilon);
set => Damping = SpringMath.HalfLifeToDamping(value, Epsilon);
}
public float Frequency {
get => SpringMath.StiffnessToFrequency(Stiffness);
set => Stiffness = SpringMath.FrequencyToStiffness(value);
}
public void Update(float Δt) {
var (x, vx) = SpringMath.CriticalSpring(
Position.X, Velocity.X, GoalPosition.X, GoalVelocity.X, Damping, Δt);
var (y, vy) = SpringMath.CriticalSpring(
Position.Y, Velocity.Y, GoalPosition.Y, GoalVelocity.Y, Damping, Δt);
Position = new(x, y);
Velocity = new(vx, vy);
}
}
public static class SpringMath {
private const float LN_2 = 0.69314718056f;
[Pure]
public static (float position, float velocity) Spring(
float position,
float velocity,
float goalPosition,
float goalVelocity,
float stiffness,
float damping,
float Δt,
float epsilon = 1e-6f
) {
// Constant offset
var c = goalPosition + (damping * goalVelocity / (stiffness + epsilon));
// Exponential coefficient
var y = damping / 2;
// Oscillation frequency
var w = stiffness - (damping.Square() / 4);
// Offset
var o = position - c;
if (MathF.Abs(w) < epsilon) {
// Critically damped
var j1 = velocity + (o * y);
var exponential = (-y * Δt).Exp();
position = (o * exponential) + (Δt * j1 * exponential) + c;
velocity = (-y * o * exponential) - (y * Δt * j1 * exponential) + (j1 * exponential);
}
else if (w > 0) {
// Under-damped
var j = (((velocity + (y * o)).Square() / ((w * w) + epsilon)) + o.Square()).Sqrt();
var p = MathF.Atan((velocity + (y * o)) / ((-o * w) + epsilon));
j = o > 0 ? j : -j;
var exponential = (-y * Δt).Exp();
var cos = j * exponential * MathF.Cos((w * Δt) + p);
var sin = j * exponential * MathF.Sin((w * Δt) + p);
position = cos + c;
velocity = (-y * cos) - (w * sin);
}
else {
// Over-damped
var y0 = (damping + (damping.Square() - (4 * stiffness)).Sqrt()) / 2;
var y1 = (damping - (damping.Square() - (4 * stiffness)).Sqrt()) / 2;
var j1 = ((c * y0) - (position * y0) - velocity) / (y1 - y0);
var j0 = o - j1;
var exponential0 = (-y0 * Δt).Exp();
var exponential1 = (-y1 * Δt).Exp();
position = (j0 * exponential0) + (j1 * exponential1) + c;
velocity = -(y0 * j0 * exponential0) - (y1 * j1 * exponential1);
}
return (position, velocity);
}
[Pure]
public static (float position, float velocity) CriticalSpring(
float position,
float velocity,
float goalPosition,
float goalVelocity,
float damping,
float Δt
) {
var c = goalPosition + (damping * goalVelocity / (damping.Square() / 4));
var y = damping / 2;
var o = position - c;
var j1 = velocity + (o * y);
var exponential = (-y * Δt).Exp();
position = (exponential * (o + (j1 * Δt))) + c;
velocity = exponential * (velocity - (j1 * y * Δt));
return (position, velocity);
}
[Pure]
public static (float position, float velocity) SimpleSpring(
float position,
float velocity,
float goal,
float damping,
float Δt
) {
var y = damping / 2;
var o = position - goal;
var j1 = velocity + (o * y);
var exponential = (-y * Δt).Exp();
position = (exponential * (o + (j1 * Δt))) + goal;
velocity = exponential * (velocity - (j1 * y * Δt));
return (position, velocity);
}
[Pure]
public static (float position, float velocity) SpringDecay(
float position,
float velocity,
float damping,
float Δt
) {
var y = damping / 2;
var j1 = velocity + (position * y);
var exponential = (-y * Δt).Exp();
position = exponential * (position + (j1 * Δt));
velocity = exponential * (velocity - (j1 * y * Δt));
return (position, velocity);
}
[Pure]
public static float HalfLifeToDamping(float halfLife, float epsilon = 1e-6f) =>
4 * LN_2 / (halfLife + epsilon);
[Pure]
public static float DampingToHalfLife(float damping, float epsilon = 1e-6f) =>
4 * LN_2 / (damping + epsilon);
[Pure]
public static float FrequencyToStiffness(float frequency) =>
(MathF.Tau * frequency).Square();
[Pure]
public static float StiffnessToFrequency(float stiffness) =>
stiffness.Sqrt() / MathF.Tau;
[Pure]
public static float CriticalDamping(float stiffness) => (stiffness * 4).Sqrt();
[Pure]
public static float CriticalStiffness(float damping) => damping.Square() / 4;
[Pure]
public static float CriticalHalfLife(float frequency) =>
DampingToHalfLife(CriticalDamping(FrequencyToStiffness(frequency)));
[Pure]
public static float CriticalFrequency(float halfLife) =>
StiffnessToFrequency(CriticalStiffness(HalfLifeToDamping(halfLife)));
[Pure]
public static float DampingRatioToStiffness(float ratio, float damping) =>
(damping / (ratio * 2)).Square();
[Pure]
public static float DampingRatioToDamping(float ratio, float stiffness) =>
ratio * 2 * stiffness.Sqrt();
[Pure]
public static float DampingRatio(float stiffness, float damping) =>
damping / (2 * stiffness.Sqrt());
}
file static class FloatExtensions {
[Pure]
public static float Square(this float x) => x * x;
[Pure]
public static float Sqrt(this float x) => MathF.Sqrt(x);
[Pure]
public static float Exp(this float x) => MathF.Exp(x);
}

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

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

View file

@ -29,6 +29,8 @@ public sealed partial class Hitbox : Area2D
AreaEntered += OnEntered; AreaEntered += OnEntered;
AreaExited += OnExited; AreaExited += OnExited;
SetPhysicsProcess(true);
} }
public void OnResolved() => HitboxOwner.OnHitboxAdded(this); public void OnResolved() => HitboxOwner.OnHitboxAdded(this);
@ -71,14 +73,12 @@ public sealed partial class Hitbox : Area2D
if (Engine.IsEditorHint()) Visible = true; if (Engine.IsEditorHint()) Visible = true;
_immunityEntries.Clear(); _immunityEntries.Clear();
_checkingForHits = true; _checkingForHits = true;
SetPhysicsProcess(true);
} }
public void StopAttack() public void StopAttack()
{ {
if (Engine.IsEditorHint()) Visible = false; if (Engine.IsEditorHint()) Visible = false;
_checkingForHits = false; _checkingForHits = false;
SetPhysicsProcess(false);
} }
private sealed class ImmunityEntry private sealed class ImmunityEntry

View file

@ -10,11 +10,11 @@ public sealed partial class Birb : CharacterBody2D, IAttackable,
{ {
public override void _Notification(int what) => this.Notify(what); public override void _Notification(int what) => this.Notify(what);
[Export] public float Speed { get; set; } = 90f; [Export] public float Speed { get; set; } = 60f;
[Export] public float AttackDistance { get; set; } = 30f; [Export] public float AttackDistance { get; set; } = 15f;
[Export] public float Weight { get; set; } = 20f; [Export] public float Weight { get; set; } = 20f;
[Export] public float MaxHp { get; set; } = 4f; [Export] public float MaxHp { get; set; } = 4f;
[Export] public float Acceleration { get; set; } = 700f; [Export] public float Acceleration { get; set; } = 300f;
[Node] private ProgressBar HealthBar { get; set; } = null!; [Node] private ProgressBar HealthBar { get; set; } = null!;
[Node] private AnimatedSprite2D Body { get; set; } = null!; [Node] private AnimatedSprite2D Body { get; set; } = null!;

BIN
src/creatures/birb/birb.aseprite (Stored with Git LFS)

Binary file not shown.

BIN
src/creatures/birb/birb.png (Stored with Git LFS)

Binary file not shown.

View file

@ -4,39 +4,39 @@
[ext_resource type="Texture2D" uid="uid://drr8qkupay1am" path="res://src/creatures/birb/birb.png" id="2_bu42x"] [ext_resource type="Texture2D" uid="uid://drr8qkupay1am" path="res://src/creatures/birb/birb.png" id="2_bu42x"]
[ext_resource type="Script" uid="uid://b4k42r866lkut" path="res://src/combat/Hitbox.cs" id="3_bu42x"] [ext_resource type="Script" uid="uid://b4k42r866lkut" path="res://src/combat/Hitbox.cs" id="3_bu42x"]
[sub_resource type="AtlasTexture" id="AtlasTexture_bu42x"]
atlas = ExtResource("2_bu42x")
region = Rect2(0, 0, 32, 24)
[sub_resource type="AtlasTexture" id="AtlasTexture_rbw7q"] [sub_resource type="AtlasTexture" id="AtlasTexture_rbw7q"]
atlas = ExtResource("2_bu42x") atlas = ExtResource("2_bu42x")
region = Rect2(0, 0, 64, 48) region = Rect2(32, 0, 32, 24)
[sub_resource type="AtlasTexture" id="AtlasTexture_ha735"] [sub_resource type="AtlasTexture" id="AtlasTexture_ha735"]
atlas = ExtResource("2_bu42x") atlas = ExtResource("2_bu42x")
region = Rect2(0, 48, 64, 48) region = Rect2(64, 0, 32, 24)
[sub_resource type="AtlasTexture" id="AtlasTexture_8ar45"] [sub_resource type="AtlasTexture" id="AtlasTexture_8ar45"]
atlas = ExtResource("2_bu42x") atlas = ExtResource("2_bu42x")
region = Rect2(0, 96, 64, 48) region = Rect2(96, 0, 32, 24)
[sub_resource type="AtlasTexture" id="AtlasTexture_nb8qb"] [sub_resource type="AtlasTexture" id="AtlasTexture_nb8qb"]
atlas = ExtResource("2_bu42x") atlas = ExtResource("2_bu42x")
region = Rect2(64, 96, 64, 48) region = Rect2(128, 0, 32, 24)
[sub_resource type="AtlasTexture" id="AtlasTexture_hb86p"] [sub_resource type="AtlasTexture" id="AtlasTexture_hb86p"]
atlas = ExtResource("2_bu42x") atlas = ExtResource("2_bu42x")
region = Rect2(128, 96, 64, 48) region = Rect2(160, 0, 32, 24)
[sub_resource type="AtlasTexture" id="AtlasTexture_k3bhq"] [sub_resource type="AtlasTexture" id="AtlasTexture_k3bhq"]
atlas = ExtResource("2_bu42x") atlas = ExtResource("2_bu42x")
region = Rect2(192, 96, 64, 48) region = Rect2(192, 0, 32, 24)
[sub_resource type="AtlasTexture" id="AtlasTexture_ovgui"]
atlas = ExtResource("2_bu42x")
region = Rect2(256, 96, 64, 48)
[sub_resource type="SpriteFrames" id="SpriteFrames_580w7"] [sub_resource type="SpriteFrames" id="SpriteFrames_580w7"]
animations = [{ animations = [{
"frames": [{ "frames": [{
"duration": 1.0, "duration": 1.0,
"texture": SubResource("AtlasTexture_rbw7q") "texture": SubResource("AtlasTexture_bu42x")
}], }],
"loop": 1, "loop": 1,
"name": &"idle", "name": &"idle",
@ -44,7 +44,7 @@ animations = [{
}, { }, {
"frames": [{ "frames": [{
"duration": 1.0, "duration": 1.0,
"texture": SubResource("AtlasTexture_ha735") "texture": SubResource("AtlasTexture_rbw7q")
}], }],
"loop": 1, "loop": 1,
"name": &"prepare_attack", "name": &"prepare_attack",
@ -52,6 +52,9 @@ animations = [{
}, { }, {
"frames": [{ "frames": [{
"duration": 1.0, "duration": 1.0,
"texture": SubResource("AtlasTexture_ha735")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_8ar45") "texture": SubResource("AtlasTexture_8ar45")
}, { }, {
"duration": 1.0, "duration": 1.0,
@ -62,9 +65,6 @@ animations = [{
}, { }, {
"duration": 1.0, "duration": 1.0,
"texture": SubResource("AtlasTexture_k3bhq") "texture": SubResource("AtlasTexture_k3bhq")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_ovgui")
}], }],
"loop": 1, "loop": 1,
"name": &"stab", "name": &"stab",
@ -72,10 +72,11 @@ animations = [{
}] }]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_8bh2b"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_8bh2b"]
size = Vector2(34, 2) size = Vector2(20, 1)
[sub_resource type="CapsuleShape2D" id="CapsuleShape2D_feb5d"] [sub_resource type="CapsuleShape2D" id="CapsuleShape2D_feb5d"]
radius = 6.0 radius = 3.0
height = 14.0
[sub_resource type="CircleShape2D" id="CircleShape2D_lyo8d"] [sub_resource type="CircleShape2D" id="CircleShape2D_lyo8d"]
radius = 60.0 radius = 60.0
@ -95,26 +96,26 @@ step = 0.1
tracks/0/type = "value" tracks/0/type = "value"
tracks/0/imported = false tracks/0/imported = false
tracks/0/enabled = true tracks/0/enabled = true
tracks/0/path = NodePath("Body:animation") tracks/0/path = NodePath("Body:frame")
tracks/0/interp = 1 tracks/0/interp = 1
tracks/0/loop_wrap = true tracks/0/loop_wrap = true
tracks/0/keys = { tracks/0/keys = {
"times": PackedFloat32Array(0), "times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1), "transitions": PackedFloat32Array(1),
"update": 1, "update": 0,
"values": [&"idle"] "values": [0]
} }
tracks/1/type = "value" tracks/1/type = "value"
tracks/1/imported = false tracks/1/imported = false
tracks/1/enabled = true tracks/1/enabled = true
tracks/1/path = NodePath("Body:frame") tracks/1/path = NodePath("Body:animation")
tracks/1/interp = 1 tracks/1/interp = 1
tracks/1/loop_wrap = true tracks/1/loop_wrap = true
tracks/1/keys = { tracks/1/keys = {
"times": PackedFloat32Array(0), "times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1), "transitions": PackedFloat32Array(1),
"update": 1, "update": 0,
"values": [0] "values": [&"idle"]
} }
[sub_resource type="Animation" id="Animation_ba6cv"] [sub_resource type="Animation" id="Animation_ba6cv"]
@ -124,26 +125,26 @@ step = 0.1
tracks/0/type = "value" tracks/0/type = "value"
tracks/0/imported = false tracks/0/imported = false
tracks/0/enabled = true tracks/0/enabled = true
tracks/0/path = NodePath("Body:animation") tracks/0/path = NodePath("Body:frame")
tracks/0/interp = 1 tracks/0/interp = 1
tracks/0/loop_wrap = true tracks/0/loop_wrap = true
tracks/0/keys = { tracks/0/keys = {
"times": PackedFloat32Array(0), "times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1), "transitions": PackedFloat32Array(1),
"update": 1, "update": 0,
"values": [&"idle"] "values": [0]
} }
tracks/1/type = "value" tracks/1/type = "value"
tracks/1/imported = false tracks/1/imported = false
tracks/1/enabled = true tracks/1/enabled = true
tracks/1/path = NodePath("Body:frame") tracks/1/path = NodePath("Body:animation")
tracks/1/interp = 1 tracks/1/interp = 1
tracks/1/loop_wrap = true tracks/1/loop_wrap = true
tracks/1/keys = { tracks/1/keys = {
"times": PackedFloat32Array(0), "times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1), "transitions": PackedFloat32Array(1),
"update": 1, "update": 0,
"values": [0] "values": [&"idle"]
} }
[sub_resource type="Animation" id="Animation_1i6dk"] [sub_resource type="Animation" id="Animation_1i6dk"]
@ -153,78 +154,53 @@ step = 0.1
tracks/0/type = "value" tracks/0/type = "value"
tracks/0/imported = false tracks/0/imported = false
tracks/0/enabled = true tracks/0/enabled = true
tracks/0/path = NodePath("Body:animation") tracks/0/path = NodePath("Body:frame")
tracks/0/interp = 1 tracks/0/interp = 1
tracks/0/loop_wrap = true tracks/0/loop_wrap = true
tracks/0/keys = { tracks/0/keys = {
"times": PackedFloat32Array(0), "times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1), "transitions": PackedFloat32Array(1),
"update": 1, "update": 0,
"values": [&"prepare_attack"] "values": [0]
} }
tracks/1/type = "value" tracks/1/type = "value"
tracks/1/imported = false tracks/1/imported = false
tracks/1/enabled = true tracks/1/enabled = true
tracks/1/path = NodePath("Body:frame") tracks/1/path = NodePath("Body:animation")
tracks/1/interp = 1 tracks/1/interp = 1
tracks/1/loop_wrap = true tracks/1/loop_wrap = true
tracks/1/keys = { tracks/1/keys = {
"times": PackedFloat32Array(0), "times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1), "transitions": PackedFloat32Array(1),
"update": 1, "update": 0,
"values": [0] "values": [&"prepare_attack"]
} }
[sub_resource type="Animation" id="Animation_3ly8m"] [sub_resource type="Animation" id="Animation_3ly8m"]
resource_name = "stab" resource_name = "stab"
length = 1.0833334 length = 1.0833334
step = 0.016666668 step = 0.016666668
capture_included = true
tracks/0/type = "value" tracks/0/type = "value"
tracks/0/imported = false tracks/0/imported = false
tracks/0/enabled = true tracks/0/enabled = true
tracks/0/path = NodePath("Body:animation") tracks/0/path = NodePath("Body:frame")
tracks/0/interp = 1 tracks/0/interp = 0
tracks/0/loop_wrap = true tracks/0/loop_wrap = true
tracks/0/keys = { tracks/0/keys = {
"times": PackedFloat32Array(0), "times": PackedFloat32Array(0, 0.1, 0.75000006, 0.8000001, 0.8833334),
"transitions": PackedFloat32Array(1), "transitions": PackedFloat32Array(1, 1, 1, 1, 1),
"update": 1, "update": 2,
"values": [&"stab"] "values": [0, 1, 2, 3, 4]
} }
tracks/1/type = "value" tracks/1/type = "method"
tracks/1/imported = false tracks/1/imported = false
tracks/1/enabled = true tracks/1/enabled = true
tracks/1/path = NodePath("Body:frame") tracks/1/path = NodePath("Body/StabHitbox")
tracks/1/interp = 1 tracks/1/interp = 1
tracks/1/loop_wrap = true tracks/1/loop_wrap = true
tracks/1/keys = { tracks/1/keys = {
"times": PackedFloat32Array(0, 0.1, 0.75000006, 0.8000001, 0.8833334), "times": PackedFloat32Array(0.75000006, 0.8),
"transitions": PackedFloat32Array(1, 1, 1, 1, 1),
"update": 1,
"values": [0, 1, 2, 3, 4]
}
tracks/2/type = "method"
tracks/2/imported = false
tracks/2/enabled = true
tracks/2/path = NodePath(".")
tracks/2/interp = 1
tracks/2/loop_wrap = true
tracks/2/keys = {
"times": PackedFloat32Array(1.0833334),
"transitions": PackedFloat32Array(1),
"values": [{
"args": [],
"method": &"OnAttackFinished"
}]
}
tracks/3/type = "method"
tracks/3/imported = false
tracks/3/enabled = true
tracks/3/path = NodePath("Body/StabHitbox")
tracks/3/interp = 1
tracks/3/loop_wrap = true
tracks/3/keys = {
"times": PackedFloat32Array(0.75000006, 0.8833334),
"transitions": PackedFloat32Array(1, 1), "transitions": PackedFloat32Array(1, 1),
"values": [{ "values": [{
"args": [], "args": [],
@ -234,6 +210,32 @@ tracks/3/keys = {
"method": &"StopAttack" "method": &"StopAttack"
}] }]
} }
tracks/2/type = "value"
tracks/2/imported = false
tracks/2/enabled = true
tracks/2/path = NodePath("Body:animation")
tracks/2/interp = 0
tracks/2/loop_wrap = true
tracks/2/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 2,
"values": [&"stab"]
}
tracks/3/type = "method"
tracks/3/imported = false
tracks/3/enabled = true
tracks/3/path = NodePath(".")
tracks/3/interp = 1
tracks/3/loop_wrap = true
tracks/3/keys = {
"times": PackedFloat32Array(1.0833334),
"transitions": PackedFloat32Array(1),
"values": [{
"args": [],
"method": &"OnAttackFinished"
}]
}
[sub_resource type="AnimationLibrary" id="AnimationLibrary_1i6dk"] [sub_resource type="AnimationLibrary" id="AnimationLibrary_1i6dk"]
_data = { _data = {
@ -251,7 +253,7 @@ metadata/_edit_group_ = true
[node name="Body" type="AnimatedSprite2D" parent="." unique_id=581143480] [node name="Body" type="AnimatedSprite2D" parent="." unique_id=581143480]
unique_name_in_owner = true unique_name_in_owner = true
position = Vector2(7, -20) position = Vector2(0, -8)
sprite_frames = SubResource("SpriteFrames_580w7") sprite_frames = SubResource("SpriteFrames_580w7")
animation = &"idle" animation = &"idle"
offset = Vector2(9, -4) offset = Vector2(9, -4)
@ -264,11 +266,12 @@ collision_mask = 2
script = ExtResource("3_bu42x") script = ExtResource("3_bu42x")
[node name="CollisionShape2D" type="CollisionShape2D" parent="Body/StabHitbox" unique_id=1331922714] [node name="CollisionShape2D" type="CollisionShape2D" parent="Body/StabHitbox" unique_id=1331922714]
position = Vector2(-7, -7)
shape = SubResource("RectangleShape2D_8bh2b") shape = SubResource("RectangleShape2D_8bh2b")
debug_color = Color(0.99607843, 0, 0.23137255, 0.41960785) debug_color = Color(0.99607843, 0, 0.23137255, 0.41960785)
[node name="CollisionShape2D" type="CollisionShape2D" parent="." unique_id=1744954023] [node name="CollisionShape2D" type="CollisionShape2D" parent="." unique_id=1744954023]
position = Vector2(0, -15) position = Vector2(0, -7)
shape = SubResource("CapsuleShape2D_feb5d") shape = SubResource("CapsuleShape2D_feb5d")
[node name="Detection" type="Area2D" parent="." unique_id=151830843] [node name="Detection" type="Area2D" parent="." unique_id=151830843]

View file

@ -4,6 +4,7 @@
[ext_resource type="PackedScene" uid="uid://cgkcjnr8svcs2" path="res://src/level/world.ldtk" id="2_cdx1q"] [ext_resource type="PackedScene" uid="uid://cgkcjnr8svcs2" path="res://src/level/world.ldtk" id="2_cdx1q"]
[ext_resource type="PackedScene" uid="uid://ce2k04khaub8n" path="res://src/player/player.tscn" id="2_feb5d"] [ext_resource type="PackedScene" uid="uid://ce2k04khaub8n" path="res://src/player/player.tscn" id="2_feb5d"]
[ext_resource type="Theme" uid="uid://bybov1cpwtf7p" path="res://src/base_theme.tres" id="5_bs6l1"] [ext_resource type="Theme" uid="uid://bybov1cpwtf7p" path="res://src/base_theme.tres" id="5_bs6l1"]
[ext_resource type="PackedScene" uid="uid://bosbrhc3oprau" path="res://src/creatures/birb/birb.tscn" id="9_4bcji"]
[ext_resource type="Script" uid="uid://cwwftbimomctp" path="res://src/level/FollowRootCamera.cs" id="9_qiocs"] [ext_resource type="Script" uid="uid://cwwftbimomctp" path="res://src/level/FollowRootCamera.cs" id="9_qiocs"]
[ext_resource type="Script" uid="uid://duwojyl38rkj0" path="res://src/level/LayeredSubViewport.cs" id="11_qiocs"] [ext_resource type="Script" uid="uid://duwojyl38rkj0" path="res://src/level/LayeredSubViewport.cs" id="11_qiocs"]
[ext_resource type="Shader" uid="uid://cvuaiyxntwd88" path="res://src/foreground_outline.gdshader" id="11_xdrfk"] [ext_resource type="Shader" uid="uid://cvuaiyxntwd88" path="res://src/foreground_outline.gdshader" id="11_xdrfk"]
@ -23,7 +24,7 @@ z_index = 10
z_as_relative = false z_as_relative = false
[node name="Player" parent="Entities" unique_id=500011307 instance=ExtResource("2_feb5d")] [node name="Player" parent="Entities" unique_id=500011307 instance=ExtResource("2_feb5d")]
position = Vector2(168, 392) position = Vector2(312, 520)
[node name="Static" type="CanvasLayer" parent="." unique_id=884024747] [node name="Static" type="CanvasLayer" parent="." unique_id=884024747]
layer = 20 layer = 20
@ -96,3 +97,6 @@ Player = NodePath("../../../Entities/Player")
metadata/_edit_use_anchors_ = true metadata/_edit_use_anchors_ = true
[node name="world" parent="." unique_id=289922319 instance=ExtResource("2_cdx1q")] [node name="world" parent="." unique_id=289922319 instance=ExtResource("2_cdx1q")]
[node name="Birb" parent="." unique_id=428810294 instance=ExtResource("9_4bcji")]
position = Vector2(376, 536)

Binary file not shown.

Binary file not shown.

File diff suppressed because it is too large Load diff

View file

@ -71,10 +71,10 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
/// The direction the player is currently holding. /// The direction the player is currently holding.
/// This vector's components should always be -1/0/1. /// This vector's components should always be -1/0/1.
private Vector2 _currentDirection = Vector2.Zero; public Vector2 CurrentDirection = Vector2.Zero;
/// The raw direction the player is currently holding, no guarantees. /// The raw direction the player is currently holding, no guarantees.
private Vector2 _currentRawDirection = Vector2.Zero; public Vector2 CurrentRawDirection = Vector2.Zero;
public bool IsFacingRight { get; private set; } = true; public bool IsFacingRight { get; private set; } = true;
public List<BufferedInput> InputBuffer { get; } = []; public List<BufferedInput> InputBuffer { get; } = [];
@ -85,8 +85,8 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
public float DashPoints { get; set; } public float DashPoints { get; set; }
public float BankedDashPoints { get; set; } public float BankedDashPoints { get; set; }
public float StaminaPoints { get; set; } public float StaminaPoints { get; set; }
public Vector2 LastWallJumpNormal;
public Vector2 LastWallJumpNormal { get; set; } public Vector2 LastSafeGroundPosition;
[Node] private AnimationTree AnimationTree { get; set; } = null!; [Node] private AnimationTree AnimationTree { get; set; } = null!;
[Node] private Area2D SubmersedSensor { get; set; } = null!; [Node] private Area2D SubmersedSensor { get; set; } = null!;
@ -106,6 +106,7 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
[Node] private Timer PlatformIgnoreTimer { get; set; } = null!; [Node] private Timer PlatformIgnoreTimer { get; set; } = null!;
[Node] private Timer ParryTimer { get; set; } = null!; [Node] private Timer ParryTimer { get; set; } = null!;
[Node] private SpriteTrail SpriteTrail { get; set; } = null!; [Node] private SpriteTrail SpriteTrail { get; set; } = null!;
[Node] private Node SafeGroundCheckers { get; set; } = null!;
public void OnReady() public void OnReady()
{ {
@ -120,11 +121,12 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
DashTimer.Timeout += () => DashTimer.Timeout += () =>
{ {
Velocity = Velocity.LimitLength(WaterMaxSpeed); Velocity = Velocity.LimitLength(WaterMaxSpeed);
var dot = _currentDirection.Normalized().Dot(Velocity.Normalized()); var dot = CurrentDirection.Normalized().Dot(Velocity.Normalized());
var slowdown = Mathf.Clamp(-dot + 0.2f, 0.0f, 0.5f); var slowdown = Mathf.Clamp(-dot + 0.2f, 0.0f, 0.5f);
Velocity *= 1f - slowdown; Velocity *= 1f - slowdown;
DashHitbox.StopAttack(); DashHitbox.StopAttack();
}; };
LastSafeGroundPosition = GlobalPosition;
} }
public override void _UnhandledInput(InputEvent @event) public override void _UnhandledInput(InputEvent @event)
@ -138,19 +140,19 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
|| @event.IsAction(ActionName.MoveUp) || @event.IsAction(ActionName.MoveUp)
|| @event.IsAction(ActionName.MoveDown)) || @event.IsAction(ActionName.MoveDown))
{ {
_currentRawDirection = Input.GetVector( CurrentRawDirection = Input.GetVector(
ActionName.MoveLeft, ActionName.MoveLeft,
ActionName.MoveRight, ActionName.MoveRight,
ActionName.MoveUp, ActionName.MoveUp,
ActionName.MoveDown); ActionName.MoveDown);
_currentDirection = To8DirectionVector(_currentRawDirection); CurrentDirection = To8DirectionVector(CurrentRawDirection);
} }
if (Action.TryFromEvent(@event, out var action)) if (Action.TryFromEvent(@event, out var action))
{ {
InputBuffer.Add(new() InputBuffer.Add(new()
{ {
Direction = _currentDirection, Direction = CurrentDirection,
Action = action, Action = action,
IsPressed = @event.IsPressed(), IsPressed = @event.IsPressed(),
Time = Time.GetTicksUsec(), Time = Time.GetTicksUsec(),
@ -177,9 +179,9 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
{ {
if (!DashDirectionChangeTimer.IsStopped()) if (!DashDirectionChangeTimer.IsStopped())
{ {
DashDirection = _currentDirection.IsZeroApprox() DashDirection = CurrentDirection.IsZeroApprox()
? FacingVector ? FacingVector
: _currentDirection.Normalized(); : CurrentDirection.Normalized();
} }
velocity = DashDirection * DashSpeed; velocity = DashDirection * DashSpeed;
@ -188,7 +190,7 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
else if (IsInWater) else if (IsInWater)
{ {
var velocityWasTooLarge = velocity.Length() > WaterMaxSpeed; var velocityWasTooLarge = velocity.Length() > WaterMaxSpeed;
var inputDirection = _currentDirection; var inputDirection = CurrentDirection;
if (Input.IsActionPressed(ActionName.Jump)) if (Input.IsActionPressed(ActionName.Jump))
{ {
inputDirection.Y = -1f; inputDirection.Y = -1f;
@ -214,7 +216,7 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
// Walking on the floor // Walking on the floor
else if (IsOnFloor()) else if (IsOnFloor())
{ {
var walkInput = _currentDirection.X; var walkInput = CurrentDirection.X;
if (Mathf.Sign(velocity.X) != Mathf.Sign(walkInput)) if (Mathf.Sign(velocity.X) != Mathf.Sign(walkInput))
{ {
velocity.X *= Mathf.Pow(GroundedSpeedStopDecay, deltaF); velocity.X *= Mathf.Pow(GroundedSpeedStopDecay, deltaF);
@ -238,9 +240,9 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
{ {
var velocityWasTooLarge = Mathf.Abs(velocity.X) > AirMaxSpeed; var velocityWasTooLarge = Mathf.Abs(velocity.X) > AirMaxSpeed;
if (!velocityWasTooLarge || Mathf.Sign(_currentDirection.X) != Mathf.Sign(velocity.X)) if (!velocityWasTooLarge || Mathf.Sign(CurrentDirection.X) != Mathf.Sign(velocity.X))
{ {
velocity.X += Mathf.Sign(_currentDirection.X) * AirAccel * deltaF; velocity.X += Mathf.Sign(CurrentDirection.X) * AirAccel * deltaF;
} }
var velocityIsTooLarge = Mathf.Abs(velocity.X) > AirMaxSpeed; var velocityIsTooLarge = Mathf.Abs(velocity.X) > AirMaxSpeed;
@ -249,7 +251,7 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
velocity.X = Mathf.Clamp(velocity.X, -AirMaxSpeed, AirMaxSpeed); velocity.X = Mathf.Clamp(velocity.X, -AirMaxSpeed, AirMaxSpeed);
} }
dragAmount = _currentDirection.Normalized().Dot(Velocity.Normalized()) < 0.1f dragAmount = CurrentDirection.Normalized().Dot(Velocity.Normalized()) < 0.1f
? 0.015f ? 0.015f
: 0.002f; : 0.002f;
} }
@ -264,8 +266,8 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
if (!WallClimbDirectionChangeTimer.IsStopped()) if (!WallClimbDirectionChangeTimer.IsStopped())
{ {
if (IsWallClimbing if (IsWallClimbing
&& (_currentDirection.Y >= 0f && (CurrentDirection.Y >= 0f
|| Mathf.Sign(_currentDirection.X) == Mathf.Sign(LastWallJumpNormal.X))) || Mathf.Sign(CurrentDirection.X) == Mathf.Sign(LastWallJumpNormal.X)))
{ {
GD.Print("climb to bounce change!"); GD.Print("climb to bounce change!");
// Turn the climb to a bounce and refund the stamina // Turn the climb to a bounce and refund the stamina
@ -293,10 +295,35 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
IsFacingRight = Mathf.Sign(LastWallJumpNormal.X) > 0; IsFacingRight = Mathf.Sign(LastWallJumpNormal.X) > 0;
} }
var collideWithPlatforms = PlatformIgnoreTimer.IsStopped(); var collideWithPlatforms = PlatformIgnoreTimer.IsStopped();
// layer 7 is platforms SetCollisionMaskValue((int)PhysicsLayer.Platform, collideWithPlatforms);
SetCollisionMaskValue(7, collideWithPlatforms);
if (IsOnFloor())
{
var allChecksPassed = true;
foreach (var child in SafeGroundCheckers.GetChildren())
{
if (child is not RayCast2D raycast)
{
GD.PushError(
$"Node {SafeGroundCheckers.Name} contains a child that was not a RayCast2D.");
continue;
}
if (!raycast.IsColliding()
|| CheckIfCollisionIsHazard(raycast.GetCollider(), raycast.GetColliderRid())
is not null)
{
allChecksPassed = false;
break;
}
}
if (allChecksPassed)
{
LastSafeGroundPosition = GlobalPosition;
}
}
Velocity = velocity; Velocity = velocity;
MoveAndSlide(); MoveAndSlide();
@ -306,24 +333,11 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
for (var i = 0; i < count; i++) for (var i = 0; i < count; i++)
{ {
var collision = GetSlideCollision(i); var collision = GetSlideCollision(i);
if (CheckIfCollisionIsHazard(collision.GetCollider(), collision.GetColliderRid()) is not
if (collision.GetCollider() is not TileMapLayer tileMapLayer) { } hazardData)
continue; continue;
TakeDamage(hazardData.Damage);
// This is legal because spikes and other options with attached tile data ResetToLastSafePosition();
// live on a layer without physics chunking.
var colliderRid = collision.GetColliderRid();
var cellCoords = tileMapLayer.GetCoordsForBodyRid(colliderRid);
if (tileMapLayer.GetCellTileData(cellCoords) is not { } tileData)
continue;
if (!tileData.HasCustomData("damage")
|| tileData.GetCustomData("damage") is not { VariantType: Variant.Type.Float } variant)
continue;
if (variant.AsSingle() is > 0f and var damage)
TakeDamage(damage);
} }
} }
@ -353,6 +367,40 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
} }
} }
private void ResetToLastSafePosition()
{
GlobalPosition = LastSafeGroundPosition;
Velocity = Vector2.Zero;
var tween = CreateTween();
tween.SetLoops(3);
tween.TweenProperty(this, "modulate", new Color(.5f, .5f, .5f), 0.02);
tween.TweenInterval(0.1);
tween.TweenProperty(this, "modulate", new Color(1f, 1f, 1f), 0.02);
tween.TweenInterval(0.1);
}
private record struct HazardData(float Damage);
private HazardData? CheckIfCollisionIsHazard(GodotObject collider, Rid colliderRid)
{
if (collider is not TileMapLayer tileMapLayer)
return null;
var cellCoords = tileMapLayer.GetCoordsForBodyRid(colliderRid);
if (tileMapLayer.GetCellTileData(cellCoords) is not { } tileData)
return null;
if (!tileData.HasCustomData("damage")
|| tileData.GetCustomData("damage") is not { VariantType: Variant.Type.Float } variant)
return null;
if (variant.AsSingle() is not (> 0f and var damage))
return null;
return new HazardData(damage);
}
public void OnEnteredWater(Node2D body) public void OnEnteredWater(Node2D body)
{ {
IsInWater = true; IsInWater = true;
@ -367,9 +415,9 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
private void AdjustFacing() private void AdjustFacing()
{ {
if (_currentDirection.X < 0) if (CurrentDirection.X < 0)
IsFacingRight = false; IsFacingRight = false;
else if (_currentDirection.X > 0) else if (CurrentDirection.X > 0)
IsFacingRight = true; IsFacingRight = true;
} }
@ -383,10 +431,10 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
return !IsOnFloor() return !IsOnFloor()
&& IsOnWall() && IsOnWall()
// player holding into the wall // player holding into the wall
&& Mathf.Sign(wallNormal.X) != Mathf.Sign(_currentDirection.X) && Mathf.Sign(wallNormal.X) != Mathf.Sign(CurrentDirection.X)
// wall isn't too bad of an overhang // wall isn't too bad of an overhang
&& wallNormal.Y <= 0.05f && wallNormal.Y <= 0.05f
&& _currentDirection.X != 0; && CurrentDirection.X != 0;
} }
} }
@ -575,9 +623,9 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
GD.Load<PackedScene>("res://src/player/attack_placeholder.tscn"); GD.Load<PackedScene>("res://src/player/attack_placeholder.tscn");
var attackPlaceholder = attackPlaceholderScene.Instantiate<Node2D>(); var attackPlaceholder = attackPlaceholderScene.Instantiate<Node2D>();
attackPlaceholder.Position = HandAttachment.GlobalPosition - GlobalPosition; attackPlaceholder.Position = HandAttachment.GlobalPosition - GlobalPosition;
var targetDir = _currentDirection.IsZeroApprox() var targetDir = CurrentDirection.IsZeroApprox()
? FacingVector ? FacingVector
: _currentDirection; : CurrentDirection;
attackPlaceholder.Rotation = Vector2.Up.AngleTo(targetDir); attackPlaceholder.Rotation = Vector2.Up.AngleTo(targetDir);
AddChild(attackPlaceholder); AddChild(attackPlaceholder);
CreateTween().TweenCallback(Callable.From(attackPlaceholder.QueueFree)).SetDelay(0.2f); CreateTween().TweenCallback(Callable.From(attackPlaceholder.QueueFree)).SetDelay(0.2f);
@ -597,9 +645,9 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
DashTimer.Start(); DashTimer.Start();
DashDirectionChangeTimer.Start(); DashDirectionChangeTimer.Start();
DashCooldownTimer.Start(); DashCooldownTimer.Start();
DashDirection = (_currentDirection.IsZeroApprox() DashDirection = (CurrentDirection.IsZeroApprox()
? FacingVector ? FacingVector
: _currentDirection : CurrentDirection
).Normalized(); ).Normalized();
DashHitbox.StartAttack(); DashHitbox.StartAttack();
AnimationTree.Set("parameters/dash/request", (int)AnimationNodeOneShot.OneShotRequest.Fire); AnimationTree.Set("parameters/dash/request", (int)AnimationNodeOneShot.OneShotRequest.Fire);

View file

@ -834,12 +834,27 @@ OverrideMaterial = SubResource("ShaderMaterial_5f2sf")
CreateAfterImages = false CreateAfterImages = false
[node name="CollisionShape2D" type="CollisionShape2D" parent="." unique_id=84142517] [node name="CollisionShape2D" type="CollisionShape2D" parent="." unique_id=84142517]
visible = false
position = Vector2(0, -7) position = Vector2(0, -7)
shape = SubResource("RectangleShape2D_crv7r") shape = SubResource("RectangleShape2D_crv7r")
[node name="SafeGroundCheckers" type="Node2D" parent="." unique_id=239517375]
unique_name_in_owner = true
[node name="RayCast2D" type="RayCast2D" parent="SafeGroundCheckers" unique_id=249765984]
position = Vector2(-6, -1)
target_position = Vector2(0, 2)
hit_from_inside = true
collide_with_areas = true
[node name="RayCast2D2" type="RayCast2D" parent="SafeGroundCheckers" unique_id=1995168452]
position = Vector2(6, -1)
target_position = Vector2(0, 2)
hit_from_inside = true
collide_with_areas = true
[node name="SubmersedSensor" type="Area2D" parent="." unique_id=1595680866] [node name="SubmersedSensor" type="Area2D" parent="." unique_id=1595680866]
unique_name_in_owner = true unique_name_in_owner = true
visible = false
collision_layer = 0 collision_layer = 0
collision_mask = 32 collision_mask = 32