Skip to content

Instantly share code, notes, and snippets.

@jacobthemyth
Last active September 23, 2015 20:34
Show Gist options
  • Save jacobthemyth/32c251c590b6d8e80a04 to your computer and use it in GitHub Desktop.
Save jacobthemyth/32c251c590b6d8e80a04 to your computer and use it in GitHub Desktop.
// Declare a variable that will later store the selected character and enemy
var selectedCharacter;
var selectedEnemy;
// Define a constructor
window.Character = function(params) {
_.extend(this, params);
};
// Give all characters an attack function
Character.prototype.attack = function(enemy) {
enemy.health = enemy.health - this.attack;
$(document).trigger('health:changed');
};
// Create some character instances
window.characters = {
'Bulbasaur': new Character({
health: 12,
attack: 1
}),
'Charizard': new Character({
health: 6,
attack: 2
})
};
$(document).on('character:selected', function(character) {
selectedCharacter = character;
});
$(document).on('enemy:selected', function(enemy) {
selectedEnemy = enemy;
});
$(document).on('attack:enemy', function() {
selectedCharacter.attack(selectedEnemy);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment