C++ Project 2016-2017: Arkanoid
sfml_factory.cpp
Go to the documentation of this file.
1 
3 #include "sfml_factory.h"
4 #include "nlohmann/json.hpp"
5 #include "../gui/entity_sfml/player_sfml/player_sfml.h"
6 #include "../gui/entity_sfml/ball_sfml/ball_sfml.h"
7 #include "../gui/entity_sfml/wall_sfml/wall_sfml.h"
8 #include "../gui/entity_sfml/block_sfml/block_sfml.h"
9 #include "../gui/entity_sfml/block_sfml/special_block_sfml/ball_speed_block_sfml.h"
10 #include "../gui/entity_sfml/block_sfml/special_block_sfml/player_speed_block_sfml.h"
11 #include "../gui/entity_sfml/block_sfml/special_block_sfml/invis_block_sfml.h"
12 
13 #include <iostream>
14 #include <fstream>
15 #include <vector>
16 #include <memory>
17 #include <string>
18 #include <unordered_map> // needed for json parse library (represents json object)
19 
20 using namespace std;
21 using json = nlohmann::json;
22 
23 SFMLFactory::SFMLFactory(sf::RenderWindow &window) : windowSFML(window) {}
24 
25 unique_ptr<arkanoid::Player> SFMLFactory::createPlayer() {
26  unique_ptr<arkanoid::Player> player(new arkanoidSFML::PlayerSFML(400.0, 600.0, windowSFML));
27  return player;
28 }
29 
30 vector<unique_ptr<arkanoid::Wall>> SFMLFactory::createWalls() {
31 
32  vector<unique_ptr<arkanoid::Wall>> walls;
33 
34  // Create walls on the left and right side of the screen
35  for(double y = 0.0; y < 700.0; y += 32.0) {
36  {
37  unique_ptr<arkanoid::Wall> wall(new arkanoidSFML::WallSFML(0.0, y, windowSFML));
38  walls.push_back(std::move(wall));
39  }
40  {
41  unique_ptr<arkanoid::Wall> wall(new arkanoidSFML::WallSFML(864.0, y, windowSFML));
42  walls.push_back(std::move(wall));
43  }
44  }
45  // Create walls on the top side of the screen
46  for(double x = 0.0; x < 900.0; x += 32) {
47  unique_ptr<arkanoid::Wall> wall(new arkanoidSFML::WallSFML(x, 0.0, windowSFML));
48  walls.push_back(std::move(wall));
49  }
50 
51  return walls;
52 }
53 
54 vector<unique_ptr<arkanoid::Block>> SFMLFactory::createBlocks(const string &file) {
55  json jsonFile;
56  ifstream stream(file);
57 
58  if(!stream.good()) {
59  throw runtime_error("Couldn't open " + file);
60  }
61 
62  // Parse json file
63  try {
64  stream >> jsonFile;
65  } catch(...) {
66  throw runtime_error("Couldn't parse " + file);
67  }
68 
69  vector<unique_ptr<arkanoid::Block>> blocks;
70  try {
71 
72  // Construct data
73  vector<vector<string>> data = jsonFile["blocks"].get<vector<vector<string>>>();
74 
75  constexpr double offset = 32.0;
76  constexpr double blockSizeW = 64.0;
77  constexpr double blockSizeH = 32.0;
78  for(double r = 0; r < data.size(); r++) { // row
79  for(double c = 0; c < data[r].size(); c++) { // column
80 
81  // Check what type the current Block is
82  if(data[r][c] == "Y" || data[r][c] == "P") {
83  // BallSpeedBlock
84 
85  double speedFactor = 0.0;
86  string color;
87  if(data[r][c] == "Y") {
88  speedFactor = 1.5;
89  color = "yellow";
90  } else {
91  speedFactor = 0.5;
92  color = "purple";
93  }
94 
95  unique_ptr<arkanoid::Block> block(new arkanoidSFML::BallSpeedBlockSFML(offset + blockSizeW*c, offset + blockSizeH*r, windowSFML, speedFactor, "data/sprites/blocks/" + color + "_block.png"));
96  blocks.push_back(std::move(block));
97 
98  } else if(data[r][c] == "R") {
99  // InvisBlock
100  unique_ptr<arkanoid::Block> block(new arkanoidSFML::InvisBlockSFML(offset + blockSizeW*c, offset + blockSizeH*r, windowSFML, "data/sprites/blocks/red_block.png"));
101  blocks.push_back(std::move(block));
102 
103  } else if(data[r][c] == "G") {
104  // PlayerSpeedBlock
105  unique_ptr<arkanoid::Block> block(new arkanoidSFML::PlayerSpeedBlockSFML(offset + blockSizeW*c, offset + blockSizeH*r, windowSFML, 2.0, "data/sprites/blocks/green_block.png"));
106  blocks.push_back(std::move(block));
107 
108  } else if(data[r][c] == "B") {
109  // Normal Block
110  unique_ptr<arkanoid::Block> block(new arkanoidSFML::BlockSFML(offset + blockSizeW*c, offset + blockSizeH*r, windowSFML, "data/sprites/blocks/blue_block.png"));
111  blocks.push_back(std::move(block));
112  }
113 
114  }
115  }
116 
117  } catch(...) {
118  throw runtime_error("Invalid data in " + file);
119  }
120 
121  return blocks;
122 }
123 
124 unique_ptr<arkanoid::Ball> SFMLFactory::createBall() {
125  unique_ptr<arkanoid::Ball> ball(new arkanoidSFML::BallSFML(400.00, 578.0, windowSFML));
126  return ball;
127 }
The SFML element that represents the arkanoid::Ball in the game.
Definition: ball_sfml.h:19
unique_ptr< arkanoid::Player > createPlayer()
The SFML element that represents the arkanoid::InvisBlock in the game.
vector< unique_ptr< arkanoid::Wall > > createWalls()
The SFML element that represents the arkanoid::Block in the game.
Definition: block_sfml.h:19
The SFML element that represents the arkanoid::Player in the game.
Definition: player_sfml.h:20
SFMLFactory(sf::RenderWindow &window)
vector< unique_ptr< arkanoid::Block > > createBlocks(const string &file)
The SFML element that represents the arkanoid::PlayerSpeedBlock in the game.
The SFML element that represents the arkanoid::Wall in the game.
Definition: wall_sfml.h:19
The SFML element that represents the arkanoid::BallSpeedBlock in the game.
unique_ptr< arkanoid::Ball > createBall()