Skip to content

Commit

Permalink
feat: handles gdscript additional methods on ability
Browse files Browse the repository at this point in the history
  • Loading branch information
OctoD committed Jan 15, 2024
1 parent ef94f67 commit 5d20de9
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
11 changes: 11 additions & 0 deletions src/system/ability/ability.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ using namespace godot;

namespace ggs
{
/// @brief The Ability class.
/// @details This class is used to represent an ability.
/// It contains all the information needed to use an ability.
/// You can set (in gdscript) the methods:
/// - can_activate(ability_owner, ability_container) -> bool;
/// - can_block(ability_owner, ability_container) -> bool;
/// - can_cancel(ability_owner, ability_container) -> bool;
/// - can_grant(ability_owner, ability_container) -> bool;
/// - on_activate(ability_owner, ability_container) -> void;
/// - on_block(ability_owner, ability_container) -> void;
/// - on_cancel(ability_owner, ability_container) -> void;
class Ability : public Resource
{
GDCLASS(Ability, Resource);
Expand Down
44 changes: 38 additions & 6 deletions src/system/ability/ability_container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,13 @@ void AbilityContainer::activate_by_name(const StringName &p_ability_name)

if (ability != nullptr && ability.is_valid() && can_activate_by_name(p_ability_name))
{
if (ability->has_method("activate"))
if (ability->has_method("can_activate"))
{
ability->call("activate", this, ability_owner);
if (!ability->call("can_activate", this, ability_owner))
{
emit_signal("ability_activation_fail", ability);
return;
}
}

tags.add_tags(ability->get_tags_added_on_activation());
Expand All @@ -205,6 +209,11 @@ void AbilityContainer::activate_by_name(const StringName &p_ability_name)
ability_queue->enqueue(ability_ptr);
emit_signal("ability_enqueued", ability);
}

if (ability->has_method("on_activate"))
{
ability->call("on_activate", this, ability_owner);
}
}
else
{
Expand Down Expand Up @@ -242,9 +251,13 @@ void AbilityContainer::block_by_name(const StringName &p_ability_name)

if (ability != nullptr && ability.is_valid() && can_block_by_name(p_ability_name))
{
if (ability->has_method("block"))
if (ability->has_method("can_block"))
{
ability->call("block", this, ability_owner);
if (!ability->call("can_block", this, ability_owner))
{
emit_signal("ability_block_fail", ability);
return;
}
}

emit_signal("ability_blocked", ability);
Expand All @@ -258,6 +271,11 @@ void AbilityContainer::block_by_name(const StringName &p_ability_name)
}

tags.remove_tags(ability->get_tags_removed_on_block());

if (ability_ptr->has_method("on_block"))
{
ability_ptr->call("on_block", this, ability_owner);
}
}
else
{
Expand Down Expand Up @@ -368,9 +386,13 @@ void AbilityContainer::cancel_by_name(const StringName &p_ability_name)

if (ability != nullptr && ability.is_valid() && can_cancel_by_name(p_ability_name))
{
if (ability->has_method("cancel"))
if (ability->has_method("can_cancel"))
{
ability->call("cancel", this, ability_owner);
if (!ability->call("can_cancel", this, ability_owner))
{
emit_signal("ability_cancellation_failed", ability);
return;
}
}

Ability *ability_ptr = ability.ptr();
Expand All @@ -384,6 +406,11 @@ void AbilityContainer::cancel_by_name(const StringName &p_ability_name)
emit_signal("ability_cancelled", ability);

tags.remove_tags(ability->get_tags_removed_on_cancel());

if (ability_ptr->has_method("on_cancel"))
{
ability_ptr->call("on_cancel", this, ability_owner);
}
}
else
{
Expand Down Expand Up @@ -451,6 +478,11 @@ void AbilityContainer::grant_ability(Ability *p_ability)

if (ability_grant != nullptr)
{
if (p_ability->has_method("can_grant") && !p_ability->call("can_grant", this, ability_owner))
{
return;
}

ability_grant->set_grant(true);
emit_signal("ability_granted", Ref<Ability>(p_ability));
tags.add_tags(ability_grant->get_ability()->get_tags_added_on_grant());
Expand Down

0 comments on commit 5d20de9

Please sign in to comment.