2023-12-17 23:21:54 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* equipment_data.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* PANDEMONIUM ENGINE */
|
|
|
|
/* https://github.com/Relintai/pandemonium_engine */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* Copyright (c) 2022-present Péter Magyar. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2022-03-15 13:29:32 +01:00
|
|
|
|
|
|
|
#include "equipment_data.h"
|
|
|
|
|
|
|
|
#include "item_instance.h"
|
|
|
|
#include "item_template.h"
|
|
|
|
|
|
|
|
#include "../../singletons/ess.h"
|
|
|
|
|
|
|
|
Ref<ItemTemplate> EquipmentData::get_slot(int index) {
|
2022-03-19 13:52:08 +01:00
|
|
|
ERR_FAIL_INDEX_V(index, _entries.size(), Ref<ItemTemplate>());
|
2022-03-15 13:29:32 +01:00
|
|
|
|
|
|
|
return _entries[index];
|
|
|
|
}
|
|
|
|
void EquipmentData::set_slot(int index, Ref<ItemTemplate> entry) {
|
2022-03-19 13:52:08 +01:00
|
|
|
ERR_FAIL_INDEX(index, _entries.size());
|
2022-03-15 13:29:32 +01:00
|
|
|
|
|
|
|
_entries.write[index] = entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<ItemInstance> EquipmentData::get_item(int index) {
|
2022-03-19 13:52:08 +01:00
|
|
|
ERR_FAIL_INDEX_V(index, _entries.size(), Ref<ItemInstance>());
|
2022-03-15 13:29:32 +01:00
|
|
|
|
|
|
|
Ref<ItemTemplate> ede = _entries[index];
|
|
|
|
|
|
|
|
if (!ede.is_valid())
|
|
|
|
return Ref<ItemInstance>();
|
|
|
|
|
|
|
|
return ede->create_item_instance();
|
|
|
|
}
|
|
|
|
|
|
|
|
EquipmentData::EquipmentData() {
|
2022-03-17 08:41:22 +01:00
|
|
|
if (ESS::get_singleton()) {
|
|
|
|
_entries.resize(ESS::get_singleton()->equip_slot_get_count());
|
|
|
|
}
|
2022-03-15 13:29:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
EquipmentData::~EquipmentData() {
|
|
|
|
_entries.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EquipmentData::_set(const StringName &p_name, const Variant &p_value) {
|
2022-03-19 13:52:08 +01:00
|
|
|
ERR_FAIL_COND_V(!ESS::get_singleton(), false);
|
|
|
|
|
2022-03-15 13:29:32 +01:00
|
|
|
String name = p_name;
|
|
|
|
|
|
|
|
if (name.get_slicec('/', 0) == "slot") {
|
|
|
|
StringName prop = name.get_slicec('/', 1);
|
|
|
|
|
|
|
|
if (ESS::get_singleton()->equip_slot_is_property(prop)) {
|
|
|
|
int id = ESS::get_singleton()->equip_slot_get_property_id(prop);
|
|
|
|
|
|
|
|
if (_entries.size() < id) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
_entries.set(id, p_value);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EquipmentData::_get(const StringName &p_name, Variant &r_ret) const {
|
2022-03-19 13:52:08 +01:00
|
|
|
ERR_FAIL_COND_V(!ESS::get_singleton(), false);
|
|
|
|
|
2022-03-15 13:29:32 +01:00
|
|
|
String name = p_name;
|
|
|
|
|
|
|
|
if (name.get_slicec('/', 0) == "slot") {
|
|
|
|
StringName prop = name.get_slicec('/', 1);
|
|
|
|
|
|
|
|
if (ESS::get_singleton()->equip_slot_is_property(prop)) {
|
|
|
|
int id = ESS::get_singleton()->equip_slot_get_property_id(prop);
|
|
|
|
|
|
|
|
if (_entries.size() < id) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
r_ret = _entries[id];
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EquipmentData::_get_property_list(List<PropertyInfo> *p_list) const {
|
2022-03-19 13:52:08 +01:00
|
|
|
ERR_FAIL_COND(!ESS::get_singleton());
|
|
|
|
|
2022-03-15 13:29:32 +01:00
|
|
|
for (int i = 0; i < ESS::get_singleton()->equip_slot_get_count(); ++i) {
|
|
|
|
p_list->push_back(PropertyInfo(Variant::OBJECT, "slot/" + ESS::get_singleton()->equip_slot_get_property_name(i), PROPERTY_HINT_RESOURCE_TYPE, "ItemTemplate"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EquipmentData::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("get_slot", "index"), &EquipmentData::get_slot);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_slot", "index", "entry"), &EquipmentData::set_slot);
|
|
|
|
}
|