Skip to content

Commit

Permalink
fix view
Browse files Browse the repository at this point in the history
refactor code
  • Loading branch information
David Arutiunian committed Dec 21, 2017
1 parent 264c0e1 commit e6ded80
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 32 deletions.
16 changes: 11 additions & 5 deletions Doodler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,26 @@ void Doodler::updatePosition(const float deltaTime)
double nextY = m_position.y - m_initialSpeed * m_timeAccumulator + 0.5 * G * std::pow(m_timeAccumulator, 2);
const sf::Vector2f nextPosition = {m_position.x, static_cast<float>(nextY)};
m_shape.setPosition(nextPosition);
setFalling(static_cast<float>(nextY));
setFallingState(static_cast<float>(nextY));
setPosition(nextPosition);
}

void Doodler::checkCollision()
{
const float currentBottomPosition = m_shape.getPosition().y + m_size.x / 2 + m_outlineThickness;
const bool isAtZeroLevel = currentBottomPosition >= m_floor;
const bool isAtZeroLevel = m_areFuzzyEqual(currentBottomPosition, m_floor);
const bool isGameOverLevel = m_floor == WINDOW_HEIGHT;
if (isAtZeroLevel && m_isFalling && !isGameOverLevel)
{
printf("%f\n", m_floor);
m_timeAccumulator = 0;
}
}

// TODO: fix side collision check
void Doodler::setVerticalPosition(const float nextX, const float deltaTime)
{
const KeysMap &keysMap = p_m_keyboardState->getKeysMap();
const KeysMap &keysMap = m_p_keyboardState->getKeysMap();
const bool isMaxRightPosition = m_position.x + m_size.x / 2 + m_outlineThickness < WINDOW_WIDTH;
const bool isMaxLeftPosition = m_position.x - m_size.x / 2 - m_outlineThickness > 0;
if (keysMap.at(sf::Keyboard::Right) && isMaxRightPosition)
Expand Down Expand Up @@ -76,12 +77,17 @@ void Doodler::setFloor(const float nextFloor)
m_position.y = m_floor - m_size.x / 2 - m_outlineThickness;
}

void Doodler::setFalling(const float nextY)
void Doodler::setFallingState(float nextY)
{
m_isFalling = getPosition().y - nextY <= 0;
}

void Doodler::addKeyboardState(const std::shared_ptr<KeyboardState> p_keyboardState)
{
p_m_keyboardState = p_keyboardState;
m_p_keyboardState = p_keyboardState;
}

bool Doodler::getFallingState() const
{
return m_isFalling;
}
30 changes: 25 additions & 5 deletions Doodler.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,14 @@ class Doodler : public IEntity

void addKeyboardState(std::shared_ptr<KeyboardState> p_keyboardState);

bool getFallingState() const override;

void setFallingState(float nextY);

void setFloor(float nextFloor) override;

private:
std::shared_ptr<KeyboardState> p_m_keyboardState;
std::shared_ptr<KeyboardState> m_p_keyboardState;

sf::Vector2f m_size = sf::Vector2f(35.f, 50.f);
const float m_outlineThickness = 2;
Expand All @@ -36,15 +42,29 @@ class Doodler : public IEntity
sf::Vector2f m_position = sf::Vector2f(WINDOW_WIDTH / 2, m_floor - m_size.x / 2 - m_outlineThickness);
sf::RectangleShape m_shape;

const Lambda<bool(float, float)> m_areCloseAbsolute = [&](float lhs, float rhs) -> bool {
constexpr float tolerance = 0.001f;
return std::abs(lhs - rhs) < tolerance;
};

const Lambda<bool(float, float)> m_areCloseRelative = [&](float lhs, float rhs) -> bool {
constexpr float tolerance = 0.001f;
return std::abs((lhs - rhs) / rhs) < tolerance;
};

const Lambda<bool(float, float)> m_areFuzzyEqual = [&](float lhs, float rhs) -> bool {
if (std::abs(rhs) > 1.f)
{
return m_areCloseRelative(lhs, rhs);
}
return m_areCloseAbsolute(lhs, rhs);
};

void draw(sf::RenderTarget &target, sf::RenderStates states) const override;

void checkCollision() override;

void setVerticalPosition(float nextX, float deltaTime);

void setFloor(float nextFloor) override;

void setFalling(float nextY);
};

#endif //DOODLE_JUMP_DOODLER_H
9 changes: 4 additions & 5 deletions Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ void Engine::checkCollision(Entities &entities)

