-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTutorial.cpp
245 lines (208 loc) · 6.87 KB
/
Tutorial.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include "Tutorial.h"
#include "Utils.h"
#include "Menu.h"
#include "GameSettings.h"
#include "Styles.h"
Tutorial::Tutorial(StateManager& stateManager) : StateScreen(stateManager)
{
using namespace std;
text.setFont(assetManager->getFont("game font"));
text.setFillColor(sf::Color::White);
helpButton.setPosition(sf::Vector2f(2048 - 40 - 10, 10));
helpButton.setSize(sf::Vector2f(40, 40));
helpButton.setHighlightColor(TRStyles::btnHLColor);
helpButton.setBaseColor(TRStyles::btnBaseColor);
blankPopup.setFillColor(sf::Color(0, 0, 0, 230));
blankPopup.setBaseColor(sf::Color::White);
blankPopup.setHighlightColor(sf::Color::Transparent);
blankPopup.setSize(sf::Vector2f(2048 - 400, 1152 - 200));
blankPopup.setPosition(sf::Vector2f(200, 100));
instructionSprite.setTexture(AssetManager::getInstance()->getTexture("instruction"));
instructionSprite.setOrigin(instructionSprite.getLocalBounds().width / 2.0f, instructionSprite.getLocalBounds().height / 2.0f);
currentPiecePtr = bag.at(counter);
board.setBoard(tutorialBoards.at(counter));
if (!sfxBuffer.loadFromFile("SFX/drop.ogg"))
{
cerr << "Unable to open file drop.ogg" << endl;
}
sfx.setBuffer(sfxBuffer);
sfx.setVolume(GameSettings::getInstance()->getSettings()->sfx);
}
Tutorial::~Tutorial()
{
}
void Tutorial::tick(const float& dt, sf::RenderWindow& window)
{
if (stageRender > 0)
{
stageRender--;
if (stageRender == 0) // move to the next piece (stage of the tutorial)
{
counter++;
if (counter < bag.size())
{
currentPiecePtr = bag.at(counter);
board.setBoard(tutorialBoards.at(counter));
}
else
{
tutorialOver = true;
}
}
}
}
void Tutorial::render(sf::RenderWindow& window)
{
text.setCharacterSize(100);
text.setString("How to play");
text.setPosition(boardX + boardWidth * boardSquareSize/2 - text.getLocalBounds().width/2, 20);
window.draw(text);
if (mouseInBox(window, 20, 20, 40, 40)) // back button
{
window.draw(assetManager->getDrawable("back button hl"));
}
else
{
window.draw(assetManager->getDrawable("back button"));
}
window.draw(helpButton);
text.setFillColor(sf::Color::White);
if (tutorialOver)
{
text.setCharacterSize(100);
text.setString("Congratulation!\nYou have finished the tutorial");
text.setPosition(boardX + boardWidth * boardSquareSize/2 - text.getLocalBounds().width/2, 300);
window.draw(text);
return;
}
text.setCharacterSize(50);
text.setString("Place pieces correctly on the board by drawing on the board.");
text.setPosition(boardX + boardWidth * boardSquareSize/2 - text.getLocalBounds().width/2, boardY + boardSquareSize * boardHeight + 20);
window.draw(text);
text.setCharacterSize(50);
text.setString("Hover over the help button for directions.");
text.setPosition(boardX + boardWidth * boardSquareSize/2 - text.getLocalBounds().width/2, boardY + boardSquareSize * boardHeight + 70);
window.draw(text);
board.render(window);
currentPiecePtr.render(window, boardX + (boardSquareSize/ 2) + boardSquareSize* boardWidth + 10, boardY + boardSquareSize / 2+ boardSquareSize* 2);
window.draw(inputVertex);
if (stageRender > 0)
{
text.setCharacterSize(100);
text.setString("Nice");
text.setPosition(boardX + boardWidth * boardSquareSize/2 - text.getLocalBounds().width/2, 300);
window.draw(text);
}
if (helpButton.mouseInButton(window))
{
window.draw(blankPopup);
helpButton.setHighlight(true);
instructionSprite.setPosition(window.getView().getSize().x / 2, window.getView().getSize().y / 2);
window.draw(instructionSprite);
}
else
{
helpButton.setHighlight(false);
}
}
void Tutorial::keyEvent(const float& dt, sf::Event event)
{
}
void Tutorial::mouseEvent(const float& dt, sf::RenderWindow& window, sf::Event event)
{
using namespace sf;
using namespace std;
if (event.type == Event::MouseButtonPressed && event.mouseButton.button == Mouse::Left && mouseInBox(window, 20, 20, 40, 40)) // back button
{
stateManager.addState(std::unique_ptr<StateScreen>(new Menu(stateManager)));
return;
}
if (tutorialOver)
{
}
else
{
if (keyMouseRegistered == true || event.type == sf::Event::MouseButtonPressed)
{
locked = true;
keyMouseRegistered = false;
lastMousePos = (Mouse::getPosition(window));
}
if (keyMouseReleased == true || event.type == sf::Event::MouseButtonReleased)
{
keyMouseReleased = false;
locked = false; // Reset
if (inputVertex.getVertexCount() >= 2)
{
Vertex firstPoint = inputVertex[0];
Vertex lastPoint = inputVertex[inputVertex.getVertexCount() - 1];
int xDir = lastPoint.position.x - firstPoint.position.x;
int yDir = lastPoint.position.y - firstPoint.position.y;
Moving_Direction mouseDirection = Moving_Direction::UP_DIR;
int XorYdir = max(abs(xDir), abs(yDir));
if (XorYdir >= 50) // only register input if it's long enough
{
if (XorYdir == abs(xDir))// favor x direction
{
if (xDir > 0) // mouse move right
{
mouseDirection = Moving_Direction::RIGHT_DIR;
}
else if (xDir < 0)
{
mouseDirection = Moving_Direction::LEFT_DIR;
}
}
else // favor y direction
{
if (yDir > 0)
{
mouseDirection = Moving_Direction::DOWN_DIR;
}
else if (yDir < 0)
{
mouseDirection = Moving_Direction::UP_DIR;
}
}
int minX = -currentPiecePtr.getMinX();
int minY = -currentPiecePtr.getMinY();
int maxX = 9 - currentPiecePtr.getMaxX();
int maxY = 9 - currentPiecePtr.getMaxY();
// x - 1, y - 1 to offset to center of the piece
int x = std::floor((firstPoint.position.x - boardX) / boardSquareSize);
int y = std::floor((firstPoint.position.y - boardY) / boardSquareSize);
bool possible = currentPiecePtr.setPiece(x, y, mouseDirection, board);
if (possible) // if set piece sucessfully, move to next piece
{
sfx.stop();
sfx.play();
stageRender = 60;
cout << "success" << endl;
}
}
}
inputVertex.clear();
}
if (locked)
{
if (lastMousePos != sf::Mouse::getPosition(window)) // When the Mouse hasn't moved don't add any new Vertex (save memory)
{
Vector2f prevMouseViewPos = window.mapPixelToCoords(lastMousePos);
Vector2i currentMousePos = Mouse::getPosition(window);
Vector2f mouseViewPos = window.mapPixelToCoords(currentMousePos);
sf::Vector2f direction = mouseViewPos - prevMouseViewPos;
sf::Vector2f unitDirection = direction / std::sqrt(direction.x * direction.x + direction.y * direction.y);
sf::Vector2f unitPerpendicular(-unitDirection.y, unitDirection.x);
sf::Vector2f offset = (inputThickness / 2.f) * unitPerpendicular;
inputVertex.append(prevMouseViewPos + offset);
inputVertex.append(mouseViewPos + offset);
inputVertex.append(mouseViewPos - offset);
inputVertex.append(prevMouseViewPos - offset);
lastMousePos = currentMousePos;
}
}
}
}
void Tutorial::drawInstructionPopup(sf::RenderWindow& window)
{
}