feat: added on_unconscious and movement speed to unit

This commit is contained in:
Sara 2024-08-08 14:39:07 +02:00
parent 973ba952c2
commit d94b24f617
2 changed files with 52 additions and 35 deletions

View file

@ -14,6 +14,7 @@ void Unit::_bind_methods() {
GDSIGNAL("plan_failed"); GDSIGNAL("plan_failed");
GDSIGNAL("plan_interrupted"); GDSIGNAL("plan_interrupted");
GDPROPERTY_HINTED(configure_team, gd::Variant::INT, gd::PROPERTY_HINT_ENUM, UnitTeam::get_property_hint()); 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); GDFUNCTION(use_weapon);
} }
@ -87,44 +88,25 @@ void Unit::aim_at(gd::Node3D *target) {
this->set_global_basis({x, z.cross(x), z}); 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) { void Unit::on_death(Unit *damage_source) {
this->destroy_state(); this->destroy_state();
this->anim_player->stop(); this->anim_player->stop();
this->anim_player->play("death"); 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) { void Unit::on_velocity_computed(gd::Vector3 vel) {
gd::Vector3 const pos{this->get_global_position()}; 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) if(vel.x == 0 && vel.z == 0)
return; return;
if(gd::Object::cast_to<MoveTo>(this->state) == nullptr) if(gd::Object::cast_to<MoveTo>(this->state) == nullptr)
return; 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}); this->look_at(pos - gd::Vector3{vel.x, 0.f, vel.z});
} }
@ -143,7 +125,7 @@ void Unit::state_finished() {
void Unit::next_action() { void Unit::next_action() {
// cannot perform actions while dead // cannot perform actions while dead
if(this->health->get_injury_current() <= 0) if(!this->health->is_conscious())
return; return;
// destroy active state if relevant // destroy active state if relevant
if(this->state != nullptr && !this->state->is_queued_for_deletion()) if(this->state != nullptr && !this->state->is_queued_for_deletion())
@ -183,3 +165,35 @@ void Unit::set_goal_and_plan(gd::Ref<goap::Goal> 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;
}

View file

@ -35,15 +35,8 @@ public:
virtual void use_weapon(); virtual void use_weapon();
void aim_at(gd::Node3D *node); void aim_at(gd::Node3D *node);
void on_unconscious(Unit *damage_source);
void on_death(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: private:
void on_velocity_computed(gd::Vector3 vel); void on_velocity_computed(gd::Vector3 vel);
void destroy_state(); void destroy_state();
@ -51,6 +44,15 @@ private:
void next_action(); void next_action();
void replan_goal(); void replan_goal();
void set_goal_and_plan(gd::Ref<goap::Goal> goal); void set_goal_and_plan(gd::Ref<goap::Goal> 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: private:
gd::Callable on_state_finished{callable_mp(this, &Unit::state_finished)}; gd::Callable on_state_finished{callable_mp(this, &Unit::state_finished)};
gd::Callable on_plan_failed{callable_mp(this, &Unit::replan_goal)}; gd::Callable on_plan_failed{callable_mp(this, &Unit::replan_goal)};
@ -60,6 +62,7 @@ protected:
goap::State *state{nullptr}; goap::State *state{nullptr};
UnitTeam team{UnitTeam::Neutral}; UnitTeam team{UnitTeam::Neutral};
float movement_speed{2.f};
gd::NavigationAgent3D *agent{nullptr}; gd::NavigationAgent3D *agent{nullptr};
gd::AnimationPlayer *anim_player{nullptr}; gd::AnimationPlayer *anim_player{nullptr};