Skip to content

Commit

Permalink
Arch. Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ufrshubham committed Oct 11, 2020
1 parent fe4dd30 commit 38d15c6
Show file tree
Hide file tree
Showing 24 changed files with 559 additions and 243 deletions.
58 changes: 57 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,59 @@
{
"C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools"
"C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools",
"files.associations": {
"algorithm": "cpp",
"array": "cpp",
"atomic": "cpp",
"cctype": "cpp",
"cmath": "cpp",
"compare": "cpp",
"concepts": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"deque": "cpp",
"exception": "cpp",
"initializer_list": "cpp",
"ios": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"iterator": "cpp",
"limits": "cpp",
"locale": "cpp",
"map": "cpp",
"memory": "cpp",
"new": "cpp",
"ostream": "cpp",
"set": "cpp",
"stack": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"string": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"typeinfo": "cpp",
"utility": "cpp",
"vector": "cpp",
"xfacet": "cpp",
"xiosbase": "cpp",
"xlocale": "cpp",
"xlocbuf": "cpp",
"xlocinfo": "cpp",
"xlocmes": "cpp",
"xlocmon": "cpp",
"xlocnum": "cpp",
"xloctime": "cpp",
"xmemory": "cpp",
"xstddef": "cpp",
"xstring": "cpp",
"xtr1common": "cpp",
"xtree": "cpp",
"xutility": "cpp"
}
}
23 changes: 23 additions & 0 deletions include/Ball.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include "Body.hpp"

struct Context;

class Ball : public Body
{
private:
/**
* \brief This method creates a new dynamic b2Body. It internally attaches a drawable object
* as userData in body definition. Use b2Body::GetUserData() to get this drawable.
*
* \param radius Radius of circle in pixel space.
* \param position Position of the ball in pixel space.
* \return A new circular b2Body.
*/
b2Body* CreateBall(const float& radius, const sf::Vector2f& position);
public:
Ball(std::shared_ptr<Context>& context, const float& radius, const sf::Vector2f position);

virtual BodyType GetType() const override;
};
41 changes: 41 additions & 0 deletions include/Body.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#pragma once

#include <SFML/Graphics/Drawable.hpp>
#include <SFML/Graphics/Transformable.hpp>
#include <SFML/System/Time.hpp>
#include <SFML/Graphics/Shape.hpp>

#include <box2d/b2_body.h>

#include <memory>

struct Context;

enum class BodyType
{
Ball,
Paddle,
Wall,
Brick
};

class Body : public sf::Drawable
{
protected:
b2Body* m_body;
sf::Shape* m_shape;

std::shared_ptr<Context> m_context;

public:
Body(std::shared_ptr<Context> &context);
virtual ~Body();

virtual BodyType GetType() const = 0;
b2Body* GetB2Body();

void Update(const sf::Time& deltaTime);

protected:
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
};
21 changes: 21 additions & 0 deletions include/Brick.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include "Body.hpp"

class Brick : public Body
{
private:
/**
* \brief This method creates a new dynamic b2Body. It internally attaches a drawable object
* as userData in body definition. Use b2Body::GetUserData() to get this drawable.
*
* \param size Size of the paddle in pixel space.
* \param position Position of the brick in pixel space.
* \return A new rectangular b2Body.
*/
b2Body* CreateBrick(const sf::Vector2f& size, const sf::Vector2f position);
public:
Brick(std::shared_ptr<Context>& context, const sf::Vector2f& size, const sf::Vector2f position);

virtual BodyType GetType() const override;
};
13 changes: 6 additions & 7 deletions include/ContactListener.hpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
#pragma once

#include "Ball.hpp"

#include <box2d/b2_world_callbacks.h>
#include <box2d/b2_body.h>

#include <vector>
#include <map>

class ContactListener : public b2ContactListener
{
private:
const b2Body* m_ball;
const b2Body* m_paddle;
std::shared_ptr<std::multimap<BodyType, std::unique_ptr<Body>>> m_entityMap;
std::vector<b2Body*> m_bodiesToBeDeleted;

public:
ContactListener();
ContactListener(std::shared_ptr<std::multimap<BodyType, std::unique_ptr<Body>>>& entityMap);
virtual ~ContactListener();

void SetBall(const b2Body* ball);
void SetPaddle(const b2Body* paddle);

virtual void EndContact(b2Contact* contact) final;

void DeleteCollidedBodies(b2World* world, std::vector<b2Body*>& bricks);
void DeleteCollidedBodies(b2World* world);
};
2 changes: 1 addition & 1 deletion include/Game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "AssetMan.hpp"
#include "StateMan.hpp"

#include "ContactListener.hpp"

enum AssetID
Expand All @@ -30,7 +31,6 @@ struct Context
m_assets = std::make_unique<Engine::AssetMan>();
m_states = std::make_unique<Engine::StateMan>();
m_window = std::make_unique<sf::RenderWindow>();
// Todo: Make gravity zero.
m_world = std::make_unique<b2World>(b2Vec2(0.f, 0.f));
}
};
Expand Down
3 changes: 2 additions & 1 deletion include/GameOver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
#include <SFML/Graphics/Text.hpp>

