mirror of
https://github.com/Relintai/entity_spell_system.git
synced 2025-02-20 17:14:44 +01:00
Implemented bag serialization.
This commit is contained in:
parent
5c1c47f5ce
commit
66018908d5
@ -315,7 +315,16 @@ Dictionary Entity::_to_dict() {
|
||||
|
||||
//// Equipment ////
|
||||
|
||||
//todo
|
||||
Dictionary equipment;
|
||||
|
||||
for (int i = 0; i < ItemEnums::EQUIP_SLOT_EQUIP_SLOT_MAX; ++i) {
|
||||
Ref<ItemInstance> ii = _equipment[i];
|
||||
|
||||
if (ii.is_valid())
|
||||
equipment[i] = ii->to_dict();
|
||||
}
|
||||
|
||||
dict["equipment"] = equipment;
|
||||
|
||||
//// Resources ////
|
||||
|
||||
@ -431,6 +440,8 @@ Dictionary Entity::_to_dict() {
|
||||
|
||||
dict["skills"] = skills;
|
||||
|
||||
//// Bags ////
|
||||
|
||||
if (_s_bag.is_valid())
|
||||
dict["bag"] = _s_bag->to_dict();
|
||||
|
||||
@ -465,7 +476,17 @@ void Entity::_from_dict(const Dictionary &dict) {
|
||||
|
||||
//// Equipment ////
|
||||
|
||||
//todo
|
||||
Dictionary equipment = dict.get("equipment", Dictionary());
|
||||
|
||||
for (int i = 0; i < ItemEnums::EQUIP_SLOT_EQUIP_SLOT_MAX; ++i) {
|
||||
if (equipment.has(String::num(i))) {
|
||||
Ref<ItemInstance> ii = _equipment[i];
|
||||
|
||||
ii->from_dict(dict[String::num(i)]);
|
||||
|
||||
_equipment[i] = ii;
|
||||
}
|
||||
}
|
||||
|
||||
//// Resources ////
|
||||
|
||||
@ -632,7 +653,9 @@ void Entity::_from_dict(const Dictionary &dict) {
|
||||
_c_skills.push_back(r);
|
||||
}
|
||||
|
||||
Dictionary bagd = dict.get("known_spells", Dictionary());
|
||||
//// Bags ////
|
||||
|
||||
Dictionary bagd = dict.get("bag", Dictionary());
|
||||
|
||||
if (!bagd.empty()) {
|
||||
if (!_s_bag.is_valid()) {
|
||||
@ -641,7 +664,7 @@ void Entity::_from_dict(const Dictionary &dict) {
|
||||
|
||||
bag->from_dict(bagd);
|
||||
|
||||
sets_bag(_s_bag);
|
||||
sets_bag(bag);
|
||||
} else {
|
||||
_s_bag->from_dict(bagd);
|
||||
}
|
||||
@ -3770,18 +3793,21 @@ void Entity::cclear_talents() {
|
||||
Ref<Bag> Entity::gets_bag() const {
|
||||
return _s_bag;
|
||||
}
|
||||
void Entity::sets_bag(const Ref<Bag> bag) {
|
||||
_s_bag = bag;
|
||||
|
||||
emit_signal("sbag_changed", this, _s_bag);
|
||||
|
||||
SEND_RPC(rpc("setc_bag", bag), setc_bag(bag));
|
||||
}
|
||||
|
||||
Ref<Bag> Entity::getc_bag() const {
|
||||
return _c_bag;
|
||||
}
|
||||
|
||||
void Entity::sets_bag(const Ref<Bag> bag) {
|
||||
_s_bag = bag;
|
||||
|
||||
SEND_RPC(rpc("setc_bag", bag), setc_bag(bag));
|
||||
}
|
||||
void Entity::setc_bag(const Ref<Bag> bag) {
|
||||
_c_bag = bag;
|
||||
|
||||
emit_signal("cbag_changed", this, _c_bag);
|
||||
}
|
||||
|
||||
Ref<Bag> Entity::gets_target_bag() const {
|
||||
@ -4703,6 +4729,9 @@ void Entity::_bind_methods() {
|
||||
|
||||
//// Inventory System ////
|
||||
|
||||
ADD_SIGNAL(MethodInfo("sbag_changed", PropertyInfo(Variant::OBJECT, "entity", PROPERTY_HINT_RESOURCE_TYPE, "Entity"), PropertyInfo(Variant::OBJECT, "bag", PROPERTY_HINT_RESOURCE_TYPE, "Bag")));
|
||||
ADD_SIGNAL(MethodInfo("cbag_changed", PropertyInfo(Variant::OBJECT, "entity", PROPERTY_HINT_RESOURCE_TYPE, "Entity"), PropertyInfo(Variant::OBJECT, "bag", PROPERTY_HINT_RESOURCE_TYPE, "Bag")));
|
||||
|
||||
ClassDB::bind_method(D_METHOD("gets_bag"), &Entity::gets_bag);
|
||||
ClassDB::bind_method(D_METHOD("sets_bag", "bag"), &Entity::sets_bag);
|
||||
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "sbag", PROPERTY_HINT_RESOURCE_TYPE, "Bag"), "sets_bag", "gets_bag");
|
||||
|
@ -602,9 +602,9 @@ public:
|
||||
//// Inventory ////
|
||||
|
||||
Ref<Bag> gets_bag() const;
|
||||
Ref<Bag> getc_bag() const;
|
||||
|
||||
void sets_bag(const Ref<Bag> bag);
|
||||
|
||||
Ref<Bag> getc_bag() const;
|
||||
void setc_bag(const Ref<Bag> bag);
|
||||
|
||||
Ref<Bag> gets_target_bag() const;
|
||||
|
@ -21,7 +21,7 @@ void Player::setc_seed(int value) {
|
||||
void Player::_setup() {
|
||||
Entity::_setup();
|
||||
|
||||
if (gets_entity_data().is_valid()) {
|
||||
if (gets_entity_data().is_valid() && !gets_bag().is_valid()) {
|
||||
Ref<Bag> bag;
|
||||
bag.instance();
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
#include "bag.h"
|
||||
|
||||
#include "../data/item_template.h"
|
||||
#include "../data/item_instance.h"
|
||||
#include "../data/item_template.h"
|
||||
|
||||
int Bag::get_allowed_item_types() const {
|
||||
return _allowed_item_types;
|
||||
@ -133,7 +133,7 @@ bool Bag::can_add_item(const Ref<ItemInstance> item) {
|
||||
return call("_can_add_item", item);
|
||||
}
|
||||
|
||||
return true;
|
||||
return true;
|
||||
}
|
||||
|
||||
int Bag::get_item_count() {
|
||||
@ -184,8 +184,9 @@ void Bag::set_size(const int size) {
|
||||
}
|
||||
|
||||
_bag_size = size;
|
||||
}
|
||||
|
||||
emit_signal("size_changed", Ref<Bag>(this));
|
||||
}
|
||||
|
||||
bool Bag::is_full() {
|
||||
if (has_method("_is_full")) {
|
||||
@ -213,10 +214,40 @@ void Bag::from_dict(const Dictionary &dict) {
|
||||
Dictionary Bag::_to_dict() {
|
||||
Dictionary dict;
|
||||
|
||||
dict["allowed_item_types"] = _allowed_item_types;
|
||||
dict["bag_size"] = _bag_size;
|
||||
|
||||
Dictionary items;
|
||||
|
||||
for (int i = 0; i < _items.size(); ++i) {
|
||||
Ref<ItemInstance> ii = _items[i];
|
||||
|
||||
if (ii.is_valid())
|
||||
items[i] = ii->to_dict();
|
||||
}
|
||||
|
||||
dict["items"] = items;
|
||||
|
||||
return dict;
|
||||
}
|
||||
void Bag::_from_dict(const Dictionary &dict) {
|
||||
_items.clear();
|
||||
|
||||
Dictionary items = dict.get("items", Dictionary());
|
||||
|
||||
Array keys = items.keys();
|
||||
|
||||
for (int i = 0; i < keys.size(); ++i) {
|
||||
Ref<ItemInstance> ii;
|
||||
ii.instance();
|
||||
|
||||
ii->from_dict(items[String::num(i)]);
|
||||
|
||||
_items.push_back(ii);
|
||||
}
|
||||
|
||||
_allowed_item_types = dict.get("allowed_item_types", 0xFFFFFF);
|
||||
set_size(dict.get("bag_size", 0));
|
||||
}
|
||||
|
||||
Bag::Bag() {
|
||||
@ -247,25 +278,26 @@ void Bag::_bind_methods() {
|
||||
ADD_SIGNAL(MethodInfo("item_count_changed", PropertyInfo(Variant::OBJECT, "bag", PROPERTY_HINT_RESOURCE_TYPE, "Bag"), PropertyInfo(Variant::OBJECT, "item", PROPERTY_HINT_RESOURCE_TYPE, "ItemInstance"), PropertyInfo(Variant::INT, "slot_id")));
|
||||
ADD_SIGNAL(MethodInfo("overburdened", PropertyInfo(Variant::OBJECT, "bag", PROPERTY_HINT_RESOURCE_TYPE, "Bag")));
|
||||
ADD_SIGNAL(MethodInfo("overburden_removed", PropertyInfo(Variant::OBJECT, "bag", PROPERTY_HINT_RESOURCE_TYPE, "Bag")));
|
||||
ADD_SIGNAL(MethodInfo("size_changed", PropertyInfo(Variant::OBJECT, "bag", PROPERTY_HINT_RESOURCE_TYPE, "Bag")));
|
||||
|
||||
ClassDB::bind_method(D_METHOD("get_allowed_item_types"), &Bag::get_allowed_item_types);
|
||||
ClassDB::bind_method(D_METHOD("set_allowed_item_types", "count"), &Bag::set_allowed_item_types);
|
||||
ADD_PROPERTY(PropertyInfo(Variant::INT, "allowed_item_types", PROPERTY_HINT_FLAGS, ItemEnums::BINDING_STRING_ITEM_TYPE_FLAGS), "set_allowed_item_types", "get_allowed_item_types");
|
||||
|
||||
|
||||
ClassDB::bind_method(D_METHOD("add_item", "item"), &Bag::add_item);
|
||||
ClassDB::bind_method(D_METHOD("get_item", "index"), &Bag::get_item);
|
||||
ClassDB::bind_method(D_METHOD("remove_item", "index"), &Bag::remove_item);
|
||||
ClassDB::bind_method(D_METHOD("swap_items", "item1_index", "item2_index"), &Bag::swap_items);
|
||||
ClassDB::bind_method(D_METHOD("swap_items", "item1_index", "item2_index"), &Bag::swap_items);
|
||||
|
||||
ClassDB::bind_method(D_METHOD("can_add_item", "item"), &Bag::can_add_item);
|
||||
|
||||
ClassDB::bind_method(D_METHOD("can_add_item", "item"), &Bag::can_add_item);
|
||||
|
||||
ClassDB::bind_method(D_METHOD("get_item_count"), &Bag::get_item_count);
|
||||
|
||||
|
||||
ClassDB::bind_method(D_METHOD("get_size"), &Bag::get_size);
|
||||
ClassDB::bind_method(D_METHOD("set_size", "size"), &Bag::set_size);
|
||||
|
||||
ClassDB::bind_method(D_METHOD("is_full"), &Bag::is_full);
|
||||
ADD_PROPERTY(PropertyInfo(Variant::INT, "size"), "set_size", "get_size");
|
||||
|
||||
ClassDB::bind_method(D_METHOD("is_full"), &Bag::is_full);
|
||||
ClassDB::bind_method(D_METHOD("is_overburdened"), &Bag::is_overburdened);
|
||||
|
||||
//Serialization
|
||||
|
Loading…
Reference in New Issue
Block a user