Skip to content

Commit

Permalink
Add jump mechanic
Browse files Browse the repository at this point in the history
  • Loading branch information
Laguna1989 committed Sep 3, 2022
1 parent dfb3ec8 commit ae6d233
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
13 changes: 10 additions & 3 deletions impl/gamelib/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,17 @@ void Player::doUpdate(float const elapsed)
{

auto& input = getGame()->input();
if (input.keyboard()->pressed(jt::KeyCode::D)) {
if (input.keyboard()->justPressed(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_jumpTimer -= elapsed;

if (m_jumpTimer <= 0) {
if (input.keyboard()->justPressed(jt::KeyCode::W)) {
setVelocity(jt::Vector2f { 0.0f, -300.0f });
m_jumpTimer = 3.5f;
}
}

m_shape->setPosition(getPosition());
Expand Down
1 change: 1 addition & 0 deletions impl/gamelib/player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Player : public jt::Box2DObject {
private:
std::shared_ptr<jt::Shape> m_shape;

float m_jumpTimer = 0.1f;
void doCreate() override;
void doUpdate(float const elapsed) override;
void doDraw() const override;
Expand Down
10 changes: 6 additions & 4 deletions impl/gamelib/state_game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

void StateGame::doInternalCreate()
{
m_world = std::make_shared<jt::Box2DWorldImpl>(jt::Vector2f { 0.0f, 100.0f });
m_world = std::make_shared<jt::Box2DWorldImpl>(jt::Vector2f { 0.0f, 200.0f });

float const w = static_cast<float>(GP::GetWindowSize().x);
float const h = static_cast<float>(GP::GetWindowSize().y);
Expand All @@ -30,9 +30,10 @@ void StateGame::doInternalCreate()
add(m_platforms);

b2BodyDef def;
def.type = b2BodyType::b2_staticBody;
auto p = std::make_shared<Platform>(
m_world, &def, jt::Vector2f { 300.0f, 200.0f }, jt::Vector2f { GP::GetScreenSize().x, 20 });
def.type = b2BodyType::b2_kinematicBody;
auto p = std::make_shared<Platform>(m_world, &def,
jt::Vector2f { 300.0f, GP::GetScreenSize().y - 50.0f },
jt::Vector2f { GP::GetScreenSize().x, 20 });
add(p);
m_platforms->push_back(p);

Expand All @@ -51,6 +52,7 @@ void StateGame::createPlayer()
{
b2BodyDef def;
def.type = b2BodyType::b2_dynamicBody;
def.fixedRotation = true;
m_player = std::make_shared<Player>(m_world, &def);
add(m_player);
}
Expand Down

0 comments on commit ae6d233

Please sign in to comment.