-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainMenuState.cpp
More file actions
179 lines (146 loc) · 4.65 KB
/
Copy pathMainMenuState.cpp
File metadata and controls
179 lines (146 loc) · 4.65 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include "MainMenuState.h"
void MainMenuState::initVariable()
{
}
void MainMenuState::initBackground()
{
this->background.setSize(sf::Vector2f(static_cast<float>(this->window->getSize().x),static_cast<float>(this->window->getSize().y)));
if (!this->backgroundTexture.loadFromFile("Resources/BackGround/Mainmenu.jpg")) {
throw("ERROR::MAINMENUSTATE::COULD NOT LOAD BACKGROUND TEXTURE");
}
this->background.setTexture(&backgroundTexture);
}
void MainMenuState::initFonts()
{
if (!this->font.loadFromFile("Fonts/Minecraft.ttf")) {
throw("ERROR::MAINMENUSTATE::COULD NOT LOAD FONT");
}
}
void MainMenuState::initKeybinds()
{
std::ifstream ifs("Config/mainmenustateKeybinds.ini.txt");
if (ifs.is_open()) {
std::string key = "", key2 = "";
while (ifs >> key >> key2) {
this->keybinds[key] = this->supportedKeys->at(key2);
}
}
ifs.close();
}
void MainMenuState::initButtons()
{
this->buttons["GAME_STATE_BTN"] = new gui::ButtonForMainmenu(110.f, 300.f, 300.f, 75.f,
&this->font, "Start Game",30,
sf::Color::White, 40);
/* this->buttons["Setting_STATE_BTN"] = new gui::Button(70.f, 450.f, 300.f, 75.f,
&this->font, "Settings",50,
sf::Color::White, sf::Color::White, sf::Color::White,
sf::Color(70, 70, 70, 200), sf::Color(150, 150, 150, 255), sf::Color(20, 20, 20, 200));
this->buttons["EDITOR_STATE_BTN"] = new gui::Button(70.f, 550.f, 300.f, 75.f,
&this->font, "Editor",50,
sf::Color::White, sf::Color::White, sf::Color::White,
sf::Color(70, 70, 70, 200), sf::Color(150, 150, 150, 255), sf::Color(20, 20, 20, 200));*/
this->buttons["LEADERBOARD_STATE_BTN"] = new gui::ButtonForMainmenu(110.f, 400.f, 300.f, 75.f,
&this->font, "Leader Board", 30,
sf::Color::White,40);
this->buttons["HOWTOPLAY_STATE_BTN"] = new gui::ButtonForMainmenu(110.f, 500.f, 300.f, 75.f,
&this->font, "How to play", 30,
sf::Color::White, 40);
this->buttons["EXIT_STATE_BTN"] = new gui::ButtonForMainmenu(110.f, 600.f, 300.f, 75.f,
&this->font, "Quit",30,
sf::Color::White, 40);
}
void MainMenuState::initBGM()
{
if (!this->bgm.openFromFile("Resources/Sounds/Mainmenu/Mili-Vulnerability.wav")) {
throw("CANNOT LOAD BGM IN MAINMENU STATE");
}
this->bgm.setVolume(30.f);
this->bgm.setLoop(true);
this->bgm.play();
}
MainMenuState::MainMenuState(sf::RenderWindow* window, std::map<std::string, int>* supportedKeys, std::stack<State*>* states)
: State(window, supportedKeys, states)
{
this->initVariable();
this->initFonts();
this->initKeybinds();
this->initButtons();
this->initBackground();
this->initBGM();
pfirefly = new pFirefly(50);
}
MainMenuState::~MainMenuState()
{
for (auto it = this->buttons.begin(); it != this->buttons.end(); ++it) {
delete it->second;
}
}
//functions
void MainMenuState::UpdateInput(const float& deltaTime)
{
}
void MainMenuState::UpdateBTN()
{
//Update BTN and handle their function
for (auto& it:this->buttons) {
it.second->Update(this->mousePosView);
}
if (this->buttons["GAME_STATE_BTN"]->isPressed()) {
this->states->push(new GameState(this->window, this->supportedKeys,this->states));
this->bgm.stop();
}
/* if (this->buttons["Setting_STATE_BTN"]->isPressed()) {
this->states->push(new SettingState(this->window, this->supportedKeys, this->states));
}
if (this->buttons["EDITOR_STATE_BTN"]->isPressed()) {
this->states->push(new EditorState(this->window, this->supportedKeys, this->states));
}*/
if (this->buttons["LEADERBOARD_STATE_BTN"]->isPressed()) {
this->states->push(new LeaderBoard(this->window, this->supportedKeys, this->states));
}
if (this->buttons["HOWTOPLAY_STATE_BTN"]->isPressed()) {
this->states->push(new HowtoPlayState(this->window, this->supportedKeys, this->states));
}
if (this->buttons["EXIT_STATE_BTN"]->isPressed()) {
this->endState();
}
}
void MainMenuState::UpdateBGM()
{
if (this->bgm.getStatus() == this->bgm.Stopped) {
this->bgm.play();
}
}
void MainMenuState::Update(const float& deltaTime)
{
this->UpdateMousePositions();
this->UpdateInput(deltaTime);
this->UpdateBGM();
this->UpdateBTN();
pfirefly->Update(deltaTime);
}
void MainMenuState::RenderBTN(sf::RenderTarget& target)
{
for (auto& it : this->buttons) {
it.second->Render(target);
}
}
void MainMenuState::Render(sf::RenderTarget* target)
{
if (!target) {
target = this->window;
}
target->draw(this->background);
pfirefly->Render(target);
this->RenderBTN(*target);
//temporary check for mouse position
/*sf::Text mouseText;
mouseText.setPosition(this->mousePosView.x+10,this->mousePosView.y);
mouseText.setFont(this->font);
mouseText.setCharacterSize(20);
std::stringstream ss;
ss << this->mousePosView.x << " , " << this->mousePosView.y;
mouseText.setString(ss.str());
target->draw(mouseText);*/
}