C++ Project 2016-2017: Arkanoid
world.cpp
Go to the documentation of this file.
1 
3 #include "world.h"
7 
8 #include <iostream>
9 #include <vector>
10 #include <memory>
11 #include <string>
12 
13 using namespace std;
14 
15 namespace arkanoid {
16 
17  constexpr double gridW = 9.0;
18  constexpr double gridH = 7.0;
19 
20  World::World() {}
21 
22  World::~World() {}
23 
24  void World::checkCollisions() {
25  // Update Ball + Check if there are collisions on the Ball
26  ball->bounceIfPossible<Wall>(walls);
27 
28  vector<int> collisions = std::move(ball->bounceIfPossible<Block>(blocks));
29  // Destroy all the Blocks that collided
30  int blocksDeleted = 0;
31  for(auto c: collisions) {
32 
33  // Check if it's a special Block, if so, activate it's 'special power'
34  BallSpeedBlock* ballSpeedBlock = dynamic_cast<BallSpeedBlock*>((blocks[c - blocksDeleted]).get());
35  if(ballSpeedBlock) {
36  ballSpeedBlock->effectBall(ball);
37  }
38  InvisBlock* invisBlock = dynamic_cast<InvisBlock*>((blocks[c - blocksDeleted]).get());
39  if(invisBlock) {
40  invisBlock->effectBall(ball);
41  }
42  PlayerSpeedBlock* playerSpeedBlock = dynamic_cast<PlayerSpeedBlock*>((blocks[c - blocksDeleted]).get());
43  if(playerSpeedBlock) {
44  playerSpeedBlock->effectPlayer(player);
45  }
46 
47  blocks.erase(blocks.begin() + c - blocksDeleted);
48  blocksDeleted++;
49  }
50 
51  // Check if there is a collision with the Player
52  ball->bounceIfPossible(player);
53  }
54 
55  void World::update() {
56  checkCollisions();
57 
58  // Update all Entities
59  for(auto &b: blocks) {
60  b->update();
61  }
62  for(auto &w: walls) {
63  w->update();
64  }
65  ball->update();
66  player->update();
67 
68  // Check if Ball has been missed (or out of the world for some reason)
69  if(ball->getPosition().y > gridH || ball->getPosition().y < 0 ||
70  ball->getPosition().x > gridW || ball->getPosition().x < 0) {
71  player->reset();
72  ball->reset();
73  }
74 
75  }
76 
77  void World::draw() const {
78  // Draw all Entities
79  for(const auto &b: blocks) {
80  b->draw();
81  }
82  ball->draw();
83  player->draw();
84  for(const auto &w: walls) {
85  w->draw();
86  }
87  }
88 
89  void World::addBlock(unique_ptr<arkanoid::Block> block) {
90  blocks.push_back(std::move(block));
91  }
92 
93  void World::addWall(unique_ptr<arkanoid::Wall> wall) {
94  walls.push_back(std::move(wall));
95  }
96 
97  void World::setBall(unique_ptr<arkanoid::Ball> newBall) {
98  ball = std::move(newBall);
99  }
100 
101  void World::setPlayer(unique_ptr<arkanoid::Player> newPlayer) {
102  player = std::move(newPlayer);
103  }
104 
105  bool World::levelEnded() const {
106  return blocks.size() == 0;
107  }
108 
109  void World::reset() {
110  player->reset();
111  ball->reset();
112  }
113 
114 }
A "special" Block: when hit with a Ball, speed up or slow down the Player.
void effectBall(unique_ptr< Ball > &ball) const
void effectBall(unique_ptr< Ball > &ball) const
Definition: invis_block.cpp:25
A "special" Block: when hit with a Ball, make the Ball invisible.
Definition: invis_block.h:16
void effectPlayer(unique_ptr< Player > &player) const
The Block in the Arkanoid game, can be hit and destroyed by the Player.
Definition: block.h:18
The Wall (left/right/top side) in the Arkanoid game that represents a single "square".
Definition: wall.h:17
All the game logic of the Arkanoid game.
Definition: ball.cpp:15
A "special" Block: when hit with a Ball, speed up or slow down the Ball.