Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.20] Camera look straight up down WIP 2 #132

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ This feature adds the following key bindings to control the camera through the k
10. Right Alt + Look Right Key or Look East Key (default: Keypad 9) = Snaps the camera to the east block.
11. Right Alt + Look Down Key or Look South Key (default: Keypad 3) = Snaps the camera to the south block.
12. Right Alt + Look Left Key or Look West Key (default: Keypad 1) = Snaps the camera to the west block.
13. Center Camera (default: Keypad 5) = Snaps the camera to the closest cardinal direction and centers it.
14. Left Alt + Center Camera = Snaps the camera to the closest opposite cardinal direction and centers it.
13. Right Alt + double Look Up Key or Look Straight Up Key (default: Keypad 0): Snaps the camera to the look above head direction.
14. Right Alt + double Look Down Key (default: O) or Look Straight Down Key (default: Keypad `.` (decimal, dot)): Snaps the camera to the look down at feet direction.
15. Center Camera (default: Keypad 5) = Snaps the camera to the closest cardinal direction and centers it.
16. Left Alt + Center Camera = Snaps the camera to the closest opposite cardinal direction and centers it.

**Configuration Options:-**

Expand Down
11 changes: 11 additions & 0 deletions common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ dependencies {
modImplementation "net.fabricmc:fabric-loader:${rootProject.fabric_loader_version}"
// Remove the next line if you don't want to depend on the API
modApi "dev.architectury:architectury:${rootProject.architectury_version}"

// Unit test dependencies
testImplementation "org.junit.jupiter:junit-jupiter:5.10.0"
testImplementation "org.junit.jupiter:junit-jupiter-params:5.10.0"
testImplementation "org.assertj:assertj-core:3.24.2"
testImplementation "org.mockito:mockito-core:5.4.0"
testImplementation "org.mockito:mockito-junit-jupiter:5.4.0"
}

test {
useJUnitPlatform()
}

