Skip to content

Commit

Permalink
chore: fakes virtual methods.
Browse files Browse the repository at this point in the history
This is absolute crap, I hope they will add a proper support for gdscript virtuals
  • Loading branch information
OctoD committed Jan 11, 2024
1 parent e2d34aa commit 2fae24c
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 23 deletions.
23 changes: 19 additions & 4 deletions project/demos/attributes_test_ui/main.gd
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,59 @@ class BuffDex extends AttributeEffect:
application_type = AttributeEffect.ADD_BUFF
affected_attribute = "attributes.dexterity"


class BuffStr extends AttributeEffect:
func _init() -> void:
affected_amount = 5
application_type = AttributeEffect.ADD_BUFF
affected_attribute = "attributes.strength"


class SpeedDecrease extends AttributeEffect:
func _init() -> void:
affected_amount = 1
application_type = AttributeEffect.SUBTRACT_VALUE
affected_attribute = "attributes.speed"


class SpeedIncrease extends AttributeEffect:
func _init() -> void:
affected_amount = 5
application_type = AttributeEffect.ADD_BUFF
affected_attribute = "attributes.speed"


class StaminaDecrease extends AttributeEffect:
func _init() -> void:
affected_amount = 5
application_type = AttributeEffect.SUBTRACT_VALUE
affected_attribute = "attributes.stamina"


class StaminaIncrease extends AttributeEffect:
func _init() -> void:
affected_amount = 5
application_type = AttributeEffect.ADD_VALUE_OR_BUFF
affected_attribute = "attributes.stamina"


class DebuffCondition extends AttributeEffectCondition:
func get_break_type(attribute_effect: AttributeEffect, attribute_container: AttributeContainer) -> AttributeEffectCondition.BreakType:
var attribute = attribute_container.get_attribute(attribute_effect.affected_attribute)

if attribute != null and attribute.buff == 0:
return AttributeEffectCondition.BREAK

return AttributeEffectCondition.NO_BREAK


class DebuffDex extends AttributeEffect:
func _init() -> void:
affected_amount = 1
application_type = AttributeEffect.SUBTRACT_VALUE
application_type = AttributeEffect.SUBTRACT_BUFF
affected_attribute = "attributes.dexterity"
life_cycle = AttributeEffect.INFINITE_TIME_BASED

func _are_conditions_met(x, y) -> bool:
return true
conditions.append(DebuffCondition.new())


func make_gameplay_effect(attribute_effect: AttributeEffect) -> GameplayEffect:
Expand All @@ -61,6 +75,7 @@ func make_gameplay_effect(attribute_effect: AttributeEffect) -> GameplayEffect:

func _ready() -> void:
print_label()
add_child((make_gameplay_effect(DebuffDex.new())))


