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.

98 lines
2.9KB

  1. using Gamecraft.Wires;
  2. using RobocraftX.Common;
  3. using RobocraftX.Physics;
  4. using Svelto.ECS;
  5. using Unity.Mathematics;
  6. using UnityEngine;
  7. namespace GamecraftModdingAPI
  8. {
  9. /// <summary>
  10. /// A rigid body (like a cluster of connected blocks) during simulation.
  11. /// </summary>
  12. public class SimBody
  13. {
  14. public EGID Id { get; }
  15. public SimBody(EGID id)
  16. {
  17. Id = id;
  18. }
  19. public SimBody(uint id) : this(new EGID(id, CommonExclusiveGroups.SIMULATION_BODIES_GROUP))
  20. {
  21. }
  22. public float3 Position
  23. {
  24. get => GetStruct().position;
  25. set => GetStruct().position = value;
  26. }
  27. public float3 Velocity
  28. {
  29. get => GetStruct().velocity;
  30. set => GetStruct().velocity = value;
  31. }
  32. public float3 AngularVelocity
  33. {
  34. get => GetStruct().angularVelocity;
  35. set => GetStruct().angularVelocity = value;
  36. }
  37. public float3 DeltaVelocity
  38. {
  39. get => GetStruct().deltaVelocity;
  40. set => GetStruct().deltaVelocity = value;
  41. }
  42. public float3 DeltaAngularVelocity
  43. {
  44. get => GetStruct().deltaAngularVelocity;
  45. set => GetStruct().deltaAngularVelocity = value;
  46. }
  47. public float3 Rotation
  48. {
  49. get => ((Quaternion) GetStruct().rotation).eulerAngles;
  50. set
  51. {
  52. ref var str = ref GetStruct();
  53. Quaternion quaternion = str.rotation;
  54. quaternion.eulerAngles = value;
  55. str.rotation = quaternion;
  56. }
  57. }
  58. public float Mass
  59. {
  60. get => math.rcp(GetStruct().physicsMass.InverseMass);
  61. set => GetStruct().physicsMass.InverseMass = math.rcp(value);
  62. }
  63. /// <summary>
  64. /// Whether the body can be moved or static
  65. /// </summary>
  66. public bool Static => Block.BlockEngine.GetBlockInfo<MassEntityStruct>(Id).isStatic;
  67. //Setting it doesn't have any effect
  68. private ref RigidBodyEntityStruct GetStruct()
  69. {
  70. return ref Block.BlockEngine.GetBlockInfo<RigidBodyEntityStruct>(Id);
  71. }
  72. public override string ToString()
  73. {
  74. return $"{nameof(Id)}: {Id}, {nameof(Position)}: {Position}, {nameof(Mass)}: {Mass}, {nameof(Static)}: {Static}";
  75. }
  76. /// <summary>
  77. /// Returns the object identified by the given ID (A-Z).
  78. /// This has the same result as calling ObjectIdentifier.GetByID(id) and then GetRigidBody() with the duplicates filtered out.
  79. /// </summary>
  80. /// <param name="id">The alphabetical ID</param>
  81. /// <returns>An array that may be empty</returns>
  82. public static SimBody[] GetFromObjectID(char id) => Block.BlockEngine.GetSimBodiesFromID((byte) (id - 'A'));
  83. }
  84. }