简体   繁体   中英

How do I make a level purchase multiplier for Incremental games?

How do I make a level purchase multiplier for Incremental games? When a person buys any level, he has money spent and the purchase price increases by the formula:

        money = 1000;       
        baseCost = 100;
        multiplier = 1.09;
        lvl = 1;
        newCost;
    
    newCost = baseCost * Math.Pow(multiplier, lvl)

The question is: How do I make that the player can buy multiple levels, taking into account the rising price for each new level? It is also important that a person cannot buy more levels than he has money.

The multipliers will be as follows:

  1. X1 - purchase of one level.
  2. X10 - purchase of 10 levels.
  3. X50 - purchase of 50 levels.
  4. MAX - purchase of the maximum possible number of levels.

I would do as follows:

            bool BuyLevels(money, AmountOfLevelsToBuy)
            {

                multiplier = 1.09;
                switch (AmountOfLevelsToBuy)
                {
                case < 10:
                    if (CanAffordLevels(money, AmountOfLevelsToBuy, multiplier))
                    {
                        player.level += AmountOfLevelsToBuy;
                        return true; //Purchase was successful
                    }
                    return false;

                case (>= 10 && < 50):
                            multiplier *= 10;
                    if (CanAffordLevels(money, AmountOfLevelsToBuy, multiplier))
                    {
                        player.level += AmountOfLevelsToBuy;
                        return true; //Purchase was successful
                    }
                    return false;


                case (>= 50 && < MAX):
                            multiplier *= 50;
                    if (CanAffordLevels(money, AmountOfLevelsToBuy, multiplier))
                    {
                        player.level += AmountOfLevelsToBuy;
                        return true; //Purchase was successful
                    }
                    return false;

                case == MAX:
                    multiplier *=  MAX_Value;
                    if (CanAffordLevels(money, AmountOfLevelsToBuy, multiplier))
                    {
                        player.level += AmountOfLevelsToBuy;
                        return true; //Purchase was successful
                    }
                    return false;
            }
       }

       bool CanAffordLevels(money, AmountOfLevelsToBuy, out multiplier)
       {
           newCost = baseCost * Math.Pow(out multiplier, AmountOfLevelsToBuy);
           if (newCost > Money)
           {
               multiplier = 1.09;
               return false;
           }
           return true;
       }

This is a mix of C# and pseudo-code. It lacks Datatypes and access modifiers, will have to fill that in. The "out" Keyword in the CanAffordLevels method declaration allows the multiplier variable to be changed for the BuyLevels method. I will let you figure out how to the MAX_Value for Levels part yourself:).

I'm nout sure that it works correct. But it's my vision of your problem:)
I think you should create some kind of entity for multipliers. For example Enum

public const double baseCost = 100.0;
public const double multiplier = 1.09;

public enum Multipliers
{
    X1 = 1,
    X10 = 10,
    X50 = 50,
    Max = -1
}

var stringLvlToBuy = "MAX"; //input value from user
double newCost = 0;
int lvl = 1;
double money = 1000;

//Parse input value To Enum or your entity for multipliers
if (!Enum.TryParse(stringLvlToBuy, out Multipliers multiplierLvl);)
{
    //Value didn't parse so you should  do something 
}

var lvlToBuy = (int) multiplierLvl;

if (multiplierLvl == Multipliers.Max)
{
    var maxLevelsToBuy = 0;
    var nextLevel = lvl + 1;
    double totalSum = baseCost * Math.Pow(multiplier, nextLevel);
    while (money > totalSum)
    {
        nextLevel++;
        maxLevelsToBuy++;
        totalSum += baseCost * Math.Pow(multiplier, nextLevel);
    }

    lvl = maxLevelsToBuy;
    money -= (totalSum - baseCost * Math.Pow(multiplier, nextLevel));
}
else
{
    var restMoney = BuyLevelsIfPossible(money, lvl, lvlToBuy);
    if (restMoney < 0.0)
    {
        //print "You don't have enough money" 
    }
    else
    {
        money = restMoney;
    }
}
  
public double BuyLevelsIfPossible(double playerMoney, int currentLevel, int lvlToBuy)
{
    var totalSum = 0.0;
    double newCost;

    for (int i = currentLevel + 1; i <= lvlToBuy; ++i)
    {
        newCost = baseCost * Math.Pow(multiplier, i);
        totalSum += newCost;
    }

    if (totalSum > playerMoney)
    {
        //do what you gonna do if money isn't enough. e.g.
        return -1;
    }

    return playerMoney - totalSum;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM