// Continue an existing rotation void continueRotation() { bool finished = false; // Degrees to rotate float degrees = runningRotation.rotateSpeed * Time.deltaTime; // If it over-rotates then correct rotation and finish. runningRotation.totalRotated += Math.Abs(degrees); if (runningRotation.totalRotated >= 90) { degrees = (runningRotation.totalRotated - 90) * (degrees < 0 ? -1 : 1); finished = true; } // Rotate each cube part around middle cube foreach (CubePart cp in runningRotation.parts) { cp.doRotation(runningRotation.middle.transform.position, runningRotation.rotateAround, degrees); } if (finished) { rotating = false; runningRotation = null; } }
// Do a rotation void startRotation() { Rotation rotation = rotations.Dequeue(); Debug.Log("Perform rotation: " + rotation); // Find side to rotate in relation to center cube CubePart[] toRotate = getCubePartsToRotate(rotation); Debug.Log("Found " + toRotate.Length + " parts to rotate."); // Find middle cube to rotate around // TODO: Store these in a rotation to cubepart hashmap from the start CubePart middle = null; foreach (CubePart mp in middles) { middle = Array.Find(toRotate, cp => cp == mp); if (middle != null) { break; } } if (middle == null) { throw new BadProgrammingException("Could not find a middle to rotate around."); } Vector3 rotateAround = getAxisToRotateAround(rotation, middle); float rotateSpeed = getRotateSpeed(rotation); rotating = true; runningRotation = new RunningRotation(rotation, toRotate, middle, rotateAround, rotateSpeed); }