feat: implemented rifle reload with placeholder anim

This commit is contained in:
Sara 2025-08-06 18:45:02 +02:00
parent 150231c67c
commit 68abfdd383
25 changed files with 26775 additions and 3 deletions

View file

@ -9,6 +9,9 @@ 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);
ClassDB::bind_method(D_METHOD("reload_full"), &self_type::reload_full);
ClassDB::bind_method(D_METHOD("reload_num", "num"), &self_type::reload_num);
ClassDB::bind_method(D_METHOD("ammo_empty"), &self_type::ammo_empty);
}
void WeaponBase::ready() {
@ -91,3 +94,19 @@ bool WeaponBase::try_use_ammo(int amount) {
return false;
}
}
bool WeaponBase::ammo_empty() const {
return this->loaded_ammo <= 0;
}
void WeaponBase::reload_full() {
// TODO: take difference between loaded and max out of inventory
set_loaded_ammo(this->max_ammo);
}
void WeaponBase::reload_num(int num) {
int const space{ this->max_ammo - this->loaded_ammo };
num = MIN(num, space);
// TODO: take num out of ammo inventory
set_loaded_ammo(this->loaded_ammo + num);
}

View file

@ -31,6 +31,9 @@ public:
void set_loaded_ammo(int amount);
int get_loaded_ammo() const;
bool try_use_ammo(int amount = 1);
bool ammo_empty() const;
void reload_full();
void reload_num(int num);
virtual bool allows_running() const { return false; }
virtual bool allows_jumping() const { return true; }

View file

@ -42,7 +42,7 @@ void Rifle::exit_run() {
}
void Rifle::shoot() {
if (!is_animating()) {
if (!is_animating() && try_use_ammo()) {
this->muzzle->shoot();
if (this->request_alt_mode) {
get_anim()->queue("fire_aim");
@ -52,6 +52,16 @@ void Rifle::shoot() {
}
}
void Rifle::start_reload_animation() {
if (!is_animating() && get_loaded_ammo() != get_max_ammo()) {
if (this->in_alt_mode) {
get_anim()->queue("aim_to_hip");
}
get_anim()->queue("reload");
get_anim()->queue("hip");
}
}
void Rifle::play_equip_anim() {
get_anim()->play("equip", 0.0);
get_anim()->queue("hip");
@ -68,6 +78,10 @@ void Rifle::on_alt_mode(bool alt_mode) {
this->request_alt_mode = alt_mode;
}
void Rifle::on_reload() {
start_reload_animation();
}
void Rifle::on_animation_changed(String new_animation) {
if (new_animation == "aim") {
this->in_alt_mode = true;
@ -85,6 +99,7 @@ void Rifle::ready() {
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));
get_input()->connect(PlayerInput::sig_reload, callable_mp(this, &self_type::on_reload));
play_equip_anim();
}

View file

@ -14,11 +14,12 @@ class Rifle : public WeaponBase {
void queue_enter_run();
void exit_run();
void shoot();
void start_reload_animation();
void play_equip_anim();
void on_primary_fire(bool down);
void on_alt_mode(bool alt_mode);
void on_reload();
void on_animation_changed(String new_anim);
void on_run_input(bool run);
void ready();
void process(double delta);

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

File diff suppressed because it is too large Load diff

View file

@ -1,6 +1,6 @@
[gd_scene load_steps=6 format=3 uid="uid://snjgu4yp5swd"]
[ext_resource type="PackedScene" uid="uid://cfgwif53qypko" path="res://objects/weapons/revolver.tscn" id="1_eqqp1"]
[ext_resource type="PackedScene" uid="uid://ce40pq785yoyi" path="res://objects/weapons/rifle.tscn" id="1_eqqp1"]
[sub_resource type="SphereShape3D" id="SphereShape3D_eqqp1"]
radius = 0.2

View file

@ -11,6 +11,8 @@ func trigger() -> void:
[node name="Revolver" type="Revolver" node_paths=PackedStringArray("anim")]
anim = NodePath("revolver/AnimationPlayer")
max_ammo = 6
loaded_ammo = 6
single_action_spread = 0.003
double_action_spread = 0.02

View file

@ -11,6 +11,8 @@ func trigger() -> void:
[node name="Rifle" type="Rifle" node_paths=PackedStringArray("anim")]
anim = NodePath("rifle/AnimationPlayer")
max_ammo = 10
loaded_ammo = 10
[node name="rifle" parent="." instance=ExtResource("1_afgyw")]