-
Notifications
You must be signed in to change notification settings - Fork 4
Entity Directions
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.
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
All available enum values include: N, NW, W, SW, S, SE, E, NE.
For ease of texture name management, the initials of each direction can be obtained using the enumerations name()
method.
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".
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.
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.
- Home
- How To Play
- Keybindings
- Inventory and Resources
- Trees
- Portals and Worlds
- Projectiles
- Enemies
- Waves & Spawning
- Graphical assets progress overview
- Audio Files
- Design Templates
- Blender Intro
- Converting 3D models to Sprites
- Scripting in Blender
- Creating a Model in Sketchup
- Colouring and Animating in Blender
- Animation using Dope Sheet, Keyframes and Scale Transformation in Blender
- Template
- Testing
- Terrain
- Terrain Generation
- Resource Trees
- Main Menu, Chat, options
- Main Menu V2, Options V2, Sound
- Damage Trees
- Projectiles
- Enemy User Testing
- Tree planting, inventory GUI, resources, portals
- Caveman Character
- Debug Mode Menu
- Tree learning, Attack Trees, Main Menu Tutorial Resources Styling
- Revamped Resource Trees
- Squirrel User Testing
- Final evaluation
- Project Structure
- Git Workflow
- GUI
- Worlds
- Entity Directions
- Resources
- Inventory
- AbstractEntities
- BasicProperties
- MortalEntity
- ProgressBarEntity
- Multiplayer
- Trees
- TimeEvent
- Game Time
- Animations
- Enemies
- Waves
- Player
- Projectiles
- Effect
- Particles
- Path Manager
- Sounds
- Debug 'God' Mode
- 2D Collision
- Box3D {depreciated}
- Render3D
- Cheat Codes