//Declares Fisica variables
FWorld world;
FBox platform;
FBox player;
boolean left, right, onGround;
void setup() {
size(800, 400); //Creates canvas
Fisica.init(this); //Initiates Fisica library
world = new FWorld(); //Instantiates world as FWorld()
world.setEdges(); //Sets edges of the screen to physical border
player = new FBox(30, 30);
player.setRotatable(false);
player.setPosition(width / 2, height / 8);
platform = new FBox(width, 30);
platform.setPosition(width / 2, height * 0.8);
platform.setStatic(true); //Sets "platform" to static (won't be affected by forces such as gravity
//Adds content to world.
world.add(player);
world.add(platform);
}
void draw() {
background(30); //Sets background to grey, clears previous content
fill(255);
text("A & D to move left & right", 10, 15);
text("Space to jump", 10, 30);
platformCheck(); //Detects whether or not player is touching platform.
world.step(); //Moves "world" forward by 1/60th of a second
world.draw(); //Draws all content contained in "world"
if (left) {
player.setVelocity(-100, player.getVelocityY()); //Moves player left
}
if (right) {
player.setVelocity(100, player.getVelocityY()); //Moves player right
}
}
void platformCheck() {
//Keeps track of if the player touching platform for ease of access
if (player.isTouchingBody(platform)) {
onGround = true;
} else {
onGround = false;
}
}
void keyPressed() {
//Keeps track if player is moving left or right
if (key == 'a' || key == 'A') {
left = true;
}
if (key == 'd' || key == 'D') {
right = true;
}
if (key == ' ' && onGround) {
player.addForce(0, - (player.getMass() * 10000)); //Makes player jump
}
}
void keyReleased() {
//Keeps track if player is going left & right, if not it slows down player quickly
if (key == 'a' || key == 'A') {
left = false;
player.setVelocity(player.getVelocityX() * 0.5, player.getVelocityY()); //Reduces player's velocity by half
}
if (key == 'd' || key == 'D') {
right = false;
player.setVelocity(player.getVelocityX() * 0.5, player.getVelocityY()); //Reduces player's velocity by half
}
}