aboutsummaryrefslogtreecommitdiff
path: root/game/coins/CoinScript.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'game/coins/CoinScript.cpp')
-rw-r--r--game/coins/CoinScript.cpp68
1 files changed, 55 insertions, 13 deletions
diff --git a/game/coins/CoinScript.cpp b/game/coins/CoinScript.cpp
index 5d4e8fe..514f4de 100644
--- a/game/coins/CoinScript.cpp
+++ b/game/coins/CoinScript.cpp
@@ -1,29 +1,71 @@
#include "CoinScript.h"
-#include "api/CircleCollider.h"
-#include "api/Sprite.h"
+
#include "manager/SaveManager.h"
+
#include "../Config.h"
-#include "../Events.h"
+#include "../hud/HudScript.h"
+
+#include <crepe/api/Animator.h>
+#include <crepe/api/AudioSource.h>
+#include <crepe/api/CircleCollider.h>
+#include <crepe/api/Sprite.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;
+bool CoinScript::on_collision(const CollisionEvent & collisionData) {
+ if (collisionData.info.other.metadata.tag != "coin") return false;
+ if (!this->get_components_by_name<Sprite>(collisionData.info.other.metadata.name)
+ .front()
+ .get()
+ .active)
+ return 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;
+
+ AudioSource & audio = this->get_components_by_id<AudioSource>(
+ collisionData.info.other.metadata.game_object_id
+ )
+ .front();
+ audio.play();
+
+ this->get_components_by_name<Sprite>(collisionData.info.other.metadata.name)
+ .back()
+ .get()
+ .active
+ = true;
+ this->get_components_by_name<Animator>(collisionData.info.other.metadata.name)
+ .back()
+ .get()
+ .active
+ = true;
+
+ return false;
+}
+
+void CoinScript::init() {
+ this->subscribe<CollisionEvent>([this](const CollisionEvent & ev) -> bool {
+ return this->on_collision(ev);
+ });
}
-void CoinScript::init(){
- this->subscribe<CollisionEvent>([this](const CollisionEvent & ev) -> bool { return this->on_collision(ev); });
- 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(){
+bool CoinScript::save() {
SaveManager & savemgr = this->get_save_manager();
savemgr.set(TOTAL_COINS_RUN, this->amount);
- this->amount = 0;
return false;
}