-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameState.cpp
191 lines (167 loc) · 5.05 KB
/
GameState.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
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
189
190
191
#include "GameState.h"
void GameState::initfont()
{
if (!this->font.loadFromFile("content/Fonts/Dosis-Light.ttf"))
{
cout << "ERROR::MAINMENUSTATE::COULD NOT LOAD FONT" << endl;
}
}
void GameState::initPlayer()
{
player = new Player(0, 0, this->textures["PLAYER_SHEET"]);
}
void GameState::initTextures()
{
Texture temp;
temp.loadFromFile("content/Textures/Character.png");
this->textures["PLAYER_SHEET"] = temp;
temp.loadFromFile("content/Textures/Background.png");
this->textures["BACKGROUND"] = temp;
}
void GameState::initkeybinds()
{
ifstream ifs("Config/gamestate_keybinds.ini");
if (ifs.is_open())
{
string key = "";
string key2 = "";
while (ifs >> key >> key2)
{
this->keybinds[key] = this->supportedkeys->at(key2);
}
}
ifs.close();
// this->keybinds["Close"]= this->supportedkeys->at("Escape");
// this->keybinds["Move_Left"] = this->supportedkeys->at("A");
// this->keybinds["Move_Right"] = this->supportedkeys->at("D");
// this->keybinds["Move_Up"] = this->supportedkeys->at("W");
// this->keybinds["Move_Down"] = this->supportedkeys->at("S");
}
void GameState::initWorldBackground()
{
this->WorldBackground.setTexture(this->textures["BACKGROUND"]);
}
void GameState::initview()
{
//Float rect that stores the size of the view around the player
//view.setCenter(sf::Vector2f(player->getpos().x, player->getpos().y));
view.setSize(sf::Vector2f(455.f, 256.f));
//view.setCenter(sf::Vector2f(player->getpos().x, player->getpos().y));
this->window->setView(view);
}
GameState::GameState(RenderWindow* window, map<string, int>* supportedkeys, stack<State*>* states) :State(window, supportedkeys, states)
{
this->pmenu = new PauseMenu(*this->window, this->font);
this->initfont();
this->initkeybinds();
this->initTextures();
this->initPlayer();
this->initWorldBackground();
this->initview();
}
GameState::~GameState()
{
delete this->player;
}
void GameState::updateinput(const float& dt)
{
//Update player input
if (Keyboard::isKeyPressed(Keyboard::Key(this->keybinds.at("Move_Left"))))
{
this->player->movement(dt, -1.f, 0.f);
this->player->updateAnimation(dt, 1);
}
if (Keyboard::isKeyPressed(Keyboard::Key(this->keybinds.at("Move_Right"))))
{
this->player->movement(dt, 1.f, 0.f);
this->player->updateAnimation(dt, 2);
}
if (Keyboard::isKeyPressed(Keyboard::Key(this->keybinds.at("Move_Up"))))
{
this->player->movement(dt, 0.f, -1.f);
this->player->updateAnimation(dt, 3);
}
if (Keyboard::isKeyPressed(Keyboard::Key(this->keybinds.at("Move_Down"))))
{
this->player->movement(dt, 0.f, 1.f);
this->player->updateAnimation(dt, 0);
}
}
void GameState::updatePausedInput()
{
if (Keyboard::isKeyPressed(Keyboard::Key(this->keybinds.at("Close"))))
{
if (!this->paused)
this->pausestate();
else
this->unpausedstate();
}
}
void GameState::updateView(const float& dt)
{
//updates the view to follow the player and keep it centered while also not going out of bounds
view.setCenter(this->player->getpos());
if (view.getCenter().x < view.getSize().x / 2.f)
view.setCenter(view.getSize().x / 2.f, view.getCenter().y);
if (view.getCenter().x + view.getSize().x / 2.f > this->WorldBackground.getGlobalBounds().width)
view.setCenter(this->WorldBackground.getGlobalBounds().width - view.getSize().x / 2.f, view.getCenter().y);
if (view.getCenter().y < view.getSize().y / 2.f)
view.setCenter(view.getCenter().x, view.getSize().y / 2.f);
if (view.getCenter().y + view.getSize().y / 2.f > this->WorldBackground.getGlobalBounds().height)
view.setCenter(view.getCenter().x, this->WorldBackground.getGlobalBounds().height - view.getSize().y / 2.f);
this->window->setView(view);
}
// void GameState::updateWindowBoundsCollision(RenderTarget* target)
// {
// //Left
// if (this->player->getbounds().left < 0.f)
// {
// this->player->setpos(Vector2f(0.f, this->player->getbounds().top));
// }
// //Right
// else if (this->player->getbounds().left + this->player->getbounds().width > this->window->getSize().x)
// {
// this->player->setpos(Vector2f(target->getSize().x - this->player->getbounds().width, this->player->getbounds().top));
// }
// //Top
// if (this->player->getbounds().top < 0.f)
// {
// this->player->setpos(Vector2f(this->player->getbounds().left, 0.f));
// }
// //Bottom
// else if (this->player->getbounds().top + this->player->getbounds().height > target->getSize().y)
// {
// this->player->setpos(Vector2f(this->player->getbounds().left, target->getSize().y - this->player->getbounds().height));
// }
// }
void GameState::update(const float& dt, RenderTarget* target)
{
this->updateinput(dt);
this->updatemousepos();
this->updatePausedInput();
if(!this->paused )//Unpaused update
{
this->player->update(dt);
this->player->updateWindowBoundColision(target);
this->updateView(dt);
}
else
{
//Pause menu update
this->pmenu->update(mouseposview);
}
}
void GameState::render(RenderTarget* target)
{
if(!target)
{
target = this->window;
}
target->draw(WorldBackground);
this->player->render(*target);
if(this->paused)//Pause menu render
{
//Render pause menu
this->pmenu->render(*target);
}
}