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.

91 lines
3.6KB

  1. using RobocraftX.Common;
  2. using RobocraftX.UECS;
  3. using Svelto.ECS;
  4. using Svelto.ECS.EntityStructs;
  5. using Unity.Mathematics;
  6. using UnityEngine;
  7. using GamecraftModdingAPI.Utility;
  8. using GamecraftModdingAPI.Engines;
  9. namespace GamecraftModdingAPI.Blocks
  10. {
  11. /// <summary>
  12. /// Engine which executes block movement actions
  13. /// </summary>
  14. public class RotationEngine : IApiEngine
  15. {
  16. public string Name { get; } = "GamecraftModdingAPIRotationGameEngine";
  17. public EntitiesDB entitiesDB { set; private get; }
  18. public bool isRemovable => false;
  19. public bool IsInGame = false;
  20. public void Dispose()
  21. {
  22. IsInGame = false;
  23. }
  24. public void Ready()
  25. {
  26. IsInGame = true;
  27. }
  28. // implementations for Rotation static class
  29. internal float3 RotateBlock(EGID blockID, BlockEngine.BlockInitData data, Vector3 vector)
  30. {
  31. if (!entitiesDB.Exists<RotationEntityStruct>(blockID))
  32. {
  33. if (data.Group == null) return float3.zero;
  34. var init = new EntityInitializer(blockID, data.Group);
  35. init.GetOrCreate<RotationEntityStruct>().rotation = Quaternion.Euler(vector);
  36. init.GetOrCreate<GridRotationStruct>().rotation = Quaternion.Euler(vector);
  37. init.GetOrCreate<LocalTransformEntityStruct>().rotation = Quaternion.Euler(vector);
  38. return vector;
  39. }
  40. ref RotationEntityStruct rotStruct = ref this.entitiesDB.QueryEntity<RotationEntityStruct>(blockID);
  41. ref GridRotationStruct gridStruct = ref this.entitiesDB.QueryEntity<GridRotationStruct>(blockID);
  42. ref LocalTransformEntityStruct transStruct = ref this.entitiesDB.QueryEntity<LocalTransformEntityStruct>(blockID);
  43. ref UECSPhysicsEntityStruct phyStruct = ref this.entitiesDB.QueryEntity<UECSPhysicsEntityStruct>(blockID);
  44. // main (persistent) position
  45. Quaternion newRotation = rotStruct.rotation;
  46. newRotation.eulerAngles = vector;
  47. rotStruct.rotation = newRotation;
  48. // placement grid rotation
  49. Quaternion newGridRotation = gridStruct.rotation;
  50. newGridRotation.eulerAngles = vector;
  51. gridStruct.rotation = newGridRotation;
  52. // rendered position
  53. Quaternion newTransRotation = rotStruct.rotation;
  54. newTransRotation.eulerAngles = vector;
  55. transStruct.rotation = newTransRotation;
  56. // collision position
  57. FullGameFields._physicsWorld.EntityManager.SetComponentData(phyStruct.uecsEntity, new Unity.Transforms.Rotation
  58. {
  59. Value = rotStruct.rotation
  60. });
  61. entitiesDB.QueryEntity<GridConnectionsEntityStruct>(blockID).isProcessed = false;
  62. return ((Quaternion)rotStruct.rotation).eulerAngles;
  63. }
  64. internal float3 GetRotation(EGID blockID, BlockEngine.BlockInitData data)
  65. {
  66. if (!entitiesDB.Exists<RotationEntityStruct>(blockID))
  67. {
  68. if (data.Group == null) return float3.zero;
  69. var init = new EntityInitializer(blockID, data.Group);
  70. return init.Has<RotationEntityStruct>()
  71. ? (float3) ((Quaternion) init.Get<RotationEntityStruct>().rotation).eulerAngles
  72. : float3.zero;
  73. }
  74. ref RotationEntityStruct rotStruct = ref entitiesDB.QueryEntity<RotationEntityStruct>(blockID);
  75. return ((Quaternion) rotStruct.rotation).eulerAngles;
  76. }
  77. }
  78. }