// Copyright © Gamesmiths Guild.
using Gamesmiths.Forge.Core;
using Gamesmiths.Forge.Godot.Resources.Statescript;
using Gamesmiths.Forge.Statescript;
using Godot;
using Godot.Collections;
namespace Gamesmiths.Forge.Godot.Resources;
///
/// Resource containing a collection of shared variable definitions for an entity. Assign this to a
/// (or custom implementation) to define which shared
/// variables the entity exposes at runtime.
///
[Tool]
[GlobalClass]
[Icon("uid://cu6ncpuumjo20")]
public partial class ForgeSharedVariableSet : Resource
{
///
/// Gets or sets the shared variable definitions.
///
[Export]
public Array Variables { get; set; } = [];
///
/// Populates a bag with all the definitions in this set, using each variable's name and
/// initial value.
///
/// The instance to populate.
public void PopulateVariables(Variables target)
{
foreach (ForgeSharedVariableDefinition definition in Variables)
{
if (string.IsNullOrEmpty(definition.VariableName))
{
continue;
}
var key = new StringKey(definition.VariableName);
if (definition.IsArray)
{
var initialValues = new Variant128[definition.InitialArrayValues.Count];
for (var i = 0; i < definition.InitialArrayValues.Count; i++)
{
initialValues[i] = StatescriptVariableTypeConverter.GodotVariantToForge(
definition.InitialArrayValues[i],
definition.VariableType);
}
target.DefineArrayVariable(key, initialValues);
}
else
{
Variant128 value = StatescriptVariableTypeConverter.GodotVariantToForge(
definition.InitialValue,
definition.VariableType);
target.DefineVariable(key, value);
}
}
}
}