void Engine::processCollision(const std::shared_ptr<IEntity> &p_entity)
{
if (doesIntersect(p_entity) && !m_shouldSetFloor)
if (doesIntersect(p_entity) && m_p_doodler->getFallingState() && !m_shouldSetFloor)
{
m_shouldSetFloor = true;
m_floor = p_entity->getPosition().y - p_entity->getBounds().y;
Expand All @@ -28,8 +28,7 @@ void Engine::processCollision(const std::shared_ptr<IEntity> &p_entity)
// TODO: adjust intersection check
bool Engine::doesIntersect(const std::shared_ptr<IEntity> &p_entity) const
{
return !(m_p_doodler->getPosition().x + m_p_doodler->getBounds().x < p_entity->getPosition().x ||
p_entity->getPosition().x + p_entity->getBounds().x < m_p_doodler->getPosition().x ||
m_p_doodler->getPosition().y + m_p_doodler->getBounds().y < p_entity->getBounds().y ||
p_entity->getPosition().y + p_entity->getBounds().y < m_p_doodler->getPosition().y);
sf::Rect<float> rhs(p_entity->getPosition(), p_entity->getBounds());
sf::Rect<float> lhs(m_p_doodler->getPosition(), m_p_doodler->getBounds());
return lhs.intersects(rhs);
}
11 changes: 5 additions & 6 deletions Engine.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
#ifndef DOODLE_JUMP_ENGINE_H
#define DOODLE_JUMP_ENGINE_H

#include <functional>
#include "consts.h"
#include "Doodler.h"
#include "IEntity.h"

template<typename T, class R>
using Lambda = std::function<T(const std::shared_ptr<R> &)>;

class Engine
{
public:
Expand All @@ -18,11 +15,13 @@ class Engine
bool m_shouldSetFloor = false;
float m_floor = WINDOW_HEIGHT;

const Lambda<bool, IEntity> m_isDoodler = [&](const std::shared_ptr<IEntity> &p_entity) -> bool {
const Lambda<bool(const std::shared_ptr<IEntity> &)> m_isDoodler = [&](
const std::shared_ptr<IEntity> &p_entity) -> bool {
return p_entity->getType() == Types::Doodler;
};

const Lambda<void, IEntity> m_applyForEach = [&](const std::shared_ptr<IEntity> &p_entity) -> void {
const Lambda<void(const std::shared_ptr<IEntity> &)> m_applyForEach = [&](
const std::shared_ptr<IEntity> &p_entity) -> void {
if (m_p_doodler == nullptr || m_isDoodler(p_entity))
{
return;
Expand Down
8 changes: 4 additions & 4 deletions EventLoop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ void EventLoop::pollEvents()
sf::Event event{};
while (m_window.pollEvent(event))
{
p_m_keyboardState->onKeyEventHandler(event);
m_p_keyboardState->onKeyEventHandler(event);
onWindowEventHandler(event);
}
}

void EventLoop::redrawFrame(const Entities &entities)
{
m_window.clear(sf::Color::White);
m_window.setView(p_m_view->getView());
m_window.setView(m_p_view->getView());
std::for_each(entities.begin(), entities.end(), [&](const std::shared_ptr<IEntity> &p_item) -> void {
m_window.draw(*p_item);
});
Expand Down Expand Up @@ -65,11 +65,11 @@ const sf::RenderWindow &EventLoop::getWindow() const

void EventLoop::addKeyboardState(const std::shared_ptr<KeyboardState> p_keyboardState)
{
p_m_keyboardState = p_keyboardState;
m_p_keyboardState = p_keyboardState;
}

void EventLoop::addView(const std::shared_ptr<View> p_view)
{
p_m_view = p_view;
m_p_view = p_view;
}

4 changes: 2 additions & 2 deletions EventLoop.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class EventLoop
sf::RenderWindow m_window;
sf::Clock m_clock;

std::shared_ptr<KeyboardState> p_m_keyboardState;
std::shared_ptr<View> p_m_view;
std::shared_ptr<KeyboardState> m_p_keyboardState;
std::shared_ptr<View> m_p_view;

float m_deltaTime = 0.f;

Expand Down
2 changes: 2 additions & 0 deletions IEntity.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class IEntity : public IPhysicsObject, public sf::Drawable, public sf::Transform

virtual void setFloor(float nextFloor) { (void) &nextFloor; };

virtual bool getFallingState() const { return false; };

private:
virtual void checkCollision() {};
};
Expand Down
2 changes: 0 additions & 2 deletions IPhysicsObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
class IPhysicsObject
{
public:
virtual void onContact(IPhysicsObject &object) { (void) &object; };

virtual const sf::Vector2f &getBounds() const = 0;

virtual Types getType() const = 0;
Expand Down
3 changes: 2 additions & 1 deletion View.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@

View::View()
{
m_view.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
m_view.setViewport(sf::FloatRect(0, 0, 1, 1));
}

void View::followTo(const std::shared_ptr<Doodler> p_doodler)
{
m_view.setCenter(p_doodler->getPosition());
m_view.setCenter(WINDOW_WIDTH / 2, p_doodler->getPosition().y);
}

const sf::View &View::getView() const
Expand Down
6 changes: 6 additions & 0 deletions consts.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#ifndef DOODLE_JUMP_CONSTS_H
#define DOODLE_JUMP_CONSTS_H

#include <SFML/Graphics.hpp>
#include <string>
#include <functional>
#include <memory>

/// Window params
static const unsigned WINDOW_WIDTH = 400;
Expand All @@ -18,6 +21,9 @@ static const float TIME_ACCELERATOR = 15.f;
static const float MOVE_SPEED = 500.f;
static const size_t PLATFORM_COUNT = 30;

template<typename Signature>
using Lambda = std::function<Signature>;

enum class Types : size_t
{
Platform = 0,
Expand Down
3 changes: 1 addition & 2 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@ int main()
p_doodler->addKeyboardState(p_keyboardState);
entities.push_back(p_doodler);

p_view->followTo(p_doodler);

while (eventLoop.getWindow().isOpen())
{
p_view->followTo(p_doodler);
eventLoop.pollEvents();
eventLoop.update(entities);
engine.checkCollision(entities);
Expand Down

0 comments on commit e6ded80

Please sign in to comment.