From 056791b3137e07396cf20ce28108e37c759db767 Mon Sep 17 00:00:00 2001 From: Relintai Date: Tue, 29 Oct 2019 18:12:15 +0100 Subject: [PATCH] Improved the design a bit, implemented serializing SpellDamageInfo, and SpellHealInfo. Also fixed a crash. --- data/aura.cpp | 8 ++- data/spell.cpp | 10 ++-- entities/auras/aura_data.cpp | 14 ++++- entities/auras/aura_data.h | 1 + entities/entity.cpp | 85 ++++++++++++++++++++++++------ entities/entity.h | 6 +++ infos/spell_cast_info.cpp | 40 ++++++++------ infos/spell_cast_info.h | 6 ++- pipelines/spell_damage_info.cpp | 93 +++++++++++++++++++++++++++------ pipelines/spell_damage_info.h | 29 ++++++---- pipelines/spell_heal_info.cpp | 75 +++++++++++++++++++++++--- pipelines/spell_heal_info.h | 24 ++++++--- 12 files changed, 304 insertions(+), 87 deletions(-) diff --git a/data/aura.cpp b/data/aura.cpp index 4621462..0359675 100644 --- a/data/aura.cpp +++ b/data/aura.cpp @@ -912,7 +912,7 @@ void Aura::setup_aura_data(Ref data, Ref info) { } void Aura::_setup_aura_data(Ref data, Ref info) { - ERR_FAIL_COND(info->get_caster() == NULL); + ERR_FAIL_COND(!ObjectDB::instance_validate(info->get_caster())); data->set_aura(Ref(this)); data->set_aura_id(get_id()); @@ -975,7 +975,7 @@ void Aura::_calculate_initial_damage(Ref aura_data, Ref } void Aura::_handle_aura_damage(Ref aura_data, Ref data) { - ERR_FAIL_COND(data->get_dealer() == NULL); + ERR_FAIL_COND(!ObjectDB::instance_validate(data->get_dealer())); data->set_damage(aura_data->get_damage()); @@ -1035,8 +1035,6 @@ void Aura::_sapply(Ref info) { Ref ad(memnew(AuraData())); setup_aura_data(ad, info); - - } void Aura::_sdeapply(Ref info) { @@ -1089,7 +1087,7 @@ void Aura::_supdate(Ref aura, float delta) { if (aura->get_damage() != 0) { Ref dpd = Ref(memnew(SpellDamageInfo())); - dpd->set_aura_damage_source(aura); + dpd->set_aura_damage_source(Ref(this)); dpd->set_dealer(aura->get_caster()); dpd->set_receiver(aura->get_owner()); diff --git a/data/spell.cpp b/data/spell.cpp index 71a9461..46c83dd 100644 --- a/data/spell.cpp +++ b/data/spell.cpp @@ -697,9 +697,9 @@ void Spell::handle_spell_damage(Ref data) { void Spell::_sstart_casting(Ref info) { ERR_FAIL_COND(!info.is_valid() || info->get_spell() == NULL); - Ref spell = info->get_spell(); + //Ref spell = info->get_spell(); - if (spell->get_needs_target() || spell->get_has_damage()) { + if (get_needs_target() || get_has_damage()) { if (!info->get_target()) { //print_error("no target, return"); @@ -707,7 +707,7 @@ void Spell::_sstart_casting(Ref info) { } } - if (spell->get_has_cast_time()) { + if (get_has_cast_time()) { //can cast info->get_caster()->son_before_cast(info); @@ -717,10 +717,10 @@ void Spell::_sstart_casting(Ref info) { info->get_caster()->sstart_casting(info); } else { - if (spell->get_has_damage()) { + if (get_has_damage()) { Ref dpd = Ref(memnew(SpellDamageInfo())); - dpd->set_spell_damage_source(spell); + dpd->set_spell_damage_source(Ref(this)); dpd->set_dealer(info->get_caster()); dpd->set_receiver(info->get_target()); diff --git a/entities/auras/aura_data.cpp b/entities/auras/aura_data.cpp index 8996f61..a9ef3a1 100644 --- a/entities/auras/aura_data.cpp +++ b/entities/auras/aura_data.cpp @@ -124,10 +124,10 @@ void AuraData::set_spell_scale(float value) { } Ref AuraData::get_aura() { - if (_aura == NULL) { + //if (_aura == NULL) { //TODO fix! //_aura = Auras::getInstance()->GetData(get_aura_id()); - } + //} return _aura; } @@ -228,6 +228,16 @@ void AuraData::set_slow(float value) { _slow = value; } +void AuraData::resolve_references(Node *owner) { + ERR_FAIL_COND(!ObjectDB::instance_validate(owner)); + ERR_FAIL_COND(!owner->is_inside_tree()); + + _owner = Object::cast_to(owner); + + if (owner->is_inside_tree()) { + _caster = Object::cast_to(owner->get_node_or_null(_caster_path)); + } +} Dictionary AuraData::to_dict() { return call("_to_dict"); } diff --git a/entities/auras/aura_data.h b/entities/auras/aura_data.h index 48a5b4d..103e27b 100644 --- a/entities/auras/aura_data.h +++ b/entities/auras/aura_data.h @@ -70,6 +70,7 @@ public: float get_slow(); void set_slow(float value); + void resolve_references(Node *owner); Dictionary to_dict(); void from_dict(const Dictionary &dict); Dictionary _to_dict(); diff --git a/entities/entity.cpp b/entities/entity.cpp index 314c766..c71d84c 100644 --- a/entities/entity.cpp +++ b/entities/entity.cpp @@ -614,8 +614,7 @@ void Entity::_from_dict(const Dictionary &dict) { r->from_dict(auras.get(String::num(i), Dictionary())); r->set_owner(this); - //TODO hack - r->set_caster(this); + r->resolve_references(this); _s_auras.push_back(r); //_c_auras.push_back(r); @@ -1308,9 +1307,8 @@ void Entity::stake_damage(Ref info) { son_before_damage_hit(info); if (info->get_immune()) { - VRPC(con_damage_dealt, info); + VRPCOBJ(cdamage_dealt_rpc, JSON::print(info->to_dict()), con_damage_dealt, info); - //VRPCOBJ(cadd_aura_rpc, JSON::print(ad->to_dict()), cadd_aura, ad); return; } @@ -1339,7 +1337,7 @@ void Entity::stake_damage(Ref info) { emit_signal("son_damage_received", this, info); //send an event to client - VRPC(con_damage_dealt, info); + VRPCOBJ(cdamage_dealt_rpc, JSON::print(info->to_dict()), con_damage_dealt, info); if (get_health()->gets_current() <= 0) { sdie(); @@ -1360,7 +1358,7 @@ void Entity::sdeal_damage_to(Ref info) { son_dealt_damage(info); //send an event to client - VRPC(con_dealt_damage, info); + VRPCOBJ(cdealt_damage_rpc, JSON::print(info->to_dict()), con_dealt_damage, info); //signal emit_signal("son_damage_received", this, info); @@ -1382,7 +1380,7 @@ void Entity::stake_heal(Ref info) { son_before_heal_hit(info); if (info->get_immune()) { - VRPC(con_heal_dealt, info); + VRPCOBJ(cheal_dealt_rpc, JSON::print(info->to_dict()), con_heal_dealt, info); return; } @@ -1402,7 +1400,7 @@ void Entity::stake_heal(Ref info) { get_health()->sets_current(h); //send an event to client - VRPC(con_heal_dealt, info); + VRPCOBJ(cheal_dealt_rpc, JSON::print(info->to_dict()), con_heal_dealt, info); //signal emit_signal("son_heal_received", this, info); @@ -1421,6 +1419,44 @@ void Entity::sdeal_heal_to(Ref info) { sapply_passives_heal_deal(info); info->get_receiver()->stake_heal(info); son_heal_dealt(info); + + VRPCOBJ(cdealt_heal_rpc, JSON::print(info->to_dict()), con_dealt_heal, info); + + emit_signal("son_heal_dealt", this, info); +} + +//Damage, Heal RPCs +void Entity::cdamage_dealt_rpc(String data) { + Ref info; + info.instance(); + info->from_dict(data_as_dict(data)); + info->resolve_references(this); + + con_damage_dealt(info); +} +void Entity::cdealt_damage_rpc(String data) { + Ref info; + info.instance(); + info->from_dict(data_as_dict(data)); + info->resolve_references(this); + + con_dealt_damage(info); +} +void Entity::cheal_dealt_rpc(String data) { + Ref info; + info.instance(); + info->from_dict(data_as_dict(data)); + info->resolve_references(this); + + con_heal_dealt(info); +} +void Entity::cdealt_heal_rpc(String data) { + Ref info; + info.instance(); + info->from_dict(data_as_dict(data)); + info->resolve_references(this); + + con_dealt_heal(info); } //Interactions @@ -2239,7 +2275,7 @@ void Entity::cadd_aura_rpc(String data) { aura.instance(); aura->from_dict(data_as_dict(data)); aura->set_owner(this); - //aura->set_caster_bind(get_node_or_null(aura->get_caster_path())); + aura->resolve_references(this); cadd_aura(aura); } @@ -2249,7 +2285,7 @@ void Entity::cremove_aura_rpc(String data) { aura.instance(); aura->from_dict(data_as_dict(data)); aura->set_owner(this); - //aura->set_caster_bind(get_node_or_null(aura->get_caster_path())); + aura->resolve_references(this); cremove_aura(aura); } @@ -2259,7 +2295,7 @@ void Entity::cremove_aura_exact_rpc(String data) { aura.instance(); aura->from_dict(data_as_dict(data)); aura->set_owner(this); - //aura->set_caster_bind(get_node_or_null(aura->get_caster_path())); + aura->resolve_references(this); cremove_aura_exact(aura); } @@ -2269,7 +2305,7 @@ void Entity::cremove_aura_expired_rpc(String data) { aura.instance(); aura->from_dict(data_as_dict(data)); aura->set_owner(this); - //aura->set_caster_bind(get_node_or_null(aura->get_caster_path())); + aura->resolve_references(this); cremove_aura_expired(aura); } @@ -2279,7 +2315,7 @@ void Entity::cremove_aura_dispelled_rpc(String data) { aura.instance(); aura->from_dict(data_as_dict(data)); aura->set_owner(this); - //aura->set_caster_bind(get_node_or_null(aura->get_caster_path())); + aura->resolve_references(this); cremove_aura_dispelled(aura); } @@ -2289,7 +2325,7 @@ void Entity::caura_refreshed_rpc(String data) { aura.instance(); aura->from_dict(data_as_dict(data)); aura->set_owner(this); - //aura->set_caster_bind(get_node_or_null(aura->get_caster_path())); + aura->resolve_references(this); caura_refreshed(aura); } @@ -2892,7 +2928,8 @@ void Entity::sinterrupt_cast() { void Entity::cstart_casting_rpc(String data) { Ref info; info.instance(); - info->from_dict(this, data_as_dict(data)); + info->from_dict(data_as_dict(data)); + info->resolve_references(this); cstart_casting(info); } @@ -2941,7 +2978,8 @@ void Entity::sspell_cast_success(Ref info) { void Entity::cspell_cast_success_rpc(String data) { Ref info; info.instance(); - info->from_dict(this, data_as_dict(data)); + info->from_dict(data_as_dict(data)); + info->resolve_references(this); cspell_cast_success(info); } @@ -4444,6 +4482,13 @@ Entity::Entity() { SET_RPC_REMOTE("stake_heal"); SET_RPC_REMOTE("sdeal_heal_to"); + //Damage, Heal RPCs + + SET_RPC_REMOTE("cdamage_dealt_rpc"); + SET_RPC_REMOTE("cdealt_damage_rpc"); + SET_RPC_REMOTE("cheal_dealt_rpc"); + SET_RPC_REMOTE("cdealt_heal_rpc"); + //Interactions SET_RPC_REMOTE("sinteract"); @@ -4633,8 +4678,10 @@ void Entity::_bind_methods() { ADD_SIGNAL(MethodInfo("son_heal_received", PropertyInfo(Variant::OBJECT, "entity", PROPERTY_HINT_RESOURCE_TYPE, "Entity"), PropertyInfo(Variant::OBJECT, "info", PROPERTY_HINT_RESOURCE_TYPE, "SpellHealInfo"))); ADD_SIGNAL(MethodInfo("con_heal_received", PropertyInfo(Variant::OBJECT, "entity", PROPERTY_HINT_RESOURCE_TYPE, "Entity"), PropertyInfo(Variant::OBJECT, "info", PROPERTY_HINT_RESOURCE_TYPE, "SpellHealInfo"))); + ADD_SIGNAL(MethodInfo("son_dealt_heal", PropertyInfo(Variant::OBJECT, "entity", PROPERTY_HINT_RESOURCE_TYPE, "Entity"), PropertyInfo(Variant::OBJECT, "info", PROPERTY_HINT_RESOURCE_TYPE, "SpellHealInfo"))); ADD_SIGNAL(MethodInfo("con_dealt_heal", PropertyInfo(Variant::OBJECT, "entity", PROPERTY_HINT_RESOURCE_TYPE, "Entity"), PropertyInfo(Variant::OBJECT, "info", PROPERTY_HINT_RESOURCE_TYPE, "SpellHealInfo"))); + ADD_SIGNAL(MethodInfo("son_heal_dealt", PropertyInfo(Variant::OBJECT, "entity", PROPERTY_HINT_RESOURCE_TYPE, "Entity"), PropertyInfo(Variant::OBJECT, "info", PROPERTY_HINT_RESOURCE_TYPE, "SpellHealInfo"))); ADD_SIGNAL(MethodInfo("con_heal_dealt", PropertyInfo(Variant::OBJECT, "entity", PROPERTY_HINT_RESOURCE_TYPE, "Entity"), PropertyInfo(Variant::OBJECT, "info", PROPERTY_HINT_RESOURCE_TYPE, "SpellHealInfo"))); ADD_SIGNAL(MethodInfo("sentity_data_changed", PropertyInfo(Variant::OBJECT, "data", PROPERTY_HINT_RESOURCE_TYPE, "EntityData"))); @@ -4896,6 +4943,12 @@ void Entity::_bind_methods() { ClassDB::bind_method(D_METHOD("stake_heal", "data"), &Entity::stake_heal); ClassDB::bind_method(D_METHOD("sdeal_heal_to", "data"), &Entity::sdeal_heal_to); + //Damage, Heal RPCs + ClassDB::bind_method(D_METHOD("cdamage_dealt_rpc", "data"), &Entity::cdamage_dealt_rpc); + ClassDB::bind_method(D_METHOD("cdealt_damage_rpc", "data"), &Entity::cdealt_damage_rpc); + ClassDB::bind_method(D_METHOD("cheal_dealt_rpc", "data"), &Entity::cheal_dealt_rpc); + ClassDB::bind_method(D_METHOD("cdealt_heal_rpc", "data"), &Entity::cdealt_heal_rpc); + //Interactions ClassDB::bind_method(D_METHOD("cans_interact"), &Entity::cans_interact); ClassDB::bind_method(D_METHOD("sinteract"), &Entity::sinteract); diff --git a/entities/entity.h b/entities/entity.h index c72304c..578bab4 100644 --- a/entities/entity.h +++ b/entities/entity.h @@ -449,6 +449,12 @@ public: void stake_heal(Ref info); void sdeal_heal_to(Ref info); + //Damage, Heal RPCs + void cdamage_dealt_rpc(String data); + void cdealt_damage_rpc(String data); + void cheal_dealt_rpc(String data); + void cdealt_heal_rpc(String data); + //Interactions bool cans_interact(); void sinteract(); diff --git a/infos/spell_cast_info.cpp b/infos/spell_cast_info.cpp index 1e76af6..0edeef4 100644 --- a/infos/spell_cast_info.cpp +++ b/infos/spell_cast_info.cpp @@ -124,6 +124,25 @@ bool SpellCastInfo::update_cast_time(float delta) { return false; } +void SpellCastInfo::resolve_references(Node *owner) { + ERR_FAIL_COND(!ObjectDB::instance_validate(owner)); + ERR_FAIL_COND(!owner->is_inside_tree()); + + _caster = Object::cast_to(owner); + + if (owner->is_inside_tree()) { + _target = Object::cast_to(owner->get_node_or_null(_target_path)); + } + + if (EntityDataManager::get_instance() != NULL) { + Ref spell = EntityDataManager::get_instance()->get_spell(_spell_id); + + if (spell.is_valid()) { + _spell = spell; + } + } +} + Dictionary SpellCastInfo::to_dict() { Dictionary dict; @@ -144,14 +163,9 @@ Dictionary SpellCastInfo::to_dict() { return dict; } -void SpellCastInfo::from_dict(Node *owner, const Dictionary &dict) { - ERR_FAIL_COND(!ObjectDB::instance_validate(owner)); +void SpellCastInfo::from_dict(const Dictionary &dict) { ERR_FAIL_COND(dict.empty()); - if (owner->is_inside_tree()) { - _caster = Object::cast_to(owner->get_node_or_null(dict.get("caster", ""))); - _target = Object::cast_to(owner->get_node_or_null(dict.get("target", ""))); - } _has_cast_time = dict.get("has_cast_time", true); _cast_time = dict.get("cast_time", 0); @@ -160,15 +174,7 @@ void SpellCastInfo::from_dict(Node *owner, const Dictionary &dict) { _num_pushbacks = dict.get("num_pushbacks", 0); _is_casting = dict.get("is_casting", true); - int spell_id = dict.get("spell_id", 0); - - if (EntityDataManager::get_instance() != NULL) { - Ref spell = EntityDataManager::get_instance()->get_spell(spell_id); - - if (spell.is_valid()) { - _spell = spell; - } - } + _spell_id = dict.get("spell_id", 0); } SpellCastInfo::SpellCastInfo() { @@ -183,6 +189,8 @@ SpellCastInfo::SpellCastInfo() { _num_pushbacks = 0; _is_casting = false; + + _spell_id = 0; } SpellCastInfo::~SpellCastInfo() { @@ -230,6 +238,6 @@ void SpellCastInfo::_bind_methods() { ClassDB::bind_method(D_METHOD("update_cast_time", "delta"), &SpellCastInfo::update_cast_time); - ClassDB::bind_method(D_METHOD("from_dict", "owner", "dict"), &SpellCastInfo::from_dict); + ClassDB::bind_method(D_METHOD("from_dict", "dict"), &SpellCastInfo::from_dict); ClassDB::bind_method(D_METHOD("to_dict"), &SpellCastInfo::to_dict); } diff --git a/infos/spell_cast_info.h b/infos/spell_cast_info.h index 60432ea..448939f 100644 --- a/infos/spell_cast_info.h +++ b/infos/spell_cast_info.h @@ -42,8 +42,9 @@ public: bool update_cast_time(float delta); + void resolve_references(Node *owner); Dictionary to_dict(); - void from_dict(Node *owner, const Dictionary &dict); + void from_dict(const Dictionary &dict); SpellCastInfo(); ~SpellCastInfo(); @@ -63,7 +64,10 @@ private: bool _is_casting; + int _spell_id; Ref _spell; + + NodePath _target_path; }; #endif diff --git a/pipelines/spell_damage_info.cpp b/pipelines/spell_damage_info.cpp index 1429996..2e4854d 100644 --- a/pipelines/spell_damage_info.cpp +++ b/pipelines/spell_damage_info.cpp @@ -1,8 +1,8 @@ #include "spell_damage_info.h" -#include "../entities/entity.h" -#include "../data/spell.h" #include "../data/aura.h" +#include "../data/spell.h" +#include "../entities/entity.h" bool SpellDamageInfo::get_immune() { return _crit; @@ -85,32 +85,39 @@ Ref SpellDamageInfo::get_damage_source() { void SpellDamageInfo::set_damage_source(Ref value) { _damage_source_type = DAMAGE_SOURCE_UNKNOWN; _damage_source = value; + _damage_source_id = 0; + + if (value->has_method("get_id")) { + _damage_source_id = value->call("get_id"); + } } - Ref SpellDamageInfo::get_spell_damage_source() { - //cast - return NULL; + return Ref(_damage_source); } void SpellDamageInfo::set_spell_damage_source(Ref value) { _damage_source_type = DAMAGE_SOURCE_SPELL; _damage_source = value; + + ERR_FAIL_COND(!value.is_valid()); + + _damage_source_id = value->get_id(); } - Ref SpellDamageInfo::get_aura_damage_source() { - //cast - return NULL; + return Ref(_damage_source); } void SpellDamageInfo::set_aura_damage_source(Ref value) { _damage_source_type = DAMAGE_SOURCE_AURA; _damage_source = value; + + ERR_FAIL_COND(!value.is_valid()); + + _damage_source_id = value->get_id(); } - - int SpellDamageInfo::get_damage_source_id() { return _damage_source_id; } @@ -119,11 +126,11 @@ void SpellDamageInfo::set_damage_source_id(int value) { _damage_source_id = value; } -SpellDamageInfo::DamageSource SpellDamageInfo::get_damage_source_type() { +SpellDamageInfo::DamageSourceType SpellDamageInfo::get_damage_source_type() { return _damage_source_type; } -void SpellDamageInfo::set_damage_source_type(DamageSource value) { +void SpellDamageInfo::set_damage_source_type(DamageSourceType value) { _damage_source_type = value; } @@ -131,6 +138,59 @@ void SpellDamageInfo::reset() { _original_damage = -1; } +void SpellDamageInfo::resolve_references(Node *owner) { + ERR_FAIL_COND(!ObjectDB::instance_validate(owner)); + ERR_FAIL_COND(!owner->is_inside_tree()); + + _dealer = Object::cast_to(owner->get_node_or_null(_dealer_path)); + _receiver = Object::cast_to(owner->get_node_or_null(_receiver_path)); + + if (EntityDataManager::get_instance() != NULL) { + if (_damage_source_type == DAMAGE_SOURCE_SPELL) { + _damage_source = EntityDataManager::get_instance()->get_spell(_damage_source_id); + } else if (_damage_source_type == DAMAGE_SOURCE_AURA) { + _damage_source = EntityDataManager::get_instance()->get_aura(_damage_source_id); + } + } +} + +Dictionary SpellDamageInfo::to_dict() { + Dictionary dict; + + if (ObjectDB::instance_validate(_dealer)) + dict["dealer_path"] = _dealer->get_path(); + + if (ObjectDB::instance_validate(_receiver)) + dict["receiver_path"] = _receiver->get_path(); + + dict["immune"] = _immune; + dict["damage"] = _damage; + + dict["original_damage"] = _original_damage; + dict["amount_absorbed"] = _amount_absorbed; + dict["crit"] = _crit; + + dict["spell_type"] = _spell_type; + dict["damage_source_type"] = _damage_source_type; + dict["damage_source_id"] = _damage_source_id; + + return dict; +} +void SpellDamageInfo::from_dict(const Dictionary &dict) { + ERR_FAIL_COND(dict.empty()); + + _immune = dict.get("immune", true); + _damage = dict.get("damage", 0); + + _original_damage = dict.get("original_damage", 0); + _amount_absorbed = dict.get("amount_absorbed", 0); + _crit = dict.get("crit", false); + + _spell_type = static_cast(static_cast(dict.get("spell_type", SpellEnums::SPELL_TYPE_NONE))); + _damage_source_type = static_cast(static_cast(dict.get("damage_source_type", DAMAGE_SOURCE_UNKNOWN))); + _damage_source_id = dict.get("damage_source_id", 0); +} + SpellDamageInfo::SpellDamageInfo() { _damage = 0; _original_damage = 0; @@ -150,10 +210,10 @@ SpellDamageInfo::~SpellDamageInfo() { } void SpellDamageInfo::_bind_methods() { - ClassDB::bind_method(D_METHOD("get_immune"), &SpellDamageInfo::get_immune); + ClassDB::bind_method(D_METHOD("get_immune"), &SpellDamageInfo::get_immune); ClassDB::bind_method(D_METHOD("set_immune", "value"), &SpellDamageInfo::set_immune); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "immune"), "set_immune", "get_immune"); - + ClassDB::bind_method(D_METHOD("get_damage"), &SpellDamageInfo::get_damage); ClassDB::bind_method(D_METHOD("set_damage", "value"), &SpellDamageInfo::set_damage); ADD_PROPERTY(PropertyInfo(Variant::INT, "damage"), "set_damage", "get_damage"); @@ -161,7 +221,7 @@ void SpellDamageInfo::_bind_methods() { ClassDB::bind_method(D_METHOD("get_crit"), &SpellDamageInfo::get_crit); ClassDB::bind_method(D_METHOD("set_crit", "value"), &SpellDamageInfo::set_crit); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "crit"), "set_crit", "get_crit"); - + ClassDB::bind_method(D_METHOD("get_amount_absorbed"), &SpellDamageInfo::get_amount_absorbed); ClassDB::bind_method(D_METHOD("set_amount_absorbed", "value"), &SpellDamageInfo::set_amount_absorbed); ADD_PROPERTY(PropertyInfo(Variant::INT, "amount_absorbed"), "set_amount_absorbed", "get_amount_absorbed"); @@ -188,11 +248,10 @@ void SpellDamageInfo::_bind_methods() { ClassDB::bind_method(D_METHOD("get_damage_source_type"), &SpellDamageInfo::get_damage_source_type); ClassDB::bind_method(D_METHOD("set_damage_source_type", "value"), &SpellDamageInfo::set_damage_source_type); - ADD_PROPERTY(PropertyInfo(Variant::INT, "damage_source_type", PROPERTY_HINT_ENUM, "Unknown, Spell, Aura"), "set_damage_source_type", "get_damage_source_type"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "damage_source_type", PROPERTY_HINT_ENUM, "Unknown,Spell,Aura"), "set_damage_source_type", "get_damage_source_type"); ClassDB::bind_method(D_METHOD("reset"), &SpellDamageInfo::reset); - BIND_ENUM_CONSTANT(DAMAGE_SOURCE_UNKNOWN); BIND_ENUM_CONSTANT(DAMAGE_SOURCE_SPELL); BIND_ENUM_CONSTANT(DAMAGE_SOURCE_AURA); diff --git a/pipelines/spell_damage_info.h b/pipelines/spell_damage_info.h index 1ece0a4..cf05922 100644 --- a/pipelines/spell_damage_info.h +++ b/pipelines/spell_damage_info.h @@ -13,7 +13,7 @@ class SpellDamageInfo : public Reference { GDCLASS(SpellDamageInfo, Reference); public: - enum DamageSource { + enum DamageSourceType { DAMAGE_SOURCE_UNKNOWN, DAMAGE_SOURCE_SPELL, DAMAGE_SOURCE_AURA, @@ -23,9 +23,9 @@ protected: static void _bind_methods(); public: - bool get_immune(); + bool get_immune(); void set_immune(bool value); - + int get_damage(); void set_damage(int value); @@ -58,28 +58,37 @@ public: int get_damage_source_id(); void set_damage_source_id(int value); - DamageSource get_damage_source_type(); - void set_damage_source_type(DamageSource value); + DamageSourceType get_damage_source_type(); + void set_damage_source_type(DamageSourceType value); void reset(); + void resolve_references(Node *owner); + Dictionary to_dict(); + void from_dict(const Dictionary &dict); + SpellDamageInfo(); ~SpellDamageInfo(); private: - bool _immune; + bool _immune; int _damage; int _original_damage; int _amount_absorbed; bool _crit; SpellEnums::SpellType _spell_type; - Entity *_dealer; - Entity *_receiver; - DamageSource _damage_source_type; + + DamageSourceType _damage_source_type; Ref _damage_source; int _damage_source_id; + + Entity *_dealer; + Entity *_receiver; + + NodePath _dealer_path; + NodePath _receiver_path; }; -VARIANT_ENUM_CAST(SpellDamageInfo::DamageSource); +VARIANT_ENUM_CAST(SpellDamageInfo::DamageSourceType); #endif diff --git a/pipelines/spell_heal_info.cpp b/pipelines/spell_heal_info.cpp index aea08cf..e17636f 100644 --- a/pipelines/spell_heal_info.cpp +++ b/pipelines/spell_heal_info.cpp @@ -5,10 +5,10 @@ #include "../entities/entity.h" bool SpellHealInfo::get_immune() { - return _crit; + return _immune; } void SpellHealInfo::set_immune(bool value) { - _crit = value; + _immune = value; } int SpellHealInfo::get_heal() { @@ -89,23 +89,29 @@ void SpellHealInfo::set_heal_source(Ref value) { } Ref SpellHealInfo::get_spell_heal_source() { - //cast - return NULL; + return Ref(_heal_source); } void SpellHealInfo::set_spell_heal_source(Ref value) { _heal_source_type = HEAL_SOURCE_SPELL; _heal_source = value; + + ERR_FAIL_COND(!value.is_valid()); + + _heal_source_id = value->get_id(); } Ref SpellHealInfo::get_aura_heal_source() { - //cast - return NULL; + return Ref(_heal_source); } void SpellHealInfo::set_aura_heal_source(Ref value) { _heal_source_type = HEAL_SOURCE_AURA; _heal_source = value; + + ERR_FAIL_COND(!value.is_valid()); + + _heal_source_id = value->get_id(); } int SpellHealInfo::get_heal_source_id() { @@ -116,11 +122,11 @@ void SpellHealInfo::set_heal_source_id(int value) { _heal_source_id = value; } -SpellHealInfo::HealSource SpellHealInfo::get_heal_source_type() { +SpellHealInfo::HealSourceType SpellHealInfo::get_heal_source_type() { return _heal_source_type; } -void SpellHealInfo::set_heal_source_type(HealSource value) { +void SpellHealInfo::set_heal_source_type(HealSourceType value) { _heal_source_type = value; } @@ -128,6 +134,59 @@ void SpellHealInfo::reset() { _original_heal = -1; } +void SpellHealInfo::resolve_references(Node *owner) { + ERR_FAIL_COND(!ObjectDB::instance_validate(owner)); + ERR_FAIL_COND(!owner->is_inside_tree()); + + _dealer = Object::cast_to(owner->get_node_or_null(_dealer_path)); + _receiver = Object::cast_to(owner->get_node_or_null(_receiver_path)); + + if (EntityDataManager::get_instance() != NULL) { + if (_heal_source_type == HEAL_SOURCE_SPELL) { + _heal_source = EntityDataManager::get_instance()->get_spell(_heal_source_id); + } else if (_heal_source_type == HEAL_SOURCE_AURA) { + _heal_source = EntityDataManager::get_instance()->get_aura(_heal_source_id); + } + } +} + +Dictionary SpellHealInfo::to_dict() { + Dictionary dict; + + if (ObjectDB::instance_validate(_dealer)) + dict["dealer_path"] = _dealer->get_path(); + + if (ObjectDB::instance_validate(_receiver)) + dict["receiver_path"] = _receiver->get_path(); + + dict["immune"] = _immune; + dict["heal"] = _heal; + + dict["original_heal"] = _original_heal; + dict["amount_absorbed"] = _amount_absorbed; + dict["crit"] = _crit; + + dict["spell_type"] = _spell_type; + dict["heal_source_type"] = _heal_source_type; + dict["heal_source_id"] = _heal_source_id; + + return dict; +} +void SpellHealInfo::from_dict(const Dictionary &dict) { + ERR_FAIL_COND(dict.empty()); + + _immune = dict.get("immune", true); + _heal = dict.get("heal", 0); + + _original_heal = dict.get("original_heal", 0); + _amount_absorbed = dict.get("amount_absorbed", 0); + _crit = dict.get("crit", false); + + _spell_type = static_cast(static_cast(dict.get("spell_type", SpellEnums::SPELL_TYPE_NONE))); + _heal_source_type = static_cast(static_cast(dict.get("heal_source_type", HEAL_SOURCE_UNKNOWN))); + _heal_source_id = dict.get("heal_source_id", 0); +} + SpellHealInfo::SpellHealInfo() { _heal = 0; _original_heal = 0; diff --git a/pipelines/spell_heal_info.h b/pipelines/spell_heal_info.h index 30c97dd..7ee47d5 100644 --- a/pipelines/spell_heal_info.h +++ b/pipelines/spell_heal_info.h @@ -13,7 +13,7 @@ class SpellHealInfo : public Reference { GDCLASS(SpellHealInfo, Reference); public: - enum HealSource { + enum HealSourceType { HEAL_SOURCE_UNKNOWN, HEAL_SOURCE_SPELL, HEAL_SOURCE_AURA, @@ -56,27 +56,37 @@ public: int get_heal_source_id(); void set_heal_source_id(int value); - HealSource get_heal_source_type(); - void set_heal_source_type(HealSource value); + HealSourceType get_heal_source_type(); + void set_heal_source_type(HealSourceType value); void reset(); + void resolve_references(Node *owner); + Dictionary to_dict(); + void from_dict(const Dictionary &dict); + SpellHealInfo(); ~SpellHealInfo(); private: + bool _immune; int _heal; int _original_heal; int _amount_absorbed; bool _crit; SpellEnums::SpellType _spell_type; - Entity *_dealer; - Entity *_receiver; - HealSource _heal_source_type; + + HealSourceType _heal_source_type; Ref _heal_source; int _heal_source_id; + + Entity *_dealer; + Entity *_receiver; + + NodePath _dealer_path; + NodePath _receiver_path; }; -VARIANT_ENUM_CAST(SpellHealInfo::HealSource); +VARIANT_ENUM_CAST(SpellHealInfo::HealSourceType); #endif