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.

115 lines
4.3KB

  1. using System;
  2. using System.Collections.Generic;
  3. using GamecraftModdingAPI.Blocks;
  4. using GamecraftModdingAPI.Utility;
  5. using RobocraftX.Common;
  6. using Svelto.ECS;
  7. using Unity.Mathematics;
  8. namespace GamecraftModdingAPI
  9. {
  10. public class Block
  11. {
  12. private static readonly PlacementEngine PlacementEngine = new PlacementEngine();
  13. private static readonly MovementEngine MovementEngine = new MovementEngine();
  14. private static readonly RotationEngine RotationEngine = new RotationEngine();
  15. private static readonly RemovalEngine RemovalEngine = new RemovalEngine();
  16. private static readonly BlockEngine BlockEngine = new BlockEngine();
  17. /// <summary>
  18. /// 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.
  19. /// Place blocks next to each other to connect them.
  20. /// The placed block will be a complete block with a placement grid and collision which will be saved along with the game.
  21. /// </summary>
  22. /// <param name="block">The block's type</param>
  23. /// <param name="color">The block's color</param>
  24. /// <param name="darkness">The block color's darkness (0-9) - 0 is default color</param>
  25. /// <param name="position">The block's position in the grid - default block size is 0.2</param>
  26. /// <param name="rotation">The block's rotation in degrees</param>
  27. /// <param name="uscale">The block's uniform scale - default scale is 1 (with 0.2 width)</param>
  28. /// <param name="scale">The block's non-uniform scale - 0 means <paramref name="uscale"/> is used</param>
  29. /// <param name="player">The player who placed the block</param>
  30. /// <returns>The placed block or null if failed</returns>
  31. public static Block PlaceNew(BlockIDs block, float3 position,
  32. float3 rotation = default, BlockColors color = BlockColors.Default, byte darkness = 0,
  33. int uscale = 1, float3 scale = default, Player player = null)
  34. {
  35. if (PlacementEngine.IsInGame && GameState.IsBuildMode())
  36. {
  37. try
  38. {
  39. return new Block(PlacementEngine.PlaceBlock(block, color, darkness,
  40. position, uscale, scale, player, rotation));
  41. }
  42. catch (Exception e)
  43. {
  44. Logging.MetaDebugLog(e);
  45. }
  46. }
  47. return null;
  48. }
  49. /// <summary>
  50. /// Returns the most recently placed block.
  51. /// </summary>
  52. /// <returns>The block object</returns>
  53. public static Block GetLastPlacedBlock()
  54. {
  55. return new Block(BlockIdentifiers.LatestBlockID);
  56. }
  57. public Block(EGID id)
  58. {
  59. Id = id;
  60. }
  61. public Block(uint id)
  62. {
  63. Id = new EGID(id, CommonExclusiveGroups.OWNED_BLOCKS_GROUP);
  64. }
  65. public EGID Id { get; }
  66. /// <summary>
  67. /// The block's current position.
  68. /// </summary>
  69. public float3 Position
  70. {
  71. get => MovementEngine.GetPosition(Id.entityID);
  72. set => MovementEngine.MoveBlock(Id.entityID, value);
  73. }
  74. /// <summary>
  75. /// Returns an array of blocks that are connected to this one.
  76. /// </summary>
  77. public Block[] ConnectedCubes => BlockEngine.GetConnectedBlocks(Id.entityID);
  78. /// <summary>
  79. /// The block's current rotation in degrees.
  80. /// </summary>
  81. public float3 Rotation
  82. {
  83. get => RotationEngine.GetRotation(Id.entityID);
  84. set => RotationEngine.RotateBlock(Id.entityID, value);
  85. }
  86. /// <summary>
  87. /// Removes this block.
  88. /// </summary>
  89. /// <returns>True if the block exists and could be removed.</returns>
  90. public bool Remove()
  91. {
  92. return RemovalEngine.RemoveBlock(Id);
  93. }
  94. public static void Init()
  95. {
  96. GameEngineManager.AddGameEngine(PlacementEngine);
  97. GameEngineManager.AddGameEngine(MovementEngine);
  98. GameEngineManager.AddGameEngine(RotationEngine);
  99. GameEngineManager.AddGameEngine(RemovalEngine);
  100. GameEngineManager.AddGameEngine(BlockEngine);
  101. }
  102. }
  103. }