Book Image

HTML5 Game Development Hotshot

By : Seng Hin Mak, Makzan Makzan (Mak Seng Hin)
Book Image

HTML5 Game Development Hotshot

By: Seng Hin Mak, Makzan Makzan (Mak Seng Hin)

Overview of this book

Table of Contents (15 chapters)
HTML5 Game Development HOTSHOT
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Generating coins and diamonds


In this task, we create the economy of the game by generating the coins and diamonds.

Prepare for lift off

Before getting into the code, let's summarize how the buildings affect the coins and diamonds:

  • The generation of coins and diamonds depends on the number of buildings

  • By default, each coin is generated within a fixed duration

  • More coin generators speed up the coin generation process

  • The merchant building trades 200 coins for a diamond once in a while, unless there are not enough coins

  • These buildings are where the population resides

  • We count the number of power supplies to know the maximum population capacity

Engage thrusters

To generate coins and diamonds, perform the following steps:

  1. First, we define the tickCountForMerchantDiamond and coinsNeededForDiamond variables in the game settings:

    game.setting = {
    ...
      // existing game setting
      tickCountForMerchantDiamond: 800,
      coinsNeededForDiamond: 200
    };
  2. Let's create a function to increase the diamond count. We add...