Skip to content

Commit

Permalink
Add wind effect
Browse files Browse the repository at this point in the history
  • Loading branch information
Laguna1989 committed Sep 3, 2022
1 parent ae6d233 commit 10e7b5e
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 22 deletions.
4 changes: 2 additions & 2 deletions impl/gamelib/hud/hud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ void Hud::doCreate()
m_scoreP1Text->setTextAlign(jt::Text::TextAlign::LEFT);
m_scoreP1Text->setPosition({ 10, 4 });

m_scoreP1Display = std::make_shared<ScoreDisplay>(m_scoreP1Text, "P1 Score: ");
m_scoreP1Display = std::make_shared<ScoreDisplay>(m_scoreP1Text, "Speed: ");

m_scoreP2Text = std::make_shared<jt::Text>();
m_scoreP2Text->loadFont("assets/font.ttf", 16, renderTarget());
Expand All @@ -24,7 +24,7 @@ void Hud::doCreate()
m_scoreP2Text->setTextAlign(jt::Text::TextAlign::LEFT);
m_scoreP2Text->setPosition({ 600 / 2 - 10, 4 });

m_scoreP2Display = std::make_shared<ScoreDisplay>(m_scoreP2Text, "P2 Score: ");
m_scoreP2Display = std::make_shared<ScoreDisplay>(m_scoreP2Text, "Total Distance: ");
}

void Hud::doUpdate(float const elapsed)
Expand Down
11 changes: 8 additions & 3 deletions impl/gamelib/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ void Player::doCreate()

void Player::doUpdate(float const elapsed)
{

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

m_jumpTimer -= elapsed;
Expand All @@ -39,6 +40,10 @@ void Player::doUpdate(float const elapsed)

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

auto v = getVelocity();
v.x *= 0.99f;
setVelocity(v);
}

void Player::doDraw() const { m_shape->draw(renderTarget()); }
53 changes: 38 additions & 15 deletions impl/gamelib/state_game.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include "state_game.hpp"
#include "timer.hpp"
#include <box2dwrapper/box2d_world_impl.hpp>
#include <color/color.hpp>
#include <game_interface.hpp>
#include <game_properties.hpp>
#include <hud/hud.hpp>
#include <random/random.hpp>
#include <screeneffects/vignette.hpp>
#include <shape.hpp>
#include <sprite.hpp>
Expand All @@ -26,16 +28,21 @@ void StateGame::doInternalCreate()
m_background->setIgnoreCamMovement(true);
m_background->update(0.0f);

m_wind = std::make_shared<WindParticles>();
add(m_wind);

m_platforms = std::make_shared<jt::ObjectGroup<Platform>>();
add(m_platforms);

b2BodyDef def;
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);
spawnPlatform(jt::Vector2f { 300.0f, GP::GetScreenSize().y - 50.0f },
jt::Vector2f { GP::GetScreenSize().x, 20.0f });

m_baseTimerForBlockSpawns = std::make_shared<jt::Timer>(5.0f, [this]() {
spawnPlatform(
{ GP::GetScreenSize().x + 32, jt::Random::getFloat(20, GP::GetScreenSize().y - 40.0f) },
{ 32, 32 });
});
add(m_baseTimerForBlockSpawns);

createPlayer();

Expand All @@ -48,6 +55,19 @@ void StateGame::doInternalCreate()
setAutoDraw(false);
}

void StateGame::spawnPlatform(jt::Vector2f const& pos, jt::Vector2f const& size)
{
b2BodyDef def;
def.type = b2_kinematicBody;
auto p = std::make_shared<Platform>(m_world, &def, pos, size);

if (m_platforms->size() != 0) {
p->setVelocity({ -50, 0.0f });
}
add(p);
m_platforms->push_back(p);
}

