Skip to content

Commit

Permalink
Finalize game
Browse files Browse the repository at this point in the history
  • Loading branch information
Laguna1989 committed Sep 3, 2022
1 parent 10e7b5e commit 403a03f
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 14 deletions.
20 changes: 20 additions & 0 deletions impl/gamelib/platform.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "platform.hpp"
#include <game_properties.hpp>
#include <random/random.hpp>

Platform::Platform(std::shared_ptr<jt::Box2DWorldInterface> world, b2BodyDef const* def,
jt::Vector2f const& pos, jt::Vector2f const& size)
Expand All @@ -13,18 +15,36 @@ Platform::Platform(std::shared_ptr<jt::Box2DWorldInterface> world, b2BodyDef con
getB2Body()->CreateFixture(&fixtureDef);

setPosition(pos);

m_flashTimer = jt::Random::getFloat(0.2f, 1.75f);
}

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

void Platform::doUpdate(float const elapsed)
{
m_shape->setPosition(getPosition());
m_shape->update(elapsed);

if (m_shape->getPosition().x <= -100) {
kill();
}

m_flashTimer -= elapsed;
if (m_flashTimer <= 0) {
if (jt::Random::getChance()) {
m_shape->flash(0.25f, GP::getPalette().getColor(4));
} else {
m_shape->shake(0.25f, 10.0f);
}

m_flashTimer = jt::Random::getFloat(0.2f, 1.75f);
}
}
void Platform::doDraw() const { m_shape->draw(renderTarget()); }
5 changes: 4 additions & 1 deletion impl/gamelib/platform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ class Platform : public jt::Box2DObject {
Platform(std::shared_ptr<jt::Box2DWorldInterface> world, b2BodyDef const* def,
jt::Vector2f const& pos, jt::Vector2f const& size);

float m_speedScale = 1.0f;

float m_flashTimer { 1.0f };

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

jt::Vector2f m_size;

void doCreate() override;
Expand Down
10 changes: 8 additions & 2 deletions impl/gamelib/player.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "player.hpp"
#include <game_interface.hpp>
#include <game_properties.hpp>

Player::Player(std::shared_ptr<jt::Box2DWorldInterface> world, b2BodyDef const* def)
: Box2DObject(world, def)
Expand All @@ -18,6 +19,7 @@ void Player::doCreate()
m_shape = std::make_shared<jt::Shape>();
m_shape->makeRect(jt::Vector2f { 16, 48 }, textureManager());
m_shape->setOffset(jt::OffsetMode::CENTER);
m_shape->setColor(GP::getPalette().getColor(0));
}

void Player::doUpdate(float const elapsed)
Expand All @@ -30,11 +32,15 @@ void Player::doUpdate(float const elapsed)
}

m_jumpTimer -= elapsed;
if (getPosition().y >= 315) {
m_jumpTimer = 0.0f;
}

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

Expand Down
47 changes: 39 additions & 8 deletions impl/gamelib/state_game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,18 @@ void StateGame::doInternalCreate()
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 });
m_baseTimerForBlockSpawns1 = std::make_shared<jt::Timer>(1.8121412f, [this]() {
spawnPlatform({ GP::GetScreenSize().x + 32,
jt::Random::getFloat(150, GP::GetScreenSize().y - 50.0f) },
{ 24, 48 });
});
add(m_baseTimerForBlockSpawns);
add(m_baseTimerForBlockSpawns1);

add(std::make_shared<jt::Timer>(2.722345f, [this]() {
spawnPlatform({ GP::GetScreenSize().x + 32,
jt::Random::getFloat(150, GP::GetScreenSize().y - 50.0f) },
{ 48, 24 });
}));

createPlayer();

Expand All @@ -61,9 +67,9 @@ void StateGame::spawnPlatform(jt::Vector2f const& pos, jt::Vector2f const& size)
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 });
}
// if (m_platforms->size() != 0) {
// p->setVelocity({ -50, 0.0f });
// }
add(p);
m_platforms->push_back(p);
}
Expand Down Expand Up @@ -93,6 +99,31 @@ void StateGame::doInternalUpdate(float const elapsed)
if (speed < -1) {
endGame(false);
}

auto const f = 0.3f + speed / 100.0f * 1.7f;

for (auto i = 0U; i != m_platforms->size(); ++i) {
auto p = m_platforms->at(i).lock();
if (i == 0U) {
p->m_flashTimer = 50000.0f;
continue;
}

p->setVelocity(jt::Vector2f { -150.0f, 0.0f } * f);
}

if (m_player->getPosition().y >= 315 && m_lastPlayerY <= 315) {
getGame()->gfx().camera().shake(0.5f, 9.0f);
}

m_lastPlayerY = m_player->getPosition().y;

int const criticalHitX = 480;
if (m_player->getPosition().x >= criticalHitX && m_lastPlayerX < criticalHitX) {
spawnPlatform(jt::Vector2f { GP::GetScreenSize().x + 32, m_lastPlayerY }, { 48, 48 });
}

m_lastPlayerX = m_player->getPosition().x;
}

m_background->update(elapsed);
Expand Down
5 changes: 4 additions & 1 deletion impl/gamelib/state_game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class StateGame : public jt::GameState {
std::shared_ptr<Player> m_player { nullptr };

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

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

Expand All @@ -42,6 +42,9 @@ class StateGame : public jt::GameState {
int m_scoreP1 { 0 };
int m_scoreP2 { 0 };

float m_lastPlayerY = 5000;
float m_lastPlayerX = 5000;

void doInternalCreate() override;
void doInternalUpdate(float const elapsed) override;
void doInternalDraw() const override;
Expand Down
6 changes: 4 additions & 2 deletions impl/gamelib/state_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,10 @@ void StateMenu::createTextCredits()
void StateMenu::createTextExplanation()
{
float half_width = GP::GetScreenSize().x / 2;
m_text_Explanation = jt::dh::createText(
renderTarget(), "Press Space to start the game", 16U, GP::PaletteFontFront());
m_text_Explanation = jt::dh::createText(renderTarget(),
"Press Space to start the game\nReach the right side of the screen\n[D] to run forward, "
"[W, Space] to jump",
16U, GP::PaletteFontFront());
m_text_Explanation->setPosition({ half_width, 150 });
m_text_Explanation->setShadow(GP::PaletteFontShadow(), jt::Vector2f { 3, 3 });
}
Expand Down

0 comments on commit 403a03f

Please sign in to comment.