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.

SignalingBlock.cs 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. try
  26. {
  27. EGID id = PlacementEngine.PlaceBlock(block, color, darkness,
  28. position, uscale, scale, player, rotation);
  29. return new SignalingBlock(id);
  30. }
  31. catch (Exception e)
  32. {
  33. Logging.MetaDebugLog(e);
  34. }
  35. }
  36. return null;
  37. }
  38. public SignalingBlock(EGID 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. public SignalingBlock(uint id) : base(id)
  46. {
  47. if (!BlockEngine.GetBlockInfoExists<BlockPortsStruct>(this.Id))
  48. {
  49. throw new BlockTypeException($"Block is not a {this.GetType().Name} block");
  50. }
  51. }
  52. protected ref BlockPortsStruct GetBlockPortsStruct()
  53. {
  54. return ref BlockEngine.GetBlockInfo<BlockPortsStruct>(Id);
  55. }
  56. /// <summary>
  57. /// Generates the input port identifiers.
  58. /// </summary>
  59. /// <returns>The input identifiers.</returns>
  60. protected EGID[] GetInputIds()
  61. {
  62. return SignalEngine.GetSignalInputs(Id);
  63. }
  64. /// <summary>
  65. /// Generates the output port identifiers.
  66. /// </summary>
  67. /// <returns>The output identifiers.</returns>
  68. protected EGID[] GetOutputIds()
  69. {
  70. return SignalEngine.GetSignalOutputs(Id);
  71. }
  72. /// <summary>
  73. /// Gets the port struct.
  74. /// </summary>
  75. /// <returns>The port struct.</returns>
  76. /// <param name="portId">Port identifier.</param>
  77. protected ref PortEntityStruct GetPortStruct(EGID portId)
  78. {
  79. return ref BlockEngine.GetBlockInfo<PortEntityStruct>(portId);
  80. }
  81. /// <summary>
  82. /// Gets the connected wire.
  83. /// </summary>
  84. /// <returns>The connected wire.</returns>
  85. /// <param name="portId">Port identifier.</param>
  86. /// <param name="connected">Whether the port has a wire connected to it.</param>
  87. protected ref WireEntityStruct GetConnectedWire(EGID portId, out bool connected)
  88. {
  89. return ref SignalEngine.MatchPortToWire(portId, Id, out connected);
  90. }
  91. /// <summary>
  92. /// [EXPERIMENTAL] Gets the channel data.
  93. /// </summary>
  94. /// <returns>The channel data.</returns>
  95. /// <param name="portId">Port identifier.</param>
  96. /// <param name="exists">Whether the channel actually exists.</param>
  97. protected ref ChannelDataStruct GetChannelData(EGID portId, out bool exists)
  98. {
  99. return ref SignalEngine.GetChannelDataStruct(portId, out exists);
  100. }
  101. /// <summary>
  102. /// The input port count.
  103. /// </summary>
  104. public uint InputCount
  105. {
  106. get => GetBlockPortsStruct().inputCount;
  107. }
  108. /// <summary>
  109. /// The output port count.
  110. /// </summary>
  111. public uint OutputCount
  112. {
  113. get => GetBlockPortsStruct().outputCount;
  114. }
  115. }
  116. }