SupaLidlGame/Extensions/AudioStreamPlayer2D.cs

72 lines
1.8 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;
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)
{
var world = audio.GetTree().Root.GetNode("World/TileMap");
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)
{
var world = audio.GetTree().Root.GetNode("World/TileMap");
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;
}
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
}
}