SupaLidlGame/Extensions/AudioStreamPlayer2D.cs

74 lines
2.1 KiB
C#
Raw Normal View History

2023-03-19 13:54:48 -07:00
using Godot;
2023-03-19 23:36:36 -07:00
using System;
using SupaLidlGame.Utils;
2023-03-19 13:54:48 -07:00
2023-03-19 23:36:36 -07:00
namespace SupaLidlGame.Extensions
2023-03-19 13:54:48 -07:00
{
public static class AudioStreamPlayer2DExtensions
{
2023-03-19 23:36:36 -07:00
public static AudioStreamPlayer2D Clone(
this AudioStreamPlayer2D audio)
{
var clone = audio.Duplicate() as AudioStreamPlayer2D;
clone.Finished += () =>
{
clone.QueueFree();
};
return clone;
}
public static AudioStreamPlayer2D On(
this AudioStreamPlayer2D audio,
Node parent)
{
var clone = audio.Clone();
parent.AddChild(clone);
clone.GlobalPosition = audio.GlobalPosition;
return clone;
}
public static AudioStreamPlayer2D OnWorld(
this AudioStreamPlayer2D audio)
{
var world = audio.GetTree().Root.GetNode("World/TileMap");
if (world is null)
{
throw new NullReferenceException("World does not exist");
}
var clone = audio.On(world);
clone.GlobalPosition = audio.GlobalPosition;
return clone;
}
public static AudioStreamPlayer2D At(
this AudioStreamPlayer2D audio,
Vector2 globalPosition)
{
var world = audio.GetTree().Root.GetNode("World/TileMap");
if (world is null)
{
throw new NullReferenceException("World does not exist");
}
var parent = new Node2D();
world.AddChild(parent);
parent.GlobalPosition = globalPosition;
var clone = audio.On(world);
clone.Finished += () =>
{
parent.QueueFree();
};
return clone;
2023-03-19 13:54:48 -07:00
}
2023-03-19 23:36:36 -07:00
public static AudioStreamPlayer2D WithPitchDeviation(
this AudioStreamPlayer2D audio,
float deviation)
2023-03-19 13:54:48 -07:00
{
2023-03-19 23:36:36 -07:00
audio.PitchScale = (float)GD.Randfn(audio.PitchScale, deviation);
return audio;
2023-03-19 13:54:48 -07:00
}
}
}