From 3ec14aef92937d39f10cd617745938c96fa0324e Mon Sep 17 00:00:00 2001 From: Sara Date: Fri, 17 May 2024 15:11:22 +0200 Subject: [PATCH] feat: defined Key Pickup class --- src/key_pickup.cpp | 21 +++++++++++++++++++++ src/key_pickup.hpp | 17 +++++++++++++++++ src/register_types.cpp | 2 ++ 3 files changed, 40 insertions(+) create mode 100644 src/key_pickup.cpp create mode 100644 src/key_pickup.hpp diff --git a/src/key_pickup.cpp b/src/key_pickup.cpp new file mode 100644 index 0000000..9224b88 --- /dev/null +++ b/src/key_pickup.cpp @@ -0,0 +1,21 @@ +#include "key_pickup.hpp" +#include "car_player.hpp" +#include "rally_rush_game_mode.hpp" +#include "utils/game_root.hpp" + +namespace godot { +void KeyPickup::_bind_methods() { +#define CLASSNAME KeyPickup +} + +void KeyPickup::_enter_tree() { + this->connect("body_entered", callable_mp(this, &KeyPickup::on_body_entered)); +} + +void KeyPickup::on_body_entered(Node *node) { + if(!this->is_queued_for_deletion() && cast_to(node) != nullptr) { + Ref(GameRoot3D::get_singleton()->get_game_mode())->notify_key_found(); + this->queue_free(); + } +} +} diff --git a/src/key_pickup.hpp b/src/key_pickup.hpp new file mode 100644 index 0000000..2c3d3a6 --- /dev/null +++ b/src/key_pickup.hpp @@ -0,0 +1,17 @@ +#ifndef KEY_PICKUP_HPP +#define KEY_PICKUP_HPP + +#include "godot_cpp/classes/area3d.hpp" + +namespace godot { +class KeyPickup : public Area3D { + GDCLASS(KeyPickup, Area3D); + static void _bind_methods(); +public: + virtual void _enter_tree() override; +protected: + void on_body_entered(Node *node); +}; +} + +#endif // !KEY_PICKUP_HPP diff --git a/src/register_types.cpp b/src/register_types.cpp index cb22453..050ff75 100644 --- a/src/register_types.cpp +++ b/src/register_types.cpp @@ -1,6 +1,7 @@ #include "register_types.h" #include "car_physics.hpp" #include "car_player.hpp" +#include "key_pickup.hpp" #include "rally_rush_game_mode.hpp" #include "utils/game_mode.hpp" #include "utils/game_root.hpp" @@ -30,6 +31,7 @@ void initialize_gdextension_types(ModuleInitializationLevel p_level) ClassDB::register_class(); ClassDB::register_class(); ClassDB::register_class(); + ClassDB::register_class() } extern "C"