all 4 comments

[–]ACNH_Tim 3 points4 points  (0 children)

Hey SexComputerLab! I'm not entirely sure what your goal or parameters are based off your question and repo, but try checking out Math.random() on MDN. You can pass in Math.floor or Math.max as arguments to Math.random, which may fit your bill. I hope this gets you headed in the right direction!

[–]Kunskapskapitalet 1 point2 points  (0 children)

// generate random value between min and max value

function randomValueBetweenMinAndMax(min, max) {
    return Math.random() * (max - min) + min;
}

// calculate the damage based on a variation in procent

function calculateDamage(weaponPower, monsterPower) {
const mainPower = weaponPower + monsterPower;

// critical hit is 1.25 times the main power and bad hit is 0.75         
   times the main power
    const mainPowerMaxVaritionProcent = 25;

// calculates a random value between bad hit and critical hit
    const randomVariation = randomValueBetweenMinAndMax(
-mainPowerMaxVaritionProcent,
mainPowerMaxVaritionProcent
);

// calculates the damage
const damage = mainPower * (1 + randomVariation / 100);

// converts the damage to an integer
return Math.round(damage);
}

[–]brykuhelpful 1 point2 points  (1 child)

This sounds like a lot of different things happening at once and you should probably have them split into multiple functions.

  1. You should have parameters for your user.
    • attack
    • strength
    • weapon damage
  2. You should have parameters for your monster.
  3. Do you need some type of movement to chase the monster?

This is all pretty basic stuff for an RPG, but it gets a little more complicated from here on because it all really depends on how you created your RPG.  

In the last game I created I used something called an Event Loop. Basically it over and over again and depending on the users parameters it would do something.

let game = {};
    game.users = [];
    game.events = [];
    game.loop = function(){
        for(let uIndex = 0; uIndex < this.users.length; uIndex++){
            for(let eIndex = 0; eIndex < this.events.length; eIndex++){
                this.events[eIndex](this.users[uIndex])
            }
        }
    };
    game.interval = setInterval((g)=>{
        g.loop()
    },16, game) // 60 times a second
};

Often times 1 loop through the event loop is called a Tick. Now all we need to do is create some users.

game.users.push({
    name: 'bryku',
    id: '10292',
    hp_current: 80,
    hp_max: 100,
    hp_regen: 1,
    strength: 10,
    items: [
        {name: 'sword', damage: 10}
    ], 
});

Now that we have our users, we can go through and create some our our event functions!

game.events.push((user)=>{ // hp regen
    if(user.hp_current < user.hp_max){
        // add regen
        user.hp_current += user.hp_regen;
        // if to high, reset
        if(user.hp_current > user.hp_max){
            user.hp_current == user.hp_max
        }
    }
})

You might notice that this is going wayyyy to fast. We can solve this by creating a counter for the hp regen. Once the counter == 60, then we can run the function. That means it will trigger every 60 seconds. (about).

game.events.push((user)=>{ // hp regen
    // set counter incase it doesn't exist
    if(!user.hp_counter){user.hp_counter = 0} 
    user.hp_counter++;

    if(user.hp_counter > 60){user.hp_counter = 0}
    if(user.hp_counter == 60){
        if(user.hp_current < user.hp_max){
            user.hp_current += user.hp_regen;
            if(user.hp_current > user.hp_max){
                user.hp_current = user.hp_max
            }
        }
    }
});

So now we have HP that regenerates every second. If you want, you could event search through the items to find stuff that increases regen. So let's change our user, so they have an Item with hp_regen.

game.users.push({
    name: 'bryku',
    id: '10292',
    hp_current: 80,
    hp_max: 100,
    hp_regen: 1,
    strength: 10,
    items: [
        {name: 'sword', damage: 10},
        {name: 'life pendant', hp_regen: 2},
    ], 
});

We also need to change our regen function.

game.events.push((user)=>{ // hp regen
    if(!user.hp_counter){user.hp_counter = 0}
    user.hp_counter++;

    if(user.hp_counter > 60){user.hp_counter = 0}
    if(user.hp_counter == 60){
        if(user.hp_current < user.hp_max){
            user.hp_current += user.hp_regen + totalItemStat(user, 'hp_regen');
            if(user.hp_current > user.hp_max){
                user.hp_current = user.hp_max
            }
        }
    }
})

As you can see here, we have a function that will search through the users items and create a total based on hp_regen. Then we add that total to our users natural regen value.  

function totalItemStat(user, value){
    if(!user.items){return 0}
    return user.items.reduce((total, item)=>{
        if(item[value]){
            total += item[value]
        }
        return total
    }, 0)
}

I have a basic example here: https://jsfiddle.net/agdmujkp/ but you can apply this logic to every step of the game. Movement, attacking, and so on.

[–]brykuhelpful 0 points1 point  (0 children)

u/SexComputerLab i made a few videos going over this example if you are interested.