-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainMenuState.cpp
More file actions
178 lines (152 loc) · 7.44 KB
/
MainMenuState.cpp
File metadata and controls
178 lines (152 loc) · 7.44 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
#include "MainMenuState.hpp"
#include "InstructionState.hpp"
#include "Levels/Level1.hpp"
#include "Levels/Level2.hpp"
#include "Levels/Level3.hpp"
#include "DEFINITIONS.hpp"
#include <iostream>
namespace engine{
//Constructor of FinishState
MainMenuState::MainMenuState(GameDataRef data):
_data(data)
{}
//Run on initialization of state
void MainMenuState::Init() {
//stop the background music.
_data->sound.BackGroundMusic.stop();
//background Initializer
_data->assets.LoadTexture("MainMenuState Background", MAIN_MENU_BACKGROUND_FILEPATH);
_background.setTexture(_data->assets.GetTexture("MainMenuState Background"));
_background.setScale(SCREEN_WIDTH/_background.getGlobalBounds().width,SCREEN_HEIGHT/_background.getGlobalBounds().height);
//main menu titel initializer
_data->assets.LoadTexture("MainMenuTitle", MAIN_MENU_TITLE_FILEPATH);
_title.setTexture(_data->assets.GetTexture("MainMenuTitle"));
_title.setPosition(SCREEN_WIDTH/2 - _title.getGlobalBounds().width/2, _title.getGlobalBounds().height/2);
//play button Initializer
_data->assets.LoadTexture("MainMenuPlayButton", MAIN_MENU_PLAY_BUTTON_FILEPATH);
_data->assets.LoadTexture("MainMenuPlayButtonHover", MAIN_MENU_PLAY_BUTTON_HOVER_FILEPATH);
_play_button.setTexture(_data->assets.GetTexture("MainMenuPlayButton"));
_play_button.setPosition(SCREEN_WIDTH/2 + 100, _play_button.getGlobalBounds().height*2.5);
//quit button Initializer
_data->assets.LoadTexture("MainMenuQuitButton",MAIN_MENU_QUIT_BUTTON_FILEPATH );
_data->assets.LoadTexture("MainMenuQuitButtonHover", MAIN_MENU_QUIT_BUTTON_HOVER_FILEPATH);
_quit_button.setTexture(_data->assets.GetTexture("MainMenuQuitButton"));
_quit_button.setPosition(SCREEN_WIDTH/2 - 100 - _quit_button.getGlobalBounds().width, _quit_button.getGlobalBounds().height*2.5);
//instructions button Initializer
_data->assets.LoadTexture("MainMenuInstructionsButton", INSTRUCTIONS_BUTTON);
_data->assets.LoadTexture("MainMenuInstructionsButtonHover", INSTRUCTIONS_BUTTON_HOVER);
_instructions_button.setTexture(_data->assets.GetTexture("MainMenuInstructionsButton"));
_instructions_button.setPosition(SCREEN_WIDTH/2 - 220, _instructions_button.getGlobalBounds().height*5.5);
//load mute and unmute image.
_data->assets.LoadTexture("Unmute", UNMUTE_FILEPATH);
_data->assets.LoadTexture("Mute", MUTE_FILEPATH);
//change texture to mute image if music is muted
if(_data->sound.BackGroundMusic.getVolume() == 0){
mute = true;
muteSprite.setTexture(_data->assets.GetTexture("Mute"));
}
//change texture to unmute image if music is not muted
else{
mute = false;
muteSprite.setTexture(_data->assets.GetTexture("Unmute"));
}
muteSprite.setPosition(10,SCREEN_HEIGHT - muteSprite.getGlobalBounds().height - 10);
}
void MainMenuState::HandleInput() {
sf::Event event;
//Check for windows x button
while(_data->renderWindow.pollEvent(event)){
if(sf::Event::Closed == event.type){
_data->renderWindow.close();
}
}
//Change texture if hovered over play button
if(_data->input.HoverOverButton(_play_button, _data->renderWindow) && _data->renderWindow.hasFocus() &&!_hoverPlay){
_hoverPlay = true;
_play_button.setTexture(_data->assets.GetTexture("MainMenuPlayButtonHover"));
}
else if(!_data->input.HoverOverButton(_play_button, _data->renderWindow) && _data->renderWindow.hasFocus() &&_hoverPlay){
_hoverPlay = false;
_play_button.setTexture(_data->assets.GetTexture("MainMenuPlayButton"));
}
//Change texture if hovered over quit button
if(_data->input.HoverOverButton(_quit_button, _data->renderWindow) && _data->renderWindow.hasFocus() &&!_hoverQuit){
_hoverQuit = true;
_quit_button.setTexture(_data->assets.GetTexture("MainMenuQuitButtonHover"));
}
else if(!_data->input.HoverOverButton(_quit_button, _data->renderWindow) && _data->renderWindow.hasFocus() &&_hoverQuit){
_hoverQuit = false;
_quit_button.setTexture(_data->assets.GetTexture("MainMenuQuitButton"));
}
//Change texture if hovered over instructions button
if(_data->input.HoverOverButton(_instructions_button, _data->renderWindow) && _data->renderWindow.hasFocus() &&!_hoverInstructions){
_hoverInstructions = true;
_instructions_button.setTexture(_data->assets.GetTexture("MainMenuInstructionsButtonHover"));
}
else if(!_data->input.HoverOverButton(_instructions_button, _data->renderWindow) && _data->renderWindow.hasFocus() &&_hoverInstructions){
_hoverInstructions = false;
_instructions_button.setTexture(_data->assets.GetTexture("MainMenuInstructionsButton"));
}
//Check for press play button
if(_data->input.IsSpriteClicked(_play_button, sf::Mouse::Left, _data->renderWindow) && _data->renderWindow.hasFocus()&& !mouseCheck){
_data->sound._clickButtonSound.play();
_data->machine.AddState( StateRef( new Level1(_data)), true);
}
//Check for press quit button
if(_data->input.IsSpriteClicked(_quit_button, sf::Mouse::Left, _data->renderWindow) && _data->renderWindow.hasFocus()&& !mouseCheck){
_data->sound._clickButtonSound.play();
_data->renderWindow.close();
}
//Check for press instructions button
if(_data->input.IsSpriteClicked(_instructions_button, sf::Mouse::Left, _data->renderWindow) && _data->renderWindow.hasFocus() && !mouseCheck) {
_data->sound._clickButtonSound.play();
_data->machine.AddState(StateRef(new InstructionState(_data)), true);
}
//Check for keypress "1" key
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Num1) &&
_data->renderWindow.hasFocus()){
_data->sound._clickButtonSound.play();
_data->machine.AddState( StateRef( new Level1(_data)), true);
}
//Check for keypress "2" key
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Num2) &&
_data->renderWindow.hasFocus()){
_data->sound._clickButtonSound.play();
_data->machine.AddState( StateRef( new Level2(_data)), true);
}
//Check for keypress "3" key
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Num3) &&
_data->renderWindow.hasFocus()){
_data->sound._clickButtonSound.play();
_data->machine.AddState( StateRef( new Level3(_data)), true);
}
//change mute or unmute image if clicked
if(!mouseCheck && sf::Mouse::isButtonPressed(sf::Mouse::Left)){
if(muteSprite.getGlobalBounds().contains(sf::Vector2f{sf::Mouse::getPosition(_data->renderWindow)})){
mouseCheck = true;
if(mute){
muteSprite.setTexture(_data->assets.GetTexture("Unmute"));
_data->sound.setVolume();
mute = false;
}else{
muteSprite.setTexture(_data->assets.GetTexture("Mute"));
_data->sound.mute();
mute = true;
}
}
}
if(mouseCheck && not sf::Mouse::isButtonPressed(sf::Mouse::Left)){
mouseCheck = false;
}
}
void MainMenuState::Draw(float dt) {
_data->renderWindow.clear();
_data->renderWindow.draw(_background);
_data->renderWindow.draw(_title);
_data->renderWindow.draw(_play_button);
_data->renderWindow.draw(_quit_button);
_data->renderWindow.draw(_instructions_button);
_data->renderWindow.draw(muteSprite);
_data->renderWindow.display();
}
}