using System;
using Gamecraft.Wires;
using Svelto.ECS;
using Unity.Mathematics;
using GamecraftModdingAPI;
using GamecraftModdingAPI.Utility;
namespace GamecraftModdingAPI.Blocks
{
///
/// Common implementation for blocks that support wiring.
///
public class SignalingBlock : Block
{
///
/// Places a new signaling block.
/// Any valid functional block type with IO ports will work.
/// This re-implements Block.PlaceNew(...)
///
public static new SignalingBlock PlaceNew(BlockIDs block, float3 position,
float3 rotation = default, BlockColors color = BlockColors.Default, byte darkness = 0,
int uscale = 1, float3 scale = default, Player player = null)
{
if (PlacementEngine.IsInGame && GameState.IsBuildMode())
{
try
{
EGID id = PlacementEngine.PlaceBlock(block, color, darkness,
position, uscale, scale, player, rotation);
Sync();
return new SignalingBlock(id);
}
catch (Exception e)
{
Logging.MetaDebugLog(e);
}
}
return null;
}
public SignalingBlock(EGID id) : base(id)
{
if (!BlockEngine.GetBlockInfoExists(this.Id))
{
throw new BlockTypeException($"Block is not a {this.GetType().Name} block");
}
}
public SignalingBlock(uint id) : base(id)
{
if (!BlockEngine.GetBlockInfoExists(this.Id))
{
throw new BlockTypeException($"Block is not a {this.GetType().Name} block");
}
}
protected ref BlockPortsStruct GetBlockPortsStruct()
{
return ref BlockEngine.GetBlockInfo(Id);
}
///
/// Generates the input port identifiers.
///
/// The input identifiers.
protected EGID[] GetInputIds()
{
return SignalEngine.GetSignalInputs(Id);
}
///
/// Generates the output port identifiers.
///
/// The output identifiers.
protected EGID[] GetOutputIds()
{
return SignalEngine.GetSignalOutputs(Id);
}
///
/// Gets the port struct.
///
/// The port struct.
/// Port identifier.
protected ref PortEntityStruct GetPortStruct(EGID portId)
{
return ref BlockEngine.GetBlockInfo(portId);
}
///
/// Gets the connected wire.
///
/// The connected wire.
/// Port identifier.
/// Whether the port has a wire connected to it.
protected ref WireEntityStruct GetConnectedWire(EGID portId, out bool connected)
{
return ref SignalEngine.MatchPortToWire(portId, Id, out connected);
}
///
/// [EXPERIMENTAL] Gets the channel data.
///
/// The channel data.
/// Port identifier.
/// Whether the channel actually exists.
protected ref ChannelDataStruct GetChannelData(EGID portId, out bool exists)
{
return ref SignalEngine.GetChannelDataStruct(portId, out exists);
}
///
/// The input port count.
///
public uint InputCount
{
get => GetBlockPortsStruct().inputCount;
}
///
/// The output port count.
///
public uint OutputCount
{
get => GetBlockPortsStruct().outputCount;
}
}
}