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.

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