break-utopia/modules/break_utopia/level_status.cpp
2026-01-14 22:37:53 +01:00

142 lines
4.1 KiB
C++

#include "level_status.h"
#include "core/config/engine.h"
void LevelStatus::_bind_methods() {
ClassDB::bind_static_method("LevelStatus", "get_instance", &self_type::get_instance);
ClassDB::bind_method(D_METHOD("notify_object_destroyed", "value"), &self_type::notify_object_destroyed);
ADD_SIGNAL(MethodInfo(sig_object_destroyed, PropertyInfo(Variant::FLOAT, "style"), PropertyInfo(Variant::INT, "score")));
ADD_SIGNAL(MethodInfo(sig_meter_changed, PropertyInfo(Variant::FLOAT, "meter")));
ADD_SIGNAL(MethodInfo(sig_health_changed, PropertyInfo(Variant::FLOAT, "health")));
ADD_SIGNAL(MethodInfo(sig_style_changed, PropertyInfo(Variant::FLOAT, "style")));
ADD_SIGNAL(MethodInfo(sig_score_changed, PropertyInfo(Variant::INT, "score")));
BIND_PROPERTY(Variant::FLOAT, energy);
BIND_PROPERTY(Variant::FLOAT, style);
BIND_PROPERTY(Variant::INT, score);
BIND_PROPERTY(Variant::FLOAT, player_health);
BIND_PROPERTY(Variant::INT, total_objects);
BIND_PROPERTY(Variant::INT, destroyed_objects);
BIND_HPROPERTY(Variant::OBJECT, game_over_scene, PROPERTY_HINT_RESOURCE_TYPE, "PackedScene");
}
void LevelStatus::process(double delta) {
this->time_since_last += delta;
}
void LevelStatus::_notification(int what) {
if (Engine::get_singleton()->is_editor_hint()) {
return;
}
switch (what) {
default:
return;
case NOTIFICATION_ENTER_TREE:
instance = this;
return;
case NOTIFICATION_EXIT_TREE:
if (instance == this) {
instance = nullptr;
}
return;
case NOTIFICATION_READY:
set_process(true);
return;
case NOTIFICATION_PROCESS:
process(get_process_delta_time());
return;
}
}
void LevelStatus::notify_object_destroyed(int value) {
double const style_value{ (this->max_time_between > this->time_since_last ? (this->max_time_between - this->time_since_last) : 0.0) };
this->time_since_last = 0;
this->style += style_value / this->max_time_between;
if (this->style > 6) {
this->style = 6;
}
double const style_mult{ 1.0 + this->style };
this->score += value * style_mult * 10.;
this->energy += double(value * style_mult) * 0.01;
if (this->energy > 1.0) {
this->energy = 1.0;
}
emit_signal(sig_meter_changed, this->energy);
emit_signal(sig_style_changed, this->style);
emit_signal(sig_score_changed, this->score);
emit_signal(sig_object_destroyed, this->style, this->score);
}
bool LevelStatus::try_use_energy(double amount) {
if (amount > this->energy) {
return false;
} else {
this->energy -= amount;
emit_signal(sig_meter_changed, this->energy);
return true;
}
}
void LevelStatus::set_energy(double amount) {
this->energy = amount > 1.0 ? 1.0 : (amount < 0.0 ? 0.0 : amount);
emit_signal(sig_meter_changed, amount);
}
double LevelStatus::get_energy() const {
return this->energy;
}
void LevelStatus::set_style(double style) {
this->style = style;
emit_signal(sig_style_changed, style);
}
double LevelStatus::get_style() const {
return this->style;
}
void LevelStatus::set_score(int score) {
this->score = score;
emit_signal(sig_score_changed, score);
}
int LevelStatus::get_score() const {
return this->score;
}
void LevelStatus::set_player_health(double health) {
if (health < this->player_health) {
this->style = 0;
}
if (this->player_health > 0.0 && health <= 0.0) {
callable_mp(get_tree(), &SceneTree::change_scene_to_packed).call_deferred(this->game_over_scene);
}
this->player_health = health;
emit_signal(sig_health_changed, health);
}
double LevelStatus::get_player_health() const {
return this->player_health;
}
void LevelStatus::increment_destroyed_objects() {
this->destroyed_objects++;
}
void LevelStatus::increment_total_objects() {
this->total_objects++;
}
void LevelStatus::give_energy(double amount) {
set_energy(get_energy() + amount);
}
String const LevelStatus::sig_object_destroyed{ "object_destroyed" };
String const LevelStatus::sig_health_changed{ "health_changed" };
String const LevelStatus::sig_meter_changed{ "meter_changed" };
String const LevelStatus::sig_style_changed{ "style_changed" };
String const LevelStatus::sig_score_changed{ "score_changed" };
LevelStatus *LevelStatus::instance{ nullptr };
LevelStatus *LevelStatus::get_instance() {
return instance;
}