0

I'm making a game where the player can pick 3 different characters. However I am running into a glaring problem, that being, when I create a function (like a attack function), it is linked to only 1 specific character.

I would rather have my code be written where when the person picks their character, all can use the same attack skill without me having to write 3 different ones. Also, the attack skills are linked to a button, so it must be diverse.

I can't have a designated attack button for X player. So how do I make my code so it can add all characters instead of just 1 specified character?

Example: Looking at my function below for the strike attack. I can set it to dwarf & angel which is fine. However what if the player picks a ELF character instead? Then the function will not work because it believes the character is a dwarf, fighting a angel. How can I fix this?

New=Object.create;

actor = {
    primaryStats: function (level, hp, hpcap, balance, balancecap, exp){
        this.level = level;
        this.hp = hp;
        this.hpcap = hpcap;
        this.balance = balance;
        this.balancecap = balancecap;
        this.exp = exp;
},

player = New (actor),
monster = New (actor),

dwarf = New(player),
human = New(player),
elf = New(player),

angel = New(monster),
demon = New(monster),
dragon = New(monster);

//ATTACK SKILL ONE
dom.el("strike").onclick = function strike() {

    playerHitCalc(dwarf, angel);
    };   

    playerHitCalc = function(character, boss){
        roll = Math.floor(Math.random() * character.accuracy + 1);
        if (roll > boss.agility){
            var hit = true;
        }
        else {
            hit = false;
            logMessage(boss.ID + " " + "has evaded your attack!")
        }
        playerDamCalc = function(){
            if (hit == true){ //If you score a successful hit
                var damage = Math.floor(Math.random() * character.strength + 1);
                var totalDamage = damage - boss.armor; // Subtract Damage from Bosses Armor
                if(totalDamage <= 0)totalDamage += boss.armor; // Add boss armor to prevent Negative Numbers
                boss.hp -= totalDamage; // Subtract bosses HP from damage.
                character.exp += totalDamage * 0.25; // Gain 1 exp point per 4 damage done
                dom.setText("bosshealthcounter", boss.hp) // Update Bosses Health
                logMessage("You hit " + boss.ID + " for " + totalDamage + " damage!");
            }
tharindu_DG
  • 8,900
  • 6
  • 52
  • 64
Shawn
  • 1,175
  • 2
  • 11
  • 20

2 Answers2

0

You can use data attributes to link a dom element to particular characters. For example -

 <button class="attack-btn" data-attacker="dwarf" data-target="angel">Attack</button>

Then in the on click handler, you can extract that particular element's attributes attacker and target instead of hardcoding the values.

Hope that helps!

hkasera
  • 2,118
  • 3
  • 23
  • 32
0

One thing you are doing is creating Player and Monster from Actor and then specific player class from Player and the same for monster.

Problem lies in that now you have a specific handle for each type of player and each type of monster.

If we would want to edit current code, you would have to add currentPlayer and currentMonster variables that you would make equal to the Player and Monster you want to fight. Then you could avoid referencing specific player and specific monster and just use

playerHitCalc(currentPlayer, currentMonster);

But I would suggest changing things a little bit and create objects in a little different way.

Criss Lion
  • 209
  • 1
  • 7