// returns 1.0 if going clockwise, -1.0 if going counter-clockwise, 0 if no rotation. private static float gun_rotation_factor(GunsRotation rotation_direction) { switch (rotation_direction) { case GunsRotation.rotate_clockwise: return(1.0f); case GunsRotation.rotate_counter_clockwise: return(-1.0f); default: return(0.0f); } }
private static AxesDirections next_axis(float current_angle, GunsRotation rotation) { if (rotation == GunsRotation.none) { return(direction(current_angle)); } if (rotation == GunsRotation.rotate_clockwise) { if (current_angle > angle(AxesDirections.west)) { return(AxesDirections.north); } if (current_angle > angle(AxesDirections.south)) { return(AxesDirections.west); } if (current_angle > angle(AxesDirections.east)) { return(AxesDirections.south); } return(AxesDirections.east); } else { if (current_angle < angle(AxesDirections.east)) { return(AxesDirections.north); } if (current_angle < angle(AxesDirections.south)) { return(AxesDirections.east); } if (current_angle < angle(AxesDirections.west)) { return(AxesDirections.south); } return(AxesDirections.west); } }
private void rotate_guns(GunsRotation rotation_from_commands) { // We want rotations to always happen in lock-steps of axis, // that is, rotation can only change/start when guns are aligned with the axis. // If the player maintain a rotation direction, we should seemlessly continue // rotating until they don't, then we stop rotation once reaching an axis orientation. if (current_guns_rotation == GunsRotation.none) // If we are already in locked position, take any command, otherwise ignore commands until next lock position. { current_guns_rotation = rotation_from_commands; if (rotation_from_commands != GunsRotation.none) { start_rotation_sound(); } } float rotation_factor = gun_rotation_factor(current_guns_rotation); float next_orientation = (current_guns_angle + rotation_speed * Time.deltaTime * rotation_factor); while (next_orientation > MAX_GUNS_ORIENTATION_DEGREES) { next_orientation -= MAX_GUNS_ORIENTATION_DEGREES; } while (next_orientation < 0) { next_orientation += MAX_GUNS_ORIENTATION_DEGREES; } var new_rotation_direction = direction(next_orientation); var new_target_axis = next_axis(next_orientation, current_guns_rotation); var current_target_axis = next_axis(current_guns_angle, current_guns_rotation); if (new_target_axis != current_target_axis) { // We are passing over/through a lock position. // We either stop here or change direction following commands. // It all depends on what is the command from input. var previous_gun_rotation = current_guns_rotation; current_guns_rotation = rotation_from_commands; if (current_guns_rotation == GunsRotation.none || current_guns_rotation != previous_gun_rotation) // TODO: check if the second test isn't enough for both cases { // We need to stop: lock the position to align with axis. next_orientation = angle(current_target_axis); stop_rotation_sound(); play_rotation_stop_sound(); } } // Handle gun firing rotation: the firing direction gives the closest gun to that direction to fire. if (new_rotation_direction != current_guns_directions) { // We will change of gun direction. rotate_guns_directions((int)rotation_factor); current_guns_directions = new_rotation_direction; } // Apply guns rotation. current_guns_angle = next_orientation; transform.rotation = Quaternion.Euler(0.0f, 0.0f, -current_guns_angle); // Because of Z being oriented this way in Unity, we need negative values to rotate clockwise. }