SupaLidlGame/Extensions/AudioStreamPlayer2D.cs

82 lines
2.0 KiB
C#
Raw Permalink Normal View History

2023-03-19 13:54:48 -07:00
using Godot;
2023-06-13 02:55:30 -07:00
using GodotUtilities;
2023-03-19 23:36:36 -07:00
using System;
2023-03-19 13:54:48 -07:00
2023-06-03 18:21:46 -07:00
namespace SupaLidlGame.Extensions;
public static class AudioStreamPlayer2DExtensions
2023-03-19 13:54:48 -07:00
{
2023-06-03 18:21:46 -07:00
public static AudioStreamPlayer2D Clone(
this AudioStreamPlayer2D audio)
2023-03-19 13:54:48 -07:00
{
2023-06-03 18:21:46 -07:00
var clone = audio.Duplicate() as AudioStreamPlayer2D;
clone.Finished += () =>
2023-03-19 23:36:36 -07:00
{
2023-06-03 18:21:46 -07:00
clone.QueueFree();
};
return clone;
}
2023-03-19 23:36:36 -07:00
2023-06-03 18:21:46 -07:00
public static AudioStreamPlayer2D On(
this AudioStreamPlayer2D audio,
Node parent)
{
var clone = audio.Clone();
parent.AddChild(clone);
clone.GlobalPosition = audio.GlobalPosition;
return clone;
}
2023-03-19 23:36:36 -07:00
2023-06-03 18:21:46 -07:00
public static AudioStreamPlayer2D OnWorld(
this AudioStreamPlayer2D audio)
{
2023-08-03 16:17:34 -07:00
var world = audio.GetWorld().CurrentMap;
2023-06-03 18:21:46 -07:00
if (world is null)
2023-03-19 23:36:36 -07:00
{
2023-06-03 18:21:46 -07:00
throw new NullReferenceException("World does not exist");
2023-03-19 23:36:36 -07:00
}
2023-06-03 18:21:46 -07:00
var clone = audio.On(world);
clone.GlobalPosition = audio.GlobalPosition;
return clone;
}
2023-03-19 23:36:36 -07:00
2023-06-03 18:21:46 -07:00
public static AudioStreamPlayer2D At(
this AudioStreamPlayer2D audio,
Vector2 globalPosition)
{
2023-06-13 02:55:30 -07:00
var world = audio.GetAncestor<Scenes.Map>();
2023-06-03 18:21:46 -07:00
if (world is null)
2023-03-19 23:36:36 -07:00
{
2023-06-03 18:21:46 -07:00
throw new NullReferenceException("World does not exist");
2023-03-19 13:54:48 -07:00
}
2023-06-03 18:21:46 -07:00
var parent = new Node2D();
world.AddChild(parent);
parent.GlobalPosition = globalPosition;
var clone = audio.On(world);
clone.Finished += () =>
2023-03-19 13:54:48 -07:00
{
2023-06-03 18:21:46 -07:00
parent.QueueFree();
};
return clone;
}
2023-06-13 02:55:30 -07:00
public static AudioStreamPlayer2D PlayOneShot(
this AudioStreamPlayer2D audio,
float fromPosition = 0)
{
audio.Finished += () => audio.QueueFree();
audio.Play(fromPosition);
return audio;
}
2023-06-03 18:21:46 -07:00
public static AudioStreamPlayer2D WithPitchDeviation(
this AudioStreamPlayer2D audio,
float deviation)
{
audio.PitchScale = (float)GD.Randfn(audio.PitchScale, deviation);
return audio;
2023-03-19 13:54:48 -07:00
}
}