Repository navigation
Dash
sbaks2 edited this page Aug 30, 2026
·
4 revisions
This feature allows the Player to dash in the direction they are facing.
The dash is given an initial direction of "Right"
private String direction = "Right";
This is changed depending on which direction the player moves with direction being set as "Left" or "Right" to inform the program which way the player last moved:
case Keys.A:
walkDirection.add(Vector2Utils.LEFT);
direction = "Left";
triggerWalkEvent();
return true;
case Keys.D:
walkDirection.add(Vector2Utils.RIGHT);
direction = "Right";
triggerWalkEvent();
return true;
From this when the dash key 'L' is pressed, a horizontal vector is applied in the given direction of the direction the player last moved.
case Keys.L:
if (direction.equals("Left")) {
dashDirection.add(Vector2Utils.LEFT); // Adds to the x vector to the left
} else {
dashDirection.add(Vector2Utils.RIGHT); // Adds to the x vector to the right
}
triggerDashEvent();
dashed = true;
return true;
The triggerDashEvent() function then sends this direction through the events system:
private void triggerDashEvent() {
// Player has an x velocity in the direction they last went or are going
entity.getEvents().trigger("dash", dashDirection);
dashDirection.x = 0;
dashed = false;
}
This triggers the dash event and passes dashDirection to the listener for this event:
entity.getEvents().addListener("dash", this::dash);
The dash creates an impulse vector by scaling the dash direction by dashspeed then the linear impulse vector is applied to the player's body.
void dash(Vector2 direction) {
Body body = physicsComponent.getBody();
Vector2 impulse = direction.cpy().scl(dashspeed);
body.applyLinearImpulse(impulse, body.getWorldCenter(), true);
}