53 lines
1.6 KiB
C#
53 lines
1.6 KiB
C#
using Godot;
|
|
using Godot.Collections;
|
|
using Newlon.Resources;
|
|
|
|
[Tool]
|
|
public partial class SeedpacketEditor : BaseEditor
|
|
{
|
|
private const string PLANTS_DIRECTORY = "res://assets/plants/";
|
|
public override void SetEditedData(AdventureLevelResource data)
|
|
{
|
|
base.SetEditedData(data);
|
|
SetPrepickedPlants();
|
|
}
|
|
|
|
public override void Save()
|
|
{
|
|
CalculatePrepickedPlants();
|
|
base.Save();
|
|
}
|
|
private void SetPrepickedPlants()
|
|
{
|
|
for (int i = 0; i < editedResource.prepickedPlants.Count; i++)
|
|
{
|
|
if (GetNode("%PrepickedContainer").GetChild(i).GetNode("Seedpacket") is DnDSeedpacket packet)
|
|
{
|
|
packet.HeldResource = TryGetPlant(editedResource.prepickedPlants[i]);
|
|
packet.RefreshTexture();
|
|
}
|
|
}
|
|
}
|
|
private void CalculatePrepickedPlants()
|
|
{
|
|
Array<string> prepicked = new();
|
|
foreach (var child in GetNode("%PrepickedContainer").GetChildren())
|
|
{
|
|
if (child.GetNode("Seedpacket") is DnDSeedpacket packet && packet.HeldResource != null)
|
|
{
|
|
prepicked.Add(packet.HeldResource.GetInternalID());
|
|
}
|
|
}
|
|
editedResource.prepickedPlants = prepicked;
|
|
}
|
|
private PlantResource TryGetPlant(string plantName)
|
|
{
|
|
foreach (var file in ResourceLoader.ListDirectory(PLANTS_DIRECTORY))
|
|
{
|
|
if (plantName != file.TrimSuffix(".tres").ToLower()) continue;
|
|
return ResourceLoader.Load<PlantResource>(PLANTS_DIRECTORY + file);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
}
|