El Octavio  1.0
This is a video game about adventures.
El Octavo-Functions.cpp
Go to the documentation of this file.
1#include "../Header files/Precompile.h"
2#include "../Header files/El Octavo-Functions.h"
3#include "../Header files/GameClass.h"
4#include "../Header files/Additional Functions.h"
5#include "../Header files/Menu.h"
6
7// Npc class
8Npc::Npc(Texture * texture, Vector2u imageCount, float switchTime, float speed, string name) {
9 this->imageCount = imageCount;
10 this->switchTime = switchTime;
11 this->speed = speed;
12 this->totalTime = 0.0f;
13 this->currentImage.x = 0;
14 this->name = name;
15 this->posIndex = 0;
16 this->reset = false;
17 this->delay = false;
18
19 this->uvRect.width = texture->getSize().x / float(imageCount.x);
20 this->uvRect.height = texture->getSize().y / float(imageCount.y);
21
22 //movement mechanic
23 this->faceLeft = true;
24 this->row = 0;
25
26
27 this->body.setSize(Vector2f(81.0f, 90.0f));
28 this->body.setOrigin(81.0f / 2, 90.0f / 2);
29 this->body.setPosition(1849.0f, 538.0f - 90.0f / 2);
30 this->body.setTexture(texture);
31}
32
34}
35
36void Npc::draw(RenderWindow& window) {
37 window.draw(this->body);
38}
39
40void Npc::moveX(float amount) {
41 this->body.move(amount, 0.0f);
42}
43
44void Npc::update(int row, float deltaTime, bool delay) {
45 currentImage.y = row;
46 totalTime += deltaTime;
47
48 if (totalTime >= switchTime)
49 {
50 totalTime -= switchTime;
51 currentImage.x++;
52
53 if (currentImage.x >= imageCount.x)
54 {
55 currentImage.x = 0;
56 }
57 }
58
59 uvRect.top = currentImage.y * uvRect.height;
60
61 if (faceLeft)
62 {
63 uvRect.left = (currentImage.x + 1) * abs(uvRect.width);
64 uvRect.width = -abs(uvRect.width);
65 }
66 else
67 {
68 uvRect.left = currentImage.x * uvRect.width;
69 uvRect.width = abs(uvRect.width);
70 }
71
73}
74
75void Npc::moveTo(float pos[], float deltaTime, bool& done, bool& faceLeft) {
76
77 if (!delay) {
78 if (pos[posIndex] > body.getPosition().x) {
79 distance = pos[posIndex] - body.getPosition().x;
80 moveX(speed * deltaTime);
81 }
82 else {
83 distance = body.getPosition().x - pos[posIndex];
84 moveX(-speed * deltaTime);
85 }
86 }
87
88 if (distance <= 20) {
89 if (!reset) {
90 npcClock.restart();
91 reset = true;
92 delay = true;
93
94 if (faceLeft)
95 {
96 faceLeft = false;
97 }
98 else {
99 faceLeft = true;
100 }
101
102 }
103
104 npcCurrentTime = npcClock.getElapsedTime();
105 //cout << npcClock.getElapsedTime().asSeconds() << endl;
106
107 if (npcCurrentTime.asSeconds() >= 0.3) {
108 posIndex++;
109 if (posIndex > 1) {
110 posIndex = 0;
111 }
112 reset = false;
113 delay = false;
114 }
115 }
116}
117
118// CollisionBlock class
120 hitbox.setSize(size);
121 hitbox.setPosition(pos);
122}
123
125}
126
129 return true;
130 }
131 else {
132 return false;
133 }
134}
135
137 window.draw(hitbox);
138}
139
140namespace variables {
141
142 //movemenent
143 float deltaTime = 0.0f;
144 bool movementToggle = true;
145
146 //event
148
149 //background
152
153 //background for stage 2
156
157 //background for stage 2
160
161 // player / npc
164
165 //ground
167 //ramp
171
172 //sound
175
176 //time
177 Clock fpsClock = Clock::Clock();
180 Clock npcClock = Clock::Clock();
183
184 //font
186
187 //fps
188 float fps;
191
192 string cutsceneStr = "Press Q to talk with the mafia.";
194
196
197 CollisionBlock blocks[2] = {{Vector2f(2428, 416), Vector2f(107, 45)}, {Vector2f(1428, 338), Vector2f(211, 43)}};
198
199 bool done;
200
204
205 string dialogScript = "Hello, you must be the boss's people!";
207
209
210 unsigned int character = 0;
211
212 unsigned int dialogTurn = 0;
213
214 bool enterDialogue = false;
215
216 bool enterPressed = false;
217
218 bool dialogueOver = false;
219}
220
221using namespace variables;
222
223void moveStaticImages(RectangleShape& body, RenderWindow& window, Npc& test);
224
226
228{
229 bgTexture.loadFromFile("../Images and fonts/Bg/test bg.png");
231 drawBubble = false;
232
233 adventureBgTexture.loadFromFile("../Images and fonts/Bg/GameMap.png");
235
236 messageTexture.loadFromFile("../Images and fonts/Bg/message.png");
238
239 messageImage.setPosition(500, 300);
240
241 //setting position of the background
242 bgImage.setOrigin(1920, 1080);
243
244 ground.setSize(Vector2f(1280, 225));
245 ground.setPosition(Vector2f(0, 535));
246 ground.setFillColor(Color::Black);
247
248 plrT.loadFromFile("../Images and fonts/Main character/unknown.png");
249 plrT.setRepeated(true);
250
251 npcT.loadFromFile("../Images and fonts/NPCs/enemy npc sprite sheet.png");
252 npcT.setRepeated(true);
253
254 rampT.loadFromFile("../Images and fonts/Ramp Test.png");
256 ramp.setOrigin(0, 0);
257 ramp.setPosition(800, 430);
258
259 jumpBuffer.loadFromFile("../Audios/Jump.wav");
260 walkBuffer.loadFromFile("../Audios/Walk.wav");
261
264
265 soundJump.setVolume(5.0f);
266 soundWalk.setVolume(25.0f);
267
268 font.loadFromFile("../Images and fonts/Fonts/Header font.ttf");
271
275
276 for (int i = 0; i < 12; i++) {
277 points[i] = RectangleShape(Vector2f(10, 10));
278 points[i].setOrigin(5, 5);
279 }
280
281 for (int i = 0; i < 12; i++) {
282 if (i == 0) {
284 }
285 else {
286 points[i].setPosition(points[i - 1].getPosition().x + 10, points[i - 1].getPosition().y - 10);
287 }
288 }
289
290 //cutscene setup
291 kurabirovTexture.loadFromFile("../Images and fonts/Dialogues/KurabirovDialogue.png");
292 mafiaTexture.loadFromFile("../Images and fonts/Dialogues/MobsterDialogue.png");
293 nestashevTexture.loadFromFile("../Images and fonts/Dialogues/MobsterDialogue.png");
294
298
299 bool imageTurn = false;
300
305}
306
307void setup(RenderWindow& window)
308{
309 setVars();
310
311 int blocksSize = sizeof(blocks) / sizeof(*blocks);
312
313 // start of sussy variables
314
315 Clock clock;
316 Player plr(&plrT, Vector2u(3, 2), 0.3f, 225.0f);
317 Npc test(&npcT, Vector2u(3,1), 0.3f, 190.0f, "Test");
318 bgImage.setPosition(window.getSize().x / 2, window.getSize().y / 2);
319
320
321 // end of sussy variables
322
323 window.setFramerateLimit(60);
324
325 while (window.isOpen())
326 {
327 float moveToPos[2] = {blocks[1].hitbox.getPosition().x + 200, blocks[0].hitbox.getPosition().x - 200};
329
330 window.setKeyRepeatEnabled(true);
331
332 while (window.pollEvent(ev))
333 {
334 if (ev.type == Event::Closed)
335 {
336 window.close();
337 }
338
339 if (ev.type == Event::KeyPressed && ev.key.code == Keyboard::Escape)
340 {
341 window.close();
342 }
343
344 if (ev.type == Event::KeyReleased && ev.key.code == Keyboard::Enter) {
345 enterPressed = false;
346 }
347
348 }
349
350 if (plr.body.getPosition().x >= messageImage.getPosition().x - 200.0f && plr.body.getPosition().x <= messageImage.getPosition().x + 200.0f)
351 {
353 if (!dialogueOver) {
354 drawBubble = true;
355
356 }
357
358 if (Keyboard::isKeyPressed(Keyboard::Q))
359 {
360 enterDialogue = true;
361 }
362
363 }
364 else {
366 drawBubble = false;
367 }
368
369 if (enterDialogue) {
370 //cout << "ent" << endl;
371
372
373 if (Keyboard::isKeyPressed(Keyboard::Enter) && !enterPressed)
374 {
375 dialogTurn++;
376
377 cout << dialogTurn << endl;
378 enterPressed = true;
379 character = 0;
380
381 }
382
383 if (dialogTurn == 1)
384 {
385 dialogScript = "Yes we are. Anyway, your task is to find Nestashev and get netractor certificate.";
386 }
387
388 if (dialogTurn == 2)
389 {
390 dialogScript = "Certificate for netractors? Are you OK!";
391 }
392
393 if (dialogTurn == 3)
394 {
395 dialogScript = "Yes this is the order. Now go this way and you will reach nestashev.";
396 }
397
398 if (dialogTurn == 4)
399 {
400 dialogScript = "All right, I'll go. See you later. I have to beat nestashev for money!";
401 }
402
403 if (dialogTurn == 5)
404 {
406 enterDialogue = false;
407 dialogueOver = true;
408 }
409
410 if (enterDialogue) {
411 if (timer.getElapsedTime().asSeconds() > 0.01 && character < dialogScript.length())
412 {
413 timer.restart();
414
415 character++;
416
418 }
419 }
420
421 }
422
423 if (dialogueOver)
424 {
426 drawBubble = false;
427 }
428
429
431 test.update(0, deltaTime,test.delay);
432
433 window.clear(Color::Green);
434
435 if (movementToggle) {
436 window.draw(ground);
437 }
438
439 window.draw(adventureBgImage);
440 window.draw(ramp);
441
442 for (int i = 0; i < 2; i++) {
443 window.draw(blocks[i].hitbox);
444 }
445
446 window.draw(test.body);
447
448 plr.draw(window);
449
450 test.moveTo(moveToPos, deltaTime, done, test.faceLeft);
451
452 // calculate fps
456
457 // show fps
458 roundedFps = (int)fps;
459 fpsCounter.setString(to_string(roundedFps));
460 fpsCounter.setPosition(1200, 20);
461
462 if (isShowingFPS(showFPS) == true)
463 {
464 window.draw(fpsCounter);
465 }
466
467 window.draw(cutsceneText);
468
469 for (int i = 0; i < 11; i++) {
470 window.draw(points[i]);
471 }
472
473 // draw message bubble
474 if (drawBubble) {
475 window.draw(messageImage);
476 }
477
478 moveStaticImages(plr.body, window, test);
479
480 window.draw(textDialogScript);
481
482 window.display();
483 }
484}
485
488}
489
491 /*if (ramp.getGlobalBounds().intersects(body.getGlobalBounds())) {
492 Image rampImage = rampT.copyToImage();
493
494 int pixelX = body.getPosition().x - ramp.getPosition().x;
495 int pixelY = (body.getPosition().y + 44) - ramp.getPosition().y;
496
497 if ((pixelX < 110 && pixelX > 0) && (pixelY < 110 && pixelY >= 0)) {
498 if (rampImage.getPixel(pixelX, pixelY).a != 0) {
499 return true;
500 }
501 else {
502 return false;
503 }
504 }
505 }*/
506
507 for (int i = 0; i < 12; i++) {
508 if (points[i].getGlobalBounds().intersects(body.getGlobalBounds()) && body.getPosition().x <= ramp.getPosition().x + 110) {
509 return true;
510 }
511 }
512
513 return false;
514}
515
517 return ramp.getPosition();
518}
519
521{
522 if (Keyboard::isKeyPressed(Keyboard::D))
523 {
524 if (body.getPosition().x >= window.getSize().x / 2)
525 {
526 //make images static
527 messageImage.move(-(225.0f * deltaTime), 0.f);
528 ramp.move(-(225.0f * deltaTime), 0.f);
529 test.moveX(-(225.0f * deltaTime));
530
531 for (int i = 0; i < 2; i++) {
532 blocks[i].hitbox.move(-(225.0f * deltaTime), 0.f);
533 }
534
535 for (int i = 0; i < 11; i++) {
536 points[i].move(-(225.0f * deltaTime), 0.f);
537 }
538 }
539 }
540}
void setVars()
bool checkCollideWithRamp(RectangleShape &body)
void moveStaticImages(RectangleShape &body, RenderWindow &window, Npc &test)
Vector2f getRampPos()
bool checkCollideWithGround(RectangleShape &body)
void setup(RenderWindow &window)
bool showFPS
Definition: Menu.cpp:7
bool isShowingFPS(bool &showFPS)
Definition: Menu.cpp:221
void drawHitbox(RenderWindow &window)
CollisionBlock(Vector2f pos, Vector2f size)
RectangleShape hitbox
bool checkForCollision(RectangleShape &body)
void draw(RenderWindow &window)
IntRect uvRect
RectangleShape body
void update(int row, float deltaTime, bool delay)
Npc(Texture *texture, Vector2u imageCount, float switchTime, float speed, string name)
void moveX(float amount)
bool faceLeft
void moveTo(float pos[], float deltaTime, bool &done, bool &faceLeft)
RectangleShape body
Definition: GameClass.h:22
void updateMovement(float deltaTime, RenderWindow &window, Sprite &adventureBgImage, Sound &soundWalk, Sound &soundJump, bool &toggle, CollisionBlock blocks[], int blocksSize)
Definition: GameClass.cpp:99
void draw(RenderWindow &window)
Definition: GameClass.cpp:299
Utility class that measures the elapsed time.
Definition: Clock.hpp:42
Time restart()
Restart the clock.
Time getElapsedTime() const
Get the elapsed time.
Utility class for manipulating RGBA colors.
Definition: Color.hpp:41
Defines a system event and its parameters.
Definition: Event.hpp:45
KeyEvent key
Key event parameters (Event::KeyPressed, Event::KeyReleased)
Definition: Event.hpp:224
EventType type
Type of the event.
Definition: Event.hpp:219
Class for loading and manipulating character fonts.
Definition: Font.hpp:51
bool loadFromFile(const std::string &filename)
Load the font from a file.
T width
Width of the rectangle.
Definition: Rect.hpp:159
T height
Height of the rectangle.
Definition: Rect.hpp:160
T left
Left coordinate of the rectangle.
Definition: Rect.hpp:157
T top
Top coordinate of the rectangle.
Definition: Rect.hpp:158
bool intersects(const Rect< T > &rectangle) const
Check the intersection between two rectangles.
Definition: Rect.inl:100
Specialized shape representing a rectangle.
void setSize(const Vector2f &size)
Set the size of the rectangle.
void draw(const Drawable &drawable, const RenderStates &states=RenderStates::Default)
Draw a drawable object to the render target.
void clear(const Color &color=Color(0, 0, 0, 255))
Clear the entire target with a single color.
Window that can serve as a target for 2D drawing.
virtual Vector2u getSize() const
Get the size of the rendering region of the window.
void setTextureRect(const IntRect &rect)
Set the sub-rectangle of the texture that the shape will display.
void setFillColor(const Color &color)
Set the fill color of the shape.
FloatRect getGlobalBounds() const
Get the global (non-minimal) bounding rectangle of the entity.
void setTexture(const Texture *texture, bool resetRect=false)
Change the source texture of the shape.
Storage for audio samples defining a sound.
Definition: SoundBuffer.hpp:50
bool loadFromFile(const std::string &filename)
Load the sound buffer from a file.
Regular sound that can be played in the audio environment.
Definition: Sound.hpp:46
void setBuffer(const SoundBuffer &buffer)
Set the source buffer containing the audio data to play.
void setVolume(float volume)
Set the volume of the sound.
Drawable representation of a texture, with its own transformations, color, etc.
Definition: Sprite.hpp:48
void setTexture(const Texture &texture, bool resetRect=false)
Change the source texture of the sprite.
Graphical text that can be drawn to a render target.
Definition: Text.hpp:49
void setFont(const Font &font)
Set the text's font.
void setString(const String &string)
Set the text's string.
void setFillColor(const Color &color)
Set the fill color of the text.
void setCharacterSize(unsigned int size)
Set the character size.
Image living on the graphics card that can be used for drawing.
Definition: Texture.hpp:49
bool loadFromFile(const std::string &filename, const IntRect &area=IntRect())
Load the texture from a file on disk.
Vector2u getSize() const
Return the size of the texture.
void setRepeated(bool repeated)
Enable or disable repeating.
Represents a time value.
Definition: Time.hpp:41
float asSeconds() const
Return the time value as a number of seconds.
void setPosition(float x, float y)
set the position of the object
void setOrigin(float x, float y)
set the local origin of the object
void move(float offsetX, float offsetY)
Move the object by a given offset.
const Vector2f & getPosition() const
get the position of the object
T x
X coordinate of the vector.
Definition: Vector2.hpp:75
T y
Y coordinate of the vector.
Definition: Vector2.hpp:76
bool pollEvent(Event &event)
Pop the event on top of the event queue, if any, and return it.
void close()
Close the window and destroy all the attached resources.
void display()
Display on screen what has been rendered to the window so far.
bool isOpen() const
Tell whether or not the window is open.
void setKeyRepeatEnabled(bool enabled)
Enable or disable automatic key-repeat.
void setFramerateLimit(unsigned int limit)
Limit the framerate to a maximum fixed frequency.
Vector2< unsigned int > Vector2u
Definition: Vector2.hpp:254
Vector2< float > Vector2f
Definition: Vector2.hpp:255
SoundBuffer walkBuffer
RectangleShape ground
RectangleShape points[12]
unsigned int character
SoundBuffer jumpBuffer
unsigned int dialogTurn
CollisionBlock blocks[2]
Texture adventureBgTexture
Texture kurabirovTexture
Texture nestashevTexture
Texture texture
Definition: Menu.cpp:17
Clock clock
Definition: GameClass.cpp:7
Keyboard::Key code
Code of the key that has been pressed.
Definition: Event.hpp:64