void StateGame::createPlayer()
{
b2BodyDef def;
Expand All @@ -61,14 +81,17 @@ void StateGame::doInternalUpdate(float const elapsed)
{
if (m_running) {
m_world->step(elapsed, GP::PhysicVelocityIterations(), GP::PhysicPositionIterations());
// update game logic here
if (getGame()->input().keyboard()->justPressed(jt::KeyCode::A)) {
m_scoreP1++;
m_hud->getObserverScoreP1()->notify(m_scoreP1);

auto const speed = m_player->getPosition().x / GP::GetScreenSize().x * 100.0f;
m_hud->getObserverScoreP1()->notify(static_cast<int>(speed));

m_wind->m_windSpeed = 0.1f + speed / 100.0f * 4.0f;

if (speed > 100) {
endGame(true);
}
if (getGame()->input().keyboard()->justPressed(jt::KeyCode::D)) {
m_scoreP2++;
m_hud->getObserverScoreP2()->notify(m_scoreP2);
if (speed < -1) {
endGame(false);
}
}

Expand All @@ -84,7 +107,7 @@ void StateGame::doInternalDraw() const
m_hud->draw();
}

void StateGame::endGame()
void StateGame::endGame(bool win)
{
if (m_hasEnded) {
// trigger this function only once
Expand Down
10 changes: 8 additions & 2 deletions impl/gamelib/state_game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "object_group.hpp"
#include "platform.hpp"
#include "player.hpp"
#include "timer.hpp"
#include "wind_particles.hpp"
#include <box2dwrapper/box2d_world_interface.hpp>
#include <game_state.hpp>
#include <memory>
Expand All @@ -29,7 +31,10 @@ class StateGame : public jt::GameState {
std::shared_ptr<jt::Box2DWorldInterface> m_world { nullptr };
std::shared_ptr<Player> m_player { nullptr };

std::shared_ptr<jt::ObjectGroup<Platform>> m_platforms;
std::shared_ptr<jt::ObjectGroup<Platform>> m_platforms { nullptr };
std::shared_ptr<jt::Timer> m_baseTimerForBlockSpawns { nullptr };

std::shared_ptr<WindParticles> m_wind { nullptr };

bool m_running { true };
bool m_hasEnded { false };
Expand All @@ -41,8 +46,9 @@ class StateGame : public jt::GameState {
void doInternalUpdate(float const elapsed) override;
void doInternalDraw() const override;

void endGame();
void endGame(bool win);
void createPlayer();
void spawnPlatform(jt::Vector2f const& pos, jt::Vector2f const& size);
};

#endif
41 changes: 41 additions & 0 deletions impl/gamelib/wind_particles.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

#include "wind_particles.hpp"
#include <game_properties.hpp>
#include <random/random.hpp>
#include <screeneffects/screen_wrap.hpp>

void WindParticles::doCreate()
{
for (auto i = 0U; i != 100U; ++i) {
auto s = std::make_shared<jt::Shape>();
s->makeRect({ 8, 2 }, textureManager());
s->setPosition(
jt::Random::getRandomPointIn({ 0, 0, GP::GetScreenSize().x, GP::GetScreenSize().y }));
s->setScreenSizeHint(GP::GetScreenSize());
if (jt::Random::getChance()) {
s->setColor(GP::getPalette().getColor(1));
} else {
s->setColor(GP::getPalette().getColor(6));
}

m_shapes.push_back(s);
}
}
void WindParticles::doUpdate(float const elapsed)
{
jt::Vector2f const windSpeed { -150.0f, 0.0f };
for (auto& s : m_shapes) {
auto p = s->getPosition();
// float const f =
p += windSpeed * elapsed * m_windSpeed;
s->setPosition(p);
jt::wrapOnScreen(*s.get());
s->update(elapsed);
}
}
void WindParticles::doDraw() const
{
for (auto const& s : m_shapes) {
s->draw(renderTarget());
}
}
18 changes: 18 additions & 0 deletions impl/gamelib/wind_particles.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef INC_1HGJ384_WIND_PARTICLES_HPP
#define INC_1HGJ384_WIND_PARTICLES_HPP

#include "game_object.hpp"
#include "shape.hpp"
class WindParticles : public jt::GameObject {

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

std::vector<std::shared_ptr<jt::Shape>> m_shapes;

public:
float m_windSpeed { 1.0f };
};

#endif // INC_1HGJ384_WIND_PARTICLES_HPP

0 comments on commit 10e7b5e

Please sign in to comment.