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”.

Wish there was more?

I'm considering writing an ebook - click here.


Luke is the primary editor of Building Browsergames, and has written a large portion of the articles that you read here. He generally has no idea what to say when asked to write about himself in the third person.

Tags: , ,

Monday, September 29th, 2008 buildingbrowsergames, medieval
blog comments powered by Disqus


Building Browsergames is a blog about browsergames(also known as PBBG's). It's geared towards the beginner to intermediate developer who has an interest in building their own browsergame.


Got Something to Say?

Send an e-mail to, or get in touch through Twitter at