Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
function RaidPlayer(playerController) {
debugger;
this.pc = playerController;
this.killProgram = false;
this.moveByKey = function(direction, enemies){
if(this.pc.canMove(direction)){
this.pc.move(direction)
}
else{
window.alert("That is not a valid move direction:");
this.mainMenu(enemies);
}
};
this.meleeMenu = function(enemies){
if(this.killProgram){
return;
}
var inRange = enemies.meleeTargets;
if(inRange.length === 1){
this.pc.meleeAttack(inRange[0].location);
return
}
else if(inRange.length === 0){
window.alert("There are no melee targets.");
this.mainMenu(enemies);
return;
}
var targetText = "MELEE MENU:\N" +
"back to main menu: back\n";
for(var e = 0; e < inRange.length; e++){
var target = inRange[e];
targetText += e + ": " + target.name + " (HP" + target.hp + ")\n";
}
var text = window.prompt(targetText);
var enemy = inRange[Number(text)];
if(text === "KILL"){
this.killProgram = true;
}
else if(text === "back"){
this.mainMenu(enemies);
}
else if(enemy != null){
this.pc.meleeAttack(enemy.location);
}
else{
window.alert("Not a valid melee target.");
this.meleeMenu(enemies);
}
};
this.magicMenu = function(enemies){
if(this.killProgram){
return;
}
var inRange = enemies.magicTargets;
if(inRange.length === 1){
this.pc.magicAttack(inRange[0].location);
return
}
else if(inRange.length === 0){
window.alert("There are no magic targets.");
this.mainMenu(enemies);
return;
}
var myLoc = this.pc.getCurrentLocation();
var targetText = "MAGIC MENU:\N" +
"back to main menu: back\n";
inRange.sort(function(a, b){
return myLoc.distanceSquaredTo(a.location) - myLoc.distanceSquaredTo(b.location);
});
for(var e = 0; e < inRange.length; e++){
var target = inRange[e];
targetText += e + ": " + target.name + " (HP" + target.hp + ")\n";
}
var text = window.prompt(targetText);
var enemy = inRange[Number(text)];
if(text === "KILL"){
this.killProgram = true;
}
else if(text === "back"){
this.mainMenu(enemies);
}
else if(enemy != null){
this.pc.magicAttack(enemy.location);
}
else{
window.alert("Not a valid magic target.");
this.magicMenu(enemies);
}
};
this.rangedMenu = function(enemies){
if(this.killProgram){
return;
}
var inRange = enemies.rangedTargets;
if(inRange.length === 1){
this.pc.rangedAttack(inRange[0].location);
return
}
else if(inRange.length === 0){
window.alert("There are no ranged targets.");
this.mainMenu(enemies);
return;
}
var myLoc = this.pc.getCurrentLocation();
var targetText = "RANGED MENU\n" +
"back to main menu: back\n";
inRange.sort(function(a, b){
return myLoc.distanceSquaredTo(a.location) - myLoc.distanceSquaredTo(b.location);
});
for(var e = 0; e < inRange.length; e++){
var target = inRange[e];
targetText += e + ": " + target.name + " (HP" + target.hp + ")\n";
}
var text = window.prompt(targetText);
var enemy = inRange[Number(text)];
if(text === "KILL"){
this.killProgram = true;
}
else if(text === "back"){
this.mainMenu(enemies);
}
else if(enemy != null){
this.pc.rangedAttack(enemy.location);
}
else{
window.alert("Not a valid ranged target.");
this.rangedMenu(enemies);
}
};
this.mainMenu = function(enemies){
if(this.killProgram){
return;
}
var displayText = "MOVEMENT: \n" +
"N: u \n" +
"NE: i \n" +
"NW: y \n" +
"W: h \n" +
"E: k \n" +
"SE: m \n" +
"S: n \n" +
"SW: b \n" +
"\n" +
"ACTIONS: \n" +
"heal: d \n" +
"wait: q \n";
if(enemies.meleeTargets.length > 0){
displayText += "melee: g \n"
}
if(enemies.magicTargets.length > 0){
displayText += "magic: t \n"
}
if(enemies.rangedTargets.length > 0){
displayText += "ranged: f \n"
}
var text = window.prompt(displayText);
console.log(text);
switch(text){
case "u":
this.moveByKey(Direction.NORTH, enemies);
break;
case "i":
this.moveByKey(Direction.NORTH_EAST, enemies);
break;
case "y":
this.moveByKey(Direction.NORTH_WEST, enemies);
break;
case "h":
this.moveByKey(Direction.WEST, enemies);
break;
case "k":
this.moveByKey(Direction.EAST, enemies);
break;
case "m":
this.moveByKey(Direction.SOUTH_EAST, enemies);
break;
case "n":
this.moveByKey(Direction.SOUTH, enemies);
break;
case "b":
this.moveByKey(Direction.SOUTH_WEST, enemies);
break;
case "d":
this.pc.heal();
break;
case "t":
this.magicMenu(enemies);
break;
case "g":
this.meleeMenu(enemies);
break;
case "f":
this.rangedMenu(enemies);
break;
case "q":
// do nothing
break;
case "KILL":
this.killProgram = true;
break;
default:
window.alert("That is not a valid action.");
this.mainMenu(enemies);
}
};
}
RaidPlayer.prototype = {
act: function() {
if(this.killProgram){
return;
}
var allEnemies = this.pc.senseNearbyEnemies();
var meleeTargets = [];
var magicTargets = [];
var rangedTargets = [];
for(var e = 0; e < allEnemies.length; e++){
var enemy = allEnemies[e];
if(this.pc.canMeleeAttack(enemy.location)){
meleeTargets.push(enemy);
}
if(this.pc.canMagicAttack(enemy.location)){
magicTargets.push(enemy);
}
if(this.pc.canRangedAttack(enemy.location)){
rangedTargets.push(enemy);
}
}
var enemies = {
meleeTargets: meleeTargets,
magicTargets: magicTargets,
rangedTargets: rangedTargets
};
this.mainMenu(enemies);
}
};
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.