aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--game/coins/CoinScript.cpp20
-rw-r--r--game/coins/CoinScript.h3
2 files changed, 16 insertions, 7 deletions
diff --git a/game/coins/CoinScript.cpp b/game/coins/CoinScript.cpp
index e40f658..5d4e8fe 100644
--- a/game/coins/CoinScript.cpp
+++ b/game/coins/CoinScript.cpp
@@ -3,21 +3,27 @@
#include "api/Sprite.h"
#include "manager/SaveManager.h"
#include "../Config.h"
+#include "../Events.h"
using namespace crepe;
using namespace std;
bool CoinScript::on_collision(const CollisionEvent & collisionData){
- if(collisionData.info.other.metadata.name != PLAYER_NAME) return true;
- this->get_component<Sprite>().active = false;
- this->get_component<CircleCollider>().active = false;
- SaveManager & savemgr = this->get_save_manager();
- int amount = savemgr.get<int>(TOTAL_COINS_RUN,0).get() + 1;
- savemgr.set(TOTAL_COINS_RUN, amount);
+ 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->amount++;
return true;
}
void CoinScript::init(){
- subscribe<CollisionEvent>([this](const CollisionEvent & ev) -> bool { return this->on_collision(ev); });
+ this->subscribe<CollisionEvent>([this](const CollisionEvent & ev) -> bool { return this->on_collision(ev); });
+ this->subscribe<EndGameEvent>([this](const EndGameEvent e)-> bool { return this->save(); });
}
+bool CoinScript::save(){
+ SaveManager & savemgr = this->get_save_manager();
+ savemgr.set(TOTAL_COINS_RUN, this->amount);
+ this->amount = 0;
+ return false;
+}
diff --git a/game/coins/CoinScript.h b/game/coins/CoinScript.h
index 96e54fa..e88a860 100644
--- a/game/coins/CoinScript.h
+++ b/game/coins/CoinScript.h
@@ -6,4 +6,7 @@ class CoinScript : public crepe::Script {
public:
void init() override;
bool on_collision(const crepe::CollisionEvent & collisionData);
+ bool save();
+private:
+ int amount = 0;
};