godot_voxel/terrain/instancing/voxel_instancer_rigidbody.h
Marc Gilleron 922f361cb0 Made it compile in Godot 4
- Made a bunch of changes to comply with Godot 4 API
- Use Godot's Vector3i and add the missing stuff with helper functions
- Transvoxel uses custom attributes API, the old way would not work
- Wrap MeshOptimizer in a unique namespace (see build script why)
- Added clang-format file for the module as some rules now differ
- Prevent thirdparty code and lookup tables from being clang-formatted
- Very likely full of runtime bugs that need fixing
2021-12-13 21:38:10 +00:00

67 lines
1.7 KiB
C++

#ifndef VOXEL_INSTANCER_RIGIDBODY_H
#define VOXEL_INSTANCER_RIGIDBODY_H
#include "voxel_instancer.h"
#include <scene/3d/physics_body_3d.h>
// Provides collision to VoxelInstancer multimesh instances
class VoxelInstancerRigidBody : public RigidDynamicBody3D {
GDCLASS(VoxelInstancerRigidBody, RigidDynamicBody3D);
public:
VoxelInstancerRigidBody() {
set_freeze_mode(RigidDynamicBody3D::FREEZE_MODE_STATIC);
set_freeze_enabled(true);
}
void set_data_block_position(Vector3i data_block_position) {
_data_block_position = data_block_position;
}
void set_render_block_index(unsigned int render_block_index) {
_render_block_index = render_block_index;
}
void set_instance_index(int instance_index) {
_instance_index = instance_index;
}
void attach(VoxelInstancer *parent) {
_parent = parent;
}
void detach_and_destroy() {
_parent = nullptr;
queue_delete();
}
// Note, for this the body must switch to convex shapes
// void detach_and_become_rigidbody() {
// //...
// }
protected:
void _notification(int p_what) {
switch (p_what) {
// TODO Optimization: this is also called when we quit the game or destroy the world
// which can make things a bit slow, but I don't know if it can easily be avoided
case NOTIFICATION_UNPARENTED:
// The user could queue_free() that node in game,
// so we have to notify the instancer to remove the multimesh instance and pointer
if (_parent != nullptr) {
_parent->on_body_removed(_data_block_position, _render_block_index, _instance_index);
_parent = nullptr;
}
break;
}
}
private:
VoxelInstancer *_parent = nullptr;
Vector3i _data_block_position;
unsigned int _render_block_index;
int _instance_index = -1;
};
#endif // VOXEL_INSTANCER_RIGIDBODY_H