wave-survival/modules/wave_survival/weapons/revolver.cpp
2025-08-04 13:45:16 +02:00

116 lines
2.8 KiB
C++

#include "revolver.h"
#include "scene/animation/animation_player.h"
#include "wave_survival/macros.h"
#include "wave_survival/player_camera.h"
#include "wave_survival/player_input.h"
void Revolver::_bind_methods() {
ClassDB::bind_method(D_METHOD("start_recoil"), &self_type::start_recoil);
BIND_PROPERTY(Variant::FLOAT, single_action_spread);
BIND_PROPERTY(Variant::FLOAT, double_action_spread);
}
void Revolver::play_equip_anim() {
get_anim()->play("equip", 0.0);
get_anim()->queue("idle_double");
get_anim()->advance(0.0);
}
void Revolver::shoot() {
if (!is_animating()) {
if (this->alt_active) {
get_anim()->queue("fire_single");
this->alt_active = false;
this->muzzle->set_spread(this->single_action_spread);
this->muzzle->shoot();
start_recoil();
} else {
this->muzzle->set_spread(this->double_action_spread);
get_anim()->queue("fire_double");
}
get_anim()->queue("idle_double");
}
}
void Revolver::cock_hammer() {
if (!this->alt_active && !is_animating()) {
this->alt_active = true;
get_anim()->queue("to_single");
get_anim()->queue("idle_single");
}
}
void Revolver::on_primary_fire(bool pressed) {
if (pressed) {
shoot();
}
}
void Revolver::on_alt_mode(bool pressed) {
if (!this->alt_active && pressed) {
cock_hammer();
}
}
void Revolver::ready() {
this->muzzle = cast_to<HitscanMuzzle>(get_node(NodePath("%HitscanMuzzle")));
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));
play_equip_anim();
}
void Revolver::process(double delta) {
//String const current{ get_anim()->get_current_animation() };
//double animation_time{ get_anim()->get_current_animation_position() };
if (this->recoil_timer > 0.0) {
this->recoil_timer -= delta;
double t{ 1.0 - CLAMP(this->recoil_timer / this->recoil_time, 0.0, 1.0) };
get_camera()->recoil(Math::lerp((double)this->recoil_force, 0.0, t) * delta);
}
}
void Revolver::_notification(int what) {
if (Engine::get_singleton()->is_editor_hint()) {
return;
}
switch (what) {
default:
return;
case NOTIFICATION_READY:
set_process(true);
ready();
return;
case NOTIFICATION_PROCESS:
process(get_process_delta_time());
return;
}
}
void Revolver::notify_selected() {
play_equip_anim();
}
bool Revolver::allows_running() const {
return true;
}
void Revolver::start_recoil() {
this->recoil_timer = this->recoil_time;
}
void Revolver::set_single_action_spread(float value) {
this->single_action_spread = value;
}
float Revolver::get_single_action_spread() const {
return this->single_action_spread;
}
void Revolver::set_double_action_spread(float value) {
this->double_action_spread = value;
}
float Revolver::get_double_action_spread() const {
return this->double_action_spread;
}