feat: added blood effect
This commit is contained in:
		
							parent
							
								
									94641f81bb
								
							
						
					
					
						commit
						be5a412008
					
				| 
						 | 
					@ -4,17 +4,22 @@
 | 
				
			||||||
#include "utils/godot_macros.hpp"
 | 
					#include "utils/godot_macros.hpp"
 | 
				
			||||||
#include <godot_cpp/classes/physics_direct_space_state3d.hpp>
 | 
					#include <godot_cpp/classes/physics_direct_space_state3d.hpp>
 | 
				
			||||||
#include <godot_cpp/classes/physics_ray_query_parameters3d.hpp>
 | 
					#include <godot_cpp/classes/physics_ray_query_parameters3d.hpp>
 | 
				
			||||||
 | 
					#include <godot_cpp/classes/scene_tree.hpp>
 | 
				
			||||||
#include <godot_cpp/classes/world3d.hpp>
 | 
					#include <godot_cpp/classes/world3d.hpp>
 | 
				
			||||||
#include <godot_cpp/variant/utility_functions.hpp>
 | 
					#include <godot_cpp/variant/utility_functions.hpp>
 | 
				
			||||||
 | 
					#include <godot_cpp/classes/resource_loader.hpp>
 | 
				
			||||||
 | 
					#include <godot_cpp/classes/resource.hpp>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void HitscanMuzzle::_bind_methods() {
 | 
					void HitscanMuzzle::_bind_methods() {
 | 
				
			||||||
#define CLASSNAME HitscanMuzzle
 | 
					#define CLASSNAME HitscanMuzzle
 | 
				
			||||||
    GDFUNCTION(fire); // allow fire() to be called from animation tracks
 | 
					    GDFUNCTION(fire); // allow fire() to be called from animation tracks
 | 
				
			||||||
 | 
					    GDPROPERTY_HINTED(blood_effect, gd::Variant::OBJECT, gd::PROPERTY_HINT_RESOURCE_TYPE, "PackedScene");
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void HitscanMuzzle::_ready() {
 | 
					void HitscanMuzzle::_ready() {
 | 
				
			||||||
    this->set_enabled(false);
 | 
					    this->set_enabled(false);
 | 
				
			||||||
    this->set_physics_process(false);
 | 
					    this->set_physics_process(false);
 | 
				
			||||||
 | 
					    gd::ResourceLoader::get_singleton()->load(this->blood_effect->get_path());
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void HitscanMuzzle::_physics_process(double) {
 | 
					void HitscanMuzzle::_physics_process(double) {
 | 
				
			||||||
| 
						 | 
					@ -26,6 +31,14 @@ void HitscanMuzzle::fire() {
 | 
				
			||||||
    this->set_physics_process(true); // offload physics checks to physics process to avoid multithreading issues
 | 
					    this->set_physics_process(true); // offload physics checks to physics process to avoid multithreading issues
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void HitscanMuzzle::set_blood_effect(gd::Ref<gd::PackedScene> scene) {
 | 
				
			||||||
 | 
					    this->blood_effect = scene;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					gd::Ref<gd::PackedScene> HitscanMuzzle::get_blood_effect() const {
 | 
				
			||||||
 | 
					    return this->blood_effect;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void HitscanMuzzle::fire_physics_check() {
 | 
					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.
 | 
					    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()};
 | 
					    gd::Object *hit{this->get_collider()};
 | 
				
			||||||
| 
						 | 
					@ -34,4 +47,10 @@ void HitscanMuzzle::fire_physics_check() {
 | 
				
			||||||
    DamageableEntity *damage_iface{dynamic_cast<DamageableEntity*>(hit)};
 | 
					    DamageableEntity *damage_iface{dynamic_cast<DamageableEntity*>(hit)};
 | 
				
			||||||
    if(damage_iface == nullptr) return; // hit object can't be damaged.
 | 
					    if(damage_iface == nullptr) return; // hit object can't be damaged.
 | 
				
			||||||
    damage_iface->damage();
 | 
					    damage_iface->damage();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    gd::Vector3 const location{this->get_collision_point()};
 | 
				
			||||||
 | 
					    gd::Node3D *effect{gd::Object::cast_to<gd::Node3D>(this->blood_effect->instantiate())};
 | 
				
			||||||
 | 
					    this->get_tree()->get_current_scene()->add_child(effect);
 | 
				
			||||||
 | 
					    effect->set_global_basis(this->get_global_basis());
 | 
				
			||||||
 | 
					    effect->set_global_position(location);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -2,6 +2,7 @@
 | 
				
			||||||
#define HITSCAN_MUZZLE_HPP
 | 
					#define HITSCAN_MUZZLE_HPP
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <godot_cpp/classes/node3d.hpp>
 | 
					#include <godot_cpp/classes/node3d.hpp>
 | 
				
			||||||
 | 
					#include <godot_cpp/classes/packed_scene.hpp>
 | 
				
			||||||
#include <godot_cpp/classes/physics_body3d.hpp>
 | 
					#include <godot_cpp/classes/physics_body3d.hpp>
 | 
				
			||||||
#include <godot_cpp/classes/ray_cast3d.hpp>
 | 
					#include <godot_cpp/classes/ray_cast3d.hpp>
 | 
				
			||||||
#include <godot_cpp/templates/vector.hpp>
 | 
					#include <godot_cpp/templates/vector.hpp>
 | 
				
			||||||
| 
						 | 
					@ -15,8 +16,12 @@ public:
 | 
				
			||||||
    virtual void _physics_process(double) override;
 | 
					    virtual void _physics_process(double) override;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    void fire(); // prep a deferred call to fire_physics_check
 | 
					    void fire(); // prep a deferred call to fire_physics_check
 | 
				
			||||||
 | 
					    void set_blood_effect(gd::Ref<gd::PackedScene> scene);
 | 
				
			||||||
 | 
					    gd::Ref<gd::PackedScene> get_blood_effect() const;
 | 
				
			||||||
private:
 | 
					private:
 | 
				
			||||||
    void fire_physics_check();
 | 
					    void fire_physics_check();
 | 
				
			||||||
 | 
					private:
 | 
				
			||||||
 | 
					    gd::Ref<gd::PackedScene> blood_effect{};
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#endif // !HITSCAN_MUZZLE_HPP
 | 
					#endif // !HITSCAN_MUZZLE_HPP
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue