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.

88 lines
3.2KB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using RobocraftX;
  7. using RobocraftX.Blocks;
  8. using RobocraftX.Blocks.Ghost;
  9. using RobocraftX.Common;
  10. using RobocraftX.Multiplayer;
  11. using RobocraftX.SimulationModeState;
  12. using RobocraftX.UECS;
  13. using Unity.Entities;
  14. using Svelto.Context;
  15. using Svelto.ECS;
  16. using Svelto.ECS.EntityStructs;
  17. using Unity.Transforms;
  18. using Unity.Mathematics;
  19. using UnityEngine;
  20. using GamecraftModdingAPI.Utility;
  21. namespace GamecraftModdingAPI.Blocks
  22. {
  23. /// <summary>
  24. /// Engine which executes block movement actions
  25. /// </summary>
  26. public class MovementEngine : IApiEngine
  27. {
  28. public string Name { get; } = "GamecraftModdingAPIMovementGameEngine";
  29. public IEntitiesDB entitiesDB { set; private get; }
  30. public bool IsInGame = false;
  31. public void Dispose()
  32. {
  33. IsInGame = false;
  34. }
  35. public void Ready()
  36. {
  37. IsInGame = true;
  38. }
  39. // implementations for Movement static class
  40. public float3 MoveBlock(uint blockID, float3 vector)
  41. {
  42. ref PositionEntityStruct posStruct = ref this.entitiesDB.QueryEntity<PositionEntityStruct>(blockID, CommonExclusiveGroups.OWNED_BLOCKS_GROUP);
  43. ref GridRotationStruct gridStruct = ref this.entitiesDB.QueryEntity<GridRotationStruct>(blockID, CommonExclusiveGroups.OWNED_BLOCKS_GROUP);
  44. ref LocalTransformEntityStruct transStruct = ref this.entitiesDB.QueryEntity<LocalTransformEntityStruct>(blockID, CommonExclusiveGroups.OWNED_BLOCKS_GROUP);
  45. ref UECSPhysicsEntityStruct phyStruct = ref this.entitiesDB.QueryEntity<UECSPhysicsEntityStruct>(blockID, CommonExclusiveGroups.OWNED_BLOCKS_GROUP);
  46. // main (persistent) position
  47. posStruct.position += vector;
  48. // placement grid position
  49. gridStruct.position += vector;
  50. // rendered position
  51. transStruct.position += vector;
  52. // collision position
  53. FullGameFields._physicsWorld.EntityManager.SetComponentData(phyStruct.uecsEntity, new Translation
  54. {
  55. Value = posStruct.position
  56. });
  57. return posStruct.position;
  58. }
  59. public float3 MoveConnectedBlocks(uint blockID, float3 vector)
  60. {
  61. Stack<uint> cubeStack = new Stack<uint>();
  62. Gamecraft.DataStructures.FasterList<uint> cubesToMove = new Gamecraft.DataStructures.FasterList<uint>();
  63. ConnectedCubesUtility.TreeTraversal.GetConnectedCubes(entitiesDB, blockID, cubeStack, cubesToMove, (in GridConnectionsEntityStruct g) => { return false; });
  64. for (int i = 0; i < cubesToMove.Count; i++)
  65. {
  66. MoveBlock(cubesToMove[i], vector);
  67. entitiesDB.QueryEntity<GridConnectionsEntityStruct>(cubesToMove[i], CommonExclusiveGroups.OWNED_BLOCKS_GROUP).isProcessed = false;
  68. }
  69. return this.entitiesDB.QueryEntity<PositionEntityStruct>(blockID, CommonExclusiveGroups.OWNED_BLOCKS_GROUP).position;
  70. }
  71. public bool IsBuildMode()
  72. {
  73. return GamecraftModdingAPI.Utility.GameState.IsBuildMode();
  74. }
  75. }
  76. }