Skip to content
Merged
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
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ add_executable(main
src/InputManager.cpp
src/MainManager.cpp
src/Pad.cpp
src/Block.cpp
src/TitleScreen.cpp
)
target_compile_features(main PRIVATE cxx_std_17)
Expand Down
45 changes: 42 additions & 3 deletions src/Ball.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include "Ball.h"
#include <iostream>
#include <cmath>

const float PI = 3.14159265358979323846;

Ball::Ball(sf::RenderWindow *_window, unsigned int WIDTH, unsigned int HEIGHT)
: window(_window),
Expand All @@ -10,7 +13,7 @@ Ball::Ball(sf::RenderWindow *_window, unsigned int WIDTH, unsigned int HEIGHT)
maxPosX(WIDTH - (ballRadius * 2) - 25),
minPosY(25),
maxPosY(HEIGHT - (ballRadius * 2) - 25),
moveSpeed(150),
moveSpeed(300),
ballColor(10, 10, 200),
ballCircle(ballRadius),
directionX(1),
Expand All @@ -32,8 +35,6 @@ void Ball::move(float deltaTime)
// Update position
posX = std::max(std::min(posX + (moveSpeed * deltaTime * directionX), maxPosX), minPosY);
posY = std::max(std::min(posY + (moveSpeed * deltaTime * directionY), maxPosY), minPosY);
std::cout << "time " << deltaTime << " move speed " << moveSpeed << " direction " << directionX << std::endl;
std::cout << "setting pos x to " << posX << std::endl;
ballCircle.setPosition({posX, posY});

// If hit the edge, switch directions
Expand Down Expand Up @@ -66,4 +67,42 @@ void Ball::checkPadCollision(sf::FloatRect padBounds)
ballCircle.setPosition({posX, posY});
directionY *= -1;
}
}

// Checks for collisions with all blocks in the given vector
// Returns the index of the block it collides with, if any
int Ball::checkBlockCollision(vector<Block> &blocks)
{
sf::FloatRect ballBound = ballCircle.getGlobalBounds();

// Go through all blocks
for (int i = 0; i < blocks.size(); i++)
{
sf::FloatRect blockBounds = blocks[i].getBounds();

// If ball collides with the block, bounce and return the index of block
if (blockBounds.findIntersection(ballBound))
{
// Get the angle between the two shapes
sf::Vector2f blockCenter = blockBounds.getCenter();
sf::Vector2f ballCenter = ballBound.getCenter();
float angle = atan2(ballCenter.y - blockCenter.y,
ballCenter.x - blockCenter.x) *
180 / PI;

// Use the angle to determine which way the ball should bounce
if ((blockCenter.x < ballCenter.x && abs(angle) < 45) || (blockCenter.x > ballCenter.x && abs(angle) > 135))
{
directionX *= -1;
}
else
{
directionY *= -1;
}

return i;
}
}

return -1; // No collisions
}
4 changes: 4 additions & 0 deletions src/Ball.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
#define BALL_H

#include <SFML/Graphics.hpp>
#include "Block.h"
#include <vector>
using namespace std;

