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

63 lines
1.8 KiB
C++

#include "destructable_object.h"
#include "break_utopia/level_status.h"
#include "break_utopia/macros.h"
#include "core/object/object.h"
#include "scene/resources/packed_scene.h"
void DestructableObject::_bind_methods() {
BIND_HPROPERTY(Variant::OBJECT, destroyed_object, PROPERTY_HINT_RESOURCE_TYPE, "PackedScene");
BIND_HPROPERTY(Variant::OBJECT, shake_object, PROPERTY_HINT_NODE_TYPE, "Node3D");
BIND_PROPERTY(Variant::BOOL, count_in_total);
}
void DestructableObject::process(double delta) {
if (this->shake_object && this->shake_timer > 0.0) {
Vector3 position{ this->shake_base_position };
this->shake_timer -= delta;
if (this->shake_timer > 0.0) {
position.z += Math::sin(this->shake_timer * 100.0) * 0.2;
}
shake_object->set_position(position);
}
}
void DestructableObject::_notification(int what) {
if (Engine::get_singleton()->is_editor_hint()) {
return;
}
switch (what) {
default:
return;
case NOTIFICATION_READY:
if (this->count_in_total) {
LevelStatus::get_instance()->increment_total_objects();
}
return;
case NOTIFICATION_PROCESS:
process(get_process_delta_time());
return;
}
}
void DestructableObject::damaged(int level) {
if (this->destroyed_object.is_valid()) {
Node3D *instance{ cast_to<Node3D>(this->destroyed_object->instantiate()) };
instance->set_position(get_global_position());
instance->set_rotation(get_global_rotation());
get_tree()->get_current_scene()->add_child(instance);
}
if (this->count_in_total) {
LevelStatus::get_instance()->increment_destroyed_objects();
}
get_owner()->queue_free();
LevelStatus::get_instance()->notify_object_destroyed(get_defense());
}
void DestructableObject::damage_blocked(int level) {
if (!this->shake_object) {
return;
}
this->shake_timer = 0.1;
this->shake_base_position = this->shake_object->get_position();
set_process(true);
}