pvb-practice/src/player.cpp

77 lines
2.5 KiB
C++
Raw Normal View History

#include "player.hpp"
#include <utility>
#include "godot_cpp/classes/animation_tree.hpp"
#include "godot_cpp/classes/node3d.hpp"
#include "godot_cpp/classes/skeleton3d.hpp"
#include "godot_cpp/variant/utility_functions.hpp"
#include "godot_macros.h"
namespace godot {
void Player::_bind_methods() {
#define CLASSNAME Player
#ifndef NDEBUG
GDPROPERTY(selected, Variant::STRING);
GDPROPERTY(index, Variant::INT);
#endif
}
void Player::_enter_tree() {
this->model = this->get_node<Node3D>("Model");
this->animTree = this->get_node<AnimationTree>("AnimationTree");
this->customization_init();
}
void Player::_exit_tree() {
for(std::pair<const String, std::pair<size_t, std::vector<Node3D*>>>& pair: this->customization) {
for(Node3D *option: pair.second.second) {
if(!option->is_inside_tree()) {
option->queue_free();
}
}
}
}
void Player::_process(double deltaTime) {}
void Player::_physics_process(double deltaTime) {}
void Player::customization_init() {
customizationParent = this->get_node<Skeleton3D>("Model/RootNode/Skeleton3D");
if(!customizationParent)
return;
for(int i = 0; i < customizationParent->get_child_count(); ++i) {
Node3D *child = Object::cast_to<Node3D>(customizationParent->get_child(i));
if(!child)
continue;
String name = child->get_name();
PackedStringArray slices = name.split("_", false);
if(customization.find(slices[1]) != customization.end()) {
auto &array = customization.at(slices[1]);
array.second.push_back(child);
customizationParent->remove_child(child);
--i;
} else {
customization.insert({slices[1], {0, {child}}});
}
}
UtilityFunctions::print("customization categories:");
for(std::pair<const String, std::pair<size_t, std::vector<Node3D *>>>& pair: this->customization) {
UtilityFunctions::print("- ", pair.first);
}
}
void Player::select_customization(String key, int index) {
std::pair<size_t, std::vector<Node3D *>> &pair = this->customization.at(key);
if(index > pair.second.size())
return;
if(index == pair.first)
return;
customizationParent->remove_child(pair.second[pair.first]);
customizationParent->add_child(pair.second[index]);
pair.first = index;
}
#ifndef NDEBUG
void Player::set_selected(String value) { this->selected = value; }
String Player::get_selected() const { return this->selected; }
#endif
} // namespace godot