-
Notifications
You must be signed in to change notification settings - Fork 2
Added VectorFromValues resolver. #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
142 changes: 142 additions & 0 deletions
142
Forge.Tests/Statescript/Resolvers/VectorFromValuesResolverTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| // Copyright © Gamesmiths Guild. | ||
|
|
||
| using System.Numerics; | ||
| using FluentAssertions; | ||
| using Gamesmiths.Forge.Statescript; | ||
| using Gamesmiths.Forge.Statescript.Properties; | ||
|
|
||
| namespace Gamesmiths.Forge.Tests.Statescript.Resolvers; | ||
|
|
||
| public class VectorFromValuesResolverTests | ||
| { | ||
| [Fact] | ||
| [Trait("Resolver", "VectorFromValues")] | ||
| public void VectorFromValues_resolver_two_components_value_type_is_vector2() | ||
| { | ||
| var resolver = new VectorFromValuesResolver( | ||
| new VariantResolver(new Variant128(1.0f), typeof(float)), | ||
| new VariantResolver(new Variant128(2.0f), typeof(float))); | ||
|
|
||
| resolver.ValueType.Should().Be(typeof(Vector2)); | ||
| } | ||
|
|
||
| [Fact] | ||
| [Trait("Resolver", "VectorFromValues")] | ||
| public void VectorFromValues_resolver_three_components_value_type_is_vector3() | ||
| { | ||
| var resolver = new VectorFromValuesResolver( | ||
| new VariantResolver(new Variant128(1.0f), typeof(float)), | ||
| new VariantResolver(new Variant128(2.0f), typeof(float)), | ||
| new VariantResolver(new Variant128(3.0f), typeof(float))); | ||
|
|
||
| resolver.ValueType.Should().Be(typeof(Vector3)); | ||
| } | ||
|
|
||
| [Fact] | ||
| [Trait("Resolver", "VectorFromValues")] | ||
| public void VectorFromValues_resolver_four_components_value_type_is_vector4() | ||
| { | ||
| var resolver = new VectorFromValuesResolver( | ||
| new VariantResolver(new Variant128(1.0f), typeof(float)), | ||
| new VariantResolver(new Variant128(2.0f), typeof(float)), | ||
| new VariantResolver(new Variant128(3.0f), typeof(float)), | ||
| new VariantResolver(new Variant128(4.0f), typeof(float))); | ||
|
|
||
| resolver.ValueType.Should().Be(typeof(Vector4)); | ||
| } | ||
|
|
||
| [Fact] | ||
| [Trait("Resolver", "VectorFromValues")] | ||
| public void VectorFromValues_resolver_creates_vector2() | ||
| { | ||
| var resolver = new VectorFromValuesResolver( | ||
| new VariantResolver(new Variant128(1.5f), typeof(float)), | ||
| new VariantResolver(new Variant128(-2.0f), typeof(float))); | ||
|
|
||
| resolver.Resolve(new GraphContext()).AsVector2().Should().Be(new Vector2(1.5f, -2.0f)); | ||
| } | ||
|
|
||
| [Fact] | ||
| [Trait("Resolver", "VectorFromValues")] | ||
| public void VectorFromValues_resolver_creates_vector3() | ||
| { | ||
| var resolver = new VectorFromValuesResolver( | ||
| new VariantResolver(new Variant128(1.0f), typeof(float)), | ||
| new VariantResolver(new Variant128(2.0f), typeof(float)), | ||
| new VariantResolver(new Variant128(3.0f), typeof(float))); | ||
|
|
||
| resolver.Resolve(new GraphContext()).AsVector3().Should().Be(new Vector3(1.0f, 2.0f, 3.0f)); | ||
| } | ||
|
|
||
| [Fact] | ||
| [Trait("Resolver", "VectorFromValues")] | ||
| public void VectorFromValues_resolver_creates_vector4() | ||
| { | ||
| var resolver = new VectorFromValuesResolver( | ||
| new VariantResolver(new Variant128(1.0f), typeof(float)), | ||
| new VariantResolver(new Variant128(2.0f), typeof(float)), | ||
| new VariantResolver(new Variant128(3.0f), typeof(float)), | ||
| new VariantResolver(new Variant128(4.0f), typeof(float))); | ||
|
|
||
| resolver.Resolve(new GraphContext()).AsVector4().Should().Be(new Vector4(1.0f, 2.0f, 3.0f, 4.0f)); | ||
| } | ||
|
|
||
| [Fact] | ||
| [Trait("Resolver", "VectorFromValues")] | ||
| public void VectorFromValues_resolver_supports_nested_component_resolvers() | ||
| { | ||
| var resolver = new VectorFromValuesResolver( | ||
| new AddResolver( | ||
| new VariantResolver(new Variant128(1.0f), typeof(float)), | ||
| new VariantResolver(new Variant128(2.0f), typeof(float))), | ||
| new MultiplyResolver( | ||
| new VariantResolver(new Variant128(3.0f), typeof(float)), | ||
| new VariantResolver(new Variant128(4.0f), typeof(float))), | ||
| new NegateResolver( | ||
| new VariantResolver(new Variant128(5.0f), typeof(float)))); | ||
|
|
||
| resolver.Resolve(new GraphContext()).AsVector3().Should().Be(new Vector3(3.0f, 12.0f, -5.0f)); | ||
| } | ||
|
|
||
| [Fact] | ||
| [Trait("Resolver", "VectorFromValues")] | ||
| public void VectorFromValues_resolver_throws_for_non_float_x_component() | ||
| { | ||
| #pragma warning disable CA1806 | ||
| Action act = () => new VectorFromValuesResolver( | ||
| new VariantResolver(new Variant128(1), typeof(int)), | ||
| new VariantResolver(new Variant128(2.0f), typeof(float))); | ||
| #pragma warning restore CA1806 | ||
|
|
||
| act.Should().Throw<ArgumentException>(); | ||
| } | ||
|
|
||
| [Fact] | ||
| [Trait("Resolver", "VectorFromValues")] | ||
| public void VectorFromValues_resolver_throws_for_non_float_z_component() | ||
| { | ||
| #pragma warning disable CA1806 | ||
| Action act = () => new VectorFromValuesResolver( | ||
| new VariantResolver(new Variant128(1.0f), typeof(float)), | ||
| new VariantResolver(new Variant128(2.0f), typeof(float)), | ||
| new VariantResolver(new Variant128(true), typeof(bool))); | ||
| #pragma warning restore CA1806 | ||
|
|
||
| act.Should().Throw<ArgumentException>(); | ||
| } | ||
|
|
||
| [Fact] | ||
| [Trait("Resolver", "VectorFromValues")] | ||
| public void VectorFromValues_resolver_throws_for_non_float_w_component() | ||
| { | ||
| #pragma warning disable CA1806 | ||
| Action act = () => new VectorFromValuesResolver( | ||
| new VariantResolver(new Variant128(1.0f), typeof(float)), | ||
| new VariantResolver(new Variant128(2.0f), typeof(float)), | ||
| new VariantResolver(new Variant128(3.0f), typeof(float)), | ||
| new VariantResolver(new Variant128(4.0), typeof(double))); | ||
| #pragma warning restore CA1806 | ||
|
|
||
| act.Should().Throw<ArgumentException>(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
Forge/Statescript/Properties/VectorFromValuesResolver.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| // Copyright © Gamesmiths Guild. | ||
|
|
||
| using System.Numerics; | ||
|
|
||
| namespace Gamesmiths.Forge.Statescript.Properties; | ||
|
|
||
| /// <summary> | ||
| /// Resolves a vector created from component resolver values. | ||
| /// </summary> | ||
| /// <param name="x">The resolver for the X component.</param> | ||
| /// <param name="y">The resolver for the Y component.</param> | ||
| public class VectorFromValuesResolver(IPropertyResolver x, IPropertyResolver y) : IPropertyResolver | ||
| { | ||
| private readonly IPropertyResolver _x = x; | ||
|
|
||
| private readonly IPropertyResolver _y = y; | ||
|
|
||
| private readonly IPropertyResolver? _z; | ||
|
|
||
| private readonly IPropertyResolver? _w; | ||
|
|
||
| /// <inheritdoc/> | ||
| public Type ValueType { get; } = ValidateValueType(x.ValueType, y.ValueType, null, null); | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="VectorFromValuesResolver"/> class that creates a | ||
| /// <see cref="Vector3"/> from component resolvers. | ||
| /// </summary> | ||
| /// <param name="x">The resolver for the X component.</param> | ||
| /// <param name="y">The resolver for the Y component.</param> | ||
| /// <param name="z">The resolver for the Z component.</param> | ||
| public VectorFromValuesResolver(IPropertyResolver x, IPropertyResolver y, IPropertyResolver z) | ||
| : this(x, y) | ||
| { | ||
| _z = z; | ||
| ValueType = ValidateValueType(x.ValueType, y.ValueType, z.ValueType, null); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="VectorFromValuesResolver"/> class that creates a | ||
| /// <see cref="Vector4"/> from component resolvers. | ||
| /// </summary> | ||
| /// <param name="x">The resolver for the X component.</param> | ||
| /// <param name="y">The resolver for the Y component.</param> | ||
| /// <param name="z">The resolver for the Z component.</param> | ||
| /// <param name="w">The resolver for the W component.</param> | ||
| public VectorFromValuesResolver(IPropertyResolver x, IPropertyResolver y, IPropertyResolver z, IPropertyResolver w) | ||
| : this(x, y, z) | ||
| { | ||
| _w = w; | ||
| ValueType = ValidateValueType(x.ValueType, y.ValueType, z.ValueType, w.ValueType); | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public Variant128 Resolve(GraphContext graphContext) | ||
| { | ||
| float xValue = _x.Resolve(graphContext).AsFloat(); | ||
| float yValue = _y.Resolve(graphContext).AsFloat(); | ||
|
|
||
| if (ValueType == typeof(Vector2)) | ||
| { | ||
| return new Variant128(new Vector2(xValue, yValue)); | ||
| } | ||
|
|
||
| float zValue = _z!.Resolve(graphContext).AsFloat(); | ||
|
|
||
| if (ValueType == typeof(Vector3)) | ||
| { | ||
| return new Variant128(new Vector3(xValue, yValue, zValue)); | ||
| } | ||
|
|
||
| float wValue = _w!.Resolve(graphContext).AsFloat(); | ||
| return new Variant128(new Vector4(xValue, yValue, zValue, wValue)); | ||
| } | ||
|
|
||
| private static Type ValidateValueType(Type xType, Type yType, Type? zType, Type? wType) | ||
| { | ||
| ValidateComponentType(xType, nameof(x)); | ||
| ValidateComponentType(yType, nameof(y)); | ||
|
|
||
| if (zType is null) | ||
| { | ||
| return typeof(Vector2); | ||
| } | ||
|
|
||
| ValidateComponentType(zType, "z"); | ||
|
|
||
| if (wType is null) | ||
| { | ||
| return typeof(Vector3); | ||
| } | ||
|
|
||
| ValidateComponentType(wType, "w"); | ||
| return typeof(Vector4); | ||
| } | ||
|
|
||
| private static void ValidateComponentType(Type componentType, string componentName) | ||
| { | ||
| if (componentType != typeof(float)) | ||
| { | ||
| throw new ArgumentException( | ||
| $"VectorFromValuesResolver requires component '{componentName}' to be float. Got '{componentType}'."); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| # VectorFromValuesResolver | ||
|
|
||
| > **Type:** `Gamesmiths.Forge.Statescript.Properties.VectorFromValuesResolver` | ||
| > **Output Type:** `Vector2`, `Vector3`, or `Vector4` | ||
|
|
||
| Creates a vector from component resolver values. Use this when each axis value is computed independently and you want to assemble the final `Vector2`, `Vector3`, or `Vector4` inside a Statescript property graph. | ||
|
|
||
| ## Constructor | ||
|
|
||
| ```csharp | ||
| new VectorFromValuesResolver(x, y) | ||
| new VectorFromValuesResolver(x, y, z) | ||
| new VectorFromValuesResolver(x, y, z, w) | ||
| ``` | ||
|
|
||
| | Parameter | Type | Description | | ||
| |-----------|------|-------------| | ||
| | x | `IPropertyResolver` | The resolver for the X component. Must resolve to `float`. | | ||
| | y | `IPropertyResolver` | The resolver for the Y component. Must resolve to `float`. | | ||
| | z | `IPropertyResolver` | The resolver for the Z component. Must resolve to `float`. Required for `Vector3` and `Vector4`. | | ||
| | w | `IPropertyResolver` | The resolver for the W component. Must resolve to `float`. Required for `Vector4`. | | ||
|
|
||
| ## Behavior | ||
|
|
||
| - Resolves each component through its `IPropertyResolver` instance. | ||
| - Creates a `Vector2`, `Vector3`, or `Vector4` depending on the constructor overload used. | ||
| - Requires every component resolver to produce `float`. | ||
| - Validates component types at construction time (fail-fast), not at runtime. | ||
|
|
||
| ## Usage | ||
|
|
||
| ```csharp | ||
| graph.VariableDefinitions.DefineProperty("velocity", | ||
| new VectorFromValuesResolver( | ||
| new VariableResolver("velocityX", typeof(float)), | ||
| new VariableResolver("velocityY", typeof(float)), | ||
| new VariantResolver(new Variant128(0.0f), typeof(float)))); | ||
|
lextatic marked this conversation as resolved.
|
||
| ``` | ||
|
|
||
| ## Composition | ||
|
|
||
| ```csharp | ||
| graph.VariableDefinitions.DefineProperty("offset", | ||
| new VectorFromValuesResolver( | ||
| new MultiplyResolver( | ||
|
lextatic marked this conversation as resolved.
|
||
| new VariableResolver("speed", typeof(float)), | ||
| new VariableResolver("directionX", typeof(float))), | ||
| new MultiplyResolver( | ||
| new VariableResolver("speed", typeof(float)), | ||
| new VariableResolver("directionY", typeof(float))))); | ||
| ``` | ||
|
|
||
| ## See Also | ||
|
|
||
| - [Resolvers Overview](README.md) | ||
| - [VectorComponentResolver](vectorcomponent-resolver.md) | ||
| - [PlaneFromNormalResolver](planefromnormal-resolver.md) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.