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 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using System;
  2. using Svelto.ECS;
  3. using RobocraftX.Common;
  4. using Unity.Mathematics;
  5. using GamecraftModdingAPI.Blocks;
  6. using GamecraftModdingAPI.Utility;
  7. namespace GamecraftModdingAPI
  8. {
  9. public class Block
  10. {
  11. private static readonly PlacementEngine PlacementEngine = new PlacementEngine();
  12. private static readonly MovementEngine MovementEngine = new MovementEngine();
  13. private static readonly RotationEngine RotationEngine = new RotationEngine();
  14. private static readonly RemovalEngine RemovalEngine = new RemovalEngine();
  15. private static readonly BlockEngine BlockEngine = new BlockEngine();
  16. /// <summary>
  17. /// 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.
  18. /// Place blocks next to each other to connect them.
  19. /// The placed block will be a complete block with a placement grid and collision which will be saved along with the game.
  20. /// </summary>
  21. /// <param name="block">The block's type</param>
  22. /// <param name="color">The block's color</param>
  23. /// <param name="darkness">The block color's darkness (0-9) - 0 is default color</param>
  24. /// <param name="position">The block's position in the grid - default block size is 0.2</param>
  25. /// <param name="rotation">The block's rotation in degrees</param>
  26. /// <param name="uscale">The block's uniform scale - default scale is 1 (with 0.2 width)</param>
  27. /// <param name="scale">The block's non-uniform scale - 0 means <paramref name="uscale"/> is used</param>
  28. /// <param name="player">The player who placed the block</param>
  29. /// <returns>The placed block or null if failed</returns>
  30. public static Block PlaceNew(BlockIDs block, float3 position,
  31. float3 rotation = default, BlockColors color = BlockColors.Default, byte darkness = 0,
  32. int uscale = 1, float3 scale = default, Player player = null)
  33. {
  34. if (PlacementEngine.IsInGame && GameState.IsBuildMode())
  35. {
  36. try
  37. {
  38. return new Block(PlacementEngine.PlaceBlock(block, color, darkness,
  39. position, uscale, scale, player, rotation));
  40. }
  41. catch (Exception e)
  42. {
  43. Logging.MetaDebugLog(e);
  44. }
  45. }
  46. return null;
  47. }
  48. /// <summary>
  49. /// Returns the most recently placed block.
  50. /// </summary>
  51. /// <returns>The block object</returns>
  52. public static Block GetLastPlacedBlock()
  53. {
  54. return new Block(BlockIdentifiers.LatestBlockID);
  55. }
  56. public Block(EGID id)
  57. {
  58. Id = id;
  59. }
  60. public Block(uint id)
  61. {
  62. Id = new EGID(id, CommonExclusiveGroups.OWNED_BLOCKS_GROUP);
  63. }
  64. public EGID Id { get; }
  65. /// <summary>
  66. /// The block's current position.
  67. /// </summary>
  68. public float3 Position
  69. {
  70. get => MovementEngine.GetPosition(Id.entityID);
  71. set => MovementEngine.MoveBlock(Id.entityID, value);
  72. }
  73. /// <summary>
  74. /// The block's current rotation in degrees.
  75. /// </summary>
  76. public float3 Rotation
  77. {
  78. get => RotationEngine.GetRotation(Id.entityID);
  79. set => RotationEngine.RotateBlock(Id.entityID, value);
  80. }
  81. /// <summary>
  82. /// The block's type (ID). Changing from or to a functional part may crash the game.
  83. /// </summary>
  84. public BlockIDs Type
  85. {
  86. get => (BlockIDs) BlockEngine.GetBlockInfo<DBEntityStruct>(Id).DBID;
  87. set
  88. {
  89. BlockEngine.GetBlockInfo<DBEntityStruct>(Id).DBID = (uint) value;
  90. uint prefabId = PrefabsID.GetPrefabId((uint) value, 0);
  91. BlockEngine.GetBlockInfo<GFXPrefabEntityStructGPUI>(Id).prefabID = prefabId;
  92. BlockEngine.GetBlockInfo<PhysicsPrefabEntityStruct>(Id) = new PhysicsPrefabEntityStruct(prefabId);
  93. }
  94. }
  95. public BlockColors Color
  96. {
  97. get => (BlockColors) (BlockEngine.GetBlockInfo<ColourParameterEntityStruct>(Id).indexInPalette % 10);
  98. set
  99. {
  100. ref var color = ref BlockEngine.GetBlockInfo<ColourParameterEntityStruct>(Id);
  101. color.indexInPalette = (byte) (color.indexInPalette / 10 * 10 + value);
  102. color.needsUpdate = true;
  103. }
  104. }
  105. public byte ColorDarkness
  106. {
  107. get => (byte) (BlockEngine.GetBlockInfo<ColourParameterEntityStruct>(Id).indexInPalette / 10);
  108. set
  109. {
  110. ref var color = ref BlockEngine.GetBlockInfo<ColourParameterEntityStruct>(Id);
  111. color.indexInPalette = (byte) (10 * (byte) value + color.indexInPalette % 10);
  112. color.needsUpdate = true;
  113. }
  114. }
  115. /// <summary>
  116. /// Returns an array of blocks that are connected to this one.
  117. /// </summary>
  118. public Block[] GetConnectedCubes() => BlockEngine.GetConnectedBlocks(Id.entityID);
  119. /// <summary>
  120. /// Removes this block.
  121. /// </summary>
  122. /// <returns>True if the block exists and could be removed.</returns>
  123. public bool Remove() => RemovalEngine.RemoveBlock(Id);
  124. public override string ToString()
  125. {
  126. return $"{nameof(Id)}: {Id}, {nameof(Position)}: {Position}, {nameof(Rotation)}: {Rotation}";
  127. }
  128. public static void Init()
  129. {
  130. GameEngineManager.AddGameEngine(PlacementEngine);
  131. GameEngineManager.AddGameEngine(MovementEngine);
  132. GameEngineManager.AddGameEngine(RotationEngine);
  133. GameEngineManager.AddGameEngine(RemovalEngine);
  134. GameEngineManager.AddGameEngine(BlockEngine);
  135. }
  136. }
  137. }