Звуковая подсистема, звуки шагов, музыка.
This commit is contained in:
parent
aa948eb252
commit
7ca54bdb38
15 changed files with 272 additions and 35 deletions
86
scripts/AudioCollection.cs
Normal file
86
scripts/AudioCollection.cs
Normal file
|
@ -0,0 +1,86 @@
|
|||
using System;
|
||||
using Godot;
|
||||
|
||||
|
||||
public partial class AudioCollection : Node
|
||||
{
|
||||
private enum State
|
||||
{
|
||||
Error,
|
||||
Ready,
|
||||
Playing,
|
||||
Timeout
|
||||
}
|
||||
|
||||
[Export] public bool Shuffle = false;
|
||||
[Export] public double Timeout = 0;
|
||||
|
||||
private State _state;
|
||||
|
||||
private RandomNumberGenerator _rng = new RandomNumberGenerator();
|
||||
|
||||
private int _count;
|
||||
|
||||
private int _currentChild;
|
||||
|
||||
private double _currentTimeout = 0;
|
||||
|
||||
private AudioStreamPlayer _currentPlayer;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_rng.Randomize();
|
||||
_count = GetChildCount();
|
||||
_currentChild = Shuffle ? _rng.RandiRange(0, _count) : 0;
|
||||
_state = _count == 0 ? State.Error : State.Ready;
|
||||
}
|
||||
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
switch (_state)
|
||||
{
|
||||
case State.Playing:
|
||||
if (!_currentPlayer.IsPlaying())
|
||||
{
|
||||
_state = State.Timeout;
|
||||
}
|
||||
|
||||
break;
|
||||
case State.Timeout:
|
||||
if (_currentTimeout < Timeout)
|
||||
{
|
||||
_currentTimeout += delta;
|
||||
}
|
||||
else
|
||||
{
|
||||
_state = State.Ready;
|
||||
_currentTimeout = 0;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void Play()
|
||||
{
|
||||
switch (_state)
|
||||
{
|
||||
case State.Ready:
|
||||
var player = GetChild<AudioStreamPlayer>(_currentChild);
|
||||
player.Play();
|
||||
_state = State.Playing;
|
||||
_currentChild = Shuffle ? _rng.RandiRange(0, _count) : 0;
|
||||
_currentPlayer = player;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
switch (_state)
|
||||
{
|
||||
case State.Playing:
|
||||
_currentPlayer.Stop();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -69,6 +69,7 @@ public partial class Player : CharacterBody2D
|
|||
protected AnimatedSprite2D Sprite;
|
||||
protected PanelContainer ChatLog;
|
||||
protected Camera2D Camera;
|
||||
protected AudioCollection Footsteps;
|
||||
|
||||
private Vector2 _newPosition;
|
||||
private double _currentTransitionTime = 0;
|
||||
|
@ -82,6 +83,7 @@ public partial class Player : CharacterBody2D
|
|||
Sprite = (AnimatedSprite2D)FindChild("AnimatedSprite2D");
|
||||
ChatLog = (PanelContainer)FindChild("ChatLog");
|
||||
Camera = (Camera2D)FindChild("Camera2D");
|
||||
Footsteps = (AudioCollection)FindChild("Footsteps");
|
||||
}
|
||||
|
||||
public override void _PhysicsProcess(double delta)
|
||||
|
@ -118,6 +120,12 @@ public partial class Player : CharacterBody2D
|
|||
Vector2 direction = Input.GetVector("move_left", "move_right", "move_up", "move_down");
|
||||
if (direction != Vector2.Zero)
|
||||
{
|
||||
Footsteps.Play();
|
||||
// if (!Footsteps.IsPlaying())
|
||||
// {
|
||||
// // Footsteps.PitchScale = (float)GD.RandRange(0.5, 1.0);
|
||||
// Footsteps.Play();
|
||||
// }
|
||||
velocity.X = direction.X * Speed;
|
||||
Sprite.Play("walk");
|
||||
Sprite.FlipH = direction.X switch
|
||||
|
@ -129,6 +137,7 @@ public partial class Player : CharacterBody2D
|
|||
}
|
||||
else
|
||||
{
|
||||
// Footsteps.Stop();
|
||||
velocity.X = Mathf.MoveToward(Velocity.X, 0, Speed);
|
||||
Sprite.Play("default");
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue