Asteria
Asteria

Reputation: 330

Edit a variable within an array

I'm attempting to create a Choose Your Own Adventure type of game, and I'm currently trying to write a 'battle' script. What I've got so far is:

var name = "Anon";
var health = 100;
var youAttack = [name + " hits the " + opp + " with his sword", name + " uses magic!", name + " is too scared to fight!"];
var youBattle = function() {
    var youBattle = youAttack[Math.floor(Math.random() * 3)];
    return youBattle;
};



var opp = "Orc";
var oppHealth = 100;
var oppAttack = ["The " + opp + " hits you with his hammer!", "The " + opp + " does nothing!", "The " + opp + " back hands you!"];
var oppBattle = function() {
    var oppBattle = oppAttack[Math.floor(Math.random() * 3)];
    return oppBattle;
};

oppBattle();

youBattle();

I've done it like this so the opponent and player names can easily be changed.

What I'm struggling to figure out is how I can add / remove health from both the opponent and the player depending what attack is used. Obviously no health would be removed if the opp / player does nothing.

Is there a way I can do this without a bunch of messy if / else statements?

I was hoping for something easy like name + " hits the " + opp + " with his sword" + health = health - 10; but obviously that didn't work.

Thanks in advance!

Upvotes: 0

Views: 107

Answers (2)

tbh__
tbh__

Reputation: 324

http://jsbin.com/qerud/3/edit

Hope this isn't too much code:

var Attack = function(hero,opp,damageReceived,damageGiven,message){ 

        this.message = message;
        this.damageGiven = damageGiven;
        this.damageReceived = damageReceived;
        this.opp = opp;
        this.hero = hero;
        this.attack = function(opp){
            this.hero.health -= damageReceived;
            this.opp.health -= damageGiven;
            return this.message;
        };

};

var Character = function(name,health){
    this.name = name;
    this.health = health;
};

hero = new Character('Anon',100);
orc = new Character('Orc',150);

attack1 = new Attack(hero,orc,5,0,"The " + orc.name + " back hands you!");
attack2 = new Attack(hero,orc,0,0,hero.name + " is too scared to fight!");
attack3 = new Attack(hero,orc,15,0,"The " + orc.name + " hits you with his hammer!");
attack4 = new Attack(hero,orc,0,25,hero.name + " uses magic!");

attacks = [attack1,attack2,attack3,attack4];

while(hero.health > 0 && orc.health > 0){
    console.log(attacks[Math.floor(Math.random() * 4)].attack());
    console.log('Hero Health: '+ hero.health);
    console.log('Orc Health: '+ orc.health);
}

if(hero.health  > 0 ){
    console.log(hero.name + ' won');
} else {
    console.log('The ' + orc.name + ' won');

}

Upvotes: 2

NocFenix
NocFenix

Reputation: 701

I can tell you first hand that trying to write this type of code uses a lot of if/else and more statements, regardless of what language you're using. You can use an array to hold the values of your attack patterns:

var attackName = ["Punch", "Sword", "Magic"]
var attackDamage = [3, 5, 4]

function youAttack(ATK, PHit) {

    if(playerHit) {
        playerDamage = ATK + PHit;
        oppHealth = oppHealth - playerDamage;
        return oppHeath;
    } else {
        alert("You missed!");
    }
}

But, without seeing exactly what you're doing I cannot say how you should do your attacks and damages. I can only assume. You will need a system of evaluating attacks, misses, etc. that does use IF/ELSE Statements at least somewhere.

Upvotes: 1

Related Questions