Introduction
This is part 2 of my ongoing journey to make a simple platformer utilizing Love2d.
In this part we will utilize basic OOP functionality and also make the enemy character move across the screen.
OOP
To improve upon our code, we are going to utilize basic principles of OOP(or as OOP as lua gets.)
The first thing we can do to improve our code is make a class for our characters.
The first step is to create a CHARACTER class followed by a constructor function:CHARACTER:new()
In the constructor we make a new character object then assign the variables in that object the correct values.
In a file called 'Characters.lua' we add the code:
The next step is to make some changes in main.lua
When need to let lua know about our character class,so we add require "Characters"
Next we need to create character objects for the player and enemy, so we change the code in love.load()
The final bit of change is that we need to use the x and y in these objects therefore we alter the code in love.draw()
and love.keypress()
The updated main.lua code can be seen below:
Moving the enemy character
Now we can work on getting the enemy character to move left and right.
we want him to move right until he reaches the limit set by WIDTH, then have him move left till he hit the edge.then rinse repeat
To accomplish this we are going to add a function in Character.lua to handle this. CHARACTER:move()
As long as move_r
is true then the enemy should move to the right.
We employ the same bounds checking as we did in key.press
in part 1.
When the enemy reaches the right hand boundary, we have to change his direction, that's where move_r = false
comes into play.
And when the enemy reaches the left hand side, we simply set move_r back to true.
You can see the complete CHARACTER:move()
function below.
We next need to update our code in main.lua
We need to use the function love.update()
- This function updates at the start of each frame.
- Put everything which needs to update or run with each frame here.
Inside this function we call the CHARACTER:move()
function on the enemy object.
You can see our new function in the code below.
Improving our code further
To further improve our code we can employ encapsulation.
We can create an enemy class. The player character has no need for the move function nor move_r variable
So we create a class for the enemy object and attach that function and variable to it.
We have to modify our Characters.lua file to add the enemy class, constructor function, and a slight change to the move function.
We also need to make sure to set the metatable and index of the enemy class so that it is a subclass of the character class.
The code we added is below.
After adding our enemy class we need to make one slight change to our main.lua
just change the type of object for enemy in our love.load
function.
Final
We should now have a program which does the same thing as before, but now it is much more organize.
More importantly for our needs it can much more easily be expand and added to.
Using our class system we can now easily add many new objects and add or remove functions and variables to them.
as always, complete code can be found on github.
Thank you for sticking around for my part 2. Continue on to part 3
No comments:
Post a Comment