From fc57d2e47c41ec3646fcd0d87a10f559cb1bdd8d Mon Sep 17 00:00:00 2001 From: Noel Berry Date: Sun, 3 Jan 2021 21:54:18 -0800 Subject: [PATCH] it's done!!! --- src/components/ghost_frog.cpp | 10 +++++++++- src/components/hurtable.cpp | 15 +++++++++------ src/components/hurtable.h | 1 + src/components/player.h | 2 +- src/game.cpp | 27 +++++++++++++++++++++++++-- src/game.h | 3 +++ src/world.h | 3 +++ 7 files changed, 51 insertions(+), 10 deletions(-) diff --git a/src/components/ghost_frog.cpp b/src/components/ghost_frog.cpp index ea8f135..8e59bb8 100644 --- a/src/components/ghost_frog.cpp +++ b/src/components/ghost_frog.cpp @@ -6,6 +6,7 @@ #include "orb.h" #include "../masks.h" #include "../factory.h" +#include "../game.h" using namespace TL; @@ -183,8 +184,13 @@ void GhostFrog::update() { if (Vec2(orb->entity()->position - orb->target()).length() < 16) { + auto sign = Calc::sign(orb->entity()->position.x - x); + if (sign != 0) + m_facing = sign; + anim->play("reflect"); orb->on_hit(); + m_reflect_count++; m_timer = 0; } @@ -195,7 +201,7 @@ void GhostFrog::update() { Factory::pop(world(), entity()->position + Point(0, -8)); orb->entity()->destroy(); - on_hurt(nullptr); + get()->hurt(); m_timer = 0; } } @@ -205,6 +211,7 @@ void GhostFrog::update() else if (m_state == st_dead_state) { anim->play("dead"); + world()->game->shake(1.0f); if (Time::on_interval(0.25f)) { @@ -219,6 +226,7 @@ void GhostFrog::update() Factory::pop(world(), entity()->position + Point(x * 12, -8 + y * 12)); Time::pause_for(0.3f); + world()->game->shake(0.1f); entity()->destroy(); } } diff --git a/src/components/hurtable.cpp b/src/components/hurtable.cpp index 7f2bcbe..a45900e 100644 --- a/src/components/hurtable.cpp +++ b/src/components/hurtable.cpp @@ -3,17 +3,20 @@ using namespace TL; +void Hurtable::hurt() +{ + Time::pause_for(0.1f); + stun_timer = 0.5f; + flicker_timer = 0.5f; + on_hurt(this); +} + void Hurtable::update() { if (collider && on_hurt && stun_timer <= 0) { if (collider->check(hurt_by)) - { - Time::pause_for(0.1f); - stun_timer = 0.5f; - flicker_timer = 0.5f; - on_hurt(this); - } + hurt(); } stun_timer -= Time::delta; diff --git a/src/components/hurtable.h b/src/components/hurtable.h index 3ba8417..947c20f 100644 --- a/src/components/hurtable.h +++ b/src/components/hurtable.h @@ -17,6 +17,7 @@ namespace TL uint32_t hurt_by = 0; std::function on_hurt; + void hurt(); void update() override; }; } \ No newline at end of file diff --git a/src/components/player.h b/src/components/player.h index 5d4aea9..e47da5c 100644 --- a/src/components/player.h +++ b/src/components/player.h @@ -14,7 +14,7 @@ namespace TL static constexpr int st_attack = 1; static constexpr int st_hurt = 2; static constexpr int st_start = 3; - static constexpr int max_health = 30; + static constexpr int max_health = 4; int health = max_health; diff --git a/src/game.cpp b/src/game.cpp index e7b8891..0399df0 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -18,6 +18,8 @@ namespace void Game::startup() { + world.game = this; + // load our content Content::load(); @@ -29,7 +31,7 @@ void Game::startup() m_draw_colliders = false; // load first room - load_room(Point(12, 0)); + load_room(Point(0, 0)); camera = Vec2(room.x * width, room.y * height); } @@ -154,6 +156,7 @@ void Game::shutdown() void Game::update() { + // Toggle Collider Render if (Input::pressed(Key::F1)) m_draw_colliders = !m_draw_colliders; @@ -169,8 +172,23 @@ void Game::update() // Normal Update if (!m_transition) { + // Screen Shake + m_shake_timer -= Time::delta; + if (m_shake_timer > 0) + { + if (Time::on_interval(0.05f)) + { + m_shake.x = Calc::rand_int(0, 2) == 0 ? -1 : 1; + m_shake.y = Calc::rand_int(0, 2) == 0 ? -1 : 1; + } + } + else + m_shake = Point::zero; + + // Upodate Objects world.update(); + // Check for transition / death auto player = world.first(); if (player) { @@ -281,7 +299,7 @@ void Game::render() buffer->clear(0x150e22); // push camera offset - batch.push_matrix(Mat3x2::create_translation(-camera)); + batch.push_matrix(Mat3x2::create_translation(-camera + m_shake)); // draw gameplay objects world.render(batch); @@ -364,3 +382,8 @@ void Game::render() batch.clear(); } } + +void Game::shake(float time) +{ + m_shake_timer = time; +} diff --git a/src/game.h b/src/game.h index fa95c73..a5c444b 100644 --- a/src/game.h +++ b/src/game.h @@ -31,6 +31,7 @@ namespace TL void shutdown(); void update(); void render(); + void shake(float time); private: bool m_draw_colliders; @@ -39,5 +40,7 @@ namespace TL Point m_next_room; Point m_last_room; Vector m_last_entities; + Point m_shake; + float m_shake_timer = 0; }; } \ No newline at end of file diff --git a/src/world.h b/src/world.h index d7a124d..834a5c0 100644 --- a/src/world.h +++ b/src/world.h @@ -3,6 +3,7 @@ namespace TL { + class Game; class World; class Entity; @@ -110,6 +111,8 @@ namespace TL public: static constexpr int max_component_types = 256; + Game* game; + World() = default; World(const World&) = delete; World(World&&) = delete;