feat: implemented basics for weapons

This commit is contained in:
Sara 2025-07-17 12:49:32 +02:00
parent 6fa9d11af5
commit aecc40ed6e
15 changed files with 163 additions and 51 deletions

Binary file not shown.

Binary file not shown.

View file

@ -88,5 +88,9 @@ PlayerBody *PlayerBody::get_singleton() {
}
bool PlayerBody::get_is_running() const {
return this->try_running && this->movement_input.y > 0.f && this->weapons->get_current_weapon()->allows_running();
return get_wants_to_run() && this->weapons->get_current_weapon()->allows_running();
}
bool PlayerBody::get_wants_to_run() const {
return this->try_running && this->movement_input.y > 0.f && this->is_on_floor();
}

View file

@ -25,6 +25,7 @@ protected:
public:
static PlayerBody *get_singleton();
bool get_is_running() const;
bool get_wants_to_run() const;
private:
bool try_running{ false };

View file

@ -9,6 +9,9 @@ class PlayerInput : public Node {
void _notification(int what);
virtual void unhandled_input(Ref<InputEvent> const &event) override;
public:
bool run_input_down() const;
public:
static String sig_movement_input;
static String sig_look_input;

View file

@ -1,6 +1,8 @@
#include "weapon_base.h"
#include "macros.h"
#include "player_body.h"
#include "player_camera.h"
#include "player_input.h"
#include "scene/animation/animation_player.h"
void WeaponBase::_bind_methods() {
@ -8,7 +10,9 @@ void WeaponBase::_bind_methods() {
}
void WeaponBase::ready() {
this->camera = cast_to<PlayerCamera>(get_node(NodePath("%PlayerCamera")));
this->input = cast_to<PlayerInput>(get_parent()->get_node(NodePath("%PlayerInput")));
this->camera = cast_to<PlayerCamera>(get_parent());
this->body = cast_to<PlayerBody>(get_parent()->get_owner());
if (this->anim) {
this->anim->play("RESET");
}
@ -34,6 +38,10 @@ AnimationPlayer *WeaponBase::get_anim() const {
return this->anim;
}
PlayerInput *WeaponBase::get_input() const {
return this->input;
}
PlayerCamera *WeaponBase::get_camera() const {
return this->camera;
}

View file

@ -5,6 +5,7 @@
class AnimationPlayer;
class PlayerCamera;
class PlayerBody;
class PlayerInput;
class WeaponBase : public Node3D {
GDCLASS(WeaponBase, Node3D);
@ -17,6 +18,7 @@ protected:
public:
void set_anim(AnimationPlayer *player);
AnimationPlayer *get_anim() const;
PlayerInput *get_input() const;
PlayerCamera *get_camera() const;
void set_body(PlayerBody *body);
@ -27,6 +29,7 @@ public:
virtual void notify_selected() {}
private:
PlayerInput *input{ nullptr };
AnimationPlayer *anim{ nullptr };
PlayerCamera *camera{ nullptr };
PlayerBody *body{ nullptr };

View file

@ -19,7 +19,7 @@ void WeaponInventory::ready() {
this->weapon_parent->add_child(fallback_as_node);
if ((this->fallback_weapon = cast_to<WeaponBase>(fallback_as_node))) {
this->select_weapon(this->fallback_weapon);
} else if(fallback_as_node != nullptr) {
} else if (fallback_as_node != nullptr) {
fallback_as_node->queue_free();
}
}
@ -78,7 +78,7 @@ WeaponBase *WeaponInventory::get_current_weapon() const {
void WeaponInventory::pickup_weapon(Ref<PackedScene> weapon_scene) {
Node *weapon_as_node{ weapon_scene->instantiate() };
if (WeaponBase *weapon{ cast_to<WeaponBase>(weapon_as_node) }) {
if (WeaponBase * weapon{ cast_to<WeaponBase>(weapon_as_node) }) {
if (this->weapons[0] == nullptr) {
this->weapons[0] = weapon;
select_weapon(weapon);

View file

@ -5,8 +5,44 @@
void Rifle::_bind_methods() {}
void Rifle::on_primary_fire(bool pressed) {
if (pressed && get_anim()->get_queue().size() == 0) {
void Rifle::queue_start_aim() {
get_anim()->queue("hip_to_aim");
get_anim()->queue("aim");
}
void Rifle::queue_stop_ads_anim() {
if (run_requested()) {
this->running = true;
get_anim()->clear_queue();
get_anim()->queue("aim_to_run");
get_anim()->queue("run");
} else {
get_anim()->queue("aim_to_hip");
get_anim()->queue("hip");
}
}
void Rifle::queue_start_run_anim() {
this->running = true;
get_anim()->clear_queue();
get_anim()->queue("hip_to_run");
get_anim()->queue("run");
}
void Rifle::stop_run_anim() {
this->running = false;
get_anim()->clear_queue();
if (this->request_alt_mode) {
get_anim()->play("run_to_aim", 0.0);
get_anim()->queue("aim");
} else {
get_anim()->play("run_to_hip", 0.0);
get_anim()->queue("hip");
}
}
void Rifle::shoot() {
if (get_anim()->get_queue().size() == 0 && get_anim()->get_current_animation() == "") {
if (this->request_alt_mode) {
get_anim()->queue("fire_aim");
} else if (get_anim()->get_current_animation() == "") {
@ -15,6 +51,17 @@ void Rifle::on_primary_fire(bool pressed) {
}
}
void Rifle::play_equip_anim() {
get_anim()->play("equip", 0.0);
get_anim()->queue("hip");
}
void Rifle::on_primary_fire(bool pressed) {
if (pressed) {
shoot();
}
}
void Rifle::on_alt_mode(bool alt_mode) {
this->request_alt_mode = alt_mode;
}
@ -23,24 +70,23 @@ void Rifle::on_animation_changed(String new_animation) {
if (new_animation == "aim") {
this->in_alt_mode = true;
get_camera()->set_fov_factor(this->ads_factor);
} else if (new_animation == "RESET") {
} else if (new_animation == "hip") {
this->in_alt_mode = false;
get_camera()->set_fov_factor(1.f);
get_camera()->set_fov_factor(1.0);
} else if (new_animation == "run") {
get_camera()->set_fov_factor(this->run_factor);
}
print_line(vformat("playing %s", new_animation));
Vector<String> queue{ get_anim()->get_queue() };
print_line(queue);
}
void Rifle::ready() {
get_anim()->connect("current_animation_changed", callable_mp(this, &self_type::on_animation_changed));
PlayerInput *input{ cast_to<PlayerInput>(get_node(NodePath("%PlayerInput"))) };
input->connect(PlayerInput::sig_primary_fire, callable_mp(this, &self_type::on_primary_fire));
input->connect(PlayerInput::sig_alt_mode, callable_mp(this, &self_type::on_alt_mode));
get_anim()->animation_set_next("hip_to_aim", "aim");
get_anim()->animation_set_next("fire_aim", "aim");
get_anim()->animation_set_next("aim_to_hip", "RESET");
get_anim()->animation_set_next("fire_hip", "RESET");
get_anim()->animation_set_next("equip", "RESET");
get_anim()->play("equip");
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 Rifle::process(double delta) {
@ -50,12 +96,27 @@ void Rifle::process(double delta) {
get_camera()->set_fov_factor(Math::lerp(1.f, this->ads_factor, progress));
} else if (current == "aim_to_hip") {
get_camera()->set_fov_factor(Math::lerp(this->ads_factor, 1.f, progress));
} else if (current == "aim_to_run") {
get_camera()->set_fov_factor(Math::lerp(this->ads_factor, this->run_factor, progress));
} else if (current == "run_to_aim") {
get_camera()->set_fov_factor(Math::lerp(this->run_factor, this->ads_factor, progress));
} else if (current == "hip_to_run") {
get_camera()->set_fov_factor(Math::lerp(1.f, this->run_factor, progress));
} else if (current == "run_to_hip") {
get_camera()->set_fov_factor(Math::lerp(this->run_factor, 1.f, progress));
} else if (this->request_alt_mode != this->in_alt_mode && current.is_empty()) {
get_anim()->clear_queue();
if (this->request_alt_mode) {
get_anim()->queue("hip_to_aim");
queue_start_aim();
} else {
get_anim()->queue("aim_to_hip");
queue_stop_ads_anim();
}
}
bool run_requested{ this->run_requested() };
if (this->running != run_requested) {
if (run_requested) {
queue_start_run_anim();
} else {
stop_run_anim();
}
}
}
@ -79,7 +140,11 @@ void Rifle::_notification(int what) {
bool Rifle::allows_running() const {
String const animation{ get_anim()->get_current_animation() };
return animation.is_empty();
return animation == "run" && !this->request_alt_mode;
}
bool Rifle::run_requested() const {
return get_body()->get_wants_to_run() && !this->request_alt_mode;
}
void Rifle::notify_selected() {

View file

@ -8,9 +8,16 @@ class PlayerBody;
class Rifle : public WeaponBase {
GDCLASS(Rifle, WeaponBase);
static void _bind_methods();
void queue_start_aim();
void queue_stop_ads_anim();
void queue_start_run_anim();
void stop_run_anim();
void shoot();
void play_equip_anim();
void on_primary_fire(bool down);
void on_alt_mode(bool alt_mode);
void on_animation_changed(String new_anim);
void on_run_input(bool run);
void ready();
void process(double delta);
@ -18,12 +25,15 @@ public:
void _notification(int what);
virtual bool allows_running() const override;
bool run_requested() const;
virtual void notify_selected() override;
private:
float ads_factor{ 0.5f };
float run_factor{ 1.5f };
bool request_alt_mode{ false };
bool in_alt_mode{ false };
bool running{ false };
};
#endif // !WEAPONS_RIFLE_H

View file

@ -2,6 +2,7 @@
[resource]
resource_name = "arms"
cull_mode = 2
albedo_color = Color(0.9063318, 0.9063318, 0.9063318, 1)
roughness = 0.5
use_z_clip_scale = true

View file

@ -34,9 +34,9 @@ animation/trimming=false
animation/remove_immutable_tracks=true
animation/import_rest_as_RESET=false
import_script/path=""
materials/extract=0
materials/extract=1
materials/extract_format=0
materials/extract_path=""
materials/extract_path="res://assets/materials/weapons"
_subresources={
"materials": {
"arms": {

View file

@ -40,167 +40,180 @@ slide_on_ceiling = false
use_collision = true
[node name="CSGBox3D2" type="CSGBox3D" parent="CSGCombiner3D"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -10.948, 3.13319, -1.86438)
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -7.424776, 3.13319, -1.86438)
use_collision = true
size = Vector3(10.441, 7.09393, 25.165)
size = Vector3(10.441, 7.09393, 42.19336)
material = ExtResource("1_ng1ul")
[node name="CSGBox3D27" type="CSGBox3D" parent="CSGCombiner3D"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 8.086981, 3.13319, -1.86438)
use_collision = true
size = Vector3(10.441, 7.09393, 42.19336)
material = ExtResource("1_ng1ul")
[node name="CSGBox3D3" type="CSGBox3D" parent="CSGCombiner3D"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -10.9688, 1.13, 6.77715)
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -7.4455757, 1.13, 6.77715)
operation = 2
use_collision = true
size = Vector3(9.8421, 3.074, 7.48355)
material = ExtResource("1_ng1ul")
[node name="CSGBox3D5" type="CSGBox3D" parent="CSGCombiner3D"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -10.9688, 1.13, -1.92168)
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -7.4455757, 1.13, -1.92168)
operation = 2
use_collision = true
size = Vector3(9.8421, 3.074, 9.25056)
material = ExtResource("1_ng1ul")
[node name="CSGBox3D6" type="CSGBox3D" parent="CSGCombiner3D"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -10.9688, 1.13, -10.4467)
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -7.4455757, 1.13, -10.4467)
operation = 2
use_collision = true
size = Vector3(9.8421, 3.074, 7.48355)
material = ExtResource("1_ng1ul")
[node name="CSGBox3D10" type="CSGBox3D" parent="CSGCombiner3D"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -10.9688, 4.73767, 6.77715)
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -7.4455757, 4.73767, 6.77715)
operation = 2
use_collision = true
size = Vector3(9.8421, 3.074, 7.48355)
material = ExtResource("1_ng1ul")
[node name="CSGBox3D11" type="CSGBox3D" parent="CSGCombiner3D"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -10.9688, 4.73767, -1.92168)
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -7.4455757, 4.73767, -1.92168)
operation = 2
use_collision = true
size = Vector3(9.8421, 3.074, 9.25056)
material = ExtResource("1_ng1ul")
[node name="CSGBox3D12" type="CSGBox3D" parent="CSGCombiner3D"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -10.9688, 4.73767, -10.4467)
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -7.4455757, 4.73767, -10.4467)
operation = 2
use_collision = true
size = Vector3(9.8421, 3.074, 7.48355)
material = ExtResource("1_ng1ul")
[node name="CSGBox3D4" type="CSGBox3D" parent="CSGCombiner3D"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -6.01349, 0.843177, 5.73304)
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -2.4902663, 0.843177, 5.73304)
operation = 2
use_collision = true
size = Vector3(2.16355, 2.81146, 1.43037)
size = Vector3(1.5039063, 2.81146, 1.43037)
material = ExtResource("1_ng1ul")
[node name="CSGBox3D19" type="CSGBox3D" parent="CSGCombiner3D"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -12.7667, 3.00782, 8.93745)
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -9.243476, 3.00782, 8.93745)
operation = 2
use_collision = true
size = Vector3(4.11031, 1.01367, 3.05259)
material = ExtResource("1_ng1ul")
[node name="CSGBox3D13" type="CSGBox3D" parent="CSGCombiner3D"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -6.03735, 5.15978, 8.92892)
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -2.5141263, 5.15978, 8.92892)
operation = 2
use_collision = true
size = Vector3(1.64708, 1.47165, 1.75998)
material = ExtResource("1_ng1ul")
[node name="CSGBox3D14" type="CSGBox3D" parent="CSGCombiner3D"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -6.03735, 5.15978, 5.36833)
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -2.5141263, 5.15978, 5.36833)
operation = 2
use_collision = true
size = Vector3(1.64708, 1.47165, 3.01134)
material = ExtResource("1_ng1ul")
[node name="CSGBox3D17" type="CSGBox3D" parent="CSGCombiner3D"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -6.03735, 5.15978, -4.15471)
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -2.5141263, 5.15978, -4.15471)
operation = 2
use_collision = true
size = Vector3(1.64708, 1.47165, 1.75998)
material = ExtResource("1_ng1ul")
[node name="CSGBox3D20" type="CSGBox3D" parent="CSGCombiner3D"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -6.03735, 1.63843, -12.371)
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -2.5141263, 1.63843, -12.371)
operation = 2
use_collision = true
size = Vector3(1.64708, 1.47165, 1.75998)
material = ExtResource("1_ng1ul")
[node name="CSGBox3D21" type="CSGBox3D" parent="CSGCombiner3D"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -6.03735, 1.63843, -3.73614)
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -2.5141263, 1.63843, -3.73614)
operation = 2
use_collision = true
size = Vector3(1.64708, 1.47165, 1.75998)
material = ExtResource("1_ng1ul")
[node name="CSGBox3D23" type="CSGBox3D" parent="CSGCombiner3D"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -6.03735, 1.63843, 0.557484)
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -2.5141263, 1.63843, 0.557484)
operation = 2
use_collision = true
size = Vector3(1.64708, 1.47165, 1.75998)
material = ExtResource("1_ng1ul")
[node name="CSGBox3D22" type="CSGBox3D" parent="CSGCombiner3D"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -6.03735, 1.63843, 8.83479)
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -2.5141263, 1.63843, 8.83479)
operation = 2
use_collision = true
size = Vector3(1.64708, 1.47165, 1.75998)
material = ExtResource("1_ng1ul")
[node name="CSGBox3D18" type="CSGBox3D" parent="CSGCombiner3D"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -6.03735, 5.15978, -8.38057)
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -2.5141263, 5.15978, -8.38057)
operation = 2
use_collision = true
size = Vector3(1.64708, 1.47165, 1.75998)
material = ExtResource("1_ng1ul")
[node name="CSGBox3D15" type="CSGBox3D" parent="CSGCombiner3D"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -6.03735, 5.15978, -0.349048)
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -2.5141263, 5.15978, -0.349048)
operation = 2
use_collision = true
size = Vector3(1.64708, 1.47165, 3.46446)
material = ExtResource("1_ng1ul")
[node name="CSGBox3D16" type="CSGBox3D" parent="CSGCombiner3D"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -6.03735, 5.15978, -11.8593)
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -2.5141263, 5.15978, -11.8593)
operation = 2
use_collision = true
size = Vector3(1.64708, 1.47165, 3.46446)
material = ExtResource("1_ng1ul")
[node name="CSGBox3D7" type="CSGBox3D" parent="CSGCombiner3D"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -5.70662, 0.843177, -9.61435)
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -2.1833963, 0.843177, -9.61435)
operation = 2
use_collision = true
size = Vector3(1.20117, 2.81146, 1.43037)
material = ExtResource("1_ng1ul")
[node name="CSGBox3D8" type="CSGBox3D" parent="CSGCombiner3D"]
transform = Transform3D(-4.37114e-08, 0, 1, 0, 1, 0, -1, 0, -4.37114e-08, -14.1813, 0.843177, -6.9353)
transform = Transform3D(-4.37114e-08, 0, 1, 0, 1, 0, -1, 0, -4.37114e-08, -10.658076, 0.843177, -6.9353)
operation = 2
use_collision = true
size = Vector3(1.71875, 2.81146, 1.43037)
material = ExtResource("1_ng1ul")
[node name="CSGBox3D9" type="CSGBox3D" parent="CSGCombiner3D"]
transform = Transform3D(-4.37114e-08, 0, 1, 0, 1, 0, -1, 0, -4.37114e-08, -8.47255, 1.11953, 3.07464)
transform = Transform3D(-4.37114e-08, 0, 1, 0, 1, 0, -1, 0, -4.37114e-08, -4.9493265, 1.11953, 3.07464)
operation = 2
use_collision = true
size = Vector3(1.43066, 2.25876, 1.43037)
material = ExtResource("1_ng1ul")
[node name="CSGBox3D24" type="CSGBox3D" parent="CSGCombiner3D"]
transform = Transform3D(-4.37114e-08, 0, 1, 0, 1, 0, -1, 0, -4.37114e-08, -8.47255, 4.34085, 3.07464)
transform = Transform3D(-4.37114e-08, 0, 1, 0, 1, 0, -1, 0, -4.37114e-08, -4.9493265, 4.34085, 3.07464)
operation = 2
use_collision = true
size = Vector3(1.43066, 2.25876, 1.43037)
material = ExtResource("1_ng1ul")
[node name="CSGBox3D26" type="CSGBox3D" parent="CSGCombiner3D"]
transform = Transform3D(-1, 0, -8.742279e-08, 0, 1, 0, 8.742279e-08, 0, -1, -12.553127, 4.340849, -9.004332)
operation = 2
use_collision = true
size = Vector3(1.43066, 2.25876, 1.43037)
material = ExtResource("1_ng1ul")
[node name="CSGBox3D25" type="CSGBox3D" parent="CSGCombiner3D"]
transform = Transform3D(-4.37114e-08, 0, 1, 0, 1, 0, -1, 0, -4.37114e-08, -14.0385, 4.34085, -6.63972)
transform = Transform3D(-4.37114e-08, 0, 1, 0, 1, 0, -1, 0, -4.37114e-08, -10.515276, 4.34085, -6.63972)
operation = 2
use_collision = true
size = Vector3(1.43066, 2.25876, 1.43037)

View file

@ -1,4 +1,6 @@
[gd_scene load_steps=2 format=3 uid="uid://snjgu4yp5swd"]
[gd_scene load_steps=3 format=3 uid="uid://snjgu4yp5swd"]
[ext_resource type="PackedScene" uid="uid://ce40pq785yoyi" path="res://objects/weapons/rifle.tscn" id="1_eqqp1"]
[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_bxedw"]
@ -17,3 +19,5 @@ transform = Transform3D(-1, 0, -8.74228e-08, 0, 1, 0, 8.74228e-08, 0, -1, 0, 1.6
fov = 60.0
[node name="WeaponInventory" type="WeaponInventory" parent="."]
unique_name_in_owner = true
fallback_weapon = ExtResource("1_eqqp1")