Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions Controller/FinishScene.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#ifndef INC_2048BALLS_FINISHSCENE_H
#define INC_2048BALLS_FINISHSCENE_H

#include <SFML/Graphics/RenderWindow.hpp>
#include "scn.h"
#include "../View/MyDraw.h"

class FinishScene {
public:
FinishScene();
int show(sf::RenderWindow *window, MyDraw *draw, double totalTime);
void setScore(int score);
private:
int score;
Label label;
};

FinishScene::FinishScene() : label(410 - 10, 460 - 10, 20, 20, "", 50) {}

/**
* Call on every step to show
* @param window sf::RenderWindow
* @param draw implementation of MyDraw
* @return the id of the next scene to show
*/
int FinishScene::show(sf::RenderWindow *window, MyDraw *draw, double totalTime) {
label.st = "You fail! \nYour score is " + std::to_string(score) + "!!!";
draw->drawLabel(label, (int) (50.0 * totalTime));
window->setMouseCursorVisible(false);
if (totalTime > 5) {
window->setMouseCursorVisible(true);
return scn::MENU;
}
return scn::FINISH;
}

void FinishScene::setScore(int score) {
this->score = score;
}

#endif //INC_2048BALLS_FINISHSCENE_H
92 changes: 92 additions & 0 deletions Controller/Game.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#ifndef INC_2048BALLS_GAME_H
#define INC_2048BALLS_GAME_H

#include <SFML/Graphics.hpp>
#include <sstream>
#include <iostream>
#include <vector>

#include "../model/Engine.h"
#include "../View/MyDraw.h"
#include "GameScene.h"
#include "OpenningScene.h"
#include "MenuScene.h"
#include "FinishScene.h"

class Game {
public:
Game();
void run();

private:
void manageScenes(double time);
sf::RenderWindow *window;
sf::Clock clock;

MyDraw *draw;
OpeningScene openingScene;
MenuScene menuScene;
GameScene gameScene;
FinishScene finishScene;

int currentScene = scn::OPENING;
float totalTime = 0;

const sf::Color BACKGROUND_COLOR = sf::Color(182, 92, 0);
};

Game::Game() {
window = new sf::RenderWindow(sf::VideoMode(820, 920), "2048 - Balls", sf::Style::None);
draw = new MyDraw(window);
}

/**
* Call to start the main loop
*/
void Game::run() {
while (window->isOpen()) {
window->clear(BACKGROUND_COLOR);

double time = clock.getElapsedTime().asMicroseconds();
clock.restart();
totalTime += time / 1e6;
time = time / 1.8e5 * 0.6;
time = (time > 0.5) ? 0.5 : time;

manageScenes(time);
window->display();
}
}

/**
* Show scenes in some order
* @param time timestamp
* @param totalTime time since the start of the game
*/
void Game::manageScenes(double time) {
int newScene = scn::OPENING;
if (currentScene == scn::OPENING) {
newScene = openingScene.show(window, draw, totalTime);
} else
if (currentScene == scn::MENU) {
newScene = menuScene.show(window, draw);
if (newScene == scn::GAME)
gameScene.init();
} else
if (currentScene == scn::GAME) {
newScene = gameScene.show(window, draw, time);
if (newScene == scn::FINISH)
finishScene.setScore(gameScene.getScore());
} else
if (currentScene == scn::FINISH) {
newScene = finishScene.show(window, draw, totalTime);
}

if (newScene != currentScene) {
totalTime = 0;
currentScene = newScene;
}
}


#endif //INC_2048BALLS_GAME_H
94 changes: 94 additions & 0 deletions Controller/GameScene.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#ifndef GAME_SCENE_H_INCLUDED
#define GAME_SCENE_H_INCLUDED


#include "scn.h"

class GameScene {
public:
GameScene();
int show(sf::RenderWindow *window, MyDraw *draw, double time);
int init();
int getScore();
private:
Engine engine;
bool push_ball = false;

Label titleLabel;
Label swellLabel;
};

GameScene::GameScene() : titleLabel(Label(10, 10, 220, 90, "2048", 92)),
swellLabel(Label(350, 60, 220, 45, "doing swell", 50)) {}

int GameScene::init() {
engine.setLose(false);
engine.initMap();
push_ball = false;
}

/**
* Call on every step to show
* @param window sf::RenderWindow
* @param draw implementation of MyDraw
* @return the id of the next scene to show
*/

