feat: implemented utility item locks

This commit is contained in:
Sara 2024-09-09 11:13:59 +02:00
parent 428d42b672
commit c4d87f0f8a
14 changed files with 116 additions and 21 deletions

View file

@ -45,6 +45,7 @@ void initialize_gdextension_types(gd::ModuleInitializationLevel p_level)
goap::ActionDB::register_action<GetInRange>();
goap::ActionDB::register_action<TankSelfHeal>();
goap::ActionDB::register_action<TakeCover>();
goap::ActionDB::register_action<ActivateLock>();
// same for items,
// make sure ItemDB::get_enum_hint is fully populated out before _bind_methods.

View file

@ -2,6 +2,7 @@
#include "nav_marker.hpp"
#include "nav_room.hpp"
#include "rts_states.hpp"
#include "utility_lock.hpp"
#include "goap/actor_world_state.hpp"
#include "goap/state.hpp"
#include <godot_cpp/core/memory.hpp>
@ -19,11 +20,10 @@ goap::State *MoveToTarget::get_apply_state(goap::ActorWorldState *context) const
if(target == nullptr) {
gd::UtilityFunctions::push_warning("Failed to get target node of ", context->get_path());
return nullptr;
} else {
MoveTo *state{this->create_state<MoveTo>()};
state->target_node = target;
return state;
}
MoveTo *state{this->create_state<MoveTo>()};
state->target_node = target;
return state;
}
UseWeapon::UseWeapon()
@ -140,6 +140,40 @@ float TakeCover::score_cover_marker(class NavMarker* marker, const gd::Vector3&
float const context_distance{marker_position.distance_to(context_position)};
// score is a weighted comparison between distances to the target and context
// marker distance from context grows faster than marker distance from target
return (target_distance) - (context_distance);
return target_distance - context_distance;
}
ActivateLock::ActivateLock() {
this->required.insert("is_at_target", true);
this->effects.insert("is_target_activated", true);
}
bool ActivateLock::procedural_is_possible(goap::ActorWorldState *context) const {
Unit *context_unit{gd::Object::cast_to<Unit>(context->get_parent())};
if(context_unit == nullptr)
return false;
UnitWorldState *unit_context{context_unit->get_world_state()};
if(unit_context == nullptr)
return false;
UtilityLock *lock{gd::Object::cast_to<UtilityLock>(unit_context->get_target_node())};
if(lock == nullptr)
return false;
return lock->can_use(context_unit->get_node<Inventory>("%Inventory")->get_utility(), context_unit);
}
goap::State *ActivateLock::get_apply_state(goap::ActorWorldState *context) const {
UnitWorldState *unit_context{gd::Object::cast_to<UnitWorldState>(context)};
if(unit_context == nullptr) {
gd::UtilityFunctions::push_error("ActivateLock: Context '", context->get_path(), "' is not a UnitWorldState");
return nullptr;
}
UtilityLock *lock{gd::Object::cast_to<UtilityLock>(unit_context->get_target_node())};
if(lock == nullptr) {
gd::UtilityFunctions::push_error("ActivateLock: Target for '", context->get_path(), "' is not a UtilityLock");
}
Activate *activate = this->create_state<Activate>();
activate->lock = lock;
if(Inventory *inventory{unit_context->get_node<Inventory>("%Inventory")})
activate->using_item = inventory->get_utility();
return activate;
}

View file

@ -50,4 +50,12 @@ private:
float score_cover_marker(class NavMarker *marker, gd::Vector3 const &target_position, gd::Vector3 const &context_position) const;
};
class ActivateLock : public goap::Action {
GOAP_ACTION(ActivateLock);
public:
ActivateLock();
virtual bool procedural_is_possible(goap::ActorWorldState *context) const override;
virtual goap::State *get_apply_state(goap::ActorWorldState *context) const override;
};
#endif // !RTS_ACTIONS_HPP

View file

