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.

81 lines
3.0KB

  1. using RobocraftX.Common;
  2. using RobocraftX.UECS;
  3. using Svelto.ECS;
  4. using Svelto.ECS.EntityStructs;
  5. using Unity.Transforms;
  6. using Unity.Mathematics;
  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 MovementEngine : IApiEngine
  15. {
  16. public string Name { get; } = "GamecraftModdingAPIMovementGameEngine";
  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 Movement static class
  29. internal float3 MoveBlock(EGID blockID, BlockEngine.BlockInitData data, float3 vector)
  30. {
  31. if (!entitiesDB.Exists<PositionEntityStruct>(blockID))
  32. {
  33. if (data.Group == null) return float3.zero;
  34. var init = new EntityComponentInitializer(blockID, data.Group);
  35. init.Init(new PositionEntityStruct {position = vector});
  36. init.Init(new GridRotationStruct {position = vector});
  37. init.Init(new LocalTransformEntityStruct {position = vector});
  38. return vector;
  39. }
  40. ref PositionEntityStruct posStruct = ref this.entitiesDB.QueryEntity<PositionEntityStruct>(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. posStruct.position = vector;
  46. // placement grid position
  47. gridStruct.position = vector;
  48. // rendered position
  49. transStruct.position = vector;
  50. // collision position
  51. FullGameFields._physicsWorld.EntityManager.SetComponentData(phyStruct.uecsEntity, new Translation
  52. {
  53. Value = posStruct.position
  54. });
  55. entitiesDB.QueryEntity<GridConnectionsEntityStruct>(blockID).isProcessed = false;
  56. return posStruct.position;
  57. }
  58. internal float3 GetPosition(EGID blockID, BlockEngine.BlockInitData data)
  59. {
  60. if (!entitiesDB.Exists<PositionEntityStruct>(blockID))
  61. {
  62. if (data.Group == null) return float3.zero;
  63. var init = new EntityComponentInitializer(blockID, data.Group);
  64. return init.Has<PositionEntityStruct>() ? init.Get<PositionEntityStruct>().position : float3.zero;
  65. }
  66. ref PositionEntityStruct posStruct = ref this.entitiesDB.QueryEntity<PositionEntityStruct>(blockID);
  67. return posStruct.position;
  68. }
  69. }
  70. }