#include "hitscan_muzzle.hpp" #include "damageable_entity.hpp" #include "godot_cpp/variant/callable.hpp" #include "utils/godot_macros.hpp" #include #include #include #include void HitscanMuzzle::_bind_methods() { #define CLASSNAME HitscanMuzzle GDFUNCTION(fire); // allow fire() to be called from animation tracks } void HitscanMuzzle::_ready() { this->set_enabled(false); this->set_physics_process(false); } void HitscanMuzzle::_physics_process(double) { this->fire_physics_check(); this->set_physics_process(false); // since _physics_process is only used for fire_physics_check, just disable it immediately } void HitscanMuzzle::fire() { this->set_physics_process(true); // offload physics checks to physics process to avoid multithreading issues } void HitscanMuzzle::fire_physics_check() { this->force_raycast_update(); // since we disabled automatic updating (set_enabled(false) in _ready), we'll have to force update here. gd::Object *hit{this->get_collider()}; if(hit == nullptr) return; // nothing was hit // see if the hit object can be damaged, damage if so DamageableEntity *damage_iface{dynamic_cast(hit)}; if(damage_iface == nullptr) return; // hit object can't be damaged. damage_iface->damage(); }