From 0eb1ed511d857e7130f565e437154a9ec2cb3fca Mon Sep 17 00:00:00 2001 From: NGnius Date: Tue, 19 Nov 2019 12:06:02 -0500 Subject: [PATCH] Refactor part of Rotation logic and disable RotateBlocks command --- extracommands/RotateBlocksCommandEngine.cs | 75 ++++++++-------------- 1 file changed, 28 insertions(+), 47 deletions(-) diff --git a/extracommands/RotateBlocksCommandEngine.cs b/extracommands/RotateBlocksCommandEngine.cs index 21d2f9d..e8c277f 100644 --- a/extracommands/RotateBlocksCommandEngine.cs +++ b/extracommands/RotateBlocksCommandEngine.cs @@ -16,7 +16,7 @@ using UnityEngine; namespace ExtraCommands.Building { - [CustomCommand("RotateBlocks", "Rotate all blocks (including ground) from their original position")] + //[CustomCommand("RotateBlocks", "Rotate all blocks (including ground) from their original position")] [CustomCommand("RotateLastBlock", "Rotate last block from original position")] class RotateBlocksCommandEngine : CustomCommandEngine { @@ -26,7 +26,7 @@ namespace ExtraCommands.Building public override void Ready() { - CustomCommandUtility.Register("RotateBlocks", RotateBlocksCommand, "Rotate all blocks (including ground) from their original position"); + //CustomCommandUtility.Register("RotateBlocks", RotateBlocksCommand, "Rotate all blocks (including ground) from their original position"); CustomCommandUtility.Register("RotateLastBlock", RotateLastBlockCommand, "Rotate last block from original position"); } @@ -40,37 +40,12 @@ namespace ExtraCommands.Building uREPL.Log.Error("Blocks can only be moved in Build Mode"); return; } - uint rotCount, gridCount, transCount, phyCount; - RotationEntityStruct[] rotStructs = this.entitiesDB.QueryEntities(CommonExclusiveGroups.OWNED_BLOCKS_GROUP, out rotCount); - GridRotationStruct[] gridStructs = this.entitiesDB.QueryEntities(CommonExclusiveGroups.OWNED_BLOCKS_GROUP, out gridCount); - LocalTransformEntityStruct[] transStructs = this.entitiesDB.QueryEntities(CommonExclusiveGroups.OWNED_BLOCKS_GROUP, out transCount); - UECSPhysicsEntityStruct[] phyStructs = this.entitiesDB.QueryEntities(CommonExclusiveGroups.OWNED_BLOCKS_GROUP, out phyCount); - if (rotCount != transCount || transCount != phyCount) + uint count = entitiesDB.Count(CommonExclusiveGroups.OWNED_BLOCKS_GROUP); + for (uint i = 0; i < count; i++) { - uREPL.Log.Error("Block lists returned are not the same length!"); // they're arrays, not lists - return; - } - for (uint i = 0; i < rotCount; i++) - { - ref RotationEntityStruct rotStruct = ref rotStructs[i]; // main (persistent) rotation - Quaternion newRotation = (Quaternion)rotStruct.rotation; - newRotation.eulerAngles += eulerAngles; - rotStruct.rotation = (quaternion)newRotation; - ref GridRotationStruct gridStruct = ref gridStructs[i]; // placement grid rotation - Quaternion newGridRotation = (Quaternion)gridStruct.rotation; - newGridRotation.eulerAngles += eulerAngles; - gridStruct.rotation = (quaternion)newGridRotation; - ref LocalTransformEntityStruct transStruct = ref transStructs[i]; // rendered position - Quaternion newTransRotation = (Quaternion)rotStruct.rotation; - newTransRotation.eulerAngles += eulerAngles; - transStruct.rotation = newTransRotation; - ref UECSPhysicsEntityStruct phyStruct = ref phyStructs[i]; // collision position - this.physWorld.EntityManager.SetComponentData(phyStruct.uecsEntity, new Rotation - { - Value = rotStruct.rotation - }); + RotateSingleBlock(i, eulerAngles); } - uREPL.Log.Output($"Moved {rotCount} blocks"); + uREPL.Log.Output($"Moved {count} blocks"); } // Move block with highest index by vector (x,y,z) @@ -83,34 +58,40 @@ namespace ExtraCommands.Building uREPL.Log.Error("Blocks can only be moved in Build Mode"); return; } - uint rotCount, gridCount, transCount, phyCount; - RotationEntityStruct[] rotStructs = this.entitiesDB.QueryEntities(CommonExclusiveGroups.OWNED_BLOCKS_GROUP, out rotCount); - GridRotationStruct[] gridStructs = this.entitiesDB.QueryEntities(CommonExclusiveGroups.OWNED_BLOCKS_GROUP, out gridCount); - LocalTransformEntityStruct[] transStructs = this.entitiesDB.QueryEntities(CommonExclusiveGroups.OWNED_BLOCKS_GROUP, out transCount); - UECSPhysicsEntityStruct[] phyStructs = this.entitiesDB.QueryEntities(CommonExclusiveGroups.OWNED_BLOCKS_GROUP, out phyCount); - if (rotCount == 0 || gridCount == 0 || transCount == 0 || phyCount == 0) + uint count = entitiesDB.Count(CommonExclusiveGroups.OWNED_BLOCKS_GROUP); + if (count == 0) { uREPL.Log.Error("No block found"); return; } - ref RotationEntityStruct rotStruct = ref rotStructs[rotCount-1]; // main (persistent) position + Vector3 newRotation = RotateSingleBlock(count - 1, eulerAngles); + uREPL.Log.Output($"Rotated block to ({newRotation.x},{newRotation.y},{newRotation.z})"); + } + + private float3 RotateSingleBlock(uint blockID, Vector3 rotationVector) + { + ref RotationEntityStruct rotStruct = ref this.entitiesDB.QueryEntity(blockID, CommonExclusiveGroups.OWNED_BLOCKS_GROUP); + ref GridRotationStruct gridStruct = ref this.entitiesDB.QueryEntity(blockID, CommonExclusiveGroups.OWNED_BLOCKS_GROUP); + ref LocalTransformEntityStruct transStruct = ref this.entitiesDB.QueryEntity(blockID, CommonExclusiveGroups.OWNED_BLOCKS_GROUP); + ref UECSPhysicsEntityStruct phyStruct = ref this.entitiesDB.QueryEntity(blockID, CommonExclusiveGroups.OWNED_BLOCKS_GROUP); + // main (persistent) position Quaternion newRotation = (Quaternion)rotStruct.rotation; - newRotation.eulerAngles += eulerAngles; + newRotation.eulerAngles += rotationVector; rotStruct.rotation = (quaternion)newRotation; - ref GridRotationStruct gridStruct = ref gridStructs[gridCount-1]; // placement grid rotation + // placement grid rotation Quaternion newGridRotation = (Quaternion)gridStruct.rotation; - newGridRotation.eulerAngles += eulerAngles; + newGridRotation.eulerAngles += rotationVector; gridStruct.rotation = (quaternion)newGridRotation; - ref LocalTransformEntityStruct transStruct = ref transStructs[transCount-1]; // rendered position + // rendered position Quaternion newTransRotation = (Quaternion)rotStruct.rotation; - newTransRotation.eulerAngles += eulerAngles; + newTransRotation.eulerAngles += rotationVector; transStruct.rotation = newTransRotation; - ref UECSPhysicsEntityStruct phyStruct = ref phyStructs[phyCount-1]; // collision position + // collision position this.physWorld.EntityManager.SetComponentData(phyStruct.uecsEntity, new Rotation { Value = rotStruct.rotation }); - uREPL.Log.Output($"Rotated block to ({rotStruct.rotation.value.x},{rotStruct.rotation.value.y},{rotStruct.rotation.value.z})"); + return ((Quaternion)rotStruct.rotation).eulerAngles; } // unused; for future reference @@ -164,8 +145,8 @@ namespace ExtraCommands.Building public override void Dispose() { - CustomCommandUtility.Unregister("MoveBlocks"); - CustomCommandUtility.Unregister("MoveLastBlock"); + //CustomCommandUtility.Unregister("RotateBlocks"); + CustomCommandUtility.Unregister("RotateLastBlock"); } } }