Skip to content

Entity Directions

petercondoleon edited this page Oct 7, 2017 · 7 revisions

Description

Entities can conform to the HasDirection Interface to allow for management of in game directions. There are 8 discrete directions in total, each being that of the standard compass points.

compass

The directions form an enumeration and can be easily accessed publicly.

public enum Direction

For example, when referring to North East, the value can be retrieved by using:

Direction.NE

Direction Values

All available enum values include: N, NW, W, SW, S, SE, E, NE.

Getting Direction Initials

For ease of texture name management, the initials of each direction can be obtained using the enumerations name() method.

Getting String Representations of Directions

the Direction enumeration uses its toString() method to return a readable description of the compass direction. For example, the following:

Direction.SW.toString()

Returns the string "South-West".

Conforming to HasDirection

In order for an entity to have directions, it must implement the HasDirection interface. By doing so the entity class needs to define a the function getDirection() that returns the entity's current direction. Controlling the entity's direction must then be handled within the entity's class.

Examples

Below is an example of how Player.Java implements the HasDirection Interface. The class first makes use of a private variable currentDirection which stores the current direction the player faces. This allows the interface method to be easily overridden:

/**
 * Returns the current Direction of the player.
 */
@Override
public Direction getDirection() {
    return this.currentDirection;
}

To handle the Player's change in direction, two private methods and a private variable are used. The first private method is responsible for setting the currentDirection variable:

/**
 * Sets the direction of the player based on a specified direction.
 *
 * @param direction 
  * 		The direction to set the player to.
 */
private void setDirection(Direction direction) {
    if (this.currentDirection != direction) {
        this.currentDirection = direction;
        LOGGER.info("Set player direction to " + direction.toString());
        updateSprites();
    }
}

Note that the direction is only changed if it is different. For the second private method, an external private variable oldPos is needed. This variable is used as a way to check if the player has moved. If the player hasn't moved then we handle that accordingly. If the player has moved, we can do some maths to figure out what angle the player moves in. An if statement is used to convert ranges of angles into Direction values, which then allow us to set the player's direction.

/**
 * Updates the direction of the player based on change in position.
 */
private void updateDirection() {
    if ((this.getPosX() - oldPos.x == 0) && (this.getPosY() - oldPos.y == 0)) {
        this.setState(PlayerState.idle);  // Not moving
    } else {
        this.setState(PlayerState.walk);  // Moving
        double angularDirection = Math.atan2(this.getPosY() - oldPos.y, 
                this.getPosX() - oldPos.x) * (180 / Math.PI);

        if (angularDirection >= -180 && angularDirection < -157.5) {
            this.setDirection(Direction.SW);
        } else if (angularDirection >= -157.5 && angularDirection < -112.5) {
            this.setDirection(Direction.W);
        } else if (angularDirection >= -112.5 && angularDirection < -67.5) {
            this.setDirection(Direction.NW);
        } else if (angularDirection >= -67.5 && angularDirection < -22.5) {
            this.setDirection(Direction.N);
        } else if (angularDirection >= -22.5 && angularDirection < 22.5) {
            this.setDirection(Direction.NE);
        } else if (angularDirection >= 22.5 && angularDirection < 67.5) {
            this.setDirection(Direction.E);
        } else if (angularDirection >= 67.5 && angularDirection < 112.5) {
            this.setDirection(Direction.SE);
        } else if (angularDirection >= 112.5 && angularDirection < 157.5) {
            this.setDirection(Direction.S);
        } else if (angularDirection >= 157.5 && angularDirection <= 180) {
            this.setDirection(Direction.SW);
        }

        oldPos = new Vector2(this.getPosX(), this.getPosY());
    }
}

updateDirection() is run in the Player's onTick() method, allowing it to evaluate the direction every frame. Through this code, the player will automatically change direction based on how it moves in the world.


Gameplay

Design

Asset Creation

User Testing

Code Guidelines


http://cultofthepartyparrot.com/parrots/partyparrot.gifhttp://cultofthepartyparrot.com/parrots/partyparrot.gifhttp://cultofthepartyparrot.com/parrots/partyparrot.gifhttp://cultofthepartyparrot.com/parrots/partyparrot.gifhttp://cultofthepartyparrot.com/parrots/partyparrot.gif

Clone this wiki locally