119 lines
4.1 KiB
C#
119 lines
4.1 KiB
C#
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();
|
|
}
|
|
}
|