wave-survival/modules/wave_survival/player_interactor.cpp

88 lines
2.6 KiB
C++

#include "player_interactor.h"
#include "interactable.h"
#include "player_body.h"
#include "player_input.h"
void PlayerInteractor::_bind_methods() {
ClassDB::bind_method(D_METHOD("pickup_demo_pack"), &self_type::pickup_demo_pack);
ClassDB::bind_method(D_METHOD("try_use_demo_pack"), &self_type::try_use_demo_pack);
}
void PlayerInteractor::highlight_removed() {
this->interactable->disconnect("tree_exiting", this->on_highlight_removed);
this->interactable->set_highlighted(this, false);
this->interactable = nullptr;
}
void PlayerInteractor::activate() {
if (interactable != nullptr) {
interactable->activate(this);
}
}
void PlayerInteractor::ready() {
PlayerInput *input{ cast_to<PlayerInput>(get_node(NodePath("%PlayerInput"))) };
input->connect(PlayerInput::sig_activate, callable_mp(this, &self_type::activate));
}
void PlayerInteractor::process(double delta) {
Interactable *new_interactable{ nullptr };
if (is_colliding()) {
if (Node * hit{ cast_to<Node>(this->get_collider(0)) }) {
TypedArray<Node> interactables = hit->find_children("*", Interactable::get_class_static());
new_interactable = interactables.size() > 0 ? cast_to<Interactable>(interactables[0]) : nullptr;
}
}
if (new_interactable != this->interactable) {
if (this->interactable != nullptr) {
this->interactable->set_highlighted(this, false);
this->interactable->disconnect("tree_exiting", this->on_highlight_removed);
}
this->interactable = new_interactable;
if (this->interactable != nullptr) {
this->interactable->set_highlighted(this, true);
this->interactable->connect("tree_exiting", this->on_highlight_removed);
}
}
}
void PlayerInteractor::_notification(int what) {
if (Engine::get_singleton()->is_editor_hint()) {
return;
}
switch (what) {
default:
return;
case NOTIFICATION_READY:
set_process(true);
ready();
return;
case NOTIFICATION_PROCESS:
process(get_process_delta_time());
return;
}
}
PackedStringArray PlayerInteractor::get_configuration_warnings() const {
PackedStringArray warnings{ super_type::get_configuration_warnings() };
if (cast_to<PlayerBody>(get_owner()) == nullptr) {
warnings.push_back("PlayerInteractor should be part of and owned by a PlayerBody node to work properly.");
}
if (get_node(NodePath("%PlayerInput")) == nullptr) {
warnings.push_back("PlayerInteractor should have a node named '%PlayerInput' in the same scene as it");
}
return warnings;
}
void PlayerInteractor::pickup_demo_pack() {
++this->num_demo_packs;
}
bool PlayerInteractor::try_use_demo_pack() {
if (this->num_demo_packs > 0) {
--this->num_demo_packs;
return true;
}
return false;
}