aboutsummaryrefslogtreecommitdiff
path: root/game/coins/CoinScript.cpp
blob: c5ca62ccdcded4eb4102ec83dd8ce0c0ec9b43e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "CoinScript.h"

#include "manager/SaveManager.h"

#include "../Config.h"
#include "../hud/HudScript.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 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++;

	AudioSource & audio = this->get_components_by_id<AudioSource>(
								  collisionData.info.other.metadata.game_object_id
	)
							  .front();
	audio.play();

	return false;
}

void CoinScript::init() {
	this->subscribe<CollisionEvent>([this](const CollisionEvent & ev) -> bool {
		return this->on_collision(ev);
	});
}

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);
	return false;
}