init
This commit is contained in:
177
src/VoxelGrid/VoxelGridNode.cs
Normal file
177
src/VoxelGrid/VoxelGridNode.cs
Normal file
@@ -0,0 +1,177 @@
|
||||
namespace ChickenGameTest;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Chickensoft.AutoInject;
|
||||
using Chickensoft.Introspection;
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
using SJK.Functional;
|
||||
|
||||
//Will Liklely be the game instead of a node like this
|
||||
[Meta(typeof(IAutoNode))]// [Tool]
|
||||
public partial class VoxelGridNode : Node3D, IProvide<IVoxelGridQuery<LayeredEquipment>>, IProvide<IEquipmentComponentRegistry>, IProvide<IVoxelGridRegistry>, IProvide<IItemRenderer>
|
||||
{
|
||||
public override void _Notification(int what) => this.Notify(what);
|
||||
private IVoxelGridQuery<LayeredEquipment> _voxelGrid = default!;
|
||||
IVoxelGridQuery<LayeredEquipment> IProvide<IVoxelGridQuery<LayeredEquipment>>.Value() => _voxelGrid;
|
||||
|
||||
private IVoxelGridRegistry _voxelGridRegistry = default!;
|
||||
IVoxelGridRegistry IProvide<IVoxelGridRegistry>.Value() => _voxelGridRegistry;
|
||||
private IItemRenderer _itemRenderer = default!;
|
||||
IItemRenderer IProvide<IItemRenderer>.Value() => _itemRenderer;
|
||||
public override void _Ready()
|
||||
{
|
||||
TestStructural.Test();
|
||||
GD.Print();
|
||||
base._Ready();
|
||||
_voxelGrid = new EquipmentVoxelGrid();
|
||||
_equipmentComponentRegistry = new EquipmentComponentRegistry();
|
||||
_voxelGridRegistry = new VoxelRegistry();
|
||||
_itemRenderer = new TestItemRendered();
|
||||
AddChild(_itemRenderer as Node);
|
||||
Timer timer = new Timer() { WaitTime = .25f, Autostart = true };//TEST
|
||||
AddChild(timer);//TEST
|
||||
// timer.Timeout += _itemRenderer.Tick;//TEST
|
||||
this.Provide();
|
||||
}
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
base._Process(delta);
|
||||
// GD.Print(_voxelGridRegistry.Get<ISlotFace>(Vector3I.Zero).First().GetConnectingFace());
|
||||
}
|
||||
|
||||
IEquipmentComponentRegistry _equipmentComponentRegistry;
|
||||
IEquipmentComponentRegistry IProvide<IEquipmentComponentRegistry>.Value() => _equipmentComponentRegistry;
|
||||
|
||||
}
|
||||
// [Meta, Id("voxel_grid")]
|
||||
public sealed partial class EquipmentVoxelGrid : IVoxelGridQuery<LayeredEquipment>
|
||||
{
|
||||
private System.Collections.Generic.Dictionary<Vector3I, LayeredEquipment> _data = new();
|
||||
public void AddEquipment(Vector3I position, Equipment equipment)
|
||||
{
|
||||
if (!_data.TryGetValue(position, out var layers))
|
||||
{
|
||||
_data[position] = layers = new();
|
||||
}
|
||||
layers.Add(equipment);
|
||||
}
|
||||
//TODO Use Options instead of nu;lable
|
||||
public LayeredEquipment GetVoxel(Vector3I position) => _data.TryGetValue(position, out var result) ? result : new();
|
||||
public IEnumerable<Equipment> GetEquipmentAt(Vector3I position) => GetVoxel(position).GetEquipments();
|
||||
// public
|
||||
// public void SetEquipmentAt(Vector3I position, T value)
|
||||
// {
|
||||
// _data[position] = value;
|
||||
// GD.Print(value);
|
||||
// }
|
||||
|
||||
public LayeredEquipment SetVoxel(Vector3I position, LayeredEquipment value) => _data[position] = value;
|
||||
public bool IsOccupied(Vector3I cell) => _data.ContainsKey(cell);
|
||||
}
|
||||
public interface IVoxelGridQuery<T>
|
||||
{
|
||||
T GetVoxel(Vector3I cell);
|
||||
T SetVoxel(Vector3I cell, T value);
|
||||
bool IsOccupied(Vector3I cell);
|
||||
}
|
||||
|
||||
public class LayeredEquipment
|
||||
{
|
||||
public int Count => _equipment.Count;
|
||||
private readonly HashSet<Equipment> _equipment = new();
|
||||
public IEnumerable<Equipment> GetEquipments() => _equipment;
|
||||
public bool Add(Equipment equipment) => _equipment.Add(equipment);
|
||||
public bool Remove(Equipment equipment) => _equipment.Remove(equipment);
|
||||
public void AddMany(params Equipment[] equipment)
|
||||
{
|
||||
foreach (var item in equipment)
|
||||
{
|
||||
Add(item);
|
||||
}
|
||||
}
|
||||
public void RemoveMany(params Equipment[] equipment)
|
||||
{
|
||||
foreach (var item in equipment)
|
||||
{
|
||||
Remove(item);
|
||||
}
|
||||
}
|
||||
public void IfAny(Action<IEnumerable<Equipment>> action)
|
||||
{
|
||||
if (Count > 0)
|
||||
{
|
||||
action(GetEquipments());
|
||||
}
|
||||
}
|
||||
}
|
||||
public sealed class SlotDescriptor {
|
||||
public SlotDirection Direction { get; }
|
||||
|
||||
public SlotDescriptor(
|
||||
SlotDirection direction
|
||||
)
|
||||
{
|
||||
Direction = direction;
|
||||
}
|
||||
}
|
||||
public abstract class SlotLogic<TPayload> {
|
||||
public SlotDescriptor Descriptor { get; }
|
||||
|
||||
protected SlotLogic(SlotDescriptor descriptor) {
|
||||
Descriptor = descriptor;
|
||||
}
|
||||
|
||||
public abstract bool CanTransfer(
|
||||
TPayload payload
|
||||
);
|
||||
|
||||
public abstract bool TryTransfer(
|
||||
TPayload payload
|
||||
);
|
||||
}
|
||||
public sealed class ItemSlotLogic
|
||||
: SlotLogic<IBeltItem> {
|
||||
|
||||
public ItemSlotLogic(SlotDescriptor descriptor)
|
||||
: base(descriptor) {}
|
||||
|
||||
public override bool CanTransfer(IBeltItem item) => false;
|
||||
// item.Count > 0;
|
||||
|
||||
public override bool TryTransfer(IBeltItem item) {
|
||||
// routing rules
|
||||
return true;
|
||||
}
|
||||
}
|
||||
[Meta]
|
||||
public partial class ItemSlotNode
|
||||
: SlotComponentNode {
|
||||
|
||||
private ItemSlotLogic _logic;
|
||||
|
||||
public override void _Ready() {
|
||||
base._Ready();
|
||||
_logic = new ItemSlotLogic(
|
||||
new SlotDescriptor(Direction)
|
||||
);
|
||||
}
|
||||
public void OnResolved()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void Tick(IBeltItem stack) {
|
||||
if (_logic.CanTransfer(stack))
|
||||
_logic.TryTransfer(stack);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public enum SlotDirection
|
||||
{
|
||||
Input,
|
||||
Output
|
||||
}
|
||||
Reference in New Issue
Block a user