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.

87 lines
3.1KB

  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 Svelto.DataStructures;
  21. using GamecraftModdingAPI.Utility;
  22. using GamecraftModdingAPI.Engines;
  23. namespace GamecraftModdingAPI.Blocks
  24. {
  25. /// <summary>
  26. /// Engine which executes block movement actions
  27. /// </summary>
  28. public class MovementEngine : IApiEngine
  29. {
  30. public string Name { get; } = "GamecraftModdingAPIMovementGameEngine";
  31. public EntitiesDB entitiesDB { set; private get; }
  32. public bool isRemovable => false;
  33. public bool IsInGame = false;
  34. public void Dispose()
  35. {
  36. IsInGame = false;
  37. }
  38. public void Ready()
  39. {
  40. IsInGame = true;
  41. }
  42. // implementations for Movement static class
  43. public float3 MoveBlock(uint blockID, float3 vector)
  44. {
  45. ref PositionEntityStruct posStruct = ref this.entitiesDB.QueryEntity<PositionEntityStruct>(blockID, CommonExclusiveGroups.OWNED_BLOCKS_GROUP);
  46. ref GridRotationStruct gridStruct = ref this.entitiesDB.QueryEntity<GridRotationStruct>(blockID, CommonExclusiveGroups.OWNED_BLOCKS_GROUP);
  47. ref LocalTransformEntityStruct transStruct = ref this.entitiesDB.QueryEntity<LocalTransformEntityStruct>(blockID, CommonExclusiveGroups.OWNED_BLOCKS_GROUP);
  48. ref UECSPhysicsEntityStruct phyStruct = ref this.entitiesDB.QueryEntity<UECSPhysicsEntityStruct>(blockID, CommonExclusiveGroups.OWNED_BLOCKS_GROUP);
  49. // main (persistent) position
  50. posStruct.position += vector;
  51. // placement grid position
  52. gridStruct.position += vector;
  53. // rendered position
  54. transStruct.position += vector;
  55. // collision position
  56. FullGameFields._physicsWorld.EntityManager.SetComponentData(phyStruct.uecsEntity, new Translation
  57. {
  58. Value = posStruct.position
  59. });
  60. return posStruct.position;
  61. }
  62. public float3 MoveConnectedBlocks(uint blockID, float3 vector)
  63. {
  64. Stack<uint> cubeStack = new Stack<uint>();
  65. FasterList<uint> cubesToMove = new FasterList<uint>();
  66. ConnectedCubesUtility.TreeTraversal.GetConnectedCubes(entitiesDB, blockID, cubeStack, cubesToMove, (in GridConnectionsEntityStruct g) => { return false; });
  67. for (int i = 0; i < cubesToMove.count; i++)
  68. {
  69. MoveBlock(cubesToMove[i], vector);
  70. entitiesDB.QueryEntity<GridConnectionsEntityStruct>(cubesToMove[i], CommonExclusiveGroups.OWNED_BLOCKS_GROUP).isProcessed = false;
  71. }
  72. return this.entitiesDB.QueryEntity<PositionEntityStruct>(blockID, CommonExclusiveGroups.OWNED_BLOCKS_GROUP).position;
  73. }
  74. }
  75. }