Skip to content

Commit

Permalink
Add basic physics
Browse files Browse the repository at this point in the history
  • Loading branch information
Laguna1989 committed Sep 3, 2022
1 parent b84f112 commit dfb3ec8
Show file tree
Hide file tree
Showing 23 changed files with 129 additions and 329 deletions.
8 changes: 4 additions & 4 deletions impl/gamelib/game_properties.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ class GP {
public:
GP() = delete;

static std::string GameName() { return "MyAwesomeGame"; }
static std::string AuthorName() { return "TODO"; }
static std::string JamName() { return "TODO"; }
static std::string JamDate() { return "TODO"; }
static std::string GameName() { return "Run Run Run!"; }
static std::string AuthorName() { return "Laguna_999"; }
static std::string JamName() { return "1hgj384"; }
static std::string JamDate() { return "2022-09-02"; }

static jt::Vector2f GetWindowSize() { return jt::Vector2f { 1200, 800 }; }
static float GetZoom() { return 2.0f; }
Expand Down
30 changes: 30 additions & 0 deletions impl/gamelib/platform.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "platform.hpp"

Platform::Platform(std::shared_ptr<jt::Box2DWorldInterface> world, b2BodyDef const* def,
jt::Vector2f const& pos, jt::Vector2f const& size)
: Box2DObject(world, def)
{
m_size = size;

b2FixtureDef fixtureDef;
b2PolygonShape boxCollider {};
boxCollider.SetAsBox(size.x / 2.0f, size.y / 2.0f);
fixtureDef.shape = &boxCollider;
getB2Body()->CreateFixture(&fixtureDef);

setPosition(pos);
}

void Platform::doCreate()
{
m_shape = std::make_shared<jt::Shape>();
m_shape->makeRect(m_size, textureManager());
m_shape->setOffset(jt::OffsetMode::CENTER);
}

void Platform::doUpdate(float const elapsed)
{
m_shape->setPosition(getPosition());
m_shape->update(elapsed);
}
void Platform::doDraw() const { m_shape->draw(renderTarget()); }
22 changes: 22 additions & 0 deletions impl/gamelib/platform.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef INC_1HGJ384_PLATFORM_HPP
#define INC_1HGJ384_PLATFORM_HPP

#include <box2dwrapper/box2d_object.hpp>
#include <shape.hpp>

class Platform : public jt::Box2DObject {
public:
Platform(std::shared_ptr<jt::Box2DWorldInterface> world, b2BodyDef const* def,
jt::Vector2f const& pos, jt::Vector2f const& size);

private:
std::shared_ptr<jt::Shape> m_shape;

jt::Vector2f m_size;

void doCreate() override;
void doUpdate(float const elapsed) override;
void doDraw() const override;
};

#endif // INC_1HGJ384_PLATFORM_HPP
37 changes: 37 additions & 0 deletions impl/gamelib/player.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "player.hpp"
#include <game_interface.hpp>

Player::Player(std::shared_ptr<jt::Box2DWorldInterface> world, b2BodyDef const* def)
: Box2DObject(world, def)
{
b2FixtureDef fixtureDef;
b2PolygonShape boxCollider {};
boxCollider.SetAsBox(8, 24);
fixtureDef.shape = &boxCollider;
getB2Body()->CreateFixture(&fixtureDef);

setPosition(jt::Vector2f { 50.0f, 50.0f });
}

void Player::doCreate()
{
m_shape = std::make_shared<jt::Shape>();
m_shape->makeRect(jt::Vector2f { 16, 48 }, textureManager());
m_shape->setOffset(jt::OffsetMode::CENTER);
}

void Player::doUpdate(float const elapsed)
{

auto& input = getGame()->input();
if (input.keyboard()->pressed(jt::KeyCode::D)) {
getB2Body()->ApplyForceToCenter(b2Vec2 { 100.0f, 0.0f }, true);
} else if (input.keyboard()->pressed(jt::KeyCode::A)) {
getB2Body()->ApplyForceToCenter(b2Vec2 { -100.0f, 0.0f }, true);
}

m_shape->setPosition(getPosition());
m_shape->update(elapsed);
}

void Player::doDraw() const { m_shape->draw(renderTarget()); }
19 changes: 19 additions & 0 deletions impl/gamelib/player.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef INC_1HGJ384_PLAYER_HPP
#define INC_1HGJ384_PLAYER_HPP

#include <box2dwrapper/box2d_object.hpp>
#include <shape.hpp>

class Player : public jt::Box2DObject {
public:
Player(std::shared_ptr<jt::Box2DWorldInterface> world, b2BodyDef const* def);

private:
std::shared_ptr<jt::Shape> m_shape;

void doCreate() override;
void doUpdate(float const elapsed) override;
void doDraw() const override;
};

#endif // INC_1HGJ384_PLAYER_HPP
58 changes: 0 additions & 58 deletions impl/gamelib/player/graphics/graphics_component_impl.cpp

This file was deleted.

26 changes: 0 additions & 26 deletions impl/gamelib/player/graphics/graphics_component_impl.hpp

This file was deleted.

This file was deleted.

21 changes: 0 additions & 21 deletions impl/gamelib/player/graphics/graphics_component_interface.hpp

This file was deleted.

28 changes: 0 additions & 28 deletions impl/gamelib/player/input/input_component_impl.cpp

This file was deleted.

18 changes: 0 additions & 18 deletions impl/gamelib/player/input/input_component_impl.hpp

This file was deleted.

1 change: 0 additions & 1 deletion impl/gamelib/player/input/input_component_interface.cpp

This file was deleted.

13 changes: 0 additions & 13 deletions impl/gamelib/player/input/input_component_interface.hpp

This file was deleted.

1 change: 0 additions & 1 deletion impl/gamelib/player/input/input_target_interface.cpp

This file was deleted.

8 changes: 0 additions & 8 deletions impl/gamelib/player/input/input_target_interface.hpp

This file was deleted.

60 changes: 0 additions & 60 deletions impl/gamelib/player/player.cpp

This file was deleted.

Loading

0 comments on commit dfb3ec8

Please sign in to comment.