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.

Servo.cs 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System;
  2. using RobocraftX.Blocks;
  3. using Svelto.ECS;
  4. using Unity.Mathematics;
  5. using GamecraftModdingAPI.Utility;
  6. namespace GamecraftModdingAPI.Blocks
  7. {
  8. public class Servo : Block
  9. {
  10. /// <summary>
  11. /// Places a new servo.
  12. /// Any valid servo type is accepted.
  13. /// This re-implements Block.PlaceNew(...)
  14. /// </summary>
  15. public static new Servo PlaceNew(BlockIDs block, float3 position,
  16. float3 rotation = default, BlockColors color = BlockColors.Default, byte darkness = 0,
  17. int uscale = 1, float3 scale = default, Player player = null)
  18. {
  19. if (!(block == BlockIDs.ServoAxle || block == BlockIDs.ServoHinge || block == BlockIDs.ServoPiston))
  20. {
  21. throw new BlockTypeException($"Block is not a {typeof(Servo).Name} block");
  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. Sync();
  30. return new Servo(id);
  31. }
  32. catch (Exception e)
  33. {
  34. Logging.MetaDebugLog(e);
  35. }
  36. }
  37. return null;
  38. }
  39. public Servo(EGID id) : base(id)
  40. {
  41. if (!BlockEngine.GetBlockInfoExists<ServoReadOnlyStruct>(this.Id))
  42. {
  43. throw new BlockTypeException($"Block is not a {this.GetType().Name} block");
  44. }
  45. }
  46. public Servo(uint id) : base(id)
  47. {
  48. if (!BlockEngine.GetBlockInfoExists<ServoReadOnlyStruct>(this.Id))
  49. {
  50. throw new BlockTypeException($"Block is not a {this.GetType().Name} block");
  51. }
  52. }
  53. // custom servo properties
  54. /// <summary>
  55. /// The servo's minimum angle.
  56. /// </summary>
  57. public float MinimumAngle
  58. {
  59. get => BlockEngine.GetBlockInfo<ServoReadOnlyStruct>(Id).minDeviation;
  60. set
  61. {
  62. ref ServoReadOnlyStruct servo = ref BlockEngine.GetBlockInfo<ServoReadOnlyStruct>(Id);
  63. servo.minDeviation = value;
  64. }
  65. }
  66. /// <summary>
  67. /// The servo's maximum angle.
  68. /// </summary>
  69. public float MaximumAngle
  70. {
  71. get => BlockEngine.GetBlockInfo<ServoReadOnlyStruct>(Id).maxDeviation;
  72. set
  73. {
  74. ref ServoReadOnlyStruct servo = ref BlockEngine.GetBlockInfo<ServoReadOnlyStruct>(Id);
  75. servo.maxDeviation = value;
  76. }
  77. }
  78. /// <summary>
  79. /// The servo's maximum force.
  80. /// </summary>
  81. public float MaximumForce
  82. {
  83. get => BlockEngine.GetBlockInfo<ServoReadOnlyStruct>(Id).maxForce;
  84. set
  85. {
  86. ref ServoReadOnlyStruct servo = ref BlockEngine.GetBlockInfo<ServoReadOnlyStruct>(Id);
  87. servo.maxForce = value;
  88. }
  89. }
  90. /// <summary>
  91. /// The servo's direction.
  92. /// </summary>
  93. public bool Reverse
  94. {
  95. get => BlockEngine.GetBlockInfo<ServoReadOnlyStruct>(Id).reverse;
  96. set
  97. {
  98. ref ServoReadOnlyStruct servo = ref BlockEngine.GetBlockInfo<ServoReadOnlyStruct>(Id);
  99. servo.reverse = value;
  100. }
  101. }
  102. }
  103. }