|
- using System;
- using System.Collections.Generic;
- using RobocraftX.Multiplayer;
- using RobocraftX.Common;
- using RobocraftX.Blocks;
- using Svelto.ECS;
- using Svelto.ECS.EntityStructs;
- using Unity.Entities;
- using Svelto.Context;
- using Svelto.DataStructures;
- using Svelto.Tasks;
- using RobocraftX;
- using RobocraftX.SimulationModeState;
- using RobocraftX.UECS;
- using Unity.Transforms;
-
- namespace ExtraCommands.Building
- {
- [CustomCommand("MoveBlocks", "Move all blocks (including ground) from their original position")]
- [CustomCommand("MoveLastBlock", "Move last block from original position")]
- class MoveBlocksCommandEngine : CustomCommandEngine
- {
- public MoveBlocksCommandEngine(UnityContext<FullGameCompositionRoot> ctxHolder, EnginesRoot enginesRoot, World physW, Action reloadGame, MultiplayerInitParameters mpParams) : base(ctxHolder, enginesRoot, physW, reloadGame, mpParams)
- {
- }
-
- public override void Ready()
- {
- CustomCommandUtility.Register<float, float, float>("MoveBlocks", MoveBlocksCommand, "Move all blocks (including ground) from their original position");
- CustomCommandUtility.Register<float, float, float>("MoveLastBlock", MoveLastBlockCommand, "Move last block from original position");
- }
-
- // Move every block by vector (x,y,z)
- private void MoveBlocksCommand(float x, float y, float z)
- {
- ref SimulationModeStateEntityStruct simMode = ref this.entitiesDB.QueryUniqueEntity<SimulationModeStateEntityStruct>(SimulationModeStateExclusiveGroups.GAME_STATE_GROUP);
- if (simMode.simulationMode != SimulationMode.Build)
- {
- uREPL.Log.Error("Blocks can only be moved in Build Mode");
- return;
- }
- uint count = this.entitiesDB.Count<PositionEntityStruct>(CommonExclusiveGroups.OWNED_BLOCKS_GROUP);
- float3 translationVector = new float3(x,y,z);
- for (uint i = 0; i < count; i++)
- {
- TranslateSingleBlock(i, translationVector);
- }
- uREPL.Log.Output($"Moved {count} blocks");
- }
-
- // Move block with highest index by vector (x,y,z)
- private void MoveLastBlockCommand(float x, float y, float z)
- {
- ref SimulationModeStateEntityStruct simMode = ref this.entitiesDB.QueryUniqueEntity<SimulationModeStateEntityStruct>(SimulationModeStateExclusiveGroups.GAME_STATE_GROUP);
- if (simMode.simulationMode != SimulationMode.Build)
- {
- uREPL.Log.Error("Blocks can only be moved in Build Mode");
- return;
- }
- uint count = this.entitiesDB.Count<PositionEntityStruct>(CommonExclusiveGroups.OWNED_BLOCKS_GROUP);
- float3 newPos = TranslateConnectedBlocks(count-1, new float3(x,y,z));
- uREPL.Log.Output($"Moved block to ({newPos.x},{newPos.y},{newPos.z})");
- }
-
- // Move the block denoted by blockID by translationVector (old_position += translationVector)
- private float3 TranslateSingleBlock(uint blockID, float3 translationVector)
- {
- ref PositionEntityStruct posStruct = ref this.entitiesDB.QueryEntity<PositionEntityStruct>(blockID, CommonExclusiveGroups.OWNED_BLOCKS_GROUP);
- ref GridRotationStruct gridStructs = this.entitiesDB.QueryEntity<GridRotationStruct>(blockID, CommonExclusiveGroups.OWNED_BLOCKS_GROUP);
- ref LocalTransformEntityStruct transStructs = this.entitiesDB.QueryEntity<LocalTransformEntityStruct>(blockID, CommonExclusiveGroups.OWNED_BLOCKS_GROUP);
- ref UECSPhysicsEntityStruct phyStructs = this.entitiesDB.QueryEntity<UECSPhysicsEntityStruct>(blockID, CommonExclusiveGroups.OWNED_BLOCKS_GROUP);
- // main (persistent) position
- posStruct.position.x += translationVector.x;
- posStruct.position.y += translationVector.y;
- posStruct.position.z += translationVector.z;
- // placement grid position
- gridStruct.position.x += translationVector.x;
- gridStruct.position.y += translationVector.y;
- gridStruct.position.z += translationVector.z;
- // rendered position
- transStruct.position.x += translationVector.x;
- transStruct.position.y += translationVector.y;
- transStruct.position.z += translationVector.z;
- // collision position
- this.physWorld.EntityManager.SetComponentData(phyStruct.uecsEntity, new Translation
- {
- Value = posStruct.position
- });
- return posStruct.position;
- }
-
- private float3 TranslateConnectedBlocks(uint blockID, float3 translationVector)
- {
- newPosition = TranslateSingleBlock(blockID, translationVector);
- uint count = this.entitiesDB.Count<PositionEntityStruct>(CommonExclusiveGroups.OWNED_BLOCKS_GROUP);
- Stack<uint> cubeStack = new Stack<uint>(count);
- FasterList<uint> cubeList = new FasterList<uint>(count);
- ConnectedCubesUtility.TreeTraversal.GetConnectedCubes(this.entitiesDB, blockID, cubeStack, cubeList, false /*unused*/);
- foreach (uint id in FasterList.ToArrayFast(cubeList))
- {
- TranslateSingleBlock(id, translationVector);
- }
- return newPosition;
- }
-
- // unused; for future reference
- private void ToggleMode()
- {
- ref SimulationModeStateEntityStruct ptr = ref this.entitiesDB.QueryUniqueEntity<SimulationModeStateEntityStruct>(SimulationModeStateExclusiveGroups.GAME_STATE_GROUP);
- ref SimulationFrameEntityStruct ptr2 = ref this.entitiesDB.QueryUniqueEntity<SimulationFrameEntityStruct>(SimulationFrame.SimulationFrameGroup);
- switch (ptr.simulationMode)
- {
- case SimulationMode.Build:
- ptr.simulationMode = SimulationMode.SwitchToSim;
- ptr.simulationModeChangeFrame = ptr2.simFrame;
- return;
- case SimulationMode.SwitchToSim:
- case SimulationMode.SwitchToBuild:
- return;
- case SimulationMode.Simulation:
- ptr.simulationMode = SimulationMode.SwitchToBuild;
- ptr.simulationModeChangeFrame = ptr2.simFrame;
- ptr.rigidBodiesCreated = false;
- return;
- default:
- throw new ArgumentOutOfRangeException();
- }
- }
-
- // unused; for future reference
- private IEnumerator<TaskContract> TriggerSwitchToSimTask()
- {
- this.ToggleMode();
- yield break;
- }
-
- // unused; for future reference
- private IEnumerator<TaskContract> WaitThenTriggerSwitchToBuildTask()
- {
- while (true)
- {
- SimulationModeStateEntityStruct modeStruct = this.entitiesDB.QueryUniqueEntity<SimulationModeStateEntityStruct>(SimulationModeStateExclusiveGroups.GAME_STATE_GROUP);
- if (modeStruct.simulationMode == SimulationMode.Simulation)
- {
- this.ToggleMode();
- break;
- } else
- {
- yield return Yield.It;
- }
- }
- yield break;
- }
-
- public override void Dispose()
- {
- CustomCommandUtility.Unregister("MoveBlocks");
- CustomCommandUtility.Unregister("MoveLastBlock");
- }
- }
- }
|