Skip to content

Instantly share code, notes, and snippets.

@cromwellryan cromwellryan/warrior.js
Last active Aug 29, 2015

Embed
What would you like to do?
DCC 3.18.2014
jsWarrior.turn = function(warrior) {
enhance(warrior);
actions = [ attack_move, heal_move, walk_move ];
available_actions = actions.filter( function(action) { return action.can(warrior); } );
available_actions[0].act(warrior);
warrior.last_health = warrior.getHealth();
}
function enhance(warrior) {
warrior.isBeingAttacked = function() {
return warrior.last_health > warrior.getHealth();
}
}
attack_move = {
can: function(warrior) { return warrior.check() == "enemy" },
act: function(warrior) { warrior.attack(); }
};
heal_move = {
can: function(warrior) { return !warrior.isBeingAttacked() && warrior.getHealth() < 20; },
act: function(warrior) { warrior.rest(); }
};
walk_move = {
can: function(_warrior) { return true; },
act: function(warrior) { warrior.walk(); }
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.