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.

Block.cs 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using System;
  2. using Svelto.ECS;
  3. using Svelto.ECS.EntityStructs;
  4. using RobocraftX.Common;
  5. using RobocraftX.Blocks.Scaling;
  6. using Unity.Mathematics;
  7. using GamecraftModdingAPI.Blocks;
  8. using GamecraftModdingAPI.Utility;
  9. namespace GamecraftModdingAPI
  10. {
  11. /// <summary>
  12. /// A single (perhaps scaled) block. Properties may return default values if the block is removed and then setting them is ignored.
  13. /// </summary>
  14. public class Block
  15. {
  16. private static readonly PlacementEngine PlacementEngine = new PlacementEngine();
  17. private static readonly MovementEngine MovementEngine = new MovementEngine();
  18. private static readonly RotationEngine RotationEngine = new RotationEngine();
  19. private static readonly RemovalEngine RemovalEngine = new RemovalEngine();
  20. private static readonly BlockEngine BlockEngine = new BlockEngine();
  21. /// <summary>
  22. /// Place a new block at the given position. If scaled, position means the center of the block. The default block size is 0.2 in terms of position.
  23. /// Place blocks next to each other to connect them.
  24. /// The placed block will be a complete block with a placement grid and collision which will be saved along with the game.
  25. /// </summary>
  26. /// <param name="block">The block's type</param>
  27. /// <param name="color">The block's color</param>
  28. /// <param name="darkness">The block color's darkness (0-9) - 0 is default color</param>
  29. /// <param name="position">The block's position in the grid - default block size is 0.2</param>
  30. /// <param name="rotation">The block's rotation in degrees</param>
  31. /// <param name="uscale">The block's uniform scale - default scale is 1 (with 0.2 width)</param>
  32. /// <param name="scale">The block's non-uniform scale - 0 means <paramref name="uscale"/> is used</param>
  33. /// <param name="player">The player who placed the block</param>
  34. /// <returns>The placed block or null if failed</returns>
  35. public static Block PlaceNew(BlockIDs block, float3 position,
  36. float3 rotation = default, BlockColors color = BlockColors.Default, byte darkness = 0,
  37. int uscale = 1, float3 scale = default, Player player = null)
  38. {
  39. if (PlacementEngine.IsInGame && GameState.IsBuildMode())
  40. {
  41. try
  42. {
  43. return new Block(PlacementEngine.PlaceBlock(block, color, darkness,
  44. position, uscale, scale, player, rotation));
  45. }
  46. catch (Exception e)
  47. {
  48. Logging.MetaDebugLog(e);
  49. }
  50. }
  51. return null;
  52. }
  53. /// <summary>
  54. /// Returns the most recently placed block.
  55. /// </summary>
  56. /// <returns>The block object</returns>
  57. public static Block GetLastPlacedBlock()
  58. {
  59. return new Block(BlockIdentifiers.LatestBlockID);
  60. }
  61. public Block(EGID id)
  62. {
  63. Id = id;
  64. }
  65. public Block(uint id)
  66. {
  67. Id = new EGID(id, CommonExclusiveGroups.OWNED_BLOCKS_GROUP);
  68. }
  69. public EGID Id { get; }
  70. /// <summary>
  71. /// The block's current position or zero if the block no longer exists.
  72. /// A block is 0.2 wide by default in terms of position.
  73. /// </summary>
  74. public float3 Position
  75. {
  76. get => Exists ? MovementEngine.GetPosition(Id.entityID) : float3.zero;
  77. set
  78. {
  79. if (Exists) MovementEngine.MoveBlock(Id.entityID, value);
  80. }
  81. }
  82. /// <summary>
  83. /// The block's current rotation in degrees or zero if the block doesn't exist.
  84. /// </summary>
  85. public float3 Rotation
  86. {
  87. get => Exists ? RotationEngine.GetRotation(Id.entityID) : float3.zero;
  88. set
  89. {
  90. if (Exists) RotationEngine.RotateBlock(Id.entityID, value);
  91. }
  92. }
  93. /// <summary>
  94. /// The block's non-uniform scale or zero if the block's invalid. Independent of the uniform scaling.
  95. /// </summary>
  96. public float3 Scale
  97. {
  98. get => BlockEngine.GetBlockInfo<ScalingEntityStruct>(Id)?.scale ?? float3.zero;
  99. set
  100. {
  101. var def = new ScalingEntityStruct();
  102. BlockEngine.GetBlockInfo(Id, ref def).scale = value;
  103. }
  104. }
  105. /// <summary>
  106. /// The block's uniform scale or zero if the block's invalid. Also sets the non-uniform scale.
  107. /// </summary>
  108. public int UniformScale
  109. {
  110. get => BlockEngine.GetBlockInfo<BlockPlacementScaleEntityStruct>(Id)?.desiredScaleFactor ?? 0;
  111. set
  112. {
  113. var def = new BlockPlacementScaleEntityStruct();
  114. ref var scaleStruct = ref BlockEngine.GetBlockInfo(Id, ref def);
  115. scaleStruct.blockPlacementHeight = scaleStruct.blockPlacementWidth =
  116. scaleStruct.desiredScaleFactor = scaleStruct.snapGridScale = value;
  117. Scale = new float3(value, value, value);
  118. }
  119. }
  120. /// <summary>
  121. /// The block's type (ID). Returns BlockIDs.Invalid if the block doesn't exist anymore.
  122. /// </summary>
  123. public BlockIDs Type => (BlockIDs) (BlockEngine.GetBlockInfo<DBEntityStruct>(Id)?.DBID ?? ushort.MaxValue);
  124. /// <summary>
  125. /// The block's color. Returns BlockColors.Default if the block no longer exists.
  126. /// </summary>
  127. public BlockColor Color
  128. {
  129. get
  130. {
  131. byte index = BlockEngine.GetBlockInfo<ColourParameterEntityStruct>(Id)?.indexInPalette ?? byte.MaxValue;
  132. if (index == byte.MaxValue) return new BlockColor {Color = BlockColors.Default};
  133. return new BlockColor {Color = (BlockColors) (index % 10), Darkness = (byte) (index / 10)};
  134. }
  135. set
  136. {
  137. var def = new ColourParameterEntityStruct();
  138. ref var color = ref BlockEngine.GetBlockInfo(Id, ref def);
  139. color.indexInPalette = (byte) (value.Color + value.Darkness * 10);
  140. color.needsUpdate = true;
  141. }
  142. }
  143. /// <summary>
  144. /// Whether the block exists. The other properties will return a default value if the block doesn't exist.
  145. /// </summary>
  146. public bool Exists => BlockEngine.BlockExists(Id);
  147. /// <summary>
  148. /// Returns an array of blocks that are connected to this one. Returns an empty array if the block doesn't exist.
  149. /// </summary>
  150. public Block[] GetConnectedCubes() => BlockEngine.GetConnectedBlocks(Id);
  151. /// <summary>
  152. /// Removes this block.
  153. /// </summary>
  154. /// <returns>True if the block exists and could be removed.</returns>
  155. public bool Remove() => RemovalEngine.RemoveBlock(Id);
  156. public override string ToString()
  157. {
  158. return $"{nameof(Id)}: {Id}, {nameof(Position)}: {Position}, {nameof(Rotation)}: {Rotation}";
  159. }
  160. public static void Init()
  161. {
  162. GameEngineManager.AddGameEngine(PlacementEngine);
  163. GameEngineManager.AddGameEngine(MovementEngine);
  164. GameEngineManager.AddGameEngine(RotationEngine);
  165. GameEngineManager.AddGameEngine(RemovalEngine);
  166. GameEngineManager.AddGameEngine(BlockEngine);
  167. }
  168. }
  169. }