class Ball
{
Expand All @@ -10,6 +13,7 @@ class Ball
void drawBall();
void move(float deltaTime);
void checkPadCollision(sf::FloatRect padBounds);
int checkBlockCollision(vector<Block> &blocks);

private:
sf::RenderWindow *window;
Expand Down
29 changes: 29 additions & 0 deletions src/Block.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "Block.h"

float Block::edgeSize = 10;

Block::Block(sf::RenderWindow *_window, int posX_, int posY_, unsigned int blockWidth_, unsigned int blockHeight_, sf::Color outerColor_, sf::Color innerColor_)
: window(_window),
blockWidth(blockWidth_),
blockHeight(blockHeight_),
posX(posX_),
posY(posY_),
outerColor(outerColor_),
innerColor(innerColor_),
innerRect(sf::Vector2f(blockWidth - (edgeSize * 2), blockHeight - (edgeSize * 2))),
outerRect(sf::Vector2f(blockWidth, blockHeight))
{
outerRect.setPosition({posX, posY});
outerRect.setFillColor(outerColor);
innerRect.setPosition({posX + edgeSize, posY + edgeSize});
innerRect.setFillColor(innerColor);
}

// Draw the block to the window
void Block::drawBlock()
{
window->draw(outerRect);
window->draw(innerRect);
}

sf::FloatRect Block::getBounds() const { return outerRect.getGlobalBounds(); }
31 changes: 31 additions & 0 deletions src/Block.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef BLOCK_H
#define BLOCK_H

#include <SFML/Graphics.hpp>
using namespace std;

class Block
{
public:
Block(sf::RenderWindow *_window, int posX_, int posY_, unsigned int blockWidth_,
unsigned int blockHeight_, sf::Color innerColor_, sf::Color outerColor_);
void drawBlock();
sf::FloatRect getBounds() const;

private:
static float edgeSize;

sf::RenderWindow *window;

int blockWidth;
int blockHeight;
float posY;
float posX;

sf::Color outerColor;
sf::Color innerColor;
sf::RectangleShape outerRect;
sf::RectangleShape innerRect;
};

#endif
17 changes: 15 additions & 2 deletions src/GameManager.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include "GameManager.h"
#include <iostream>

GameManager::GameManager(sf::RenderWindow *_window, unsigned int WIDTH, unsigned int HEIGHT)
GameManager::GameManager(sf::RenderWindow *_window, unsigned int WIDTH, unsigned int HEIGHT,
vector<Block> blocks_)
: window(_window),
gamePad(_window, WIDTH, HEIGHT),
gameBall(_window, WIDTH, HEIGHT)
gameBall(_window, WIDTH, HEIGHT),
blocks(blocks_)
{
}

Expand All @@ -13,13 +15,24 @@ void GameManager::updateGame(float timeSinceLastUpdate)
{
gameBall.move(timeSinceLastUpdate);
gameBall.checkPadCollision(gamePad.getBounds());

// Process collisions with blocks, and destroy any box it collides with
int collisionIndex = gameBall.checkBlockCollision(blocks);
if (collisionIndex != -1)
{
blocks.erase(blocks.begin() + collisionIndex);
}
}

// Draw the game
void GameManager::drawGame()
{
gamePad.drawPad();
gameBall.drawBall();
for (Block block : blocks)
{
block.drawBlock();
}
}

Pad &GameManager::pad() { return gamePad; }
Expand Down
7 changes: 6 additions & 1 deletion src/GameManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@

#include "Pad.h"
#include "Ball.h"
#include "Block.h"
#include <vector>
using namespace std;

class GameManager
{
public:
GameManager(sf::RenderWindow *window, unsigned int WIDTH, unsigned int HEIGHT);
GameManager(sf::RenderWindow *window, unsigned int WIDTH, unsigned int HEIGHT,
vector<Block> blocks);
void updateGame(float timeSinceLastUpdate);
void drawGame();
Pad &pad();
Expand All @@ -17,6 +21,7 @@ class GameManager
sf::RenderWindow *window;
Pad gamePad;
Ball gameBall;
vector<Block> blocks;
};

#endif
15 changes: 14 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "TitleScreen.h"
#include "Pad.h"
#include "GameManager.h"
#include "Block.h"
#include "InputManager.h"
#include "MainManager.h"

Expand All @@ -17,8 +18,20 @@ int main()

sf::Clock clock;

// Create the list of blocks to be in the game
// (in future, this should happen in some sort of level creator)
vector<Block> blocks;
blocks.push_back(Block(&window, 1250, 50, 150, 150, sf::Color(200, 0, 0), sf::Color(255, 0, 0)));
blocks.push_back(Block(&window, 1500, 50, 150, 150, sf::Color(0, 200, 0), sf::Color(0, 255, 0)));
blocks.push_back(Block(&window, 250, 50, 150, 150, sf::Color(0, 0, 200), sf::Color(0, 0, 255)));
blocks.push_back(Block(&window, 750, 50, 150, 150, sf::Color(200, 200, 0), sf::Color(255, 255, 0)));
for (int i = 0; i < 6; i++)
{
blocks.push_back(Block(&window, 250 * i - 200, 350, 150, 150, sf::Color(200, 200, 200), sf::Color(255, 255, 255)));
}

TitleScreen titleScreen(&window, WIDTH, HEIGHT);
GameManager gameManager(&window, WIDTH, HEIGHT);
GameManager gameManager(&window, WIDTH, HEIGHT, blocks);
MainManager mainManager(&window, &titleScreen, &gameManager);
InputManager inputManager(&mainManager, &titleScreen, &gameManager);

Expand Down