diff --git a/CHANGELOG.md b/CHANGELOG.md index 74873e1..532e168 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,15 @@ +# [1.7.0] (29/09/2024) +## Added +New `RunTime` structure +### Details +The `RunTime` structure has been added to provide runtime values ​​and functions. + +## Changed +Changes in `RunTimeInitialization`. +### Details +Form changes to the `RunTimeInitialization` class such as placing the `PriorityList` structure outside the `RunTimeInitialization` class, allowing `RunTimeInitialization` to accept negative values ​​as priority. \ +The lower the priority value, the higher its execution level will be. + # [1.6.0] (26/09/2024) ## Added New structures and methods. diff --git a/GodotEngine.Utility/Runtime/Priority.cs b/GodotEngine.Utility/Runtime/Priority.cs index 4dbb440..8cc0008 100644 --- a/GodotEngine.Utility/Runtime/Priority.cs +++ b/GodotEngine.Utility/Runtime/Priority.cs @@ -1,6 +1,9 @@ namespace Cobilas.GodotEngine.Utility.Runtime; -public enum Priority { - StartBefore, - StartLater +/// Indicates the boot priority. +public enum Priority : byte { + /// Starts before everyone else. + StartBefore = 0, + /// Starts after everyone else. + StartLater = 1 } \ No newline at end of file diff --git a/GodotEngine.Utility/Runtime/PriorityList.cs b/GodotEngine.Utility/Runtime/PriorityList.cs new file mode 100644 index 0000000..70828f0 --- /dev/null +++ b/GodotEngine.Utility/Runtime/PriorityList.cs @@ -0,0 +1,58 @@ +using Godot; +using System; +using Cobilas.Collections; +using System.Collections.Generic; + +namespace Cobilas.GodotEngine.Utility.Runtime; + /// Represents a list of priorities. + public struct PriorityList : IDisposable { + private KeyValuePair[] nodes; + + /// Adds items to the priority list. + /// Object execution priority. + /// The object to be added to the list. + /// The method will return a object with its modified priority list. + public PriorityList Add(int priority, Node node) { + nodes ??= Array.Empty>(); + ArrayManipulation.Add(new KeyValuePair(priority, node), ref nodes); + return this; + } + + /// Execute your priority list. + /// The parent node where nodes will be added to start their priority execution. + public readonly void Run(Node root) { + if (!ArrayManipulation.EmpytArray(nodes)) + foreach (KeyValuePair item in nodes) + if (item.Value != null) + root.AddChild(item.Value); + } + + /// Sort the priority list according to the priority of the list items. + public void ReorderList() { + KeyValuePair[] temp = ReorderList(nodes); + ArrayManipulation.ClearArray(ref nodes); + nodes = temp; + } + + /// + public void Dispose() => ArrayManipulation.ClearArraySafe(ref nodes); + + private static KeyValuePair[] ReorderList(KeyValuePair[] list) { + KeyValuePair[] result = Array.Empty>(); + for (long A = 0; A < list.Length; A++) { + if (result.LongLength == 0) { + ArrayManipulation.Add(list[A], ref result); + continue; + } + bool addInResult = true; + for (long B = 0; B < result.LongLength; B++) + if (list[A].Key < result[B].Key) { + ArrayManipulation.Insert(list[A], B, ref result); + addInResult = false; + break; + } + if (addInResult) ArrayManipulation.Add(list[A], ref result); + } + return result; + } + } \ No newline at end of file diff --git a/GodotEngine.Utility/Runtime/RunTime.cs b/GodotEngine.Utility/Runtime/RunTime.cs new file mode 100644 index 0000000..84ecdb6 --- /dev/null +++ b/GodotEngine.Utility/Runtime/RunTime.cs @@ -0,0 +1,12 @@ +using System.Runtime.InteropServices; + +namespace Cobilas.GodotEngine.Utility.Runtime; + +/// Provides RunTime values ​​and functions. +[StructLayout(LayoutKind.Sequential, Size = 1)] +public readonly struct RunTime { + /// The interval in seconds from the last frame to the current one (Read Only). + public const float DeltaTime = .33333333f; + /// The interval in seconds of in-game time at which physics and other fixed frame rate updates are performed. + public const float FixedDeltaTime = .02f; +} \ No newline at end of file diff --git a/GodotEngine.Utility/Runtime/RunTimeInitialization.cs b/GodotEngine.Utility/Runtime/RunTimeInitialization.cs index ff62cf8..4150552 100644 --- a/GodotEngine.Utility/Runtime/RunTimeInitialization.cs +++ b/GodotEngine.Utility/Runtime/RunTimeInitialization.cs @@ -1,15 +1,38 @@ using Godot; using System; -using Cobilas.Collections; using System.Collections.Generic; namespace Cobilas.GodotEngine.Utility.Runtime; +/// Responsible for initializing other classes marked with the attribute. +/// +/// The RunTimeInitialization class allows you to automate the Project>Project Settings>AutoLoad option. +/// To use the RunTimeInitialization class, you must create a class and make it inherit RunTimeInitialization./// +/// +/// using Cobilas.GodotEngine.Utility.Runtime; +/// // The name of the class is up to you. +/// public class RunTimeProcess : RunTimeInitialization {} +/// +/// And remember to add the class that inherits RunTimeInitialization in Project>Project Settings>AutoLoad. +/// Remembering that the RunTimeInitialization class uses the virtual method _Ready() to perform the initialization of other classes. +/// And to initialize other classes along with the RunTimeInitialization class, the class must inherit the Godot.Node class or some class that inherits Godot.Node and use the RunTimeInitializationClassAttribute attribute. +/// +/// using Godot; +/// using Cobilas.GodotEngine.Utility.Runtime; +/// [RunTimeInitializationClass] +/// public class ClassTest : Node {} +/// +/// public class RunTimeInitialization : Node { - public const float DeltaTime = .33333333f; - public const float FixedDeltaTime = .02f; + /// The interval in seconds from the last frame to the current one (Read Only) + [Obsolete("Use RunTime.DeltaTime")] + public const float DeltaTime = RunTime.DeltaTime; + /// The interval in seconds of in-game time at which physics and other fixed frame rate updates are performed. + [Obsolete("Use RunTime.FixedDeltaTime")] + public const float FixedDeltaTime = RunTime.FixedDeltaTime; + /// public override void _Ready() { Type[] components = TypeUtilitarian.GetTypes(); Dictionary pairs = new() { @@ -21,34 +44,18 @@ public override void _Ready() { if (attri != null) { Node node = item.Activator(); if (!string.IsNullOrEmpty(attri.ClassName)) - node.Name = attri.ClassName; + node.Name = string.IsNullOrEmpty(attri.ClassName) ? item.Name : attri.ClassName; pairs[attri.BootPriority] = pairs[attri.BootPriority].Add(attri.SubPriority, node); } } - pairs[Priority.StartBefore].Run(this); - pairs[Priority.StartLater].Run(this); - } - - public struct PriorityList : IDisposable { - private Node[] nodes; - public PriorityList Add(int index, Node node) { - nodes ??= Array.Empty(); - if (index > ArrayManipulation.ArrayLength(nodes)) - ArrayManipulation.Resize(ref nodes, index); - ArrayManipulation.Insert(node, index, ref nodes); - return this; + using (PriorityList list = pairs[Priority.StartBefore]) { + list.ReorderList(); + list.Run(this); } - - public readonly void Run(Node root) { - if (!ArrayManipulation.EmpytArray(nodes)) - foreach (var item in nodes) - if (item != null) - root.AddChild(item); - } - - public void Dispose() { - ArrayManipulation.ClearArraySafe(ref nodes); + using (PriorityList list = pairs[Priority.StartLater]) { + list.ReorderList(); + list.Run(this); } } } \ No newline at end of file diff --git a/GodotEngine.Utility/Runtime/RunTimeInitializationClassAttribute.cs b/GodotEngine.Utility/Runtime/RunTimeInitializationClassAttribute.cs index 2dafbb9..77c5ede 100644 --- a/GodotEngine.Utility/Runtime/RunTimeInitializationClassAttribute.cs +++ b/GodotEngine.Utility/Runtime/RunTimeInitializationClassAttribute.cs @@ -2,25 +2,70 @@ namespace Cobilas.GodotEngine.Utility.Runtime; +/// This attribute marks which classes will be called by . +/// +/// Simple example of class demarcation to be called by . +/// +/// using Godot; +/// using Cobilas.GodotEngine.Utility.Runtime; +/// [RunTimeInitializationClass] +/// public class ClassTest : Node {} +/// +/// [AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)] public sealed class RunTimeInitializationClassAttribute : Attribute { + /// The execution priority. + /// Returns the priority execution level. public int SubPriority { get; private set; } + /// The name of the priority. + /// Returns the name of the priority that will be executed. public string ClassName { get; private set; } + /// The type of priority. + /// Returns the type of priority that will be executed. public Priority BootPriority { get; private set; } + /// Instance the RunTimeInitializationClassAttribute attribute. + /// The type of priority. + /// The name of the priority. + /// The execution priority. public RunTimeInitializationClassAttribute(Priority bootPriority, string? name, int subPriority) { SubPriority = subPriority; ClassName = name!; BootPriority = bootPriority; } + /// Instance the RunTimeInitializationClassAttribute attribute. + /// By default the execution priority is 0. + /// + /// The type of priority. + /// The name of the priority. public RunTimeInitializationClassAttribute(Priority bootPriority, string? name) : this(bootPriority, name, 0) {} + /// Instance the RunTimeInitializationClassAttribute attribute. + /// By default the execution priority is 0. + /// By default the priority name and the class name that the attribute is associated with. + /// + /// The type of priority. public RunTimeInitializationClassAttribute(Priority bootPriority) : this(bootPriority, null) {} + /// Instance the RunTimeInitializationClassAttribute attribute. + /// By default the priority type is . + /// + /// The name of the priority. + /// The execution priority. public RunTimeInitializationClassAttribute(string name, int subPriority) : this(Priority.StartBefore, name, subPriority) {} + /// Instance the RunTimeInitializationClassAttribute attribute. + /// By default the priority type is . + /// By default the execution priority is 0. + /// + /// The name of the priority. public RunTimeInitializationClassAttribute(string name) : this(Priority.StartBefore, name) {} + /// Instance the RunTimeInitializationClassAttribute attribute. + /// By default the priority type is . + /// By default the execution priority is 0. + /// By default the priority name and the class name that the attribute is associated with. + /// public RunTimeInitializationClassAttribute() : this(Priority.StartBefore) {} } \ No newline at end of file diff --git a/README.md b/README.md index d955af0..af7800e 100644 --- a/README.md +++ b/README.md @@ -2,16 +2,16 @@ ### Descripition The package contains utility classes in csharp for godot engine(Godot3.5) ## RunTimeInitialization -(namespace: Cobilas.GodotEngine.Utility.Runtime)
-The `RunTimeInitialization` class allows you to automate the Project>Project Settings>AutoLoad option.
+(namespace: Cobilas.GodotEngine.Utility.Runtime) \ +The `RunTimeInitialization` class allows you to automate the Project>Project Settings>AutoLoad option. \ To use the `RunTimeInitialization` class, you must create a class and make it inherit `RunTimeInitialization`. ```c# using Cobilas.GodotEngine.Utility.Runtime; //The name of the class is up to you. public class RunTimeProcess : RunTimeInitialization {} ``` -And remember to add the class that inherits `RunTimeInitialization` in Project>Project Settings>AutoLoad.
-Remembering that the `RunTimeInitialization` class uses the virtual method `_Ready()` to perform the initialization of other classes.
+And remember to add the class that inherits `RunTimeInitialization` in Project>Project Settings>AutoLoad. \ +Remembering that the `RunTimeInitialization` class uses the virtual method `_Ready()` to perform the initialization of other classes. \ And to initialize other classes along with the `RunTimeInitialization` class, the class must inherit the `Godot.Node` class or some class that inherits `Godot.Node` and use the `RunTimeInitializationClassAttribute` attribute. ```c# using Godot; @@ -38,7 +38,7 @@ subPriority: And the execution priority order. [RunTimeInitializationClass()] ``` ## CoroutineManager -The `CoroutineManager` class is responsible for creating and managing coroutines for godot.
+The `CoroutineManager` class is responsible for creating and managing coroutines for godot. \ How to create a coroutine? ```c# using Godot; @@ -93,10 +93,10 @@ public static void StopAllCoroutines(); To include the package, open the `.csproj` file and add it. ```xml - + ``` Or use command line. ``` -dotnet add package Cobilas.Godot.Utility --version 1.5.3 +dotnet add package Cobilas.Godot.Utility --version 1.7.0 ``` diff --git a/com.cobilas.godot.utility.csproj b/com.cobilas.godot.utility.csproj index 983752f..1bc49ab 100644 --- a/com.cobilas.godot.utility.csproj +++ b/com.cobilas.godot.utility.csproj @@ -10,7 +10,7 @@ BélicusBr Cobilas Godot Utility Cobilas.Godot.Utility - 1.6.0 + 1.7.0 true C:\local.nuget git