diff --git a/src/liborng/nodes/character_nodes/character_3d.cpp b/src/liborng/nodes/character_nodes/character_3d.cpp index 4184b1d..bd33dd1 100644 --- a/src/liborng/nodes/character_nodes/character_3d.cpp +++ b/src/liborng/nodes/character_nodes/character_3d.cpp @@ -1,5 +1,5 @@ /* - * ©2023 Batty Bovine Productions, LLC. All Rights Reserved. + * ©2026 Batty Bovine Productions, LLC. All Rights Reserved. * * If this source code makes it to the public internet, this software can be * considered to be protected by the MIT licence. Have fun with it. @@ -9,26 +9,51 @@ #include "orng_macros.h" +#include "nodes/debug_draw3d.h" #include "nodes/mesh_editing_library.h" #include +#include #include +#include +#include +#include #include - using namespace godot; void Character3D::_ready() { - if (this->skeleton) + if (this->character_skeleton) { - TypedArray child_mesh_candidates = this->skeleton->get_children(); - const uint32_t num_children = child_mesh_candidates.size(); - for (uint32_t i = 0; i < num_children; i++) + if (const Skeleton3D *skeleton = this->character_skeleton->get_skeleton()) { - if (MeshInstance3D *mesh = cast_to(child_mesh_candidates[i])) + TypedArray child_mesh_candidates = skeleton->get_children(); + const uint32_t num_children = child_mesh_candidates.size(); + + const Vector3 &full_aabb_position = this->full_aabb.get_position(); + const Vector3 &full_aabb_size = this->full_aabb.get_size(); + + for (uint32_t i = 0; i < num_children; i++) { - this->meshes.emplace_back(mesh); + if (MeshInstance3D *mesh_instance = cast_to(child_mesh_candidates[i])) + { + this->meshes.emplace_back(mesh_instance); + + const AABB &aabb = mesh_instance->get_aabb(); + const Vector3 &aabb_position = aabb.get_position(); + const Vector3 &aabb_size = aabb.get_size(); + this->full_aabb.set_position(Vector3( + UtilityFunctions::minf(full_aabb_position.x, aabb_position.x), + UtilityFunctions::minf(full_aabb_position.y, aabb_position.y), + UtilityFunctions::minf(full_aabb_position.z, aabb_position.z) + )); + this->full_aabb.set_size(Vector3( + UtilityFunctions::maxf(full_aabb_position.x, aabb_size.x), + UtilityFunctions::maxf(full_aabb_position.y, aabb_size.y), + UtilityFunctions::maxf(full_aabb_position.z, aabb_size.z) + )); + } } } } @@ -43,39 +68,91 @@ void Character3D::_ready() void Character3D::take_damage(const Dictionary &hit_data) { - PRINT_LOG(CHARACTER3D_TAG, "Taking damage: ", hit_data["hit_points"]); - PRINT_LOG(CHARACTER3D_TAG, "Hit bones: ", hit_data["bone_hits"]); - - const Transform3D &global_hit_transform = (Transform3D)hit_data["hit_transform"]; - const Plane &slice_plane = this->skeleton->get_global_transform().inverse().xform(Plane(global_hit_transform.get_basis().get_column(1), global_hit_transform.origin)); - const uint32_t num_meshes = this->meshes.size(); - for (uint32_t i = 0; i < num_meshes; i++) + if (this->character_skeleton) { - MeshInstance3D *original_mesh_instance = this->meshes[i]; - Ref first_half; first_half.instantiate(); - Ref other_half; other_half.instantiate(); - PackedVector3Array &impact_points = MeshEditingLibrary::slice_mesh(original_mesh_instance, this->skeleton, slice_plane, first_half, other_half, MeshEditingLibrary::CAP_UV_FILL_MESH_BOUNDS, original_mesh_instance->get_active_material(0)); - - if (other_half.is_valid()) - original_mesh_instance->set_mesh(other_half); + if (const Skeleton3D *skeleton = this->character_skeleton->get_skeleton()) + { + PRINT_LOG(CHARACTER3D_TAG, "Taking damage: ", hit_data["hit_points"]); + PRINT_LOG(CHARACTER3D_TAG, "Hit bones: ", hit_data["bone_hits"]); + + const Transform3D &global_hit_transform = (Transform3D)hit_data["hit_transform"]; + const Plane &slice_plane = skeleton->get_global_transform().inverse().xform(Plane(global_hit_transform.get_basis().get_column(1), global_hit_transform.origin)); + const uint32_t num_meshes = this->meshes.size(); + std::vector meshes_to_add; + std::vector meshes_to_remove; + for (uint32_t i = 0; i < num_meshes; i++) + { + MeshInstance3D *original_mesh_instance = this->meshes[i]; + Ref first_half; first_half.instantiate(); + Ref other_half; other_half.instantiate(); + PackedVector3Array &impact_points = MeshEditingLibrary::slice_mesh(original_mesh_instance, skeleton, this->full_aabb, slice_plane, first_half, other_half, MeshEditingLibrary::CAP_UV_FILL_MESH_BOUNDS, this->inner_materials); + + if (first_half.is_valid()) + { + original_mesh_instance->set_mesh(first_half); + meshes_to_add.emplace_back(original_mesh_instance); + } + + if (other_half.is_valid()) + { + RandomNumberGenerator *rng = memnew(RandomNumberGenerator); + const float random_x = rng->randf_range(-0.5f, 0.5f); + const float random_z = rng->randf_range(-0.5f, 0.5f); + + CharacterSkeleton3D *new_character_skeleton = cast_to(this->character_skeleton->duplicate()); + this->character_skeleton->add_sibling(new_character_skeleton); + new_character_skeleton->set_position(Vector3(random_x, 0.0f, random_z)); + + Skeleton3D *new_skeleton = new_character_skeleton->get_skeleton(); + const uint16_t child_count = new_skeleton->get_child_count(); + TypedArray children = new_skeleton->get_children(); + for (uint16_t child = 0; child < child_count; child++) + { + if (MeshInstance3D *new_mesh_instance = cast_to(children[child])) + { + new_mesh_instance->set_skeleton_path(new_mesh_instance->get_path_to(new_skeleton)); + new_mesh_instance->set_skin(original_mesh_instance->get_skin()); + new_mesh_instance->set_mesh(other_half); + meshes_to_add.emplace_back(new_mesh_instance); + break; + } + } + } + + meshes_to_remove.emplace(meshes_to_remove.begin(), i); + } + + for (const uint32_t i : meshes_to_remove) + { + this->meshes.erase(this->meshes.begin() + i); + } + + for (MeshInstance3D *mesh : meshes_to_add) + { + this->meshes.emplace_back(mesh); + } + } } } void Character3D::_activate_ragdoll() { - if (this->physical_bone_simulator) + if (this->character_skeleton) { - const TypedArray &physical_bones = this->physical_bone_simulator->get_children(); - const uint32_t num_physical_bones = physical_bones.size(); - for (uint32_t i = 0; i < num_physical_bones; i++) + if (PhysicalBoneSimulator3D *physical_bone_simulator = this->character_skeleton->get_physical_bone_simulator()) { - if (PhysicalBone3D *bone = cast_to(physical_bones[i])) + const TypedArray &physical_bones = physical_bone_simulator->get_children(); + const uint32_t num_physical_bones = physical_bones.size(); + for (uint32_t i = 0; i < num_physical_bones; i++) { - bone->set_collision_layer(0); + if (PhysicalBone3D *bone = cast_to(physical_bones[i])) + { + bone->set_collision_layer(0); + } } + physical_bone_simulator->physical_bones_start_simulation(); } - this->physical_bone_simulator->physical_bones_start_simulation(); } } diff --git a/src/liborng/nodes/character_nodes/character_3d.h b/src/liborng/nodes/character_nodes/character_3d.h index 8420d68..55d1d40 100644 --- a/src/liborng/nodes/character_nodes/character_3d.h +++ b/src/liborng/nodes/character_nodes/character_3d.h @@ -1,5 +1,5 @@ /* - * ©2023 Batty Bovine Productions, LLC. All Rights Reserved. + * ©2026 Batty Bovine Productions, LLC. All Rights Reserved. * * If this source code makes it to the public internet, this software can be * considered to be protected by the MIT licence. Have fun with it. @@ -10,13 +10,12 @@ #include "orng_macros.h" #include "interfaces/i_damageable.h" +#include "nodes/character_nodes/character_skeleton3d.h" #include -#include #include +#include #include -#include -#include #include using namespace godot; @@ -32,23 +31,24 @@ public: virtual void take_damage(const Dictionary &hit_data) override; - Skeleton3D *get_skeleton() const { return this->skeleton; } - PhysicalBoneSimulator3D *get_physical_bone_simulator() const { return this->physical_bone_simulator; } - AnimationTree *get_animation_tree() const { return this->animation_tree; } + CharacterSkeleton3D *get_character_skeleton() const { return this->character_skeleton; } + + TypedArray get_inner_materials() const { return this->inner_materials; } virtual void _on_fell_out_of_world(); protected: - void set_skeleton(Skeleton3D *s) { this->skeleton = s; } - void set_physical_bone_simulator(PhysicalBoneSimulator3D *pbs) { this->physical_bone_simulator = pbs; } - void set_animation_tree(AnimationTree *at) { this->animation_tree = at; } + void set_character_skeleton(CharacterSkeleton3D *cs) { this->character_skeleton = cs; } + + void set_inner_materials(TypedArray materials) { this->inner_materials = materials; } private: void _activate_ragdoll(); - Skeleton3D *skeleton = nullptr; - PhysicalBoneSimulator3D *physical_bone_simulator = nullptr; - AnimationTree *animation_tree = nullptr; + CharacterSkeleton3D *character_skeleton = nullptr; + + AABB full_aabb; + TypedArray inner_materials; std::vector meshes; @@ -60,16 +60,10 @@ protected: ADD_SIGNAL(MethodInfo("fell_out_of_world")); - ClassDB::bind_method(D_METHOD("get_skeleton"), &Character3D::get_skeleton); - ClassDB::bind_method(D_METHOD("set_skeleton", "skeleton"), &Character3D::set_skeleton); - ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "skeleton", PROPERTY_HINT_NODE_TYPE, "Skeleton3D"), "set_skeleton", "get_skeleton"); + ClassDB::bind_method(D_METHOD("get_character_skeleton"), &Character3D::get_character_skeleton); + ClassDB::bind_method(D_METHOD("set_character_skeleton", "character_skeleton"), &Character3D::set_character_skeleton); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "character_skeleton", PROPERTY_HINT_NODE_TYPE, "CharacterSkeleton3D"), "set_character_skeleton", "get_character_skeleton"); - ClassDB::bind_method(D_METHOD("get_physical_bone_simulator"), &Character3D::get_physical_bone_simulator); - ClassDB::bind_method(D_METHOD("set_physical_bone_simulator", "physical_bone_simulator"), &Character3D::set_physical_bone_simulator); - ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "physical_bone_simulator", PROPERTY_HINT_NODE_TYPE, "PhysicalBoneSimulator3D"), "set_physical_bone_simulator", "get_physical_bone_simulator"); - - ClassDB::bind_method(D_METHOD("get_animation_tree"), &Character3D::get_animation_tree); - ClassDB::bind_method(D_METHOD("set_animation_tree", "animation_tree"), &Character3D::set_animation_tree); - ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "animation_tree", PROPERTY_HINT_NODE_TYPE, "AnimationTree"), "set_animation_tree", "get_animation_tree"); + ADD_GETTER_SETTER_HINTED(Character3D, inner_materials, Variant::ARRAY, PROPERTY_HINT_ARRAY_TYPE, "Material"); } };