Moar Gamecraft commands!
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

168 lignes
8.3KB

  1. using System;
  2. using System.Collections.Generic;
  3. using RobocraftX.Multiplayer;
  4. using RobocraftX.Common;
  5. using Svelto.ECS;
  6. using Svelto.ECS.EntityStructs;
  7. using Unity.Entities;
  8. using Svelto.Context;
  9. using Svelto.Tasks;
  10. using RobocraftX;
  11. using RobocraftX.SimulationModeState;
  12. using RobocraftX.UECS;
  13. using Unity.Transforms;
  14. namespace ExtraCommands.Building
  15. {
  16. [CustomCommand("MoveBlocks", "Move all blocks (including ground) from their original position")]
  17. [CustomCommand("MoveLastBlock", "Move last block from original position")]
  18. class MoveBlocksCommandEngine : CustomCommandEngine
  19. {
  20. public MoveBlocksCommandEngine(UnityContext<FullGameCompositionRoot> ctxHolder, EnginesRoot enginesRoot, World physW, Action reloadGame, MultiplayerInitParameters mpParams) : base(ctxHolder, enginesRoot, physW, reloadGame, mpParams)
  21. {
  22. }
  23. public override void Ready()
  24. {
  25. CustomCommandUtility.Register<float, float, float>("MoveBlocks", MoveBlocksCommand, "Move all blocks (including ground) from their original position");
  26. CustomCommandUtility.Register<float, float, float>("MoveLastBlock", MoveLastBlockCommand, "Move last block from original position");
  27. }
  28. // Move every block by vector (x,y,z)
  29. private void MoveBlocksCommand(float x, float y, float z)
  30. {
  31. ref SimulationModeStateEntityStruct simMode = ref this.entitiesDB.QueryUniqueEntity<SimulationModeStateEntityStruct>(SimulationModeStateExclusiveGroups.GAME_STATE_GROUP);
  32. if (simMode.simulationMode != SimulationMode.Build)
  33. {
  34. uREPL.Log.Error("Blocks can only be moved in Build Mode");
  35. return;
  36. }
  37. uint posCount, gridCount, transCount, phyCount;
  38. PositionEntityStruct[] posStructs = this.entitiesDB.QueryEntities<PositionEntityStruct>(CommonExclusiveGroups.OWNED_BLOCKS_GROUP, out posCount);
  39. GridRotationStruct[] gridStructs = this.entitiesDB.QueryEntities<GridRotationStruct>(CommonExclusiveGroups.OWNED_BLOCKS_GROUP, out gridCount);
  40. LocalTransformEntityStruct[] transStructs = this.entitiesDB.QueryEntities<LocalTransformEntityStruct>(CommonExclusiveGroups.OWNED_BLOCKS_GROUP, out transCount);
  41. UECSPhysicsEntityStruct[] phyStructs = this.entitiesDB.QueryEntities<UECSPhysicsEntityStruct>(CommonExclusiveGroups.OWNED_BLOCKS_GROUP, out phyCount);
  42. if (posCount != gridCount || gridCount != transCount || transCount != phyCount)
  43. {
  44. uREPL.Log.Error("Block lists returned are not the same length!"); // they're arrays, not lists
  45. return;
  46. }
  47. for (uint i = 0; i < posCount; i++)
  48. {
  49. ref PositionEntityStruct posStruct = ref posStructs[i]; // main (persistent) position
  50. posStruct.position.x += x;
  51. posStruct.position.y += y;
  52. posStruct.position.z += z;
  53. ref GridRotationStruct gridStruct = ref gridStructs[i]; // main (persistent) position
  54. gridStruct.position.x += x;
  55. gridStruct.position.y += y;
  56. gridStruct.position.z += z;
  57. ref LocalTransformEntityStruct transStruct = ref transStructs[i]; // rendered position
  58. transStruct.position.x += x;
  59. transStruct.position.y += y;
  60. transStruct.position.z += z;
  61. ref UECSPhysicsEntityStruct phyStruct = ref phyStructs[i]; // collision position
  62. this.physWorld.EntityManager.SetComponentData(phyStruct.uecsEntity, new Translation
  63. {
  64. Value = posStruct.position
  65. });
  66. }
  67. uREPL.Log.Output($"Moved {posCount} blocks");
  68. }
  69. // Move block with highest index by vector (x,y,z)
  70. private void MoveLastBlockCommand(float x, float y, float z)
  71. {
  72. ref SimulationModeStateEntityStruct simMode = ref this.entitiesDB.QueryUniqueEntity<SimulationModeStateEntityStruct>(SimulationModeStateExclusiveGroups.GAME_STATE_GROUP);
  73. if (simMode.simulationMode != SimulationMode.Build)
  74. {
  75. uREPL.Log.Error("Blocks can only be moved in Build Mode");
  76. return;
  77. }
  78. uint posCount, gridCount, transCount, phyCount;
  79. PositionEntityStruct[] posStructs = this.entitiesDB.QueryEntities<PositionEntityStruct>(CommonExclusiveGroups.OWNED_BLOCKS_GROUP, out posCount);
  80. GridRotationStruct[] gridStructs = this.entitiesDB.QueryEntities<GridRotationStruct>(CommonExclusiveGroups.OWNED_BLOCKS_GROUP, out gridCount);
  81. LocalTransformEntityStruct[] transStructs = this.entitiesDB.QueryEntities<LocalTransformEntityStruct>(CommonExclusiveGroups.OWNED_BLOCKS_GROUP, out transCount);
  82. UECSPhysicsEntityStruct[] phyStructs = this.entitiesDB.QueryEntities<UECSPhysicsEntityStruct>(CommonExclusiveGroups.OWNED_BLOCKS_GROUP, out phyCount);
  83. if (posCount == 0 || transCount == 0 || phyCount == 0)
  84. {
  85. uREPL.Log.Error("No block found");
  86. return;
  87. }
  88. ref PositionEntityStruct posStruct = ref posStructs[posCount-1]; // main (persistent) position
  89. posStruct.position.x += x;
  90. posStruct.position.y += y;
  91. posStruct.position.z += z;
  92. ref GridRotationStruct gridStruct = ref gridStructs[gridCount-1]; // main (persistent) position
  93. gridStruct.position.x += x;
  94. gridStruct.position.y += y;
  95. gridStruct.position.z += z;
  96. ref LocalTransformEntityStruct transStruct = ref transStructs[transCount-1]; // rendered position
  97. transStruct.position.x += x;
  98. transStruct.position.y += y;
  99. transStruct.position.z += z;
  100. ref UECSPhysicsEntityStruct phyStruct = ref phyStructs[phyCount-1]; // collision position
  101. this.physWorld.EntityManager.SetComponentData(phyStruct.uecsEntity, new Translation
  102. {
  103. Value = posStruct.position
  104. });
  105. uREPL.Log.Output($"Moved block to ({posStruct.position.x},{posStruct.position.y},{posStruct.position.z})");
  106. }
  107. // unused; for future reference
  108. private void ToggleMode()
  109. {
  110. ref SimulationModeStateEntityStruct ptr = ref this.entitiesDB.QueryUniqueEntity<SimulationModeStateEntityStruct>(SimulationModeStateExclusiveGroups.GAME_STATE_GROUP);
  111. ref SimulationFrameEntityStruct ptr2 = ref this.entitiesDB.QueryUniqueEntity<SimulationFrameEntityStruct>(SimulationFrame.SimulationFrameGroup);
  112. switch (ptr.simulationMode)
  113. {
  114. case SimulationMode.Build:
  115. ptr.simulationMode = SimulationMode.SwitchToSim;
  116. ptr.simulationModeChangeFrame = ptr2.simFrame;
  117. return;
  118. case SimulationMode.SwitchToSim:
  119. case SimulationMode.SwitchToBuild:
  120. return;
  121. case SimulationMode.Simulation:
  122. ptr.simulationMode = SimulationMode.SwitchToBuild;
  123. ptr.simulationModeChangeFrame = ptr2.simFrame;
  124. ptr.rigidBodiesCreated = false;
  125. return;
  126. default:
  127. throw new ArgumentOutOfRangeException();
  128. }
  129. }
  130. // unused; for future reference
  131. private IEnumerator<TaskContract> TriggerSwitchToSimTask()
  132. {
  133. this.ToggleMode();
  134. yield break;
  135. }
  136. // unused; for future reference
  137. private IEnumerator<TaskContract> WaitThenTriggerSwitchToBuildTask()
  138. {
  139. while (true)
  140. {
  141. SimulationModeStateEntityStruct modeStruct = this.entitiesDB.QueryUniqueEntity<SimulationModeStateEntityStruct>(SimulationModeStateExclusiveGroups.GAME_STATE_GROUP);
  142. if (modeStruct.simulationMode == SimulationMode.Simulation)
  143. {
  144. this.ToggleMode();
  145. break;
  146. } else
  147. {
  148. yield return Yield.It;
  149. }
  150. }
  151. yield break;
  152. }
  153. public override void Dispose()
  154. {
  155. CustomCommandUtility.Unregister("MoveBlocks");
  156. CustomCommandUtility.Unregister("MoveLastBlock");
  157. }
  158. }
  159. }