From d94b24f61794b7a9fa74fd632de850fdde6232b7 Mon Sep 17 00:00:00 2001 From: Sara Date: Thu, 8 Aug 2024 14:39:07 +0200 Subject: [PATCH] feat: added on_unconscious and movement speed to unit --- src/unit.cpp | 68 +++++++++++++++++++++++++++++++--------------------- src/unit.hpp | 19 ++++++++------- 2 files changed, 52 insertions(+), 35 deletions(-) diff --git a/src/unit.cpp b/src/unit.cpp index 93d219b..4d5c3a5 100644 --- a/src/unit.cpp +++ b/src/unit.cpp @@ -14,6 +14,7 @@ void Unit::_bind_methods() { GDSIGNAL("plan_failed"); GDSIGNAL("plan_interrupted"); GDPROPERTY_HINTED(configure_team, gd::Variant::INT, gd::PROPERTY_HINT_ENUM, UnitTeam::get_property_hint()); + GDPROPERTY_HINTED(movement_speed, gd::Variant::FLOAT, gd::PROPERTY_HINT_RANGE, "0.1,5.0"); GDFUNCTION(use_weapon); } @@ -87,44 +88,25 @@ void Unit::aim_at(gd::Node3D *target) { this->set_global_basis({x, z.cross(x), z}); } +void Unit::on_unconscious(Unit *damage_source) { + this->destroy_state(); + this->anim_player->stop(); + this->anim_player->play("death"); +} + void Unit::on_death(Unit *damage_source) { this->destroy_state(); this->anim_player->stop(); this->anim_player->play("death"); } -UnitWorldState *Unit::get_world_state() const { - return this->world_state; -} - -UnitTeam Unit::get_team() const { - return this->team; -} - -void Unit::set_configure_team(int team) { - this->team = team; -} - -int Unit::get_configure_team() const { - return this->team; -} - -bool Unit::has_plan() const { - return !this->current_plan.is_empty(); -} - -EntityHealth *Unit::get_entity_health() { - return this->health; -} - void Unit::on_velocity_computed(gd::Vector3 vel) { gd::Vector3 const pos{this->get_global_position()}; - gd::Vector3 const target_dir{(this->agent->get_next_path_position() - pos).normalized()}; if(vel.x == 0 && vel.z == 0) return; if(gd::Object::cast_to(this->state) == nullptr) return; - this->set_velocity(vel); + this->set_velocity(vel.normalized() * this->movement_speed); this->look_at(pos - gd::Vector3{vel.x, 0.f, vel.z}); } @@ -143,7 +125,7 @@ void Unit::state_finished() { void Unit::next_action() { // cannot perform actions while dead - if(this->health->get_injury_current() <= 0) + if(!this->health->is_conscious()) return; // destroy active state if relevant if(this->state != nullptr && !this->state->is_queued_for_deletion()) @@ -183,3 +165,35 @@ void Unit::set_goal_and_plan(gd::Ref goal) { } } } + +UnitWorldState *Unit::get_world_state() const { + return this->world_state; +} + +UnitTeam Unit::get_team() const { + return this->team; +} + +void Unit::set_configure_team(int team) { + this->team = team; +} + +int Unit::get_configure_team() const { + return this->team; +} + +bool Unit::has_plan() const { + return !this->current_plan.is_empty(); +} + +EntityHealth *Unit::get_entity_health() { + return this->health; +} + +void Unit::set_movement_speed(float speed) { + this->movement_speed = speed; +} + +float Unit::get_movement_speed() const { + return this->movement_speed; +} diff --git a/src/unit.hpp b/src/unit.hpp index d73b92c..cbcf4ce 100644 --- a/src/unit.hpp +++ b/src/unit.hpp @@ -35,15 +35,8 @@ public: virtual void use_weapon(); void aim_at(gd::Node3D *node); + void on_unconscious(Unit *damage_source); void on_death(Unit *damage_source); - - UnitWorldState *get_world_state() const; - - UnitTeam get_team() const; - void set_configure_team(int value); - int get_configure_team() const; - bool has_plan() const; - EntityHealth *get_entity_health(); private: void on_velocity_computed(gd::Vector3 vel); void destroy_state(); @@ -51,6 +44,15 @@ private: void next_action(); void replan_goal(); void set_goal_and_plan(gd::Ref goal); +public: // getter-setters + UnitWorldState *get_world_state() const; + UnitTeam get_team() const; + void set_configure_team(int value); + int get_configure_team() const; + bool has_plan() const; + EntityHealth *get_entity_health(); + void set_movement_speed(float speed); + float get_movement_speed() const; private: gd::Callable on_state_finished{callable_mp(this, &Unit::state_finished)}; gd::Callable on_plan_failed{callable_mp(this, &Unit::replan_goal)}; @@ -60,6 +62,7 @@ protected: goap::State *state{nullptr}; UnitTeam team{UnitTeam::Neutral}; + float movement_speed{2.f}; gd::NavigationAgent3D *agent{nullptr}; gd::AnimationPlayer *anim_player{nullptr};