-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAC_LevelingComponent.cpp
73 lines (61 loc) · 1.93 KB
/
AC_LevelingComponent.cpp
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
// Fill out your copyright notice in the Description page of Project Settings.
#include "AC_LevelingComponent.h"
#include "Math/UnrealMathUtility.h"
// Sets default values for this component's properties
UAC_LevelingComponent::UAC_LevelingComponent()
{
// Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
// off to improve performance if you don't need them.
PrimaryComponentTick.bCanEverTick = true;
}
// Called when the game starts
void UAC_LevelingComponent::BeginPlay()
{
Super::BeginPlay();
CurrentLevel = 1;
//Calculate Experience Table
int32 tempExp;
int32 j;
for (int32 i = 1; i < 50; i++) {
j = (i * 100) + 50;
if (i >= 30) {
j += 1000;
}
tempExp = ExperienceTable[i - 1];
ExperienceTable[i] = tempExp + j;
}
ExperienceTable[50] = 0;
//Set initial max exp needed to level up to 100
ExperienceTable[0] = CurrentMaxExp;
BufferExp = 0;
CalculatePercentage();
}
// Called every frame
void UAC_LevelingComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
CalculatePercentage();
CurrentExp = BufferExp;
if (CurrentExp >= CurrentMaxExp) {
LevelUp();
}
}
void UAC_LevelingComponent::LevelUp() {
int32 RemainingExp;
if (CurrentLevel >= MaxLevel) {
CurrentExp = CurrentMaxExp;
BufferExp = CurrentExp;
CurrentLevel = MaxLevel;
}
else {
++CurrentLevel;
RemainingExp = BufferExp - CurrentMaxExp;
CalculateMaxExp();
CurrentExp = 0;
BufferExp = 0;
AddExp(RemainingExp);
}
}
void UAC_LevelingComponent::AddExp(float Amount) { BufferExp += Amount; }
void UAC_LevelingComponent::CalculateMaxExp() { CurrentMaxExp = ExperienceTable[CurrentLevel - 1]; }
void UAC_LevelingComponent::CalculatePercentage() { CurrentPercentage = CurrentExp / MaxExp; }