feat: hit effect scenes are now preloaded
This commit is contained in:
parent
5a4ac26c72
commit
adcbf41a48
|
@ -8,6 +8,21 @@ void Hitbox::_bind_methods() {
|
|||
BIND_HPROPERTY(Variant::OBJECT, impact_effect, PROPERTY_HINT_RESOURCE_TYPE, "PackedScene");
|
||||
}
|
||||
|
||||
void Hitbox::_notification(int what) {
|
||||
if (Engine::get_singleton()->is_editor_hint()) {
|
||||
return;
|
||||
}
|
||||
switch (what) {
|
||||
default:
|
||||
return;
|
||||
case NOTIFICATION_READY:
|
||||
if (this->impact_effect.is_valid()) {
|
||||
PRELOAD_SCENE(this->impact_effect);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void Hitbox::set_health(HealthStatus *value) {
|
||||
this->health = value;
|
||||
}
|
||||
|
|
|
@ -9,6 +9,9 @@ class Hitbox : public Area3D {
|
|||
GDCLASS(Hitbox, Area3D);
|
||||
static void _bind_methods();
|
||||
|
||||
protected:
|
||||
void _notification(int what);
|
||||
|
||||
public:
|
||||
void set_health(HealthStatus *value);
|
||||
HealthStatus *get_health() const;
|
||||
|
|
|
@ -14,9 +14,9 @@ void HitscanMuzzle::instantiate_impact_effect() {
|
|||
if (get_collider() == nullptr) {
|
||||
return;
|
||||
}
|
||||
Ref<PackedScene> effect_scene{ get_collider()->call("get_impact_effect") };
|
||||
if (!effect_scene.is_valid()) {
|
||||
effect_scene = this->impact_effect;
|
||||
Ref<PackedScene> effect_scene{ this->impact_effect };
|
||||
if (get_collider()->has_method("get_impact_effect")) {
|
||||
effect_scene = get_collider()->call("get_impact_effect");
|
||||
}
|
||||
Node *effect_as_node{ effect_scene->instantiate() };
|
||||
if (Node3D * effect{ cast_to<Node3D>(effect_as_node) }) {
|
||||
|
@ -39,7 +39,9 @@ void HitscanMuzzle::try_deal_damage() {
|
|||
void HitscanMuzzle::ready() {
|
||||
this->home_transform = get_transform();
|
||||
this->impact_effect = ResourceLoader::load("res://objects/effects/bullet_impact.tscn");
|
||||
if (!this->impact_effect.is_valid()) {
|
||||
if (this->impact_effect.is_valid()) {
|
||||
PRELOAD_SCENE(this->impact_effect);
|
||||
} else {
|
||||
print_error("HitscanMuzzle::ready: impact effect is invalid");
|
||||
}
|
||||
set_enabled(false);
|
||||
|
|
|
@ -1,6 +1,17 @@
|
|||
#ifndef GODOT_EXTRA_MACROS_H
|
||||
#define GODOT_EXTRA_MACROS_H
|
||||
|
||||
#define PRELOAD_SCENE(m_scene) \
|
||||
do { \
|
||||
if (Node * node{ m_scene->instantiate() }) { \
|
||||
if (Node3D * node_3d{ cast_to<Node3D>(node) }) { \
|
||||
node_3d->set_visible(false); \
|
||||
} \
|
||||
get_tree()->get_current_scene()->add_child(node); \
|
||||
node->connect("ready", callable_mp(node, &Node::queue_free)); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#define BIND_GET_SET(m_property) \
|
||||
ClassDB::bind_method(D_METHOD("set_" #m_property, #m_property), \
|
||||
&self_type::set_##m_property); \
|
||||
|
|
Loading…
Reference in a new issue