Fisica Platformer (Part 2)

Fisica Platformer

In the first part of this tutorial we went over the platformer genre, how apparent it is in the gaming industry, and we began making the very basics of our own in Processing using the Fisica library. By the end of the tutorial we had our player able to move and jump around on a surface. In this tutorial we’ll be adding an entity that will roam around randomly and give our player the ability to shoot it. Final note before the tutorial, all code in this tutorial will be added into the final code from part 1 of this tutorial series. Firstly, we’ll need to add two ArrayList objects to store the enemies along with the bullets shot by the player. The reasoning for using an ArrayList is that the size/length of these lists will vary. The snippet of code down below will demonstrate how to declare and instantiate the two ArrayList objects. Now that we have the means of storing our enemies and bullets, we’ll add our enemies. We’ll start by adding a “addEnemy” function with two parameters, the desired x & y position of our enemy (both are floats.) The function will create an FBox named enemy and set its position to the ones given in the arguments. We will then set the color of the enemy to bright red using “setFill” and reducing the friction to ensure the enemies move smoothly using “setFriction”. After our enemies are made with the given details we will add them to the enemies ArrayList and then add it to the FWorld. The resulting code is below. We’ll make the movement of our enemies considerably random by creating a “moveEnemies” function which will just loop through the enemies ArrayList and using addForce we’ll add a random force making them move left or right. Now that we have the basics down for our enemies it’s a lot more useful if we actually used the code we just wrote. We’ll add “addEnemy(random(width), height / 8);” to our setup function along with the other “world.add” lines. This will add an enemy near the top of the screen in a random x-position that will fall down to the platform with our player. Next, we’ll simply add “moveEnemies” above “world.step()” in our draw function. Now our program should have one red square enemy which roams around with our player. To finish off this part of the tutorial we’ll be implementing bullets, collision between bullets & enemies, and tweaking the “platformCheck” function. To add the bullets we’ll create a “addBullet(float x, float y)” function. Then we’ll add Processing’s built-in “mousePressed” function which will call our “addBullet” function with the mouse x & y as the arguments. Now the obvious issue with “addBullet” is that it’s empty. Firstly, we’ll make two float variables named “heightDiff” and “widthDiff” which will equal the distance between the mouse’s x & y positions and the player’s. We’ll then create an FBox named “bullet” and give it a radius of 5. Then will use Fisica’s “setBullet(true)” as it gives fast-moving objects more accurate calculations. Then we’ll add an if-else statement that checks which side of the player the mouse is on two make the bullet come out of that side of the player. Then we’ll set the bullets x & y velocities to the width & height difference variables multiplied by 15 to give the bullets a fast speed in the direction of the mouse. Lastly, just like the addEnemy function we’ll add the bullet to the bullets ArrayList and the world. The code should appear as down below. Now that we've added our enemies and bullets we'll add a function named "bulletCheck()" which will loop through our bullets, check if they're touching anything with "getTouching()" and if so, loop through our enemies and then use "getTouching()" and if it returns true, remove the bullet & enemy from the world and their arrays.

Leave a Reply

Your email address will not be published. Required fields are marked *