#include "State.hpp"
#include "Game.hpp"

struct Context;

class GameOver : public Engine::State
{
Expand Down
42 changes: 6 additions & 36 deletions include/GamePlay.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,18 @@
#include <box2d/b2_body.h>
#include <box2d/b2_mouse_joint.h>

#include "Game.hpp"
#include "State.hpp"
#include "ContactListener.hpp"

struct Context;
class Body;
class ContactListener;
enum class BodyType;

class GamePlay : public Engine::State
{
private:
std::shared_ptr<std::multimap<BodyType, std::unique_ptr<Body>>> m_entityMap;
std::shared_ptr<Context> m_context;
std::array<b2Body*, 4> m_walls;
std::vector<b2Body*> m_bricks;

b2Body* m_ball;
b2Body* m_paddle;

b2MouseJoint* m_mouseJoint;
sf::Vector2f m_targetPosition;
Expand All @@ -48,35 +47,6 @@ class GamePlay : public Engine::State
void Start() override;

private:
/**
* \brief This method creates a new static b2Body. It internally attaches a drawable object
* as userData in body definition. Use b2Body::GetUserData() to get this drawable.
*
* \param size Size of the wall in pixel space.
* \param position Position of the wall in pixel space.
* \return A new rectangular b2Body.
*/
b2Body* CreateWall(const sf::Vector2f& size, const sf::Vector2f position);

/**
* \brief This method creates a new dynamic b2Body. It internally attaches a drawable object
* as userData in body definition. Use b2Body::GetUserData() to get this drawable.
*
* \param radius Radius of circle in pixel space.
* \param position Position of the ball in pixel space.
* \return A new circular b2Body.
*/
b2Body* CreateBall(const float& radius, const sf::Vector2f& position);

/**
* \brief This method creates a new dynamic b2Body. It internally attaches a drawable object
* as userData in body definition. Use b2Body::GetUserData() to get this drawable.
*
* \param size Size of the paddle in pixel space.
* \param position Position of the paddle in pixel space.
* \return A new rectangular b2Body.
*/
b2Body* CreatePaddle(const sf::Vector2f& size, const sf::Vector2f position);

/**
* \brief This method creates a new b2MouseJoint between given two bodies.
Expand Down
3 changes: 2 additions & 1 deletion include/MainMenu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
#include <SFML/Graphics/Text.hpp>

#include "State.hpp"
#include "Game.hpp"

struct Context;

class MainMenu : public Engine::State
{
Expand Down
21 changes: 21 additions & 0 deletions include/Paddle.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include "Body.hpp"

class Paddle : public Body
{
private:
/**
* \brief This method creates a new dynamic b2Body. It internally attaches a drawable object
* as userData in body definition. Use b2Body::GetUserData() to get this drawable.
*
* \param size Size of the paddle in pixel space.
* \param position Position of the paddle in pixel space.
* \return A new rectangular b2Body.
*/
b2Body* CreatePaddle(const sf::Vector2f& size, const sf::Vector2f position);
public:
Paddle(std::shared_ptr<Context>& context, const sf::Vector2f& size, const sf::Vector2f position);

virtual BodyType GetType() const override;
};
3 changes: 2 additions & 1 deletion include/PauseGame.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
#include <SFML/Graphics/Text.hpp>

#include "State.hpp"
#include "Game.hpp"

struct Context;

class PauseGame : public Engine::State
{
Expand Down
21 changes: 21 additions & 0 deletions include/Wall.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include "Body.hpp"

class Wall : public Body
{
private:
/**
* \brief This method creates a new static b2Body. It internally attaches a drawable object
* as userData in body definition. Use b2Body::GetUserData() to get this drawable.
*
* \param size Size of the wall in pixel space.
* \param position Position of the wall in pixel space.
* \return A new rectangular b2Body.
*/
b2Body* CreateWall(const sf::Vector2f& size, const sf::Vector2f position);
public:
Wall(std::shared_ptr<Context>& context, const sf::Vector2f& size, const sf::Vector2f position);

virtual BodyType GetType() const override;
};
4 changes: 2 additions & 2 deletions include/b2_user_settings.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include <SFML/Graphics/Shape.hpp>
#include "Body.hpp"

#include <stdarg.h>
#include <stdint.h>
Expand All @@ -15,7 +15,7 @@
/// this too much because b2BlockAllocator has a maximum object size.
#define b2_maxPolygonVertices 8

using b2BodyUserData = sf::Shape*;
using b2BodyUserData = Body*;

/// You can define this to inject whatever data you want in b2Fixture
struct b2FixtureUserData
Expand Down
Loading

0 comments on commit 38d15c6

Please sign in to comment.