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.

102 lines
3.8KB

  1. using System;
  2. using Unity.Mathematics;
  3. using UnityEngine;
  4. namespace GamecraftModdingAPI
  5. {
  6. /// <summary>
  7. /// Represents a blueprint in the inventory. When placed it becomes a block group.
  8. /// </summary>
  9. public class Blueprint : IDisposable
  10. {
  11. public uint Id { get; }
  12. internal Blueprint(uint id)
  13. {
  14. Id = id;
  15. BlockGroup._engine.InitBlueprint(id);
  16. Refresh();
  17. }
  18. /// <summary>
  19. /// The center of the blueprint. Can only be set using the StoreBlocks() method.
  20. /// </summary>
  21. public float3 Position { get; private set; }
  22. /// <summary>
  23. /// The rotation of the blueprint. Can only be set using the StoreBlocks() method.
  24. /// </summary>
  25. public float3 Rotation { get; private set; }
  26. /// <summary>
  27. /// The amount of blocks in the blueprint. Gan only be set using the StoreBlocks() method.
  28. /// </summary>
  29. public uint BlockCount { get; private set; }
  30. /// <summary>
  31. /// Creates a new, empty blueprint. It will be deleted on disposal unless the game holds a reference to it.
  32. /// </summary>
  33. /// <returns>A blueprint that doesn't have any blocks</returns>
  34. public static Blueprint Create()
  35. {
  36. return new Blueprint(BlockGroup._engine.CreateBlueprint());
  37. }
  38. /// <summary>
  39. /// Set the blocks that the blueprint contains.
  40. /// Use the BlockGroup overload for automatically calculated position and rotation.
  41. /// </summary>
  42. /// <param name="blocks">The array of blocks to use</param>
  43. /// <param name="position">The anchor (center) position of the blueprint</param>
  44. /// <param name="rotation">The base rotation of the blueprint</param>
  45. public void StoreBlocks(Block[] blocks, float3 position, float3 rotation)
  46. {
  47. BlockGroup._engine.ReplaceBlueprint(Player.LocalPlayer.Id, Id, blocks, position,
  48. quaternion.Euler(rotation));
  49. Refresh();
  50. }
  51. /// <summary>
  52. /// Store the blocks from the given group in the blueprint with correct position and rotation for the blueprint.
  53. /// </summary>
  54. /// <param name="group">The block group to store</param>
  55. public void StoreBlocks(BlockGroup group)
  56. {
  57. BlockGroup._engine.ReplaceBlueprint(Player.LocalPlayer.Id, Id, group, group.Position,
  58. Quaternion.Euler(group.Rotation));
  59. Refresh();
  60. }
  61. /// <summary>
  62. /// Places the blocks the blueprint contains at the specified position and rotation.
  63. /// </summary>
  64. /// <param name="position">The position of the blueprint</param>
  65. /// <param name="rotation">The rotation of the blueprint</param>
  66. /// <returns>An array of the placed blocks</returns>
  67. public Block[] PlaceBlocks(float3 position, float3 rotation)
  68. {
  69. return BlockGroup._engine.PlaceBlueprintBlocks(Id, Player.LocalPlayer.Id, position, rotation);
  70. }
  71. /// <summary>
  72. /// Updates the properties based on the blueprint data. Only necessary if the blueprint is changed from the game.
  73. /// </summary>
  74. public void Refresh()
  75. {
  76. BlockGroup._engine.GetBlueprintInfo(Id, out var pos, out var rot, out uint count);
  77. Position = pos;
  78. Rotation = ((Quaternion) rot).eulerAngles;
  79. BlockCount = count;
  80. }
  81. public void Dispose()
  82. {
  83. BlockGroup._engine.DisposeBlueprint(Id);
  84. }
  85. public override string ToString()
  86. {
  87. return $"{nameof(Id)}: {Id}, {nameof(Position)}: {Position}, {nameof(Rotation)}: {Rotation}, {nameof(BlockCount)}: {BlockCount}";
  88. }
  89. }
  90. }