-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLevelManager.cs
129 lines (111 loc) · 2.35 KB
/
LevelManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
namespace diep;
using Godot;
using System;
using System.Collections.Generic;
public class LevelManager
{
public int _currentXP;
public int _upgradePoints;
private int _level;
private const int MaxLevel = 45;
public List<int> _xpToLevels;
public int _spentPoints;
[Export] public int BaseXP = 200;
[Export] public float ExponentialFactor = 1.21f;
[Export] public int AdditionalXP = 50;
public LevelManager()
{
_currentXP = 0;
_level = 0;
_upgradePoints = 0;
_xpToLevels = CalculateXPRequirements(MaxLevel, BaseXP, ExponentialFactor, AdditionalXP);
}
public void AddXP(int xp)
{
_currentXP += xp;
CheckForLevelUp();
}
private void CheckForLevelUp()
{
while (_level < _xpToLevels.Count && _currentXP >= _xpToLevels[_level])
{
_level++;
GD.Print($"Level Up! New Level: {_level}");
GrantUpgradePoints();
}
}
public List<int> CalculateXPRequirements(int maxLevels, int q, float k, int p)
{
List<int> xpRequirements = new List<int>();
for (int x = 0; x < maxLevels; x++)
{
int xpRequired = (int)Math.Ceiling(q * Math.Pow(x, k) + p);
xpRequirements.Add(xpRequired);
}
return xpRequirements;
}
public int GetXPForNextLevel()
{
if (_level < _xpToLevels.Count)
return _xpToLevels[_level];
else
return int.MaxValue;
}
private void GrantUpgradePoints()
{
if (_level <= 15)
{
_upgradePoints++;
}
else if (_level <= 30)
{
if (_level % 2 != 0)
{
_upgradePoints++;
}
}
else if (_level <= MaxLevel)
{
if (_level % 3 == 0)
{
_upgradePoints++;
}
}
GD.Print($"Upgrade Points Earned: {_upgradePoints}");
}
public int GetUpgradePoints() => _upgradePoints;
public void SpendUpgradePoint()
{
if (_upgradePoints > 0)
{
_upgradePoints--;
_spentPoints += 1;
}
else
{
GD.Print("No Upgrade Points available!");
}
}
public int GetCurrentLevelXP()
{
int currentLevelIndex = Math.Max(0, _level - 1);
return _xpToLevels[currentLevelIndex];
}
public int GetNextLevelXP()
{
if (_level < _xpToLevels.Count)
return _xpToLevels[_level];
return int.MaxValue;
}
public int GetCurrentXPWithinLevel()
{
int currentLevelXP = GetCurrentLevelXP();
return _currentXP - currentLevelXP;
}
public int GetXPRangeForCurrentLevel()
{
int currentLevelXP = GetCurrentLevelXP();
int nextLevelXP = GetNextLevelXP();
return nextLevelXP - currentLevelXP;
}
}