@ -18,6 +18,9 @@ void RTSPlayer::_bind_methods() {
void RTSPlayer::_ready() {
this->camera = this->get_node<gd::Camera3D>("Camera3D");
this->local_zoom_direction = this->camera->get_position().normalized();
this->current_zoom_level = this->max_zoom_level = this->camera->get_position().length();
this->set_zoom_level(this->current_zoom_level);
}
void RTSPlayer::_process(double delta_time) {
@ -41,6 +44,7 @@ void RTSPlayer::setup_player_input(utils::PlayerInput *input) {
input->listen_to("rotate_left", "rotate_right", callable_mp(this, &RTSPlayer::on_rotate_horizontal));
input->listen_to("_mouse_left", "_mouse_right", callable_mp(this, &RTSPlayer::on_mouse_horizontal));
input->listen_to("_mouse_down", "_mouse_up", callable_mp(this, &RTSPlayer::on_mouse_vertical));
input->listen_to("zoom_in", "zoom_out", callable_mp(this, &RTSPlayer::on_zoom));
#ifdef DEBUG_ENABLED
input->listen_to("DEBUG_toggle_debug", callable_mp(this, &RTSPlayer::DEBUG_enable_debug));
#endif
@ -139,6 +143,14 @@ void RTSPlayer::select_unit(Unit *unit) {
unit->connect("tree_exited", this->on_selected_unit_destroyed);
}
void RTSPlayer::set_zoom_level(float level) {
this->current_zoom_level = gd::Math::clamp(level, this->min_zoom_level, this->max_zoom_level);
this->camera->set_position(this->local_zoom_direction * this->current_zoom_level);
gd::Vector3 const forward_vector{(this->camera->get_global_position() - this->get_global_position()).normalized()};
gd::Vector3 const left_vector{gd::Vector3{0.f, 1.f, 0.f}.cross(forward_vector)};
this->camera->set_global_basis({left_vector, forward_vector.cross(left_vector), forward_vector});
}
void RTSPlayer::on_mouse_horizontal(gd::Ref<gd::InputEvent>, float value) {
if(this->mmb_down)
this->camera_mouse_motion.x = value;
@ -182,6 +194,9 @@ void RTSPlayer::on_lclick(gd::Ref<gd::InputEvent>, float value) {
void RTSPlayer::on_mclick(gd::Ref<gd::InputEvent>, float value) {
this->mmb_down = value != 0.f;
}
void RTSPlayer::on_zoom(gd::Ref<gd::InputEvent>, float value) {
this->set_zoom_level(this->current_zoom_level + value * this->camera_zoom_speed);
}
gd::Vector3 RTSPlayer::get_forward_direction() const {
gd::Vector3 forward = this->get_global_basis().get_column(2);

View file

@ -40,6 +40,7 @@ private:
void spawn_movement_marker(gd::Vector3 at);
void clear_selected_unit();
void select_unit(Unit *unit);
void set_zoom_level(float level);
// input functions
void on_mouse_horizontal(gd::Ref<gd::InputEvent>, float value);
@ -50,12 +51,15 @@ private:
void on_lclick(gd::Ref<gd::InputEvent>, float value);
void on_rclick(gd::Ref<gd::InputEvent>, float value);
void on_mclick(gd::Ref<gd::InputEvent>, float value);
void on_zoom(gd::Ref<gd::InputEvent>, float value);
// setters & getters
gd::Vector3 get_forward_direction() const;
gd::Vector3 get_left_direction() const;
void set_ground_marker_scene(gd::Ref<gd::PackedScene> scene);
gd::Ref<gd::PackedScene> get_ground_marker_scene() const;
private:
gd::Callable const on_selected_unit_destroyed{callable_mp(this, &RTSPlayer::clear_selected_unit)};
private:
Unit* selected_unit{nullptr};
@ -75,13 +79,17 @@ private:
gd::Vector3 cursor_camera_normal{0.f, 0.f, 0.f};
gd::Camera3D *camera{nullptr};
gd::Ref<gd::PackedScene> ground_marker_scene{nullptr};
gd::Callable const on_selected_unit_destroyed{callable_mp(this, &RTSPlayer::clear_selected_unit)};
gd::Vector3 local_zoom_direction{0.f, 0.f, 0.f};
float current_zoom_level{1.f};
float max_zoom_level{1.f};
float const min_zoom_level{3.f};
float const camera_keys_speed{10.f};
float const camera_keys_rotation_speed{1.f};
float const camera_mouse_speed{0.01f};
float const camera_mouse_rotation_speed{-0.003f};
double const time_to_held{0.1};
float const camera_zoom_speed{1.f};
#ifdef DEBUG_ENABLED
private:
bool DEBUG_debug_enabled{false};

View file

@ -82,10 +82,14 @@ void Activate::_ready() {
return;
}
// start activating the lock
this->lock->begin_activate(this->using_item, this->parent_unit);
if(!this->lock->begin_activate(this->using_item, this->parent_unit)) {
this->end_state();
return;
}
this->animation = this->lock->get_animation_name();
// play activate animation
this->anim = this->parent_unit->get_node<gd::AnimationPlayer>("%AnimationPlayer");
if(!this->anim->has_animation(this->animation)) {
if(this->anim == nullptr || !this->anim->has_animation(this->animation)) {
this->end_state();
return;
}

View file

@ -40,9 +40,9 @@ public:
virtual void _process(double) override;
virtual void _end_state() override;
UtilityLock *lock{nullptr};
gd::String animation{};
private:
Item const *using_item{nullptr};
private:
gd::String animation{};
gd::AnimationPlayer *anim{nullptr};
Unit *parent_unit{nullptr};
};

View file

@ -37,7 +37,6 @@ public:
bool get_is_health_safe() const;
gd::Vector3 get_parent_global_position() const;
gd::Vector3 get_target_global_position() const;
void set_target_node(gd::Node3D *node);
gd::Node3D *get_target_node() const;
private:

View file

@ -1,10 +1,12 @@
#include "utility_lock.hpp"
#include "item_db.hpp"
#include "unit.hpp"
#include "utils/godot_macros.hpp"
void UtilityLock::_bind_methods() {
#define CLASSNAME UtilityLock
GDPROPERTY_HINTED(allowed_items, gd::Variant::ARRAY, gd::PROPERTY_HINT_ARRAY_TYPE, ItemDB::get_array_hint());
GDPROPERTY(animation_name, gd::Variant::STRING);
GDSIGNAL("begin_activate", gd::PropertyInfo(gd::Variant::INT, "with_item"), gd::PropertyInfo(gd::Variant::OBJECT, "by_unit", gd::PROPERTY_HINT_NODE_TYPE, "Unit"));
GDSIGNAL("finish_activate", gd::PropertyInfo(gd::Variant::INT, "with_item"), gd::PropertyInfo(gd::Variant::OBJECT, "by_unit", gd::PROPERTY_HINT_NODE_TYPE, "Unit"));
GDSIGNAL("interrupt_activate");
@ -60,3 +62,11 @@ gd::Array UtilityLock::get_allowed_items() const {
array.push_back(item == nullptr ? 0 : item->get_id());
return array;
}
void UtilityLock::set_animation_name(gd::String animation) {
this->animation_name = animation;
}
gd::String UtilityLock::get_animation_name() const {
return this->animation_name;
}

View file

@ -29,9 +29,12 @@ public:
void set_allowed_items(gd::Array array);
gd::Array get_allowed_items() const;
void set_animation_name(gd::String animation);
gd::String get_animation_name() const;
private:
gd::HashSet<Item const *> allowed_items{};
Unit *user{nullptr};
gd::String animation_name{};
ActivationState activation_state{ActivationState::Inactive};
};