Improvements to the EssResourceDB's api, also add a missing bind to the ESS singleton.

This commit is contained in:
Relintai 2020-04-18 20:33:55 +02:00
parent 6c9bea09bc
commit b5ed3b16c8
5 changed files with 106 additions and 24 deletions

View File

@ -48,6 +48,50 @@ void ESSResourceDB::set_xp_data(const Ref<XPData> &data) {
_xp_data = data;
}
void ESSResourceDB::clear() {
_xp_data.unref();
}
void ESSResourceDB::add_entity_resource_db(Ref<ESSResourceDB> other) {
if (!other.is_valid())
return;
if (!_xp_data.is_valid() && other->_xp_data.is_valid())
_xp_data = other->_xp_data;
for (int i = 0; i < other->get_entity_resource_count(); ++i) {
add_entity_resource(other->get_entity_resource_index(i));
}
for (int i = 0; i < other->get_entity_skill_count(); ++i) {
add_entity_skill(other->get_entity_skill_index(i));
}
for (int i = 0; i < other->get_entity_data_count(); ++i) {
add_entity_data(other->get_entity_data_index(i));
}
for (int i = 0; i < other->get_spell_count(); ++i) {
add_spell(other->get_spell_index(i));
}
for (int i = 0; i < other->get_aura_count(); ++i) {
add_aura(other->get_aura_index(i));
}
for (int i = 0; i < other->get_craft_recipe_count(); ++i) {
add_craft_recipe(other->get_craft_recipe_index(i));
}
for (int i = 0; i < other->get_item_template_count(); ++i) {
add_item_template(other->get_item_template_index(i));
}
for (int i = 0; i < other->get_entity_species_data_count(); ++i) {
add_entity_species_data(other->get_entity_species_data_index(i));
}
}
void ESSResourceDB::initialize() {
if (has_method("_initialize"))
call("_initialize");
@ -137,6 +181,9 @@ void ESSResourceDB::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_entity_species_datas"), &ESSResourceDB::get_entity_species_datas);
ClassDB::bind_method(D_METHOD("set_entity_species_datas", "recipe"), &ESSResourceDB::set_entity_species_datas);
ClassDB::bind_method(D_METHOD("clear"), &ESSResourceDB::clear);
ClassDB::bind_method(D_METHOD("add_entity_resource_db", "other"), &ESSResourceDB::add_entity_resource_db);
BIND_VMETHOD(MethodInfo("_initialize"));
ClassDB::bind_method(D_METHOD("initialize"), &ESSResourceDB::initialize);
}

View File

@ -113,6 +113,10 @@ public:
virtual Vector<Variant> get_entity_species_datas() const = 0;
virtual void set_entity_species_datas(const Vector<Variant> &data) = 0;
virtual void clear();
void add_entity_resource_db(Ref<ESSResourceDB> other);
void initialize();
ESSResourceDB();

View File

