113 lines
2.7 KiB
C++
113 lines
2.7 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);
|
|
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() {
|
|
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;
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|