feat: started work on reloading

This commit is contained in:
Sara 2025-08-05 16:20:55 +02:00
parent 2d720a983a
commit 150231c67c
17 changed files with 63 additions and 33 deletions

View file

@ -10,6 +10,7 @@ String PlayerInput::sig_run{ "run" };
String PlayerInput::sig_jump{ "jump" };
String PlayerInput::sig_crouch{ "crouch" };
String PlayerInput::sig_activate{ "activate" };
String PlayerInput::sig_reload{ "reload" };
void PlayerInput::_bind_methods() {
ADD_SIGNAL(MethodInfo(sig_movement_input, PropertyInfo(Variant::VECTOR2, "axes")));
@ -21,6 +22,7 @@ void PlayerInput::_bind_methods() {
ADD_SIGNAL(MethodInfo(sig_jump));
ADD_SIGNAL(MethodInfo(sig_crouch, PropertyInfo(Variant::BOOL, "is_crouching")));
ADD_SIGNAL(MethodInfo(sig_activate));
ADD_SIGNAL(MethodInfo(sig_reload));
}
void PlayerInput::normalize_input() {
@ -88,4 +90,7 @@ void PlayerInput::unhandled_input(Ref<InputEvent> const &event) {
if (event->is_action("activate") && event->is_pressed()) {
emit_signal(sig_activate);
}
if (event->is_action("reload") && event->is_pressed()) {
emit_signal(sig_reload);
}
}

View file

@ -20,6 +20,7 @@ public:
static String sig_jump;
static String sig_crouch;
static String sig_activate;
static String sig_reload;
private:
bool was_paused{ false };

View file

@ -7,6 +7,8 @@
void WeaponBase::_bind_methods() {
BIND_HPROPERTY(Variant::OBJECT, anim, PROPERTY_HINT_NODE_TYPE, AnimationPlayer::get_class_static());
BIND_PROPERTY(Variant::INT, max_ammo);
BIND_PROPERTY(Variant::INT, loaded_ammo);
}
void WeaponBase::ready() {
@ -64,3 +66,28 @@ void WeaponBase::set_body(PlayerBody *body) {
PlayerBody *WeaponBase::get_body() const {
return this->body;
}
void WeaponBase::set_max_ammo(int amount) {
this->max_ammo = amount;
}
int WeaponBase::get_max_ammo() const {
return MAX(0, this->max_ammo);
}
void WeaponBase::set_loaded_ammo(int amount) {
this->loaded_ammo = CLAMP(amount, 0, this->max_ammo);
}
int WeaponBase::get_loaded_ammo() const {
return this->loaded_ammo;
}
bool WeaponBase::try_use_ammo(int amount) {
if (this->loaded_ammo >= amount) {
loaded_ammo -= amount;
return true;
} else {
return false;
}
}

View file

@ -26,6 +26,12 @@ public:
void set_body(PlayerBody *body);
PlayerBody *get_body() const;
void set_max_ammo(int amount);
int get_max_ammo() const;
void set_loaded_ammo(int amount);
int get_loaded_ammo() const;
bool try_use_ammo(int amount = 1);
virtual bool allows_running() const { return false; }
virtual bool allows_jumping() const { return true; }
virtual void notify_selected() {}
@ -35,6 +41,9 @@ private:
AnimationPlayer *anim{ nullptr };
PlayerCamera *camera{ nullptr };
PlayerBody *body{ nullptr };
int loaded_ammo{ 0 };
int max_ammo{ 1 };
};
#endif // !WEAPON_BASE_H

View file

@ -35,6 +35,9 @@ private:
WeaponBase *current_weapon{ nullptr };
Ref<PackedScene> starting_weapon{};
int pistol_ammo{ 60 };
int rifle_ammo{ 0 };
};
#endif // !WEAPON_INVENTORY_H

View file

@ -26,6 +26,11 @@ void Revolver::queue_idle_anim() {
void Revolver::start_running() {
if (this->run_animation_timer <= 0.0) {
if (this->alt_active) {
get_anim()->queue("single_to_run");
} else {
get_anim()->queue("double_to_run");
}
get_anim()->queue("run");
}
}
@ -36,7 +41,7 @@ void Revolver::stop_running() {
if (current == "run") {
get_anim()->stop();
}
if (current.begins_with("run")) {
if (current.begins_with("run") || current.ends_with("run")) {
if (this->alt_active) {
get_anim()->queue("run_to_single");
} else {
@ -87,10 +92,14 @@ void Revolver::on_alt_mode(bool pressed) {
}
}
void Revolver::on_reload() {
}
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));
get_input()->connect(PlayerInput::sig_reload, callable_mp(this, &self_type::on_reload));
play_equip_anim();
}

View file

@ -16,6 +16,7 @@ class Revolver : public WeaponBase {
void cock_hammer();
void on_primary_fire(bool pressed);
void on_alt_mode(bool pressed);
void on_reload();
void ready();
void process(double delta);
@ -43,7 +44,7 @@ private:
double recoil_time{ 0.1 };
double recoil_timer{ 0.0 };
double run_animation_delay{ 0.3 };
double run_animation_delay{ 0.8 };
double run_animation_timer{ 0.0 };
};