Integrating Armor into our combat system (PHP)

We just added a way for our users to buy armor, but we still need to make it so that a user’s armor actually affects the our combat system. Today, we will integrate armor into our combat system, so that a user’s equipment can affect the results of the combat.

This is a lot easier than it sounds. We retrieve the armor’s ‘defense’ stat, and take it off of the damage that the player is going to receive. It’s a simple formula, but it works – and you’re free to tweak it as much as you’d like for your own game.

All we have to do is open up forest.php, and add our code just after we retrieve the player’s weapon and modify their attack:

require_once 'armor-stats.php'; // armor stats
$armor = array('atorso','ahead','alegs','aright','aleft');
foreach ($armor as $key) {
$id = getStat($key,$userID);
$defence = getArmorStat('defence',$id);
$player['defence'] += $defence;
}

And that’s the only change we need to make! We just loop through all the available armor slots, and then add their defense values to the player’s defense before we get to the main combat code. It’s just that simple!

Extra Credit

Make the combat system also display the different defence modifiers for the player – e.g. – “1 defence from torso armor, 2 defence from legs armor”.