int GameScene::show(sf::RenderWindow *window, MyDraw *draw, double time) {
if (!engine.getLose()) {
engine.step(time);
engine.analiseMap();
if (push_ball) {
engine.smartPushBall();
push_ball = false;
}
}
draw->drawBalls(engine.getBalls());
draw->drawLabel(titleLabel, 255);
draw->drawLabel(Label(330, 10, 220, 45, "Score: " + std::to_string(engine.score()), 50), 255);

if (engine.getLose() == 0) {
draw->drawLabel(swellLabel, 255);
} else {
return scn::FINISH;
}


sf::Event event {};
while (window->pollEvent(event)) {
if (event.type == sf::Event::Closed) window->close();
if (event.type == sf::Event::KeyPressed) {
if (event.key.code == sf::Keyboard::Escape) {
return scn::MENU;
}
if (event.key.code == sf::Keyboard::W) {
engine.setGravity(Vec2d<double>(0, -10));
push_ball = true;
}
if (event.key.code == sf::Keyboard::S) {
engine.setGravity(Vec2d<double>(0, 10));
push_ball = true;
}
if (event.key.code == sf::Keyboard::D) {
engine.setGravity(Vec2d<double>(10, 0));
push_ball = true;
}
if (event.key.code == sf::Keyboard::A) {
engine.setGravity(Vec2d<double>(-10, 0));
push_ball = true;
}

}
}
return scn::GAME;
}

/**
*
* @return player score
*/
int GameScene::getScore() {
return engine.score();
}

#endif // GAME_SCENE_H_INCLUDED
45 changes: 45 additions & 0 deletions Controller/MenuScene.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#ifndef MENU_SCENE_H_INCLUDED
#define MENU_SCENE_H_INCLUDED

class MenuScene {
public:
MenuScene();
int show(sf::RenderWindow *window, MyDraw *draw);

private:
Button startButton;
Button exitButton;
};

MenuScene::MenuScene() : startButton(410 - 200, 390 - 70, 400, 140, "START", 100),
exitButton(410 - 200, 530 - 70, 400, 140, "EXIT", 100) {}

/**
* Call on every step to show
* @param window sf::RenderWindow
* @param draw implementation of MyDraw
* @return the id of the next scene to show
*/
int MenuScene::show(sf::RenderWindow *window, MyDraw *draw) {
sf::Vector2i pixelPos = sf::Mouse::getPosition(*window);
sf::Vector2f pos = window->mapPixelToCoords(pixelPos);
draw->drawButton(startButton, pos.x, pos.y);
draw->drawButton(exitButton, pos.x, pos.y);

sf::Event event;
while (window->pollEvent(event)) {
if (event.type == sf::Event::Closed) window->close();
if (event.type == sf::Event::MouseButtonPressed)
if (event.key.code == sf::Mouse::Left) {
if (startButton.is_in(pos.x, pos.y)) {
return scn::GAME;
}
if (exitButton.is_in(pos.x, pos.y)) {
window->close();
}
}
}
return scn::MENU;
}

#endif // MENU_SCENE_H_INCLUDED
33 changes: 33 additions & 0 deletions Controller/OpenningScene.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#ifndef OPENNING_SCENE_H_INCLUDED
#define OPENNING_SCENE_H_INCLUDED

#include "scn.h"

class OpeningScene {
public:
OpeningScene();
int show(sf::RenderWindow *window, MyDraw *draw, double totalTime);

private:
Label label;
};

OpeningScene::OpeningScene() : label(410 - 10, 460 - 10, 20, 20, "Hello, Master!", 100) {}

/**
* Call on every step to show
* @param window sf::RenderWindow
* @param draw implementation of MyDraw
* @return the id of the next scene to show
*/
int OpeningScene::show(sf::RenderWindow *window, MyDraw *draw, double totalTime) {
draw->drawLabel(label, (int) (85.0 * totalTime));
window->setMouseCursorVisible(false);
if (totalTime > 3) {
window->setMouseCursorVisible(true);
return scn::MENU;
}
return scn::OPENING;
}

#endif // OPENNING_SCENE_H_INCLUDED
8 changes: 8 additions & 0 deletions Controller/scn.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef INC_2048BALLS_SCN_H
#define INC_2048BALLS_SCN_H

enum scn {
OPENING, MENU, GAME, FINISH
};

#endif //INC_2048BALLS_SCN_H
18 changes: 18 additions & 0 deletions View/Button.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef BUTTON_H_INCLUDED
#define BUTTON_H_INCLUDED

#include "Label.h"

class Button : public Label {
public:
Button(int _x, int _y, int _l, int _h, std::string _st, int _siz) : Label(_x, _y, _l, _h, _st, _siz) {}

bool is_in(int px, int py) {
bool ret = 0;
if ((px > x) && (px < x + l) && (py > y) && (py < y + h))
ret = 1;
return ret;
}
};

#endif // BUTTON_H_INCLUDED
23 changes: 23 additions & 0 deletions View/Label.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef INC_2048BALLS_LABEL_H
#define INC_2048BALLS_LABEL_H

#include <string>

class Label {
public:
int x, y;
int l, h;
int siz;
std::string st;

Label(int _x, int _y, int _l, int _h, std::string _st, int _siz) {
x = _x;
y = _y;
l = _l;
h = _h;
st = _st;
siz = _siz;
}
};

#endif //INC_2048BALLS_LABEL_H
Loading