Untitled

mail@pastecode.io avatar
unknown
csharp
a month ago
4.3 kB
3
Indexable
Never
public class TreeChopLevel : MonoBehaviour
{

    public Text treeChoppingLevelText;
    public int treeChoppingCurrentLevel = 0;
    public int treeChoppingCurrentExp = 0;
    public int treeChoppingRequiredExp = 1;
    public int treeChoppingTotalExp = 0;
    public float baseMultiplier = 1.5f;
    public int treeChoppingMaxLevel = 99;

    private Axes axes;
    private TreeInteract treeInteract;
    private PlayerValueHandler playerValueHandler;

    //This dictionary is cuz i want to make the early levels have a custom exp requirement rather then following the exp curve, the levels that arent listed will simply follow the regular curve
    private Dictionary<int, int> treeChoppingSpecificLeveExp = new Dictionary<int, int>
    {

        { 0, 10 },
        { 1, 20 },
        { 2, 40 },
        { 3, 60 },
        { 4, 80 },
        { 5, 100},
        { 6, 150 },
        { 7, 200 },
        { 8, 250 },
        { 9, 300 },
        { 10, 350 }

    };

    private void Start()
    {

        treeChoppingRequiredExp = CalculateEXPForTreeChoppingLevel(treeChoppingCurrentLevel);

        for (int i = 1; i <= treeChoppingMaxLevel; i++)
        {

            int xpForLevel = CalculateEXPForTreeChoppingLevel(i);
            int totalXP = CalculateTotalEXPForTreeChoppingLevel(i);
            Debug.Log($"Level {i} requires {xpForLevel} XP, total XP to reach this level is {totalXP}");

        }

        TreeChoppingUpdateUI();

    }

    private void Update()
    {

        if (Input.GetKeyDown(KeyCode.T))
        {

            AddTreeChoppingExp(50);

        }

    }

    public int CalculateEXPForTreeChoppingLevel(int level)
    {

        if (treeChoppingSpecificLeveExp.ContainsKey(level))
        {

            return treeChoppingSpecificLeveExp[level];

        }

        //int previousLevelExp = level > 1 ? CalculateEXPForTreeChoppingLevel(level - 1) : 0;
        int calculatedExp = Mathf.FloorToInt(Mathf.Pow(baseMultiplier, level + 1));

        if (level > 1)
        {

            int previousLevelExp = CalculateEXPForTreeChoppingLevel(level - 1);
            calculatedExp = Mathf.Max(calculatedExp, previousLevelExp + (int)(previousLevelExp * 0.1f));

        }

        //return Mathf.Max(calculatedExp, previousLevelExp + 1);
        return calculatedExp;

    }

    public int CalculateTotalEXPForTreeChoppingLevel(int level)
    {

        int totalXP = 0;
        for (int i = 1; i <= level; i++)
        {

            totalXP += CalculateEXPForTreeChoppingLevel(i);

        }

        return totalXP;

    }

    public void AddTreeChoppingExp(int amount)
    {

        if (treeChoppingCurrentLevel >= treeChoppingMaxLevel)
        {

            Debug.Log("Max level reached!");
            return;

        }

        treeChoppingCurrentExp += amount;
        treeChoppingTotalExp += amount;
        Debug.Log($"Added {amount} XP. Current XP: {treeChoppingCurrentExp}/{treeChoppingRequiredExp}.");

        while (treeChoppingCurrentExp >= treeChoppingRequiredExp && treeChoppingCurrentLevel < treeChoppingMaxLevel)
        {

            TreeChoppingLevelUp();

        }

        if (treeChoppingCurrentLevel >= treeChoppingMaxLevel)
        {

            treeChoppingCurrentExp = treeChoppingRequiredExp;
            Debug.Log("Max level reached!");

        }

        TreeChoppingUpdateUI();

    }

    private void TreeChoppingLevelUp()
    {

        treeChoppingCurrentExp -= treeChoppingRequiredExp;
        treeChoppingCurrentLevel++;

        if (treeChoppingCurrentLevel > treeChoppingMaxLevel)
        {

            treeChoppingCurrentLevel = treeChoppingMaxLevel;
            return;

        }

        treeChoppingRequiredExp = CalculateEXPForTreeChoppingLevel(treeChoppingCurrentLevel);
        Debug.Log($"Leveled up! Current Level: {treeChoppingCurrentLevel}. Next level requires {treeChoppingRequiredExp} XP.");

        TreeChoppingUpdateUI();

    }

    private void TreeChoppingUpdateUI()
    {

        treeChoppingLevelText.text = "Tree Level: " + treeChoppingCurrentLevel.ToString() + " [" + treeChoppingCurrentExp.ToString() + "/" + treeChoppingRequiredExp.ToString() + "]";

    }
}
Leave a Comment