@ -30,8 +30,15 @@ SOFTWARE.
#include "../entities/resources/entity_resource_data.h"
#include "../entities/skills/entity_skill_data.h"
bool ESSResourceDBStatic::get_remap_ids() const {
return _remap_ids;
}
void ESSResourceDBStatic::set_remap_ids(const bool value) {
_remap_ids = value;
}
Ref<EntityResourceData> ESSResourceDBStatic::get_entity_resource(int id) {
if (id < 0 || id > _entity_resources.size())
if (id < 0 || id >= _entity_resources.size())
return Ref<EntityResourceData>();
return _entity_resources.get(id);
@ -45,7 +52,7 @@ int ESSResourceDBStatic::get_entity_resource_count() {
return _entity_resources.size();
}
void ESSResourceDBStatic::add_entity_resource(Ref<EntityResourceData> cls) {
if (cls.is_valid())
if (_remap_ids && cls.is_valid())
cls->set_id(_entity_resources.size());
_entity_resources.push_back(cls);
@ -71,7 +78,7 @@ void ESSResourceDBStatic::set_entity_resources(const Vector<Variant> &data) {
}
Ref<EntitySkillData> ESSResourceDBStatic::get_entity_skill(int id) {
if (id < 0 || id > _entity_skills.size())
if (id < 0 || id >= _entity_skills.size())
return Ref<EntitySkillData>();
return _entity_skills.get(id);
@ -85,7 +92,7 @@ int ESSResourceDBStatic::get_entity_skill_count() {
return _entity_skills.size();
}
void ESSResourceDBStatic::add_entity_skill(Ref<EntitySkillData> cls) {
if (cls.is_valid())
if (_remap_ids && cls.is_valid())
cls->set_id(_entity_skills.size());
_entity_skills.push_back(cls);
@ -111,7 +118,7 @@ void ESSResourceDBStatic::set_entity_skills(const Vector<Variant> &data) {
}
Ref<EntityData> ESSResourceDBStatic::get_entity_data(int id) {
if (id < 0 || id > _entity_datas.size())
if (id < 0 || id >= _entity_datas.size())
return Ref<EntityData>();
return _entity_datas.get(id);
@ -125,7 +132,7 @@ int ESSResourceDBStatic::get_entity_data_count() {
return _entity_datas.size();
}
void ESSResourceDBStatic::add_entity_data(Ref<EntityData> cls) {
if (cls.is_valid())
if (_remap_ids && cls.is_valid())
cls->set_id(_entity_datas.size());
_entity_datas.push_back(cls);
@ -151,7 +158,7 @@ void ESSResourceDBStatic::set_entity_datas(const Vector<Variant> &data) {
}
Ref<Spell> ESSResourceDBStatic::get_spell(int id) {
if (id < 0 || id > _spells.size())
if (id < 0 || id >= _spells.size())
return Ref<Spell>();
return _spells.get(id);
@ -166,7 +173,7 @@ int ESSResourceDBStatic::get_spell_count() {
return _spells.size();
}
void ESSResourceDBStatic::add_spell(Ref<Spell> spell) {
if (spell.is_valid())
if (_remap_ids && spell.is_valid())
spell->set_id(_spells.size());
_spells.push_back(spell);
@ -192,14 +199,14 @@ void ESSResourceDBStatic::set_spells(const Vector<Variant> &data) {
}
void ESSResourceDBStatic::add_aura(Ref<Aura> aura) {
if (aura.is_valid())
if (_remap_ids && aura.is_valid())
aura->set_id(_auras.size());
_auras.push_back(aura);
}
Ref<Aura> ESSResourceDBStatic::get_aura(int id) {
if (id < 0 || id > _auras.size())
if (id < 0 || id >= _auras.size())
return Ref<Aura>();
return _auras.get(id);
@ -237,14 +244,14 @@ void ESSResourceDBStatic::set_auras(const Vector<Variant> &data) {
//Craft Data
void ESSResourceDBStatic::add_craft_recipe(Ref<CraftRecipe> cda) {
if (cda.is_valid())
if (_remap_ids && cda.is_valid())
cda->set_id(_craft_recipes.size());
_craft_recipes.push_back(cda);
}
Ref<CraftRecipe> ESSResourceDBStatic::get_craft_recipe(int id) {
if (id < 0 || id > _craft_recipes.size())
if (id < 0 || id >= _craft_recipes.size())
return Ref<CraftRecipe>();
return _craft_recipes.get(id);
@ -281,14 +288,14 @@ void ESSResourceDBStatic::set_craft_recipes(const Vector<Variant> &data) {
}
void ESSResourceDBStatic::add_item_template(Ref<ItemTemplate> cda) {
if (cda.is_valid())
if (_remap_ids && cda.is_valid())
cda->set_id(_item_templates.size());
_item_templates.push_back(cda);
}
Ref<ItemTemplate> ESSResourceDBStatic::get_item_template(int item_id) {
if (item_id < 0 || item_id > _item_templates.size())
if (item_id < 0 || item_id >= _item_templates.size())
return Ref<ItemTemplate>();
return _item_templates.get(item_id);
@ -324,7 +331,7 @@ void ESSResourceDBStatic::set_item_templates(const Vector<Variant> &data) {
}
void ESSResourceDBStatic::add_entity_species_data(Ref<EntitySpeciesData> cda) {
if (cda.is_valid())
if (_remap_ids && cda.is_valid())
cda->set_id(_entity_species_datas.size());
_entity_species_datas.push_back(cda);
@ -363,7 +370,19 @@ void ESSResourceDBStatic::set_entity_species_datas(const Vector<Variant> &data)
}
}
void ESSResourceDBStatic::clear() {
_entity_resources.clear();
_entity_skills.clear();
_entity_datas.clear();
_spells.clear();
_auras.clear();
_craft_recipes.clear();
_item_templates.clear();
_entity_species_datas.clear();
}
ESSResourceDBStatic::ESSResourceDBStatic() {
_remap_ids = false;
}
ESSResourceDBStatic::~ESSResourceDBStatic() {
@ -378,6 +397,10 @@ ESSResourceDBStatic::~ESSResourceDBStatic() {
}
void ESSResourceDBStatic::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_remap_ids"), &ESSResourceDBStatic::get_remap_ids);
ClassDB::bind_method(D_METHOD("set_remap_ids", "value"), &ESSResourceDBStatic::set_remap_ids);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "remap_ids"), "set_remap_ids", "get_remap_ids");
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "entity_resources", PROPERTY_HINT_NONE, "17/17:EntityResourceData", PROPERTY_USAGE_DEFAULT, "EntityResourceData"), "set_entity_resources", "get_entity_resources");
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "entity_skills", PROPERTY_HINT_NONE, "17/17:EntitySkillData", PROPERTY_USAGE_DEFAULT, "EntitySkillData"), "set_entity_skills", "get_entity_skills");
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "entity_datas", PROPERTY_HINT_NONE, "17/17:EntityData", PROPERTY_USAGE_DEFAULT, "EntityData"), "set_entity_datas", "get_entity_datas");

View File

@ -54,6 +54,9 @@ class ESSResourceDBStatic : public ESSResourceDB {
GDCLASS(ESSResourceDBStatic, ESSResourceDB);
public:
bool get_remap_ids() const;
void set_remap_ids(const bool value);
String get_xp_data_path();
void set_xp_data_path(String path);
@ -91,7 +94,7 @@ public:
Ref<Aura> get_aura(int aura_id);
Ref<Aura> get_aura_index(int index);
int get_aura_count();
void add_aura( Ref<Aura> aura);
void add_aura(Ref<Aura> aura);
Vector<Variant> get_auras() const;
void set_auras(const Vector<Variant> &data);
@ -116,6 +119,8 @@ public:
Vector<Variant> get_entity_species_datas() const;
void set_entity_species_datas(const Vector<Variant> &data);
void clear();
ESSResourceDBStatic();
~ESSResourceDBStatic();
@ -123,14 +128,16 @@ protected:
static void _bind_methods();
private:
Vector<Ref<EntityResourceData>> _entity_resources;
Vector<Ref<EntitySkillData>> _entity_skills;
Vector<Ref<EntityData>> _entity_datas;
Vector<Ref<Spell>> _spells;
Vector<Ref<Aura>> _auras;
Vector<Ref<CraftRecipe>> _craft_recipes;
Vector<Ref<ItemTemplate>> _item_templates;
Vector<Ref<EntitySpeciesData>> _entity_species_datas;
bool _remap_ids;
Vector<Ref<EntityResourceData> > _entity_resources;
Vector<Ref<EntitySkillData> > _entity_skills;
Vector<Ref<EntityData> > _entity_datas;
Vector<Ref<Spell> > _spells;
Vector<Ref<Aura> > _auras;
Vector<Ref<CraftRecipe> > _craft_recipes;
Vector<Ref<ItemTemplate> > _item_templates;
Vector<Ref<EntitySpeciesData> > _entity_species_datas;
};
#endif

View File

@ -228,6 +228,7 @@ void ESS::_bind_methods() {
//load
ClassDB::bind_method(D_METHOD("load_resource_db"), &ESS::load_resource_db);
ClassDB::bind_method(D_METHOD("load_entity_spawner"), &ESS::load_entity_spawner);
ClassDB::bind_method(D_METHOD("load_resource", "path", "type_hint"), &ESS::load_resource, DEFVAL(""));