Jump to content

Finding player orientation


xlDino

Recommended Posts

 

This method finds what direction the player is facing based on the previous tile, and assigns it to the 'direction' string.

    private void checkPlayerDirection() {
        int currentX = myPlayer().getX();
        int currentY = myPlayer().getY();
        int deltaX = currentX - previousX;
        int deltaY = currentY - previousY;

        if (deltaX == 0 && deltaY == 0) {
            direction = previousDirection; // Use the previous direction when not moving
        } else if (deltaX == 0) {
            direction = deltaY > 0 ? "North" : "South";
        } else if (deltaY == 0) {
            direction = deltaX > 0 ? "East" : "West";
        } else if (deltaX > 0) {
            direction = deltaY > 0 ? "North-East" : "South-East";
        } else {
            direction = deltaY > 0 ? "North-West" : "South-West";
        }

        if (!direction.equals(previousDirection)) {
            log("Player is facing: " + direction);
            previousDirection = direction;
        }
    }

 

onStart

        previousX = myPlayer().getX();
        previousY = myPlayer().getY();

 

onLoop

	checkPlayerDirection();
        previousX = myPlayer().getX();
        previousY = myPlayer().getY();

 

Variables

    int previousX;
    int previousY;
    String direction;
    String previousDirection;

 

Usage

if(direction == "North") {
        	//do something
        }

Screenshot_4.png

Edited by xlDino
Link to comment
Share on other sites

You can simply check the rotation of a player, might be easier than checking it like that :D

myPlayer().getRotation()


If you enable myPlayer debug in the debug tab, it will be included. :)
If interacting with an entity these values sometimes shift a little bit.

0 = South
256 = South-West
512 = West
768 = North-West
1024 = North
1280 = North-East
1536 = East
1792 = South-East
HHUflGS.png

Edited by Khaleesi
Link to comment
Share on other sites

2 hours ago, Khaleesi said:

You can simply check the rotation of a player, might be easier than checking it like that :D

myPlayer().getRotation()


If you enable myPlayer debug in the debug tab, it will be included. :)
If interacting with an entity these values sometimes shift a little bit.

0 = South
256 = South-West
512 = West
768 = North-West
1024 = North
1280 = North-East
1536 = East
1792 = South-East
HHUflGS.png

Found this out after hahahahaha

  • Heart 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...