YouDunIt/modules/you_done_it/clue_finder.cpp

90 lines
1.8 KiB
C++

#include "clue_finder.h"
#include "core/config/engine.h"
#include "scene/3d/xr/xr_nodes.h"
#include "scene/main/node.h"
ClueFinder *ClueFinder::singleton_instance{ nullptr };
void ClueFinder::_bind_methods() {
ClassDB::bind_method(D_METHOD("take_photo"), &ClueFinder::take_photo);
}
void ClueFinder::enter_tree() {
if (singleton_instance == nullptr) {
singleton_instance = this;
} else {
queue_free();
}
if (XRController3D * controller{ cast_to<XRController3D>(get_parent()) }) {
controller->connect("button_pressed", callable_mp(this, &self_type::on_button_pressed));
}
}
void ClueFinder::exit_tree() {
if (singleton_instance == this) {
singleton_instance = nullptr;
}
}
void ClueFinder::on_button_pressed(String button) {
if (button == "trigger_click") {
take_photo();
}
if (button == "ax_button") {
send_photo();
}
if (button == "by_button") {
delete_photo();
}
}
void ClueFinder::_notification(int what) {
if (Engine::get_singleton()->is_editor_hint()) {
return;
}
switch (what) {
case NOTIFICATION_ENTER_TREE:
enter_tree();
return;
case NOTIFICATION_EXIT_TREE:
exit_tree();
return;
default:
return;
}
}
ClueFinder *ClueFinder::get_singleton() {
return singleton_instance;
}
ClueMarker *ClueFinder::find_current_clue() {
for (ClueMarker *marker : this->clue_markers) {
if (marker->is_visible()) {
return marker;
}
}
return nullptr;
}
void ClueFinder::register_clue_marker(ClueMarker *marker) {
this->clue_markers.insert(marker);
}
void ClueFinder::remove_clue_marker(ClueMarker *marker) {
this->clue_markers.erase(marker);
}
void ClueFinder::take_photo() {
this->found_marker = find_current_clue();
}
void ClueFinder::send_photo() {
if (this->found_marker) {
this->found_marker->reveal();
}
}
void ClueFinder::delete_photo() {
this->found_marker = nullptr;
}