aboutsummaryrefslogtreecommitdiff
path: root/game/menus/endgame/EndGameSubScript.cpp
blob: 6793f3e871f1a857299b9bc81a981d116c2c688c (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include "EndGameSubScript.h"

#include "../../Config.h"
#include "../../Events.h"
#include "../ButtonReplaySubScript.h"
#include "../IFloatingWindowScript.h"
#include "ValueBroker.h"
#include "manager/SaveManager.h"

#include <string>

#include <crepe/api/Button.h>
#include <crepe/api/Sprite.h>
#include <crepe/api/Text.h>
#include <crepe/types.h>

using namespace crepe;

EndGameSubScript::EndGameSubScript(const std::string & tag) { this->tag = tag; }

void EndGameSubScript::init() {
	this->disable_all();
	this->subscribe<EndGameEvent>([this](const EndGameEvent e) { return this->enable_all(); });
	this->subscribe<EndGameEvent>([this](const EndGameEvent e) {
		return this->reset_timescale();
	});
	this->subscribe<ShowScoreEvent>([this](const ShowScoreEvent e) {
		return this->showscore();
	});
}

bool EndGameSubScript::disable_all() {
	IFloatingWindowScript::disable_all_sprites();
	RefVector<Button> buttons = this->get_components_by_tag<Button>(this->tag);
	for (Button & button : buttons) {
		button.active = false;
	}
	RefVector<Text> texts = this->get_components_by_tag<Text>(this->tag);
	for (Text & text : texts) {
		text.active = false;
	}
	return false;
}

bool EndGameSubScript::enable_all() {
	IFloatingWindowScript::enable_all_sprites();
	RefVector<Button> buttons = this->get_components_by_tag<Button>(this->tag);
	for (Button & button : buttons) {
		button.active = true;
	}
	RefVector<Text> texts = this->get_components_by_tag<Text>(this->tag);
	for (Text & text : texts) {
		text.active = true;
	}
	return false;
}

bool EndGameSubScript::reset_timescale() {
	this->get_loop_timer().set_time_scale(1);
	return false;
}

bool EndGameSubScript::showscore() {
	// Gather text
	Text & coins_text = this->get_components_by_name<Text>("gold_endgame").front().get();
	Text & distance_text
		= this->get_components_by_name<Text>("distance_endgame").front().get();
	Text & highscore_text
		= this->get_components_by_name<Text>("highscore_endgame").front().get();
	highscore_text.active = false;

	// Gather saved data
	SaveManager & savemgr = this->get_save_manager();
	ValueBroker<std::string> coins = savemgr.get<std::string>(TOTAL_COINS_RUN, "0");
	ValueBroker<std::string> distance = savemgr.get<std::string>(DISTANCE_RUN, "0");
	int distance_run = savemgr.get<int>(DISTANCE_RUN, 0).get();
	int distance_game = savemgr.get<int>(DISTANCE_GAME, 0).get();

	// Show highscore
	if (distance_run > distance_game) highscore_text.active = true;

	const float CHAR_SIZE_DIS = 20;
	// Show distance
	std::string distance_string = "DISTANCE:" + distance.get();
	distance_text.text = distance_string;
	crepe::vec2 size_distance
		= {CHAR_SIZE_DIS * distance_string.size(),
		   (CHAR_SIZE_DIS * distance_string.size() / distance_string.size()) * 2};
	distance_text.dimensions = size_distance;

	const float CHAR_SIZE_COIN = 16;
	// Show coins
	std::string coins_string = "Coins:" + coins.get();
	coins_text.text = coins_string;
	crepe::vec2 size_coins
		= {CHAR_SIZE_COIN * coins_string.size(),
		   (CHAR_SIZE_COIN * coins_string.size() / coins_string.size()) * 2};
	coins_text.dimensions = size_coins;

	return false;
}