71 lines
2.3 KiB
C#
71 lines
2.3 KiB
C#
namespace FoodFactory.Equipment;
|
|
|
|
using Godot;
|
|
using Arch.Core;
|
|
using Chickensoft.AutoInject;
|
|
using Chickensoft.Introspection;
|
|
using FoodFactory.Recipes;
|
|
using FoodFactory.Voxel;
|
|
using FoodFactory.Math;
|
|
using FoodFactory.Items;
|
|
using FoodFactory.Conveyors;
|
|
|
|
[Meta(typeof(IAutoNode))]
|
|
public partial class ItemSpawner : Node3D, IProvide<IBeltPortHost>
|
|
{
|
|
public override void _Notification(int what) => this.Notify(what);
|
|
|
|
[Dependency] public IVoxelGridRegistry GridRegistry => this.DependOn<IVoxelGridRegistry>();
|
|
[Dependency] public IBlueprintManger ItemFactory => this.DependOn<IBlueprintManger>();
|
|
[Dependency] public World World => this.DependOn<World>();
|
|
[Dependency] public IRecipes Recipes => this.DependOn<IRecipes>();
|
|
private BeltPortHost _insertLogic = default!;
|
|
public IBeltPortHost Value() => _insertLogic;
|
|
private VoxelGuid _guid;
|
|
public GridTransform3D VoxelTransform
|
|
{
|
|
get => GridTransform3D.FromGodot(GlobalTransform);
|
|
set => GlobalTransform = value.ToGodot();
|
|
}
|
|
[Export] public string ItemName { get; set; } = default!;
|
|
public override void _Ready()
|
|
{
|
|
_insertLogic = new BeltPortHost
|
|
{
|
|
Default = new DelegateInsertBeltItemLogic((_, _) => false, (_, _) => false)
|
|
};
|
|
var timer = new Timer() { Autostart = true, WaitTime = 1f };
|
|
AddChild(timer);
|
|
timer.Timeout += Tick;
|
|
this.Provide();
|
|
}
|
|
public void OnResolved() => _guid = GridRegistry.Register(this, VoxelTransform.Origin);
|
|
|
|
public override void _ExitTree() => GridRegistry.UnRegister(_guid);
|
|
public void Tick()
|
|
{
|
|
var ports = _insertLogic.GetPorts();
|
|
foreach (var item in ports)
|
|
{
|
|
var port = item.GetPortFacing(GridRegistry);
|
|
if (!port.HasValue(out var beltPort))
|
|
{
|
|
continue;
|
|
}
|
|
var itemBlueprint = ItemFactory.GetBlueprint(ItemName);
|
|
var dummyItem = new TestItem();
|
|
if (beltPort.CanAccept(dummyItem, 0))
|
|
{
|
|
var ctx = new BlueprintContext() { BluePrintId = new BlueprintId(itemBlueprint), World = World };
|
|
dummyItem.Item = itemBlueprint.Factory(ctx);
|
|
if (!beltPort.TryInsert(dummyItem, 0))
|
|
{
|
|
World.Destroy(dummyItem.Item);//TODO this should not call, but not sure
|
|
GD.PushWarning("Item failed to insert into port and removed item, item was destroyed but make sure item to prevent overhead.");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|