wave-survival/modules/wave_survival/weapon_base.cpp
2025-08-05 16:20:55 +02:00

94 lines
2.1 KiB
C++

#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() {
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() {
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());
}
void WeaponBase::_notification(int what) {
if (Engine::get_singleton()->is_editor_hint()) {
return;
}
switch (what) {
default:
return;
case NOTIFICATION_READY:
ready();
}
}
PackedStringArray WeaponBase::get_configuration_warnings() const {
PackedStringArray warnings{ super_type::get_configuration_warnings() };
if (this->anim == nullptr) {
warnings.push_back("Weapons need something to animate them.\nRemember to configure the 'anim' property on this weapon");
}
return warnings;
}
bool WeaponBase::is_animating() const {
return !get_anim()->get_current_animation().is_empty() || !get_anim()->get_queue().is_empty();
}
void WeaponBase::set_anim(AnimationPlayer *anim) {
this->anim = anim;
}
AnimationPlayer *WeaponBase::get_anim() const {
return this->anim;
}
PlayerInput *WeaponBase::get_input() const {
return this->input;
}
PlayerCamera *WeaponBase::get_camera() const {
return this->camera;
}
void WeaponBase::set_body(PlayerBody *body) {
this->body = 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;
}
}