Add FootstepGfx and JumpGfxComponent; implement particle effects for footsteps and jumps
This commit is contained in:
55
scripts/components/FootstepGfx.cs
Normal file
55
scripts/components/FootstepGfx.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
using Godot;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.components;
|
||||
|
||||
[GlobalClass]
|
||||
public partial class FootstepGfx : Node2D
|
||||
{
|
||||
[Export] private PackedScene _particles;
|
||||
[Export] private PlayerController _controller;
|
||||
[Export] private Marker2D _marker;
|
||||
|
||||
[ExportGroup("Footstep Settings")]
|
||||
[Export] private double _stepInterval = 0.5; // Time in seconds between steps
|
||||
[Export] private double _stepIntervalRandomness = 0.1; // Randomness factor for step interval
|
||||
[Export] private double _minMoveSpeed = 10.0; // Minimum speed to trigger footsteps
|
||||
[Export] private double _randomOffsetRange = 5.0; // Range for random offset
|
||||
|
||||
private double _timeSinceLastStep = 0.0;
|
||||
|
||||
public override void _PhysicsProcess(double delta)
|
||||
{
|
||||
if (_controller == null || _particles == null) return;
|
||||
|
||||
var canMakeFootstep = _controller.IsOnFloor() && _controller.Velocity.Length() >= _minMoveSpeed;
|
||||
|
||||
if (canMakeFootstep)
|
||||
{
|
||||
_timeSinceLastStep += delta;
|
||||
var randomFactor = GD.RandRange(-_stepIntervalRandomness, _stepIntervalRandomness);
|
||||
var adjustedStepInterval = _stepInterval + randomFactor;
|
||||
if (_timeSinceLastStep >= adjustedStepInterval)
|
||||
{
|
||||
SpawnFootstep();
|
||||
_timeSinceLastStep = 0.0;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_timeSinceLastStep = 0.0;
|
||||
}
|
||||
}
|
||||
|
||||
private void SpawnFootstep()
|
||||
{
|
||||
if (_marker == null) return;
|
||||
var randomOffset = new Vector2(
|
||||
(float)GD.RandRange(-_randomOffsetRange, _randomOffsetRange),
|
||||
(float)GD.RandRange(-_randomOffsetRange, _randomOffsetRange)
|
||||
);
|
||||
var newParticles = _particles.Instantiate<GpuParticles2D>();
|
||||
newParticles.GlobalPosition = _marker.GlobalPosition + randomOffset;
|
||||
GetTree().CurrentScene.AddChild(newParticles);
|
||||
newParticles.Emitting = true;
|
||||
}
|
||||
}
|
1
scripts/components/FootstepGfx.cs.uid
Normal file
1
scripts/components/FootstepGfx.cs.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://d3ksrjt1ek4gi
|
35
scripts/components/JumpGfxComponent.cs
Normal file
35
scripts/components/JumpGfxComponent.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using Godot;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.components;
|
||||
|
||||
[GlobalClass]
|
||||
public partial class JumpGfxComponent : Node2D
|
||||
{
|
||||
[Export] public PackedScene ParticleScene { get; set; }
|
||||
[Export] public PlayerController Controller { get; set; }
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
if (Controller == null)
|
||||
{
|
||||
GD.PrintErr("JumpGfxComponent must have a reference to a PlayerController.");
|
||||
SetProcess(false);
|
||||
return;
|
||||
}
|
||||
|
||||
Controller.JumpInitiated += OnJumpInitiated;
|
||||
}
|
||||
|
||||
private void OnJumpInitiated()
|
||||
{
|
||||
SpawnGfx();
|
||||
}
|
||||
|
||||
private void SpawnGfx()
|
||||
{
|
||||
var particleInstance = ParticleScene.Instantiate<GpuParticles2D>();
|
||||
particleInstance.GlobalPosition = GlobalPosition;
|
||||
GetTree().CurrentScene.AddChild(particleInstance);
|
||||
particleInstance.Emitting = true;
|
||||
}
|
||||
}
|
1
scripts/components/JumpGfxComponent.cs.uid
Normal file
1
scripts/components/JumpGfxComponent.cs.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://bpopfy6m4a0br
|
@@ -11,6 +11,7 @@ public partial class JumpPadComponent : Node
|
||||
[Export] public Sprite2D Sprite { get; set; }
|
||||
[Export] public int StartAnimationIndex { get; set; } = 0;
|
||||
[Export] public float AnimationDuration { get; set; } = 0.5f;
|
||||
[Export] public GpuParticles2D Particles { get; set; }
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
@@ -27,6 +28,7 @@ public partial class JumpPadComponent : Node
|
||||
_ = HandleLaunchPadAnimation();
|
||||
player.Velocity = new Vector2(player.Velocity.X, -JumpForce);
|
||||
player.EmitSignal(PlayerController.SignalName.JumpInitiated);
|
||||
Particles?.Restart();
|
||||
}
|
||||
|
||||
private async Task HandleLaunchPadAnimation()
|
||||
|
Reference in New Issue
Block a user