diff options
author | JAROWMR <jarorutjes07@gmail.com> | 2025-01-07 13:20:50 +0100 |
---|---|---|
committer | JAROWMR <jarorutjes07@gmail.com> | 2025-01-07 13:20:50 +0100 |
commit | bc66e7195c80facca244f933b882011e5e81fa3c (patch) | |
tree | a9417c19a7f9cecb09130ac7becfbba059b1ec5b /game | |
parent | 42cbef630ccaf3e841459d364edade1a3c72a525 (diff) |
coins use events
Diffstat (limited to 'game')
-rw-r--r-- | game/coins/CoinPool.cpp | 3 | ||||
-rw-r--r-- | game/coins/CoinScript.cpp | 11 | ||||
-rw-r--r-- | game/coins/CoinScript.h | 1 | ||||
-rw-r--r-- | game/coins/CoinSubScene.cpp | 1 | ||||
-rw-r--r-- | game/hud/HudScript.cpp | 12 | ||||
-rw-r--r-- | game/hud/HudScript.h | 6 | ||||
-rw-r--r-- | game/player/PlayerSubScene.cpp | 2 |
7 files changed, 31 insertions, 5 deletions
diff --git a/game/coins/CoinPool.cpp b/game/coins/CoinPool.cpp index 5720c2f..59b987c 100644 --- a/game/coins/CoinPool.cpp +++ b/game/coins/CoinPool.cpp @@ -1,5 +1,8 @@ #include "CoinPool.h" +#include "CoinScript.h" #include "CoinSubScene.h" +#include "api/BehaviorScript.h" +#include "api/GameObject.h" using namespace crepe; using namespace std; diff --git a/game/coins/CoinScript.cpp b/game/coins/CoinScript.cpp index 5d4e8fe..5960446 100644 --- a/game/coins/CoinScript.cpp +++ b/game/coins/CoinScript.cpp @@ -4,14 +4,15 @@ #include "manager/SaveManager.h" #include "../Config.h" #include "../Events.h" +#include "../hud/HudScript.h" using namespace crepe; using namespace std; bool CoinScript::on_collision(const CollisionEvent & collisionData){ if(collisionData.info.other.metadata.tag != "coin") return true; - this->get_components_by_name<Sprite>("").front().get().active = false; - this->get_components_by_name<CircleCollider>("").front().get().active = false; + this->get_components_by_name<Sprite>(collisionData.info.other.metadata.name).front().get().active = false; + this->get_components_by_name<CircleCollider>(collisionData.info.other.metadata.name).front().get().active = false; this->amount++; return true; } @@ -21,6 +22,12 @@ void CoinScript::init(){ this->subscribe<EndGameEvent>([this](const EndGameEvent e)-> bool { return this->save(); }); } +void CoinScript::fixed_update(crepe::duration_t dt) { + this->trigger_event(GetCoinEvent{ + .amount_of_coins = this->amount, + }); +} + bool CoinScript::save(){ SaveManager & savemgr = this->get_save_manager(); savemgr.set(TOTAL_COINS_RUN, this->amount); diff --git a/game/coins/CoinScript.h b/game/coins/CoinScript.h index e88a860..6fcb64e 100644 --- a/game/coins/CoinScript.h +++ b/game/coins/CoinScript.h @@ -5,6 +5,7 @@ class CoinScript : public crepe::Script { public: void init() override; + void fixed_update(crepe::duration_t dt) override; bool on_collision(const crepe::CollisionEvent & collisionData); bool save(); private: diff --git a/game/coins/CoinSubScene.cpp b/game/coins/CoinSubScene.cpp index 3914921..72319ca 100644 --- a/game/coins/CoinSubScene.cpp +++ b/game/coins/CoinSubScene.cpp @@ -36,6 +36,5 @@ int CoinSubScene::create(Scene & scn){ .looping = true, }); coin.add_component<AudioSource>(Asset{"asset/sfx/coin_pickup_1.ogg"}); - coin.add_component<BehaviorScript>().set_script<CoinScript>(); return coin_counter; } diff --git a/game/hud/HudScript.cpp b/game/hud/HudScript.cpp index 496a03d..d7a043a 100644 --- a/game/hud/HudScript.cpp +++ b/game/hud/HudScript.cpp @@ -17,7 +17,8 @@ void HudScript::init() { txt.text = record; txt.dimensions = {BEST_CHAR_WIDTH*record.size(),(BEST_CHAR_WIDTH)*2}; txt.offset = TOP_LEFT+FONTOFFSET+BEST_OFFSET + vec2{record.size() * BEST_CHAR_WIDTH/2,0}; - + + this->subscribe<GetCoinEvent>([this](const GetCoinEvent e)-> bool { return this->get_coin(e); }); this->subscribe<KeyPressEvent>([this](const KeyPressEvent & ev) -> bool { if(ev.key != Keycode::END) return false; Text & txt_fps = this->get_components_by_name<Text>(HUD_FPS).front(); @@ -45,7 +46,7 @@ void HudScript::frame_update(crepe::duration_t dt) { // Coins Text & txt_co = this->get_components_by_name<Text>(HUD_COINS).front(); - string amount_of_coins = to_string(savemgr->get<int>(TOTAL_COINS_RUN,0).get()); + string amount_of_coins = to_string(this->coin_amount); txt_co.text = amount_of_coins; txt_co.dimensions = {COINS_CHAR_WIDTH*amount_of_coins.size(),(COINS_CHAR_WIDTH)*2}; txt_co.offset = TOP_LEFT+FONTOFFSET+COINS_OFFSET + vec2{amount_of_coins.size() * COINS_CHAR_WIDTH/2,0}; @@ -61,3 +62,10 @@ void HudScript::frame_update(crepe::duration_t dt) { if(fps >= 50) txt_fps.data.text_color = Color::GREEN; if(fps < 30) txt_fps.data.text_color = Color::RED; } + + +bool HudScript::get_coin(const GetCoinEvent e){ + this->coin_amount = e.amount_of_coins; + return true; +} + diff --git a/game/hud/HudScript.h b/game/hud/HudScript.h index 8e7e8fc..0aa10a4 100644 --- a/game/hud/HudScript.h +++ b/game/hud/HudScript.h @@ -3,11 +3,17 @@ #include "api/Script.h" #include "manager/SaveManager.h" +struct GetCoinEvent : public crepe::Event { + int amount_of_coins; +}; + class HudScript : public crepe::Script { public: void init() override; void frame_update(crepe::duration_t dt) override; + bool get_coin(const GetCoinEvent e); private: crepe::SaveManager* savemgr; bool show_fps = false; + int coin_amount = 0; }; diff --git a/game/player/PlayerSubScene.cpp b/game/player/PlayerSubScene.cpp index c1e5e2f..fff9287 100644 --- a/game/player/PlayerSubScene.cpp +++ b/game/player/PlayerSubScene.cpp @@ -3,6 +3,7 @@ #include "PlayerScript.h" #include "../Config.h" +#include "../coins/CoinScript.h" #include <crepe/api/Animator.h> #include <crepe/api/BoxCollider.h> @@ -150,4 +151,5 @@ PlayerSubScene::PlayerSubScene(Scene & scn) { }); player.add_component<BehaviorScript>().set_script<PlayerScript>().active = false; player.add_component<BehaviorScript>().set_script<PlayerEndScript>().active = false; + player.add_component<BehaviorScript>().set_script<CoinScript>(); } |