Part 3 – Build a Simple JavaScript Game

As we continue the progress of this simple JavaScript game we need to add an enemy for our hero to battle.  The first type of enemy that we will construct will be an Orc and will be easy to model based on the elements that we established with our hero.  There is an early design element that we will consider that will add a bit of complexity initially.

In the previous article we tried to maintain focus on the minimum required elements  to make the game functional, without adding unnecessary complexity.  As a first element for our enemy, we will include an array to hold a group of enemies.  That will allow our hero to battle a group at a time versus just one.  It is a simple enough item to include initially and avoid some refactoring in the future.

// the current mob we are battling
var mob = [];

The mob array will give us a place to hold our enemy.  Now we need to construct our Orc object so that we can instantiate it for battle.  Based on what we selected for our hero, we can just replicate the same items for the creature that we will battle.

var orc = {
	name: "Orc",	
        health: 20,
        crit: 1,
        strength: 5,
        block: 10
};

We will make our early enemy clearly subordinate in health and other stats as compared to our hero.  Since the hero will battle many enemies over time, it will be necessary to have the enemies be weaker than our primary toon.

Let’s create a quick function to spawn an enemy that we can evaluate as the world ticks forward in time.  It will need to evaluate the presence of an enemy in the array and then it will instantiate an enemy for us.

function spawnEnemy() {
        if(mob.length == 0) {
           // no enemy, spawn one
           mob.push(new Object(orc));
        }
}

This is a very basic function that will evaluate if there is an enemy or not and if not, it will push a new Orc into the array.  Since the function will create no more than one enemy initially, we will add it to the world tick event to be evaluated continually to ensure our hero always has a foe to battle.  While we are in here, we will clean up the roll function and early items created as the game begins to take shape.  We will strike the min and max items from the world object and will modify our Roll function to take a min and max parameter directly.

function roll(min, max){
   return Math.floor(Math.random() * (max - min + 1) + min); 
}

Now we can put the script all together and include the most recent changes.

var world = {
   clockInterval: 1500,
};

// our hero
var hero = {
	name: "Foff",
	health: 100,
        crit: 5,
        strength: 10,
        block: 50
};

var orc = {
	name: "Orc",	
        health: 20,
        crit: 1,
        strength: 5,
        block: 10
};

// the current mob we are battling
var mob = [];

function worldClock() {
    tickEvent();
    setTimeout(worldClock, world.clockInterval); 
} 

// world tick event
function tickEvent() {
    spawnEnemy();
} 

// random roll function for the chance of the game
function roll(min, max){ 
    return Math.floor(Math.random() * (max - min + 1) + min); 
}

// spawn an enemy
function spawnEnemy() {
   if(mob.length == 0) {
     // no enemy, spawn one
     mob.push(new Object(orc));
     console.log("A new enemy " + mob[0].name + " has been spawned...");
    } 
}

// start the world clock ticking along
worldClock();

 

That will finish things up for today and we will create a function for a battle next time.

Advertisements

About b.hedge

i do database stuff and i like to solve business problems with code.
This entry was posted in coding, node js and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s