Added Arch Persitance and Relationships, styackable items, hot fixed Tags.containsAll. AutoList in Ordered!DList

This commit is contained in:
2026-05-16 12:44:51 -04:00
parent bf4d856608
commit 75c93f0c43
40 changed files with 52609 additions and 197 deletions

View File

@@ -0,0 +1,118 @@
namespace FoodFactory.Equipment;
using System;
using System.Collections.Generic;
using System.Linq;
using Arch.Core;
using Arch.Core.Extensions;
using Arch.Relationships;
using Chickensoft.AutoInject;
using Chickensoft.Introspection;
using FoodFactory;
using FoodFactory.Conveyors;
using FoodFactory.Items;
using FoodFactory.Math;
using FoodFactory.Recipes;
using FoodFactory.Voxel;
using Godot;
using SJK.Functional;
[Meta(typeof(IAutoNode))]
public partial class StackerTest : Node3D, IProvide<IBeltPortHost>, IProvide<IVoxelGridRegistry>
{
public override void _Notification(int what) => this.Notify(what);
private BeltPortHost _insertLogic = default!;
public IBeltPortHost Value() => _insertLogic;
[Dependency] public IVoxelGridRegistry GridRegistry => this.DependOn<IVoxelGridRegistry>();
[Dependency] public IFoodFactoryApi FoodFactory => this.DependOn<IFoodFactoryApi>();
[Dependency] public IRecipes Recipes => this.DependOn<IRecipes>();
IVoxelGridRegistry IProvide<IVoxelGridRegistry>.Value() => GridRegistry;
public GridTransform3D VoxelTransform
{
get => GridTransform3D.FromGodot(GlobalTransform);
set => GlobalTransform = value.ToGodot();
}
private Entity _output = Entity.Null;
private List<Entity> _inputs = [];
private VoxelGuid _guid;
public void OnResolved()
{
_insertLogic = new BeltPortHost();
_insertLogic.Bind(port => port is BeltPort beltPort && beltPort.PortName == "Input", () => new DelegateInsertBeltItemLogic(canAccept, canInsert));
_insertLogic.Bind(port => port is BeltPort beltPort && beltPort.PortName == "OutPut", () => new DelegateInsertBeltItemLogic((_, _) => false, (_, _) => false));
bool canAccept(IBeltPort port, IBeltItem item) => _inputs.Count < 2;
bool canInsert(IBeltPort beltPort, IBeltItem item)
{
if (item is IBeltItemData<Entity> itemData)
{
_inputs.Add(itemData.GetItem());
item.Dispose();
// GD.Print("Added Item to Stacker");
return true;
}
throw new NotImplementedException();
}
_guid = GridRegistry.Register(this, VoxelTransform.Origin);
this.Provide();
// var time = new Timer() { Autostart = true, OneShot = false, WaitTime = .1 };
// AddChild(time);
FoodFactory.TickManger.GameTick += _ =>
{
if (_output.Id != -1)
{
var port = _insertLogic.GetPorts().FirstOrNone(port => port is BeltPort beltPort && beltPort.PortName == "OutPut").Bind(f => f.GetPortFacing(GridRegistry));
if (port.HasValue(out var v) && v.TryInsert(new TestItem() { Item = _output }, LaneSpan.One, 0))
{
_output = Entity.Null;
}
}
if (_inputs.Count < 2)//TODO have a better way of dertming if item is valid, possibly nullable
{
return;
}
Span<Entity> items = [_inputs[0], _inputs[1]];
var context = new RecipeContext(World.Worlds[0], items);
var builder = new RecipeResultBuilder(stackalloc bool[10], new ItemBuilder[10]);
Span<int> mapping = stackalloc int[2];
// var recipe = new PotatoCookRecipe();
var recipes = Recipes.GetRecipes("stack", 2, [_inputs[0].Get<Tags>(), _inputs[1].Get<Tags>()], [], new RecipeOutput(1), mapping);
unsafe
{
while (RecipeProcessor.TryProcess(
recipes: ref recipes,
context: ref context,
builder: ref builder,
destroyEntity: &DestroyEntity,
filter: &RecipeProcessor.FilterByPass,
resultEntity: out var created
))
{
_output = created[0];
ref var relation = ref _output.GetRelationships<ParentOf>();
var count = relation.Count;
GD.Print(string.Join(',', relation._elements));
foreach (var item in relation)
{
GD.Print(string.Join(',', item.Key.GetAllComponents()));
}
_inputs.Clear();
break;
}
}
};
}
private static void DestroyEntity(Entity entity) => World.Worlds[entity.WorldId].Destroy(entity);
public override void _ExitTree()
{
GridRegistry.UnRegister(_guid);
base._ExitTree();
}
}