publishing {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

import com.github.khanshoaib3.minecraft_access.MainClass;
import com.github.khanshoaib3.minecraft_access.config.config_maps.CameraControlsConfigMap;
import com.github.khanshoaib3.minecraft_access.utils.KeyBindingsHandler;
import com.github.khanshoaib3.minecraft_access.utils.KeyUtils;
import com.github.khanshoaib3.minecraft_access.utils.PlayerPositionUtils;
import com.github.khanshoaib3.minecraft_access.utils.TimeUtils;
import com.github.khanshoaib3.minecraft_access.utils.*;
import com.github.khanshoaib3.minecraft_access.utils.condition.Interval;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.MinecraftClient;
Expand Down Expand Up @@ -36,7 +34,7 @@ public class CameraControls {

private float normalRotatingDeltaAngle;
private float modifiedRotatingDeltaAngle;
private TimeUtils.Interval interval;
private Interval interval;

public CameraControls() {
loadConfigurations();
Expand All @@ -52,7 +50,7 @@ public void update() {

loadConfigurations();
boolean wasAnyKeyPressed = keyListener();
if(wasAnyKeyPressed) interval.start();
if (wasAnyKeyPressed) interval.start();
} catch (Exception e) {
MainClass.errorLog("\nError encountered in Camera Controls feature.");
e.printStackTrace();
Expand All @@ -66,7 +64,7 @@ private void loadConfigurations() {
float delta90Degrees = 600f; // 90 / 0.15

CameraControlsConfigMap map = MainClass.config.getConfigMap().getCameraControlsConfigMap();
interval = TimeUtils.Interval.inMilliseconds(map.getDelayInMilliseconds(), interval);
interval = Interval.inMilliseconds(map.getDelayInMilliseconds(), interval);
float normalRotatingAngle = map.getNormalRotatingAngle();
float modifiedRotatingAngle = map.getModifiedRotatingAngle();
normalRotatingDeltaAngle = delta90Degrees / (90 / normalRotatingAngle);
Expand Down Expand Up @@ -96,44 +94,49 @@ private boolean keyListener() {
boolean isSouthKeyPressed = KeyUtils.isAnyPressed(kbh.cameraControlsSouth)
|| (isDownKeyPressed && isRightAltPressed && !isLeftAltPressed);
boolean isCenterCameraKeyPressed = KeyUtils.isAnyPressed(kbh.cameraControlsCenterCamera);
// TODO Add double click Up/Down, refactor F4 menu
boolean isStraightUpKeyPressed = KeyUtils.isAnyPressed(kbh.cameraControlsStraightUp);
boolean isStraightDownKeyPressed = KeyUtils.isAnyPressed(kbh.cameraControlsStraightDown);

if (isNorthKeyPressed) {
lookNorth();
rotateCameraTo(CameraDirection.NORTH);
return true;
}

if (isEastKeyPressed) {
lookEast();
rotateCameraTo(CameraDirection.EAST);
return true;
}

if (isWestKeyPressed) {
lookWest();
rotateCameraTo(CameraDirection.WEST);
return true;
}

if (isSouthKeyPressed) {
lookSouth();
rotateCameraTo(CameraDirection.SOUTH);
return true;
}

float rotateAngle = isLeftAltPressed ? modifiedRotatingDeltaAngle : normalRotatingDeltaAngle;

if (isUpKeyPressed) {
upKeyHandler(isLeftAltPressed);
rotateCameraBy(rotateAngle, RotatingDirection.UP);
return true;
}

if (isRightKeyPressed) {
rightKeyHandler(isLeftAltPressed);
rotateCameraBy(rotateAngle, RotatingDirection.RIGHT);
return true;
}

if (isDownKeyPressed) {
downKeyHandler(isLeftAltPressed);
rotateCameraBy(rotateAngle, RotatingDirection.DOWN);
return true;
}

if (isLeftKeyPressed) {
leftKeyHandler(isLeftAltPressed);
rotateCameraBy(rotateAngle, RotatingDirection.LEFT);
return true;
}

Expand All @@ -142,131 +145,115 @@ private boolean keyListener() {
return true;
}

return false;
}
if (isStraightUpKeyPressed) {
rotateCameraTo(CameraDirection.UP);
return true;
}

/**
* Executes when up key is pressed.
*/
private void upKeyHandler(boolean isLeftAltPressed) {
rotateCamera(0, (isLeftAltPressed) ? -modifiedRotatingDeltaAngle : -normalRotatingDeltaAngle, false);
}
if (isStraightDownKeyPressed) {
rotateCameraTo(CameraDirection.DOWN);
return true;
}

/**
* Executes when right key is pressed.
*/
private void rightKeyHandler(boolean isLeftAltPressed) {
rotateCamera((isLeftAltPressed) ? modifiedRotatingDeltaAngle : normalRotatingDeltaAngle, 0, true);
return false;
}

/**
* Executes when down key is pressed.
*/
private void downKeyHandler(boolean isLeftAltPressed) {
rotateCamera(0, (isLeftAltPressed) ? modifiedRotatingDeltaAngle : normalRotatingDeltaAngle, false);
}
private enum RotatingDirection {
UP(0, -1),
DOWN(0, 1),
LEFT(-1, 0),
RIGHT(1, 0);

/**
* Executes when left key is pressed.
*/
private void leftKeyHandler(boolean isLeftAltPressed) {
rotateCamera((isLeftAltPressed) ? -modifiedRotatingDeltaAngle : -normalRotatingDeltaAngle, 0, true);
final int horizontalWight;
final int verticalWight;
final boolean isRotatingHorizontal;

RotatingDirection(int horizontalWight, int verticalWight) {
this.horizontalWight = horizontalWight;
this.verticalWight = verticalWight;
this.isRotatingHorizontal = horizontalWight != 0;
}
}

/**
* Rotates the player's camera.
*
* @param deltaX The amount of rotation in X axis (in degrees).
* @param deltaY The amount of rotation in Y axis (in degrees).
* @param isRotatingHorizontal Whether the rotation is horizontal or vertical
* @param angle by given angle
* @param direction on given direction
*/
private void rotateCamera(float deltaX, float deltaY, boolean isRotatingHorizontal) {
private void rotateCameraBy(float angle, RotatingDirection direction) {
if (minecraftClient.player == null) return;

minecraftClient.player.changeLookDirection(deltaX, deltaY);
float horizontalAngleDelta = angle * direction.horizontalWight;
float verticalAngleDelta = angle * direction.verticalWight;

minecraftClient.player.changeLookDirection(horizontalAngleDelta, verticalAngleDelta);

MainClass.infoLog("Rotating camera by x:%d y:%d".formatted((int) deltaX, (int) deltaY));
// log and speak new facing direction
MainClass.infoLog("Rotating camera by x:%d y:%d".formatted((int) horizontalAngleDelta, (int) verticalAngleDelta));

PlayerPositionUtils pUtil = new PlayerPositionUtils(this.minecraftClient);
String horizontalDirection = pUtil.getHorizontalFacingDirectionInCardinal();
String verticalDirection = pUtil.getVerticalFacingDirectionInWords();

if (isRotatingHorizontal && horizontalDirection != null)
if (direction.isRotatingHorizontal && horizontalDirection != null)
MainClass.speakWithNarrator(horizontalDirection, true);
else if (!isRotatingHorizontal && verticalDirection != null)
else if (!direction.isRotatingHorizontal && verticalDirection != null)
MainClass.speakWithNarrator(verticalDirection, true);
}

/**
* Snaps the camera to the north block
*/
private void lookNorth() {
lookAtRelativeBlock(0, -1);
}

/**
* Snaps the camera to the east block
*/
private void lookEast() {
lookAtRelativeBlock(1, 0);
}

/**
* Snaps the camera to the west block
*/
private void lookWest() {
lookAtRelativeBlock(-1, 0);
}

/**
* Snaps the camera to the south block
*/
private void lookSouth() {
lookAtRelativeBlock(0, 1);
}

/**
* Snaps the camera to the north-east block
*/
private void lookNorthEast() {
lookAtRelativeBlock(1, -1);
}

/**
* Snaps the camera to the north-west block
*/
private void lookNorthWest() {
lookAtRelativeBlock(-1, -1);
}

/**
* Snaps the camera to the south-east block
*/
private void lookSouthEast() {
lookAtRelativeBlock(1, 1);
}

/**
* Snaps the camera to the south-west block
*/
private void lookSouthWest() {
lookAtRelativeBlock(-1, 1);
private enum CameraDirection {
NORTH(0, 0, -1),
EAST(1, 0, 0),
WEST(-1, 0, 0),
SOUTH(0, 0, 1),
NORTH_EAST(1, 0, -1),
NORTH_WEST(-1, 0, -1),
SOUTH_EAST(1, 0, 1),
SOUTH_WEST(-1, 0, 1),
UP(0, 1, 0),
DOWN(0, -1, 0),
;

final int x;
final int y;
final int z;

CameraDirection(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
}

/**
* Snaps the camera at a block relative to the player's position.
* Move the camera (player's view).
*
* @param deltaX The relative X position of the block.
* @param deltaZ The relative Z position of the block.
* @param direction to given direction
*/
private void lookAtRelativeBlock(int deltaX, int deltaZ) {
private void rotateCameraTo(CameraDirection direction) {
if (minecraftClient.player == null) return;

Vec3d playerBlockPosition = minecraftClient.player.getPos();
Vec3d relativeBlockPosition = new Vec3d(playerBlockPosition.x + deltaX, playerBlockPosition.y + 0, playerBlockPosition.z + deltaZ);
Vec3d targetBlockPosition = new Vec3d(
playerBlockPosition.x + direction.x,
playerBlockPosition.y + direction.y,
playerBlockPosition.z + direction.z);

minecraftClient.player.lookAt(EntityAnchorArgumentType.EntityAnchor.FEET, targetBlockPosition);

// log and speak new facing direction
MainClass.infoLog("Rotating camera to: %s".formatted(direction.name()));

minecraftClient.player.lookAt(EntityAnchorArgumentType.EntityAnchor.FEET, relativeBlockPosition);
MainClass.speakWithNarrator(new PlayerPositionUtils(minecraftClient).getHorizontalFacingDirectionInCardinal(), true);
PlayerPositionUtils pUtil = new PlayerPositionUtils(this.minecraftClient);
boolean isRotatingHorizontal = direction.y == 0;

if (isRotatingHorizontal) {
MainClass.speakWithNarrator(pUtil.getHorizontalFacingDirectionInCardinal(), true);
} else {
MainClass.speakWithNarrator(pUtil.getVerticalFacingDirectionInWords(), true);
}
}

/**
Expand All @@ -280,14 +267,14 @@ private void centerCamera(boolean lookOpposite) {
String direction = new PlayerPositionUtils(minecraftClient).getHorizontalFacingDirectionInCardinal(true, lookOpposite);

switch (direction) {
case "north" -> lookNorth();
case "east" -> lookEast();
case "west" -> lookWest();
case "south" -> lookSouth();
case "north_east" -> lookNorthEast();
case "north_west" -> lookNorthWest();
case "south_east" -> lookSouthEast();
case "south_west" -> lookSouthWest();
case "north" -> rotateCameraTo(CameraDirection.NORTH);
case "east" -> rotateCameraTo(CameraDirection.EAST);
case "west" -> rotateCameraTo(CameraDirection.WEST);
case "south" -> rotateCameraTo(CameraDirection.SOUTH);
case "north_east" -> rotateCameraTo(CameraDirection.NORTH_EAST);
case "north_west" -> rotateCameraTo(CameraDirection.NORTH_WEST);
case "south_east" -> rotateCameraTo(CameraDirection.SOUTH_EAST);
case "south_west" -> rotateCameraTo(CameraDirection.SOUTH_WEST);
}
}
}
Loading