-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenu.cpp
More file actions
188 lines (165 loc) · 5.85 KB
/
Copy pathMenu.cpp
File metadata and controls
188 lines (165 loc) · 5.85 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
180
181
182
183
184
185
186
187
188
#include "Game.h"
#include "Menu.h"
#include <iostream>
Menu::Menu(){
ak_ = &Allkit::Get();
//////GENERALIZATION OF MENU
//A new menu consist of a vector with the menubuttons stored in the menu class
//A function implements a way to select the buttons (this varies with te relative position of the buttons)
// and some code that runs when a button is selected
// UpdateMenu() takes as argument the button vector and draws the menu, only gets called after an update happens
//Add buttons to respective ButtonVector
MainMenuButtons.push_back(Button("START",Point(450,100)));
MainMenuButtons.push_back(Button("REPLAY",Point(450,200)));
MainMenuButtons.push_back(Button("QUIT",Point(450,400)));
LevelSelectButtons.push_back(Button("LEVEL 1",Point(450,100)));
LevelSelectButtons.push_back(Button("LEVEL 2",Point(450,200)));
LevelSelectButtons.push_back(Button("LEVEL 3",Point(450,300)));
LevelSelectButtons.push_back(Button("RETURN",Point(450,400)));
ReplaySelectButtons.push_back(Button("HIGHSCORE 1",Point(450,100)));
ReplaySelectButtons.push_back(Button("HIGHSCORE 2",Point(450,200)));
ReplaySelectButtons.push_back(Button("HIGHSCORE 3",Point(450,300)));
ReplaySelectButtons.push_back(Button("RETURN",Point(450,400)));
}
void Menu::Run()
{
exit_ = false;
UpdateMenu(MainMenuButtons);
// Main Menu loop
while (!exit_) {
ak_->NextEvent();
if(ak_->IsArrowKeyUpPushed()){
selectedButton == 0? selectedButton=MainMenuButtons.size()-1: selectedButton-=1;
UpdateMenu(MainMenuButtons);
}
else if(ak_->IsArrowKeyDownPushed()){
selectedButton == MainMenuButtons.size() - 1 ? selectedButton=0: selectedButton+=1;
UpdateMenu(MainMenuButtons);
}
else if(ak_->IsEnterKeyPushed()){
if(selectedButton==0){
LevelSelect();
}
else if(selectedButton==1){
ReplaySelect();
}
else if(selectedButton==2){
exit_ = true;
}
UpdateMenu(MainMenuButtons);
}
if (ak_->IsWindowClosed()) {
exit_ = true;
}
}
}
void Menu::StartGame(Context& context)
{
std::cout << "game starting" << std::endl;
Game game(context);
game.Run();
}
void Menu::LevelSelect(){
exit_ = false;
selectedButton = 0;
UpdateMenu(LevelSelectButtons);
// Level Menu loop
while (!exit_) {
ak_->NextEvent();
if(ak_->IsArrowKeyUpPushed()){
selectedButton == 0? selectedButton=LevelSelectButtons.size()-1: selectedButton-=1;
UpdateMenu(LevelSelectButtons);
}
else if(ak_->IsArrowKeyDownPushed()){
selectedButton == LevelSelectButtons.size() - 1 ? selectedButton=0: selectedButton+=1;
UpdateMenu(LevelSelectButtons);
}
else if(ak_->IsEnterKeyPushed()){
if(selectedButton==0){
Context context;
context.level = "./assets/levels/level1.txt";
StartGame(context);
}
else if(selectedButton==1){
Context context;
context.level = "./assets/levels/level2.txt";
StartGame(context);
}
else if(selectedButton==2){
Context context;
context.level = "./assets/levels/level3.txt";
StartGame(context);
}
else if(selectedButton==3){
exit_ = true;
}
UpdateMenu(LevelSelectButtons);
}
if (ak_->IsWindowClosed()) {
exit_ = true;
}
}
exit_ = false;
selectedButton = 0;
}
void Menu::ReplaySelect(){
exit_ = false;
selectedButton = 0;
UpdateMenu(ReplaySelectButtons);
// Level Menu loop
while (!exit_) {
ak_->NextEvent();
if(ak_->IsArrowKeyUpPushed()){
selectedButton == 0? selectedButton=ReplaySelectButtons.size()-1: selectedButton-=1;
UpdateMenu(ReplaySelectButtons);
}
else if(ak_->IsArrowKeyDownPushed()){
selectedButton == ReplaySelectButtons.size() - 1 ? selectedButton=0: selectedButton+=1;
UpdateMenu(ReplaySelectButtons);
}
else if(ak_->IsEnterKeyPushed()){
if(selectedButton==0){
Context context;
context.replay = true;
context.replayFile = "./assets/highscores/highscore_1.txt";
StartGame(context);
}
else if(selectedButton==1){
Context context;
context.replay = true;
context.replayFile = "./assets/highscores/highscore_2.txt";
StartGame(context);
}
else if(selectedButton==2){
Context context;
context.replay = true;
context.replayFile = "./assets/highscores/highscore_3.txt";
StartGame(context);
}
else if(selectedButton==3){
exit_ = true;
}
UpdateMenu(ReplaySelectButtons);
}
if (ak_->IsWindowClosed()) {
exit_ = true;
}
}
exit_ = false;
selectedButton = 0;
}
//Update Screen with correct buttons
void Menu::UpdateMenu(std::vector<Button> buttons){
ak_->DrawScaledBitmap(background,0,0,BACKGROUND_WIDTH,BACKGROUND_HEIGHT,0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
for(unsigned int i =0;i<buttons.size();i++){
Color color;
i == selectedButton ? color = selected : color = unselected;
ak_->DrawString(buttons[i].text, buttons[i].point,color,Allkit::ALIGN_CENTER,false);
}
ak_->DrawOnScreen();
}
//Simpel Button class
Button::Button(std::string t,Point p){
text = t;
point = p;
}