72 lines
1.8 KiB
C++
72 lines
1.8 KiB
C++
#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() };
|
|
Vector3 const position{ get_global_position() };
|
|
effect->look_at_from_position(point, point + normal, (point - position).normalized());
|
|
} 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;
|
|
}
|