So I am trying to make an Incremental Game

d3k0yd3k0y Loveland, OH Icrontian
edited July 2014 in Gaming

I need some help with it however. I know a little bit of JS, less of JQuery, and thus is the reason for my issue.

http://jsfiddle.net/Syp72/4/

That is a link to the code/HTML/CSS.

So the problem is with:

`function selectAVegetable(max) {
    return Math.floor(Math.random()*(max-1+1)+1);
}

var runHarvester = setInterval(function () {
    var availableVeggies = $('.vegetable').filter(":visible").length;
    var harvestedVegetable = selectAVegetable(availableVeggies)
    if (harvestedVegetable === 2){
        autoHarvest(potato);
    }
    else if (harvestedVegetable === 3) {
        autoHarvest(tomato);
    }
    else if (harvestedVegetable === 4) {
        autoHarvest(asparagus);
    }
    else if (harvestedVegetable === 5) {
        autoHarvest(beet);
    }
    else if (harvestedVegetable === 6) {
        autoHarvest(broccoli);
    }
    else if (harvestedVegetable === 7) {
        autoHarvest(cabbage);
    }
    else if (harvestedVegetable === 8) {
        autoHarvest(celery);
    }
    else if (harvestedVegetable === 9) {
        autoHarvest(corn);
    }
    else if (harvestedVegetable === 10) {
        autoHarvest(cucumber);
    }
    else {
        autoHarvest(carrot);
    }
    updateValues();
}, tick);`

This is a sloppy, inefficient, and not quite working function that I am using as a test base for now.

My goal is to have a function that looks to see what you have unlocked, and every tick the auto harvesters go and harvests one of the unlocked vegetables. I don't like having them hard coded like I do now because it is just messy.

I am wording this badly, and I can't think currently so I will come back to this and add some more of what I am looking for specifically, too frustrated to think right now.

Comments

  • MiracleManSMiracleManS Chambersburg, PA Icrontian

    Just as a quick note, you can clean some of this up by passing the value you're grabbing to a function (just like autoHarvest). You can also nest your function calls to clean this up. Assuming rewritten to get your veg function:

    autoHarvest(getVegetable(harvestedVegetable));

  • d3k0yd3k0y Loveland, OH Icrontian
    edited July 2014

    So I am going to have to break what I am looking to do into pieces. The most basic part of what I would like is to find a way to make an array of "sub variables" if that is even a word.

    So if I have a variable set up like this

    var vegetables = {
    -name: "vegetables",
    -carrot: {
    --amount: 0,
    --increment: 1,
    --value: 1
    -},
    -spinach: {
    --amount: 0,
    --increment: 1,
    --value: 1
    -},
    -broccoli: {
    --amount: 0,
    --increment: 1,
    --value: 1
    -}
    }

    Is there a way I can get a .length / .size of the descendant parameters in the vegetables variable. So more or less, I want a function to count how many different items there are inside of "var vegetables", in this case three (carrot, spinach, broccoli).

    I want to be able to calculate on the fly what vegetables you have available and make an array of those so I can have the autoHarvest do what it is doing now (poorly), and randomly select which vegetable it is going to harvest each tick.

    The goal is to allow you to set a focused vegetable that is the only vegetable harvested when focused, but when not focused it goes back to random.

    PS this new forum's code annotation is terrible

  • LincLinc Owner Detroit Icrontian

    @d3k0y said:
    PS this new forum's code annotation is terrible

    I introduced a bug into the code formatter by fixing Markdown's linebreak issue everyone was complaining about. It's on my to-do list.

  • MiracleManSMiracleManS Chambersburg, PA Icrontian
    edited July 2014

    @d3k0y‌ the really cool thing about javascript is that it makes writing inherited classes easy (but can also totally screw with you).

    For the specific question above, are you looking to do something like vegetables.spinach.amount and get the value out? (aka 0, 1, etc.)?

  • d3k0yd3k0y Loveland, OH Icrontian

    I don't really care to get the value out of it as much as figure out which variables have a value equal to it. For example you are playing along and during the process you buy the ability to plant broccoli and spinach, but not pumpkins. So Carrots, Broccoli, and Spinach have a value of "1", and Pumpkins have a value of "0". I want to be able to find all the vegetables with a value of "1" and use that to find the name of the parent ( ie. carrots, broccoli, and spinach) and use those names to create an array for the autoharvest.

Sign In or Register to comment.