Sup.ArcadePhysics2D.setGravity(0, -0.02);
class PlayerBehavior extends Sup.Behavior {
speed = 0.08;
jumpSpeed = 0.35;
frameSpeed = 1;
reverseSpeed = -1;
update() {
Sup.ArcadePhysics2D.collides(this.actor.arcadeBody2D, Sup.ArcadePhysics2D.getAllBodies());
// As explained above, we get the current velocity
let velocity = this.actor.arcadeBody2D.getVelocity();
// We override the `.x` component based on the player's input
if (Sup.Input.isKeyDown("DOWN")) {
this.actor.spriteRenderer.setAnimation("Crouch",true);
velocity.x = 0.000001;
//this.actor.spriteRenderer.setPlaybackSpeed(this.frameSpeed);
}else if (Sup.Input.isKeyDown("RIGHT")) {
velocity.x = this.speed;
this.actor.spriteRenderer.setAnimation("Walk");
this.actor.spriteRenderer.setPlaybackSpeed(this.frameSpeed);
} else if (Sup.Input.isKeyDown("LEFT")) {
velocity.x = -this.speed + 0.02;
this.actor.spriteRenderer.setAnimation("Walk");
this.actor.spriteRenderer.setPlaybackSpeed(this.reverseSpeed);
//velocity.x = this.speed - 0.08;
} else {
velocity.x = 0;
}
// If the player is on the ground and wants to jump,
// we update the `.y` component accordingly
let touchBottom = this.actor.arcadeBody2D.getTouches().bottom;
if (touchBottom) {
if (Sup.Input.wasKeyJustPressed("UP")) {
velocity.y = this.jumpSpeed;
this.actor.spriteRenderer.setAnimation("Jump");
} else {
// Here, we should play either "Idle" or "Run" depending on the horizontal speed
if (velocity.x === 0) {
this.actor.spriteRenderer.setAnimation("Idle");
this.actor.spriteRenderer.setPlaybackSpeed(this.frameSpeed);
} else {
if (velocity.x === 0.000001) {
this.actor.spriteRenderer.setAnimation("Crouch",false);
}
}
}
} else {
// Here, we should play either "Jump" or "Fall" depending on the vertical speed
if (velocity.y >= 0) this.actor.spriteRenderer.setAnimation("Jump");
else this.actor.spriteRenderer.setAnimation("Fall");
}
// Finally, we apply the velocity back to the ArcadePhysics body
this.actor.arcadeBody2D.setVelocity(velocity);
}
}
Sup.registerBehavior(PlayerBehavior);