A stable modding interface between Techblox and mods https://mod.exmods.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
3.7KB

  1. using System;
  2. using Gamecraft.Wires;
  3. using Svelto.ECS;
  4. using Unity.Mathematics;
  5. using GamecraftModdingAPI;
  6. using GamecraftModdingAPI.Utility;
  7. namespace GamecraftModdingAPI.Blocks
  8. {
  9. /// <summary>
  10. /// Common implementation for blocks that support wiring.
  11. /// </summary>
  12. public class SignalingBlock : Block
  13. {
  14. /// <summary>
  15. /// Places a new signaling block.
  16. /// Any valid functional block type with IO ports will work.
  17. /// This re-implements Block.PlaceNew(...)
  18. /// </summary>
  19. public static new SignalingBlock PlaceNew(BlockIDs block, float3 position,
  20. float3 rotation = default, BlockColors color = BlockColors.Default, byte darkness = 0,
  21. int uscale = 1, float3 scale = default, Player player = null)
  22. {
  23. if (PlacementEngine.IsInGame && GameState.IsBuildMode())
  24. {
  25. EGID id = PlacementEngine.PlaceBlock(block, color, darkness,
  26. position, uscale, scale, player, rotation);
  27. return new SignalingBlock(id);
  28. }
  29. return null;
  30. }
  31. public SignalingBlock(EGID id) : base(id)
  32. {
  33. if (!BlockEngine.GetBlockInfoExists<BlockPortsStruct>(this.Id))
  34. {
  35. throw new BlockTypeException($"Block is not a {this.GetType().Name} block");
  36. }
  37. }
  38. public SignalingBlock(uint id) : base(id)
  39. {
  40. if (!BlockEngine.GetBlockInfoExists<BlockPortsStruct>(this.Id))
  41. {
  42. throw new BlockTypeException($"Block is not a {this.GetType().Name} block");
  43. }
  44. }
  45. protected ref BlockPortsStruct GetBlockPortsStruct()
  46. {
  47. return ref BlockEngine.GetBlockInfo<BlockPortsStruct>(Id);
  48. }
  49. /// <summary>
  50. /// Generates the input port identifiers.
  51. /// </summary>
  52. /// <returns>The input identifiers.</returns>
  53. protected EGID[] GetInputIds()
  54. {
  55. return SignalEngine.GetSignalInputs(Id);
  56. }
  57. /// <summary>
  58. /// Generates the output port identifiers.
  59. /// </summary>
  60. /// <returns>The output identifiers.</returns>
  61. protected EGID[] GetOutputIds()
  62. {
  63. return SignalEngine.GetSignalOutputs(Id);
  64. }
  65. /// <summary>
  66. /// Gets the port struct.
  67. /// </summary>
  68. /// <returns>The port struct.</returns>
  69. /// <param name="portId">Port identifier.</param>
  70. protected ref PortEntityStruct GetPortStruct(EGID portId)
  71. {
  72. return ref BlockEngine.GetBlockInfo<PortEntityStruct>(portId);
  73. }
  74. /// <summary>
  75. /// Gets the connected wire.
  76. /// </summary>
  77. /// <returns>The connected wire.</returns>
  78. /// <param name="portId">Port identifier.</param>
  79. /// <param name="connected">Whether the port has a wire connected to it.</param>
  80. protected ref WireEntityStruct GetConnectedWire(EGID portId, out bool connected)
  81. {
  82. return ref SignalEngine.MatchPortToWire(portId, Id, out connected);
  83. }
  84. /// <summary>
  85. /// [EXPERIMENTAL] Gets the channel data.
  86. /// </summary>
  87. /// <returns>The channel data.</returns>
  88. /// <param name="portId">Port identifier.</param>
  89. /// <param name="exists">Whether the channel actually exists.</param>
  90. protected ref ChannelDataStruct GetChannelData(EGID portId, out bool exists)
  91. {
  92. return ref SignalEngine.GetChannelDataStruct(portId, out exists);
  93. }
  94. /// <summary>
  95. /// The input port count.
  96. /// </summary>
  97. public uint InputCount
  98. {
  99. get => GetBlockPortsStruct().inputCount;
  100. }
  101. /// <summary>
  102. /// The output port count.
  103. /// </summary>
  104. public uint OutputCount
  105. {
  106. get => GetBlockPortsStruct().outputCount;
  107. }
  108. }
  109. }