Created
March 26, 2019 00:30
-
-
Save FaronBracy/72350ecdad9bbf9838933feced62b112 to your computer and use it in GitHub Desktop.
Warrior JS code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let exploreState = { | |
execute: function(player,warrior) { | |
if (player.sense(warrior) === 'empty') { | |
warrior.walk(player.direction); | |
} | |
else if (player.sense(warrior) === 'wall') { | |
warrior.pivot(); | |
} | |
else if (player.sense(warrior) === 'enemy') { | |
warrior.think("It's time to kick ass and chew bubble gum..."); | |
warrior.think("and I'm all out of gum."); | |
player.changeState(meleeAttackState,warrior); | |
} | |
else if (player.sense(warrior) === 'captive') { | |
warrior.think("What do you say to some champaigne, huh, baby."); | |
player.changeState(rescueState,warrior); | |
} | |
} | |
} | |
let meleeAttackState = { | |
execute: function(player,warrior) { | |
if (player.sense(warrior) === 'enemy') { | |
warrior.attack(player.direction); | |
} | |
else { | |
warrior.think("Nobody goes out that door! Not until daylight!!"); | |
player.changeState(cautiousState,warrior); | |
} | |
} | |
} | |
/* | |
let rangedAttackState = { | |
execute: function(player,warrior) { | |
if (player.sense(warrior) === 'enemy') { | |
warrior.shoot(player.direction); | |
} | |
else { | |
warrior.think("Me and ol' double barrel here will blow your butts to kingdom come!! See if we don't!"); | |
player.changeState(cautiousState,warrior); | |
} | |
} | |
} | |
*/ | |
let cautiousState = { | |
execute: function(player,warrior) { | |
if (warrior.health() < player.health) { | |
warrior.think("I made a mistake...wait, wait!! No, I made a mistake!"); | |
player.switchDirection(); | |
player.changeState(retreatState,warrior); | |
} | |
else if (player.sense(warrior) === 'empty' && warrior.health() < warrior.maxHealth()) { | |
warrior.rest(); | |
} | |
else { | |
warrior.think("Groovy. Those pages are down there somewhere."); | |
player.changeState(exploreState,warrior); | |
} | |
} | |
} | |
let retreatState = { | |
execute: function(player,warrior) { | |
if (warrior.health() < player.health) { | |
warrior.think("You...bastards! You dirty bastards!"); | |
warrior.walk(player.direction); | |
} | |
else if ( warrior.health() < warrior.maxHealth()) { | |
warrior.rest(); | |
} | |
else { | |
warrior.think("Uh-huh, that's right. Who's laughing now? Who's laughing now?!"); | |
player.switchDirection(); | |
player.changeState(exploreState,warrior); | |
} | |
} | |
} | |
let chargeState = { | |
execute: function(player,warrior) { | |
if (player.sense(warrior) === 'empty') { | |
warrior.walk(player.direction); | |
} | |
else { | |
warrior.think("Swallow this."); | |
player.changeState(meleeAttackState,warrior); | |
} | |
} | |
} | |
let rescueState = { | |
execute: function(player,warrior) { | |
if (player.sense(warrior) === 'captive') { | |
warrior.rescue(player.direction); | |
} | |
else { | |
warrior.think("Feels like...somebody just walked over my grave.") | |
player.changeState(cautiousState,warrior); | |
} | |
} | |
} | |
class Player { | |
constructor() { | |
this.state = exploreState; | |
this.health = 20; | |
this.direction = 'forward'; | |
} | |
playTurn(warrior) { | |
this.state.execute(this,warrior); | |
this.health = warrior.health(); | |
} | |
changeState(newState,warrior) { | |
this.state = newState; | |
this.playTurn(warrior); | |
} | |
sense(warrior) { | |
let space = warrior.feel(this.direction); | |
return this.getSpaceType(space); | |
} | |
scout(warrior){ | |
let spaces = warrior.look(this.direction); | |
return spaces.map(s=>this.getSpaceType(s)); | |
} | |
switchDirection() { | |
if (this.direction === 'forward') { | |
this.direction = 'backward'; | |
} | |
else { | |
this.direction = 'forward'; | |
} | |
} | |
getSpaceType(space) { | |
if (space.isEmpty()) { | |
return 'empty'; | |
} | |
else if (space.isWall()) { | |
return 'wall'; | |
} | |
else if (space.getUnit().isEnemy()) { | |
return 'enemy'; | |
} | |
else if (space.getUnit().isBound()) { | |
return 'captive'; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment