Skip to content

Commit

Permalink
Merge pull request #17 from theowiik:instanting-improvements
Browse files Browse the repository at this point in the history
Add a SceneGetter util
  • Loading branch information
theowiik authored Oct 5, 2023
2 parents fbc9b59 + 94f4276 commit bb0892b
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 28 deletions.
4 changes: 4 additions & 0 deletions GodotSharper/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ public static void TimesDo(this int count, Action<int> action)
throw new ArgumentException("Count must be greater than or equal to 0", nameof(count));

for (var i = 0; i < count; i++)
{
action(i);
}
}

/// <summary>
Expand All @@ -30,7 +32,9 @@ public static void TimesDo(this int count, Action action)
throw new ArgumentException("Count must be greater than or equal to 0", nameof(count));

for (var i = 0; i < count; i++)
{
action();
}
}

/// <summary>
Expand Down
22 changes: 0 additions & 22 deletions GodotSharper/Instancing/InstantiableAttribute.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,11 @@ public static T Instantiate<T>()
}
else
{
var attr = (InstantiableAttribute)
Attribute.GetCustomAttribute(type, typeof(InstantiableAttribute));
var attr = (SceneAttribute)Attribute.GetCustomAttribute(type, typeof(SceneAttribute));

if (attr == null)
throw new FileNotFoundException(
"Could not find a PackedSceneAttribute for " + type
throw new ArgumentException(
$"Type {type} does not have a {nameof(SceneAttribute)}"
);

path = attr.Path;
Expand Down
22 changes: 22 additions & 0 deletions GodotSharper/Scenes/SceneAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace GodotSharper.Instancing;

/// <summary>
/// Attribute used to mark a class as a scene and specify its path.
/// </summary>
[AttributeUsage(AttributeTargets.Class)]
public sealed class SceneAttribute : Attribute
{
/// <summary>
/// Initializes a new instance of the <see cref="SceneAttribute" /> class with the specified path.
/// </summary>
/// <param name="path">The path of the scene.</param>
public SceneAttribute(string path)
{
Path = path;
}

/// <summary>
/// Gets the path of the scene.
/// </summary>
public string Path { get; }
}
30 changes: 30 additions & 0 deletions GodotSharper/Scenes/SceneGetter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Reflection;
using Godot;

namespace GodotSharper.Instancing;

/// <summary>
/// A static class that provides a method to get the path of a scene associated with a given type.
/// </summary>
public static class SceneGetter
{
/// <summary>
/// Gets the path of a scene associated with the specified type.
/// </summary>
/// <typeparam name="T">The type of the node associated with the scene.</typeparam>
/// <returns>The path of the scene associated with the specified type.</returns>
/// <exception cref="ArgumentException">Thrown when the specified type does not have a SceneAttribute.</exception>
public static string GetPath<T>()
where T : Node
{
var type = typeof(T);
var attribute = type.GetCustomAttribute<SceneAttribute>();

if (attribute == null)
{
throw new ArgumentException($"Type {type} does not have a {nameof(SceneAttribute)}");
}

return attribute.Path;
}
}
5 changes: 3 additions & 2 deletions GodotSharper/TimerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ public static Timer StartedOneShot(double waitTime, Action onTimeout = null)

return timer;
}

/// <summary>
/// Creates a new Timer object that starts automatically, triggers only once after the specified wait time, and destroys itself after triggering.
/// Creates a new Timer object that starts automatically, triggers only once after the specified wait time, and
/// destroys itself after triggering.
/// </summary>
/// <param name="waitTime">The time to wait before triggering the timer, in seconds.</param>
/// <param name="onTimeout">An optional action to execute when the timer triggers.</param>
Expand Down

0 comments on commit bb0892b

Please sign in to comment.