feat: replace 'fallback weapon' with 'starting weapon'

This commit is contained in:
Sara 2025-07-18 00:45:55 +02:00
parent a93e79cd1c
commit 01b69df872
12 changed files with 233 additions and 57 deletions

View file

@ -3,7 +3,7 @@
#include "weapon_base.h"
void WeaponInventory::_bind_methods() {
BIND_HPROPERTY(Variant::OBJECT, fallback_weapon, PROPERTY_HINT_RESOURCE_TYPE, "PackedScene");
BIND_HPROPERTY(Variant::OBJECT, starting_weapon, PROPERTY_HINT_RESOURCE_TYPE, "PackedScene");
}
void WeaponInventory::on_switch_input() {
@ -12,20 +12,8 @@ void WeaponInventory::on_switch_input() {
}
void WeaponInventory::ready() {
// initialize the fallback weapon
callable_mp(this, &self_type::pickup_weapon).call_deferred(this->starting_weapon);
this->weapon_parent = cast_to<Node3D>(get_node(NodePath("%PlayerCamera")));
if (this->fallback_weapon_scene.is_valid()) {
Node *fallback_as_node{ this->fallback_weapon_scene->instantiate() };
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) {
fallback_as_node->queue_free();
}
}
if (this->fallback_weapon == nullptr) {
print_error("WeaponInventory::ready(): fallback weapon is invalid, expect unintended behaviour.");
}
}
void WeaponInventory::_notification(int what) {
@ -41,34 +29,28 @@ void WeaponInventory::_notification(int what) {
}
}
void WeaponInventory::set_fallback_weapon(Ref<PackedScene> scene) {
this->fallback_weapon_scene = scene;
}
Ref<PackedScene> WeaponInventory::get_fallback_weapon() const {
return this->fallback_weapon_scene;
}
void WeaponInventory::select_weapon(WeaponBase *new_weapon) {
if (new_weapon == nullptr) {
new_weapon = this->fallback_weapon;
print_error("WeaponInventory::select_weapon: Weapon invalid, returning before taking any action.");
return;
}
if (new_weapon == this->weapons[0]) {
this->current = 0;
} else if (new_weapon == this->weapons[1]) {
this->current = 1;
} else {
print_error("WeaponInventory::select_weapon: Attempt to select weapon that is not in inventory. Exiting with no current weapon.");
return;
}
if (this->current_weapon != nullptr) {
this->current_weapon->set_process_mode(PROCESS_MODE_DISABLED);
this->current_weapon->set_visible(false);
}
this->current_weapon = new_weapon;
if (new_weapon != nullptr) {
if (this->current_weapon != nullptr) {
this->current_weapon->set_process_mode(PROCESS_MODE_PAUSABLE);
this->current_weapon->set_visible(true);
this->current_weapon->notify_selected();
if (new_weapon == this->weapons[0]) {
this->current = 0;
} else if (new_weapon == this->weapons[1]) {
this->current = 1;
} else {
print_error("WeaponInventory::select_weapon: Attempt to select weapon that is not in inventory");
}
}
}
@ -77,8 +59,14 @@ WeaponBase *WeaponInventory::get_current_weapon() const {
}
void WeaponInventory::pickup_weapon(Ref<PackedScene> weapon_scene) {
if (!weapon_scene.is_valid()) {
print_error("WeaponInventory::pickup_weapon: passed weapon scene");
return;
}
Node *weapon_as_node{ weapon_scene->instantiate() };
if (WeaponBase * weapon{ cast_to<WeaponBase>(weapon_as_node) }) {
this->weapon_parent->add_child(weapon_as_node);
// Where to put the new weapon, consider empty slot 1, then empty slot 2, then replace current.
if (this->weapons[0] == nullptr) {
this->weapons[0] = weapon;
select_weapon(weapon);
@ -88,12 +76,27 @@ void WeaponInventory::pickup_weapon(Ref<PackedScene> weapon_scene) {
select_weapon(weapon);
return;
} else {
this->current_weapon->queue_free();
// replace current weapon, assign the slot
this->weapons[this->current] = weapon;
// free the current weapon
this->current_weapon->queue_free();
// set the point to null, so select_weapon will skip dropping the current weapon.
this->current_weapon = nullptr;
// equip new weapon
select_weapon(weapon);
}
} else if (weapon_as_node != nullptr) {
print_error(vformat("WeaponInventory::pickup_weapon: weapon scene '%s' instantiated a node of type '%s', which does not inherit from 'WeaponBase'", weapon_scene->get_path(), weapon_as_node->get_class()));
weapon_as_node->queue_free();
} else {
print_error(vformat("WeaponInventory::pickup_weapon: passed weapon scene '%s' did not instantiate a valid node", weapon_scene->get_path()));
}
}
void WeaponInventory::set_starting_weapon(Ref<PackedScene> scene) {
this->starting_weapon = scene;
}
Ref<PackedScene> WeaponInventory::get_starting_weapon() const {
return this->starting_weapon;
}