feat: implemented bullet impacts

This commit is contained in:
Sara 2025-07-19 12:33:40 +02:00
parent 7a5c129bb9
commit a7b3f97b3b
14 changed files with 644 additions and 12 deletions

View file

@ -0,0 +1,70 @@
#include "hitscan_muzzle.h"
#include "macros.h"
#include "scene/resources/packed_scene.h"
void HitscanMuzzle::_bind_methods() {
BIND_PROPERTY(Variant::FLOAT, spread);
BIND_PROPERTY(Variant::INT, damage);
BIND_PROPERTY(Variant::INT, ray_count);
}
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()) {
print_error("HitscanMuzzle::ready: impact effect is invalid");
}
}
void HitscanMuzzle::_notification(int what) {
if (Engine::get_singleton()->is_editor_hint()) {
return;
}
switch (what) {
default:
return;
case NOTIFICATION_READY:
ready();
return;
}
}
void HitscanMuzzle::shoot() {
set_transform(this->home_transform);
rotate_object_local(Vector3(0.f, 1.f, 0.f), Math::random(-Math::PI, Math::PI));
rotate_object_local(Vector3(1.f, 0.f, 0.f), Math::random(0.f, this->spread));
force_raycast_update();
Node *effect_as_node{ this->impact_effect->instantiate() };
if (Node3D * effect{ cast_to<Node3D>(effect_as_node) }) {
get_tree()->get_current_scene()->add_child(effect);
Vector3 const point{ get_collision_point() };
Vector3 const normal{ get_collision_normal() };
effect->look_at_from_position(point, point + normal);
} else if (effect_as_node) {
effect_as_node->queue_free();
}
}
void HitscanMuzzle::set_spread(float spread) {
this->spread = spread;
}
float HitscanMuzzle::get_spread() const {
return this->spread;
}
void HitscanMuzzle::set_damage(int damage) {
this->damage = damage;
}
int HitscanMuzzle::get_damage() const {
return this->damage;
}
void HitscanMuzzle::set_ray_count(int amount) {
this->ray_count = amount;
}
int HitscanMuzzle::get_ray_count() const {
return this->ray_count;
}

View file

@ -0,0 +1,32 @@
#ifndef HITSCAN_MUZZLE_H
#define HITSCAN_MUZZLE_H
#include "scene/3d/physics/ray_cast_3d.h"
class HitscanMuzzle : public RayCast3D {
GDCLASS(HitscanMuzzle, RayCast3D);
static void _bind_methods();
void ready();
public:
void _notification(int what);
void shoot();
void shoot_multiple(int count);
void set_spread(float spread);
float get_spread() const;
void set_damage(int damage);
int get_damage() const;
void set_ray_count(int amount);
int get_ray_count() const;
private:
float spread{ 0.01f };
int damage{ 1 };
int ray_count{ 1 };
Transform3D home_transform{};
Ref<PackedScene> impact_effect{};
};
#endif // !HITSCAN_MUZZLE_H

View file

@ -1,6 +1,7 @@
#include "register_types.h"
#include "core/object/class_db.h"
#include "wave_survival/hitscan_muzzle.h"
#include "wave_survival/player_body.h"
#include "wave_survival/player_camera.h"
#include "wave_survival/player_input.h"
@ -18,6 +19,7 @@ void initialize_wave_survival_module(ModuleInitializationLevel p_level) {
GDREGISTER_RUNTIME_CLASS(WeaponBase);
GDREGISTER_CLASS(Rifle);
GDREGISTER_CLASS(WeaponInventory);
GDREGISTER_CLASS(HitscanMuzzle);
}
void uninitialize_wave_survival_module(ModuleInitializationLevel p_level) {

View file

@ -1,5 +1,6 @@
#include "rifle.h"
#include "scene/animation/animation_player.h"
#include "wave_survival/hitscan_muzzle.h"
#include "wave_survival/player_body.h"
#include "wave_survival/player_input.h"
@ -36,6 +37,7 @@ void Rifle::stop_run_anim() {
void Rifle::shoot() {
if (!is_animating()) {
this->muzzle->shoot();
if (this->request_alt_mode) {
get_anim()->queue("fire_aim");
} else {
@ -76,6 +78,7 @@ void Rifle::on_animation_changed(String new_animation) {
}
void Rifle::ready() {
this->muzzle = cast_to<HitscanMuzzle>(get_node(NodePath("%HitscanMuzzle")));
get_anim()->connect("current_animation_changed", callable_mp(this, &self_type::on_animation_changed));
get_input()->connect(PlayerInput::sig_primary_fire, callable_mp(this, &self_type::on_primary_fire));
get_input()->connect(PlayerInput::sig_alt_mode, callable_mp(this, &self_type::on_alt_mode));

View file

@ -4,6 +4,7 @@
#include "wave_survival/player_camera.h"
#include "wave_survival/weapon_base.h"
class PlayerBody;
class HitscanMuzzle;
class Rifle : public WeaponBase {
GDCLASS(Rifle, WeaponBase);
@ -35,6 +36,8 @@ private:
bool request_alt_mode{ false };
bool in_alt_mode{ false };
bool running{ false };
HitscanMuzzle *muzzle{ nullptr };
};
#endif // !WEAPONS_RIFLE_H