Skip to content

Instantly share code, notes, and snippets.

@sscotth
Last active August 29, 2015 14:13
Show Gist options
  • Save sscotth/3a2029bcca14759766fe to your computer and use it in GitHub Desktop.
Save sscotth/3a2029bcca14759766fe to your computer and use it in GitHub Desktop.
Game start
function Game(numberOfPlayers){
this.weapons = [];
this.weapons.push(new Weapon({name: 'Sword', damage: 0.2, ammo: Infinity}));
this.weapons.push(new Weapon({name: 'Shotgun', damage: 0.6, ammo: 1}));
this.weapons.push(new Weapon({name: 'Fist', damage: 0.05, ammo: Infinity}));
this.players = [];
for (var i=0; i < numberOfPlayers; i++){
var player = new Player;
player.weapon = getRandomWeapon.call(this);
this.players.push(new Player);
}
function getRandomWeapon(){
console.log(this);
return this.weapons[getRandomInt(0, this.weapons.length)];
}
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
}
function Player(obj){
this.name = obj.name;
this.health = 1;
this.isZombie = false;
this.isTrulyDead = false;
this.dexterity = 0.5;
}
Player.prototype.attack = function(otherPlayer){
if (didHit(this.weapon.accuracy)) {
otherPlayer.health -= this.weapon.damage;
}
};
function Weapon(obj){
this.name = obj.name;
this.damage = obj.damage;
this.accuracy = 1 - obj.damage;
this.ammo = obj && obj.ammo || 0;
}
function didHit(accuracy){
return accuracy > Math.random()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment