Refactor part of Rotation logic and disable RotateBlocks command

This commit is contained in:
NGnius 2019-11-19 12:06:02 -05:00
parent c6bf724f40
commit 0eb1ed511d

View file

@ -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<float, float, float>("RotateBlocks", RotateBlocksCommand, "Rotate all blocks (including ground) from their original position");
//CustomCommandUtility.Register<float, float, float>("RotateBlocks", RotateBlocksCommand, "Rotate all blocks (including ground) from their original position");
CustomCommandUtility.Register<float, float, float>("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<RotationEntityStruct>(CommonExclusiveGroups.OWNED_BLOCKS_GROUP, out rotCount);
GridRotationStruct[] gridStructs = this.entitiesDB.QueryEntities<GridRotationStruct>(CommonExclusiveGroups.OWNED_BLOCKS_GROUP, out gridCount);
LocalTransformEntityStruct[] transStructs = this.entitiesDB.QueryEntities<LocalTransformEntityStruct>(CommonExclusiveGroups.OWNED_BLOCKS_GROUP, out transCount);
UECSPhysicsEntityStruct[] phyStructs = this.entitiesDB.QueryEntities<UECSPhysicsEntityStruct>(CommonExclusiveGroups.OWNED_BLOCKS_GROUP, out phyCount);
if (rotCount != transCount || transCount != phyCount)
uint count = entitiesDB.Count<RotationEntityStruct>(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;
RotateSingleBlock(i, eulerAngles);
}
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
});
}
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<RotationEntityStruct>(CommonExclusiveGroups.OWNED_BLOCKS_GROUP, out rotCount);
GridRotationStruct[] gridStructs = this.entitiesDB.QueryEntities<GridRotationStruct>(CommonExclusiveGroups.OWNED_BLOCKS_GROUP, out gridCount);
LocalTransformEntityStruct[] transStructs = this.entitiesDB.QueryEntities<LocalTransformEntityStruct>(CommonExclusiveGroups.OWNED_BLOCKS_GROUP, out transCount);
UECSPhysicsEntityStruct[] phyStructs = this.entitiesDB.QueryEntities<UECSPhysicsEntityStruct>(CommonExclusiveGroups.OWNED_BLOCKS_GROUP, out phyCount);
if (rotCount == 0 || gridCount == 0 || transCount == 0 || phyCount == 0)
uint count = entitiesDB.Count<RotationEntityStruct>(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<RotationEntityStruct>(blockID, CommonExclusiveGroups.OWNED_BLOCKS_GROUP);
ref GridRotationStruct gridStruct = ref this.entitiesDB.QueryEntity<GridRotationStruct>(blockID, CommonExclusiveGroups.OWNED_BLOCKS_GROUP);
ref LocalTransformEntityStruct transStruct = ref this.entitiesDB.QueryEntity<LocalTransformEntityStruct>(blockID, CommonExclusiveGroups.OWNED_BLOCKS_GROUP);
ref UECSPhysicsEntityStruct phyStruct = ref this.entitiesDB.QueryEntity<UECSPhysicsEntityStruct>(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");
}
}
}