0

I am currently working on an incremental game, Gold Rush, and am trying to create a total gold earned value that stores all of the gold the user has earned throughout the game, including the gold spent. So far I have put it so every time the player earns gold by clicking or incrementors it adds to the value. As the game gets more complex I find that this becomes harder to do, and I am trying to make a function that adds 1 to the totalGold variable every time it adds one to the gold variable. So far I have tried this:

if (gameData.gold + 1) {
   gameData.totalGold + 1;
}

The incrementor profits are gathered like this

function goldPerSecond() {
  return gameData.pickaxeGold + gameData.dwarfGold + gameData.gooseGold + gameData.mineGold + gameData.dragonGold + gameData.stoneGold + gameData.stationGold + gameData.leprechaunGold + gameData.sheepGold + gameData.rayGold + gameData.mergerGold;
}

And the clikcing profits gathered like this

function collectGold() {
  gameData.gold += gameData.clickinGold
  gameData.clicks += 1
  gameData.totalGold += gameData.clickinGold
}

I am fairly new to JavaScript and do not know any other way I could do it. It would be preferable if I did not have to use JQuery. Thank you.

Squirrel
  • 15
  • 5
  • It depends on how you have structured your code, that if condition looks fairly good on its own. Can you add more code around it ? – vanshaj Dec 13 '20 at 15:23
  • This really sounds like a [XY problem](https://en.m.wikipedia.org/wiki/XY_problem) – Julian Dec 13 '20 at 15:40

3 Answers3

0

I think you should have a click event on each gold piece, when clicked, it should call a function that adds 1 to the total, and it should look like this:

function addGold() {
  gameData.totalGold++; // Stands for: gameData.totalGold = gameData.totalGold + 1; (New Value = old value + 1)
}

// A click event for each gold piece, upon clicking, we call the `addGold` to increase the total
elem.addEventListener('click', function() {
  addGold();
});

Not really sure if that's what you're looking for or I misunderstood you. A live demo on Codepen or JSBin or any alternative would be the best to help you!

Elharony
  • 711
  • 5
  • 17
0

One way you could solve this problem is by registering a listener to the gold property, such that every time it were incremented, then totalGold property would be too. Here is an example of how it could work using vanilla JS:

gameData = {
  goldInternal: 0,
  goldListener: function(val) {},
  totalGold: 0,
  set gold(val) {
    this.aInternal = val;
    this.goldListener();
  },
  get gold() {
    return this.goldInteral;
  },
  registerGoldListener: function(listener) {
    this.goldListener = listener;
  }
}

gameData.registerGoldListener(() => {
    gameData.totalGold += 1;
});

gameData.gold += 1;
console.log(gameData.totalGold);
gameData.gold += 1;
console.log(gameData.totalGold);

Please note that I referenced this earlier SO post.

RAlfredson
  • 18
  • 3
0

Normally I would've placed this in a comment but I can't comment yet so here's what I think you should do.

As you said, adding to the total gold is tedious, so instead of that, create a function that adds gold, something like this

function addGold(gold) {
  gameData.gold = gameData.gold + gold;
  gameData.totalGold = gameData.totalGold + gold
}

So whenever you have to add gold you'll just say addGold(1) or replace 1 with whatever the number of gold you wanted to add, and the gold will be added to both the current gold and total gold

Elpis
  • 62
  • 5