Repository navigation
Player movement
This page is to represent how the player is able to move through the world as largely implemented by the PlayerActions class where the input to the class is processed by the KeyboardPlayerInputComponent and is sent to the PlayerActions component by triggering an event. There's 6 main methods of moving for the player: walking, sneaking/crouching, jumping, dashing, wall jumping and sliding.
@Override
public void create() {
physicsComponent = entity.getComponent(PhysicsComponent.class);
platformerComponent = entity.getComponent(PlatformerComponent.class);
entity.getEvents().addListener("walk", this::walk);
entity.getEvents().addListener("walkStop", this::stopWalking);
entity.getEvents().addListener("attack", this::attack);
entity.getEvents().addListener("dash", this::dash);
entity.getEvents().addListener("slide", this::slide);
textureRenderComponent = entity.getComponent(TextureRenderComponent.class);
entity.getEvents().addListener("ctrlChanged", this::ctrlChanged);
}
The PlayerActions component is able to process input from the KeyboardPlayerInputComponent by adding listeners that are able to check when the key inputs for the actions are logged. Then the corresponding function is called that handles the event.
void walk(Vector2 direction) {
this.walkDirection = direction;
moving = true;
}
/** Stops the player from walking. */
void stopWalking() {
this.walkDirection = Vector2.Zero.cpy();
updateSpeed();
moving = false;
}
When the listener detects a corresponding key input (A or D for walking) then the walk(Vector2 direction) function is called that sets the global walkDirection variable and sets moving to true.
@Override
public void update() {
playMovementSound();
if (moving || platformerComponent.getJumpingBool()) {
updateSpeed();
}
}
Moving being true plays the corresponding sound through playMovementSound() and triggers the updateSpeed() function. If the player jumps (or wall jumps) (key input = 'W') then updateSpeed() is called too. See PlatformerComponent for more info.
private void updateSpeed() {
Body body = physicsComponent.getBody();
Vector2 desiredVelocity = walkDirection.cpy().scl(MAX_SPEED);
// impulse = desiredVel * mass
Vector2 impulse = desiredVelocity.scl(body.getMass());
body.applyForce(impulse, body.getWorldCenter(), true);
// For the jump portion
platformerComponent.updateJump(MAX_SPEED);
}
Update speed takes walkDirection and uses it as an impulse for the player character. The updateJump(Vector 2 MAX_SPEED) function (if the player wants to jump) is also called here. The MAX_SPEED variable is a global Vector2 variable that's used as a scaler for the speed.
/** Makes the player dash */
void dash(Vector2 direction) {
Body body = physicsComponent.getBody();
Vector2 impulse = direction.cpy().scl(dashspeed);
body.applyLinearImpulse(impulse, body.getWorldCenter(), true);
dashing = true;
}
When the 'L' key is pressed, the "dashing" listener directs the programme to running the dash(Vector2 direction function, the direction of the dash is determined in the entity's KeyboardPlayerInputComponent through the direction (which keeps track what direction the player is moving) and dashDirection (which is the Vector 2 that's given to the PlayerActions component) variables.
private void ctrlChanged(boolean pressed) {
if (pressed) {
textureRenderComponent.setTexture(CROUCH_TEXTURE);
sneaking = true;
} else {
textureRenderComponent.setTexture(NORMAL_TEXTURE);
sneaking = false;
}
}
When the player inputs "CONTROL_LEFT" then the player performs a crouch and is sneaking, this alters the texture of the character to indicate the change.
private void slide(boolean pressed) {
if (pressed) {
textureRenderComponent.setTexture(SLIDE_TEXTURE);
slidingAction(walkDirection.cpy());
} else {
sliding = false;
textureRenderComponent.setTexture(NORMAL_TEXTURE);
}
}
private void slidingAction(Vector2 direction) {
sliding = true;
Body body = physicsComponent.getBody();
Vector2 impulse = direction.cpy().scl(slidespeed);
body.applyLinearImpulse(impulse, body.getWorldCenter(), true);
}
When the slide listener is triggered by the player pressing or releasing "SHIFT_LEFT", then the slide(boolean pressed) function is triggered which alters the texture of the character and calls slidingAction(Vector2 direction) which applies a linearImpulse to the player.
For both crouching and sliding, the textures for them (CROUCH_TEXTURE and SLIDE_TEXTURE respectively) need to be loaded through the ResourceServices class into the current game area.