func _input(event: InputEvent) -> void:
Expand Down
2 changes: 1 addition & 1 deletion src/system/attribute/attribute_container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ bool AttributeContainer::are_attribute_conditions_met(Ref<AttributeEffect> &attr
Variant condition = attribute_effect->get_conditions()[i];
Ref<AttributeEffectCondition> attribute_effect_condition = Ref<AttributeEffectCondition>(condition);

if (!attribute_effect_condition->should_apply_effect(attribute_effect.ptr(), this))
if (!attribute_effect_condition->_should_apply_effect(attribute_effect.ptr(), this))
{
return false;
}
Expand Down
17 changes: 12 additions & 5 deletions src/system/attribute/attribute_effect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ using namespace ggs;
void AttributeEffect::_bind_methods()
{
/// binds methods
/// TODO: remember to change it once godot-cpp will allow virtual methods for gdscript
ClassDB::bind_method(D_METHOD("_calculate_affected_amount"), &AttributeEffect::_calculate_affected_amount);
///
ClassDB::bind_method(D_METHOD("are_conditions_met", "attribute_container"), &AttributeEffect::are_conditions_met);
ClassDB::bind_method(D_METHOD("calculate_affected_amount"), &AttributeEffect::calculate_affected_amount);
ClassDB::bind_method(D_METHOD("get_affected_amount"), &AttributeEffect::get_affected_amount);
ClassDB::bind_method(D_METHOD("get_affected_attribute"), &AttributeEffect::get_affected_attribute);
ClassDB::bind_method(D_METHOD("get_application_type"), &AttributeEffect::get_application_type);
Expand Down Expand Up @@ -79,7 +81,7 @@ bool AttributeEffect::are_conditions_met(AttributeContainer *p_attribute_contain

if (condition != nullptr)
{
if (!condition->should_apply_effect(this, p_attribute_container))
if (!condition->_should_apply_effect(this, p_attribute_container))
{
return false;
}
Expand All @@ -89,8 +91,13 @@ bool AttributeEffect::are_conditions_met(AttributeContainer *p_attribute_contain
return true;
}

float AttributeEffect::calculate_affected_amount()
float AttributeEffect::_calculate_affected_amount()
{
if (has_method("calculate_affected_amount"))
{
return call("calculate_affected_amount");
}

return affected_amount;
}

Expand Down Expand Up @@ -126,7 +133,7 @@ PackedFloat32Array AttributeEffect::get_applications() const

float AttributeEffect::get_affected_amount()
{
return calculate_affected_amount();
return _calculate_affected_amount();
}

StringName AttributeEffect::get_affected_attribute() const
Expand Down Expand Up @@ -205,7 +212,7 @@ int AttributeEffect::get_break(AttributeContainer *p_attribute_container)

if (condition != nullptr)
{
AttributeEffectCondition::BreakType break_type = condition->get_break_type(this, p_attribute_container);
AttributeEffectCondition::BreakType break_type = condition->_get_break_type(this, p_attribute_container);

if (break_type != AttributeEffectCondition::BreakType::NO_BREAK)
{
Expand Down
4 changes: 2 additions & 2 deletions src/system/attribute/attribute_effect.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ namespace ggs
/// @brief Returns whether or not this attribute effect should be removed from the container.
/// @param p_attribute_container The attribute container to check.
/// @return Returns true if the attribute effect can be applied, false otherwise.
virtual bool are_conditions_met(AttributeContainer *p_attribute_container);
bool are_conditions_met(AttributeContainer *p_attribute_container);
/// @brief Calculates the affected amount of the attribute effect.
virtual float calculate_affected_amount();
virtual float _calculate_affected_amount();
/// @brief Returns the applications of this attribute effect. Each item in the array is how the attribute effect will be applied.
/// @return
PackedFloat32Array get_applications() const;
Expand Down
34 changes: 25 additions & 9 deletions src/system/attribute/attribute_effect_condition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ using namespace ggs;

void AttributeEffectCondition::_bind_methods()
{
ClassDB::bind_method(D_METHOD("get_break_type", "attribute_effect", "attribute_container"), &AttributeEffectCondition::get_break_type);
ClassDB::bind_method(D_METHOD("should_apply_effect", "attribute_effect", "attribute_container"), &AttributeEffectCondition::should_apply_effect);
/// TODO: these will stay commented out until godot-cpp supports virtual methods properly.
// ClassDB::bind_method(D_METHOD("get_break_type", "attribute_effect", "attribute_container"), &AttributeEffectCondition::get_break_type);
// ClassDB::bind_method(D_METHOD("should_apply_effect", "attribute_effect", "attribute_container"), &AttributeEffectCondition::should_apply_effect);

BIND_ENUM_CONSTANT(NO_BREAK);
BIND_ENUM_CONSTANT(BREAK);
Expand All @@ -14,28 +15,43 @@ void AttributeEffectCondition::_bind_methods()
BIND_ENUM_CONSTANT(BREAK_REMOVE_ANY_ATTRIBUTE_EFFECT);
}

AttributeEffectCondition::BreakType AttributeEffectCondition::get_break_type(AttributeEffect *p_attribute_effect, AttributeContainer *p_attribute_container)
AttributeEffectCondition::BreakType AttributeEffectCondition::_get_break_type(AttributeEffect *p_attribute_effect, AttributeContainer *p_attribute_container)
{
if (p_attribute_container == nullptr || p_attribute_effect == nullptr)
{
{
return BreakType::BREAK;
}

if (!p_attribute_container->has_attribute(p_attribute_effect->get_affected_attribute()))
{
return BreakType::BREAK_REMOVE_ATTRIBUTE_EFFECT;
}

return BreakType::NO_BREAK;

if (has_method("get_break_type"))
{
int i_return_value = call("get_break_type", p_attribute_effect, p_attribute_container);
return (BreakType)i_return_value;
}

return BreakType::NO_BREAK;
}

bool AttributeEffectCondition::should_apply_effect(AttributeEffect *p_attribute_effect, AttributeContainer *p_attribute_container)
bool AttributeEffectCondition::_should_apply_effect(AttributeEffect *p_attribute_effect, AttributeContainer *p_attribute_container)
{
if (p_attribute_container == nullptr || p_attribute_effect == nullptr)
{
return false;
}

return p_attribute_container->has_attribute(p_attribute_effect->get_affected_attribute());
}
if (!p_attribute_container->has_attribute(p_attribute_effect->get_affected_attribute()))
{
return false;
}

if (has_method("should_apply_effect"))
{
return call("should_apply_effect", p_attribute_effect, p_attribute_container);
}

return true;
}
4 changes: 2 additions & 2 deletions src/system/attribute/attribute_effect_condition.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ namespace ggs
/// @param p_attribute_effect The attribute effect to apply.
/// @param p_attribute_container The attribute container to apply the effect to.
/// @return The break type of this attribute effect condition.
virtual AttributeEffectCondition::BreakType get_break_type(AttributeEffect *p_attribute_effect, AttributeContainer *p_attribute_container);
virtual AttributeEffectCondition::BreakType _get_break_type(AttributeEffect *p_attribute_effect, AttributeContainer *p_attribute_container);
/// @brief Returns whether or not this attribute effect condition is met.
/// @param p_attribute_effect The attribute effect to apply.
/// @param p_attribute_container The attribute container to apply the effect to.
/// @return
virtual bool should_apply_effect(AttributeEffect *p_attribute_effect, AttributeContainer *p_attribute_container);
virtual bool _should_apply_effect(AttributeEffect *p_attribute_effect, AttributeContainer *p_attribute_container);
};
}

Expand Down

0 comments on commit 2fae24c

Please sign in to comment.