TechbloxModdingAPI/GamecraftModdingAPI/Blocks/SignalingBlock.cs
NorbiPeti 5bbb54c0c5 Automatically invoke the correct block constructor
And store delegates of dynamic methods invoking constructors
Tested with the automated tests
2020-07-13 21:55:48 +02:00

104 lines
2.9 KiB
C#

using System;
using Gamecraft.Wires;
using Svelto.ECS;
using Unity.Mathematics;
using GamecraftModdingAPI;
using GamecraftModdingAPI.Utility;
namespace GamecraftModdingAPI.Blocks
{
/// <summary>
/// Common implementation for blocks that support wiring.
/// </summary>
public class SignalingBlock : Block
{
public SignalingBlock(EGID id) : base(id)
{
if (!BlockEngine.GetBlockInfoExists<BlockPortsStruct>(this.Id))
{
throw new BlockTypeException($"Block is not a {this.GetType().Name} block");
}
}
public SignalingBlock(uint id) : base(id)
{
if (!BlockEngine.GetBlockInfoExists<BlockPortsStruct>(this.Id))
{
throw new BlockTypeException($"Block is not a {this.GetType().Name} block");
}
}
protected ref BlockPortsStruct GetBlockPortsStruct()
{
return ref BlockEngine.GetBlockInfo<BlockPortsStruct>(Id);
}
/// <summary>
/// Generates the input port identifiers.
/// </summary>
/// <returns>The input identifiers.</returns>
protected EGID[] GetInputIds()
{
return SignalEngine.GetSignalInputs(Id);
}
/// <summary>
/// Generates the output port identifiers.
/// </summary>
/// <returns>The output identifiers.</returns>
protected EGID[] GetOutputIds()
{
return SignalEngine.GetSignalOutputs(Id);
}
/// <summary>
/// Gets the port struct.
/// </summary>
/// <returns>The port struct.</returns>
/// <param name="portId">Port identifier.</param>
protected ref PortEntityStruct GetPortStruct(EGID portId)
{
return ref BlockEngine.GetBlockInfo<PortEntityStruct>(portId);
}
/// <summary>
/// Gets the connected wire.
/// </summary>
/// <returns>The connected wire.</returns>
/// <param name="portId">Port identifier.</param>
/// <param name="connected">Whether the port has a wire connected to it.</param>
protected ref WireEntityStruct GetConnectedWire(EGID portId, out bool connected)
{
return ref SignalEngine.MatchPortToWire(portId, Id, out connected);
}
/// <summary>
/// [EXPERIMENTAL] Gets the channel data.
/// </summary>
/// <returns>The channel data.</returns>
/// <param name="portId">Port identifier.</param>
/// <param name="exists">Whether the channel actually exists.</param>
protected ref ChannelDataStruct GetChannelData(EGID portId, out bool exists)
{
return ref SignalEngine.GetChannelDataStruct(portId, out exists);
}
/// <summary>
/// The input port count.
/// </summary>
public uint InputCount
{
get => GetBlockPortsStruct().inputCount;
}
/// <summary>
/// The output port count.
/// </summary>
public uint OutputCount
{
get => GetBlockPortsStruct().outputCount;
}
}
}