Let’s make a game. It’s going to have a lot of Ogres.
Let’s construct a typical Ogre.
So now we have three variables: ogre1, ogre2, and ogre3 and they each have their own properties set for strength, health, defense, and attack based on our constructor function.
I’m sure you can already see why this is such a handy thing, but let’s expand on it. We need to make a few tougher-to-kill Ogres. We will call them captain ogres. This time we will make a new constructor function that allows us to pass parameters in for strength, health, defense, and attack.
Now we can specify the parameters of our captain ogres each time that we build one. We want these enemies each to be slightly different to make the player feel like they are battling enemies that are unique.
Now we’ve stored a variable capOgre1 for our 1st captain Ogre.
It has 400 strength, 2000 health, 400 defense, and 800 attack.
Our second captain ogre has 500 strength, 1800 health, 350 defense, and 600 attack.
For our third captain ogre we’re going to make a boss. He’s still going to be a captain ogre, but we’re going to make him much harder to beat. He will look like the other captain ogres but unexpectedly to the player- he will be much harder to eliminate.
bossOgre has 1000 strength, 9000 health, 800 defense, and 1200 attack.
As a simplified example- where e is the enemy and p is the players power of attack- an attack function might be defined like this:
We’ve defined our function, now let’s call this function when the player attacks ogre1.
We’ll pass in the enemy and the attack power of 100.
This function would effectively reduce the ogres health by 100.