Moar Gamecraft commands!
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

151 řádky
6.8KB

  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. using Unity.Mathematics;
  15. using UnityEngine;
  16. namespace ExtraCommands.Building
  17. {
  18. //[CustomCommand("RotateBlocks", "Rotate all blocks (including ground) from their original position")]
  19. [CustomCommand("RotateLastBlock", "Rotate last block from original position")]
  20. class RotateBlocksCommandEngine : CustomCommandEngine
  21. {
  22. public RotateBlocksCommandEngine(UnityContext<FullGameCompositionRoot> ctxHolder, EnginesRoot enginesRoot, World physW, Action reloadGame, MultiplayerInitParameters mpParams) : base(ctxHolder, enginesRoot, physW, reloadGame, mpParams)
  23. {
  24. }
  25. public override void Ready()
  26. {
  27. //CustomCommandUtility.Register<float, float, float>("RotateBlocks", RotateBlocksCommand, "Rotate all blocks (including ground) from their original position");
  28. CustomCommandUtility.Register<float, float, float>("RotateLastBlock", RotateLastBlockCommand, "Rotate last block from original position");
  29. }
  30. // Move every block by vector (x,y,z)
  31. private void RotateBlocksCommand(float x, float y, float z)
  32. {
  33. Vector3 eulerAngles = new Vector3(x, y, z);
  34. ref SimulationModeStateEntityStruct simMode = ref this.entitiesDB.QueryUniqueEntity<SimulationModeStateEntityStruct>(SimulationModeStateExclusiveGroups.GAME_STATE_GROUP);
  35. if (simMode.simulationMode != SimulationMode.Build)
  36. {
  37. uREPL.Log.Error("Blocks can only be moved in Build Mode");
  38. return;
  39. }
  40. uint count = entitiesDB.Count<RotationEntityStruct>(CommonExclusiveGroups.OWNED_BLOCKS_GROUP);
  41. for (uint i = 0; i < count; i++)
  42. {
  43. RotateSingleBlock(i, eulerAngles);
  44. }
  45. uREPL.Log.Output($"Moved {count} blocks");
  46. }
  47. // Move block with highest index by vector (x,y,z)
  48. private void RotateLastBlockCommand(float x, float y, float z)
  49. {
  50. Vector3 eulerAngles = new Vector3(x, y, z);
  51. ref SimulationModeStateEntityStruct simMode = ref this.entitiesDB.QueryUniqueEntity<SimulationModeStateEntityStruct>(SimulationModeStateExclusiveGroups.GAME_STATE_GROUP);
  52. if (simMode.simulationMode != SimulationMode.Build)
  53. {
  54. uREPL.Log.Error("Blocks can only be moved in Build Mode");
  55. return;
  56. }
  57. uint count = entitiesDB.Count<RotationEntityStruct>(CommonExclusiveGroups.OWNED_BLOCKS_GROUP);
  58. if (count == 0)
  59. {
  60. uREPL.Log.Error("No block found");
  61. return;
  62. }
  63. Vector3 newRotation = RotateSingleBlock(count - 1, eulerAngles);
  64. uREPL.Log.Output($"Rotated block to ({newRotation.x},{newRotation.y},{newRotation.z})");
  65. }
  66. private float3 RotateSingleBlock(uint blockID, Vector3 rotationVector)
  67. {
  68. ref RotationEntityStruct rotStruct = ref this.entitiesDB.QueryEntity<RotationEntityStruct>(blockID, CommonExclusiveGroups.OWNED_BLOCKS_GROUP);
  69. ref GridRotationStruct gridStruct = ref this.entitiesDB.QueryEntity<GridRotationStruct>(blockID, CommonExclusiveGroups.OWNED_BLOCKS_GROUP);
  70. ref LocalTransformEntityStruct transStruct = ref this.entitiesDB.QueryEntity<LocalTransformEntityStruct>(blockID, CommonExclusiveGroups.OWNED_BLOCKS_GROUP);
  71. ref UECSPhysicsEntityStruct phyStruct = ref this.entitiesDB.QueryEntity<UECSPhysicsEntityStruct>(blockID, CommonExclusiveGroups.OWNED_BLOCKS_GROUP);
  72. // main (persistent) position
  73. Quaternion newRotation = (Quaternion)rotStruct.rotation;
  74. newRotation.eulerAngles += rotationVector;
  75. rotStruct.rotation = (quaternion)newRotation;
  76. // placement grid rotation
  77. Quaternion newGridRotation = (Quaternion)gridStruct.rotation;
  78. newGridRotation.eulerAngles += rotationVector;
  79. gridStruct.rotation = (quaternion)newGridRotation;
  80. // rendered position
  81. Quaternion newTransRotation = (Quaternion)rotStruct.rotation;
  82. newTransRotation.eulerAngles += rotationVector;
  83. transStruct.rotation = newTransRotation;
  84. // collision position
  85. this.physWorld.EntityManager.SetComponentData(phyStruct.uecsEntity, new Rotation
  86. {
  87. Value = rotStruct.rotation
  88. });
  89. return ((Quaternion)rotStruct.rotation).eulerAngles;
  90. }
  91. // unused; for future reference
  92. private void ToggleMode()
  93. {
  94. ref SimulationModeStateEntityStruct ptr = ref this.entitiesDB.QueryUniqueEntity<SimulationModeStateEntityStruct>(SimulationModeStateExclusiveGroups.GAME_STATE_GROUP);
  95. ref SimulationFrameEntityStruct ptr2 = ref this.entitiesDB.QueryUniqueEntity<SimulationFrameEntityStruct>(SimulationFrame.SimulationFrameGroup);
  96. switch (ptr.simulationMode)
  97. {
  98. case SimulationMode.Build:
  99. ptr.nextSimulationMode = SimulationMode.SwitchToSim;
  100. return;
  101. case SimulationMode.SwitchToSim:
  102. case SimulationMode.SwitchToBuild:
  103. return;
  104. case SimulationMode.Simulation:
  105. ptr.nextSimulationMode = SimulationMode.SwitchToBuild;
  106. ptr.rigidBodiesCreated = false;
  107. return;
  108. default:
  109. throw new ArgumentOutOfRangeException();
  110. }
  111. }
  112. // unused; for future reference
  113. private IEnumerator<TaskContract> TriggerSwitchToSimTask()
  114. {
  115. this.ToggleMode();
  116. yield break;
  117. }
  118. // unused; for future reference
  119. private IEnumerator<TaskContract> WaitThenTriggerSwitchToBuildTask()
  120. {
  121. while (true)
  122. {
  123. SimulationModeStateEntityStruct modeStruct = this.entitiesDB.QueryUniqueEntity<SimulationModeStateEntityStruct>(SimulationModeStateExclusiveGroups.GAME_STATE_GROUP);
  124. if (modeStruct.simulationMode == SimulationMode.Simulation)
  125. {
  126. this.ToggleMode();
  127. break;
  128. } else
  129. {
  130. yield return Yield.It;
  131. }
  132. }
  133. yield break;
  134. }
  135. public override void Dispose()
  136. {
  137. //CustomCommandUtility.Unregister("RotateBlocks");
  138. CustomCommandUtility.Unregister("RotateLastBlock");
  139. }
  140. }
  141. }