Skip to content

Instantly share code, notes, and snippets.

@hoffination
Created July 13, 2017 02:56
Show Gist options
  • Save hoffination/8468dd73f70d4035e7e62e12dbb83428 to your computer and use it in GitHub Desktop.
Save hoffination/8468dd73f70d4035e7e62e12dbb83428 to your computer and use it in GitHub Desktop.
Battle simulation for a D&D game
let forces = [{
name: 'enemy',
health: 3360,
random: 10
}, {
name: 'friendly',
health: 2700,
random: 6
}]
let routedForces = {
7: 600,
15: 550,
22: 800
}
let round = 0
let interval = setInterval(() => {
console.log('battle: round ' + round++)
forces.forEach((f, index) => {
let strength = (f.health / 200).toFixed(0)
let damage = ((Math.random() * f.random) + 1).toFixed(0) * strength
if (damage === 0)
damage = (Math.random() * f.random).toFixed(0) + 1
if (routedForces[round] && index === 1) {
damage += routedForces[round]
console.log('heroes route ' + routedForces[round] + ' enemy forces')
}
forces[(index + 1) % 2].damageTaken = damage
})
forces = forces.map(f => {
f.health -= f.damageTaken
console.log(f.name + ' forces took ' + f.damageTaken + ' damage (' + f.health + ' remaining)')
//delete f.damageTaken
if (f.health < 0) {
clearInterval(interval);
console.log(f.name + ' forces were defeated')
}
return f
})
}, 500)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment