Initial commit
This commit is contained in:
123
engines/crab/item/Item.cpp
Normal file
123
engines/crab/item/Item.cpp
Normal file
@@ -0,0 +1,123 @@
|
||||
/* ScummVM - Graphic Adventure Engine
|
||||
*
|
||||
* ScummVM is the legal property of its developers, whose names
|
||||
* are too numerous to list here. Please refer to the COPYRIGHT
|
||||
* file distributed with this source distribution.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* This code is based on the CRAB engine
|
||||
*
|
||||
* Copyright (c) Arvind Raja Yadav
|
||||
*
|
||||
* Licensed under MIT
|
||||
*
|
||||
*/
|
||||
|
||||
#include "crab/crab.h"
|
||||
#include "crab/GameParam.h"
|
||||
#include "crab/item/Item.h"
|
||||
|
||||
namespace Crab {
|
||||
|
||||
using namespace pyrodactyl::image;
|
||||
using namespace pyrodactyl::item;
|
||||
using namespace pyrodactyl::stat;
|
||||
using namespace pyrodactyl::people;
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
// Purpose: Load
|
||||
//------------------------------------------------------------------------
|
||||
void Item::load(rapidxml::xml_node<char> *node) {
|
||||
if (nodeValid(node)) {
|
||||
loadStr(_id, "id", node);
|
||||
loadStr(_name, "name", node);
|
||||
loadStr(_type, "type", node);
|
||||
loadStr(_desc, "desc", node);
|
||||
loadImgKey(_img, "img", node);
|
||||
|
||||
_bonus.clear();
|
||||
for (auto n = node->first_node("bonus"); n != nullptr; n = n->next_sibling("bonus")) {
|
||||
Bonus b;
|
||||
b.load(n);
|
||||
_bonus.push_back(b);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Item::clear() {
|
||||
_id = "";
|
||||
_name = "";
|
||||
_type = "";
|
||||
_desc = "";
|
||||
_img = 0;
|
||||
_bonus.clear();
|
||||
_value = 0;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
// Purpose: Save and load state
|
||||
//------------------------------------------------------------------------
|
||||
void Item::saveState(rapidxml::xml_document<> &doc, rapidxml::xml_node<char> *root) {
|
||||
root->append_attribute(doc.allocate_attribute("id", _id.c_str()));
|
||||
root->append_attribute(doc.allocate_attribute("name", _name.c_str()));
|
||||
root->append_attribute(doc.allocate_attribute("type", _type.c_str()));
|
||||
root->append_attribute(doc.allocate_attribute("img", g_engine->_stringPool->get(_img)));
|
||||
root->append_attribute(doc.allocate_attribute("desc", _desc.c_str()));
|
||||
|
||||
for (const auto &i : _bonus) {
|
||||
auto n = doc.allocate_node(rapidxml::node_element, "bonus");
|
||||
switch (i._type) {
|
||||
case STAT_HEALTH:
|
||||
n->append_attribute(doc.allocate_attribute("type", STATNAME_HEALTH));
|
||||
break;
|
||||
case STAT_ATTACK:
|
||||
n->append_attribute(doc.allocate_attribute("type", STATNAME_ATTACK));
|
||||
break;
|
||||
case STAT_DEFENSE:
|
||||
n->append_attribute(doc.allocate_attribute("type", STATNAME_DEFENSE));
|
||||
break;
|
||||
case STAT_SPEED:
|
||||
n->append_attribute(doc.allocate_attribute("type", STATNAME_SPEED));
|
||||
break;
|
||||
/*case STAT_CHARISMA:n->append_attribute(doc.allocate_attribute("type", STATNAME_CHARISMA)); break;
|
||||
case STAT_INTELLIGENCE:n->append_attribute(doc.allocate_attribute("type", STATNAME_INTELLIGENCE)); break;*/
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
n->append_attribute(doc.allocate_attribute("val", g_engine->_stringPool->get(i._val)));
|
||||
root->append_node(n);
|
||||
}
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
// Purpose: Calculate effect of item on stats
|
||||
//------------------------------------------------------------------------
|
||||
void Item::statChange(pyrodactyl::people::Person &obj, bool increase) {
|
||||
for (const auto &i : _bonus)
|
||||
if (increase)
|
||||
obj._stat.change(i._type, i._val);
|
||||
else
|
||||
obj._stat.change(i._type, -i._val);
|
||||
}
|
||||
|
||||
void Item::draw(const int &x, const int &y) {
|
||||
g_engine->_imageManager->draw(x, y, _img);
|
||||
}
|
||||
|
||||
} // End of namespace Crab
|
||||
77
engines/crab/item/Item.h
Normal file
77
engines/crab/item/Item.h
Normal file
@@ -0,0 +1,77 @@
|
||||
/* ScummVM - Graphic Adventure Engine
|
||||
*
|
||||
* ScummVM is the legal property of its developers, whose names
|
||||
* are too numerous to list here. Please refer to the COPYRIGHT
|
||||
* file distributed with this source distribution.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* This code is based on the CRAB engine
|
||||
*
|
||||
* Copyright (c) Arvind Raja Yadav
|
||||
*
|
||||
* Licensed under MIT
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef CRAB_ITEM_H
|
||||
#define CRAB_ITEM_H
|
||||
|
||||
#include "crab/image/ImageManager.h"
|
||||
#include "crab/people/person.h"
|
||||
#include "crab/stat/bonus.h"
|
||||
|
||||
namespace Crab {
|
||||
|
||||
namespace pyrodactyl {
|
||||
namespace item {
|
||||
struct Item {
|
||||
// The id, name and description of the item
|
||||
Common::String _id, _name, _desc;
|
||||
|
||||
// The image for the item
|
||||
ImageKey _img;
|
||||
|
||||
// The type of item
|
||||
Common::String _type;
|
||||
|
||||
// The stat bonuses provided by the item
|
||||
Common::Array<pyrodactyl::stat::Bonus> _bonus;
|
||||
|
||||
// The price
|
||||
uint _value;
|
||||
|
||||
Item() {
|
||||
clear();
|
||||
}
|
||||
|
||||
~Item() {}
|
||||
|
||||
void clear();
|
||||
void statChange(pyrodactyl::people::Person &obj, bool increase);
|
||||
|
||||
void load(rapidxml::xml_node<char> *node);
|
||||
void draw(const int &x, const int &y);
|
||||
|
||||
void saveState(rapidxml::xml_document<> &doc, rapidxml::xml_node<char> *root);
|
||||
};
|
||||
} // End of namespace item
|
||||
} // End of namespace pyrodactyl
|
||||
|
||||
} // End of namespace Crab
|
||||
|
||||
#endif // CRAB_ITEM_H
|
||||
146
engines/crab/item/ItemCollection.cpp
Normal file
146
engines/crab/item/ItemCollection.cpp
Normal file
@@ -0,0 +1,146 @@
|
||||
/* ScummVM - Graphic Adventure Engine
|
||||
*
|
||||
* ScummVM is the legal property of its developers, whose names
|
||||
* are too numerous to list here. Please refer to the COPYRIGHT
|
||||
* file distributed with this source distribution.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* This code is based on the CRAB engine
|
||||
*
|
||||
* Copyright (c) Arvind Raja Yadav
|
||||
*
|
||||
* Licensed under MIT
|
||||
*
|
||||
*/
|
||||
|
||||
#include "crab/item/ItemCollection.h"
|
||||
|
||||
namespace Crab {
|
||||
|
||||
using namespace pyrodactyl::people;
|
||||
using namespace pyrodactyl::item;
|
||||
using namespace pyrodactyl::ui;
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
// Purpose: Load the reference information
|
||||
//------------------------------------------------------------------------
|
||||
void ItemCollection::load(rapidxml::xml_node<char> *node) {
|
||||
if (nodeValid("info", node))
|
||||
_itemInfo.load(node->first_node("info"));
|
||||
|
||||
if (nodeValid("ref", node))
|
||||
_ref.load(node->first_node("ref"));
|
||||
|
||||
if (nodeValid("inc", node))
|
||||
_inc.load(node->first_node("inc"));
|
||||
|
||||
if (nodeValid("dim", node)) {
|
||||
rapidxml::xml_node<char> *dimnode = node->first_node("dim");
|
||||
loadNum(_rows, "rows", dimnode);
|
||||
loadNum(_cols, "cols", dimnode);
|
||||
}
|
||||
|
||||
loadBool(_useKeyboard, "keyboard", node);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
// Purpose: Add a character's inventory if not added already
|
||||
//------------------------------------------------------------------------
|
||||
void ItemCollection::init(const Common::String &charId) {
|
||||
if (!_item.contains(charId))
|
||||
_item[charId].init(_ref, _inc, _rows, _cols, _useKeyboard);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
// Purpose: Handle events
|
||||
//------------------------------------------------------------------------
|
||||
void ItemCollection::handleEvents(const Common::String &charId, const Common::Event &event) {
|
||||
if (_item.contains(charId))
|
||||
_item[charId].handleEvents(event);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
// Purpose: Draw
|
||||
//------------------------------------------------------------------------
|
||||
void ItemCollection::draw(const Common::String &charId) {
|
||||
if (_item.contains(charId))
|
||||
_item[charId].draw(_itemInfo);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
// Purpose: Delete an item from a character's inventory
|
||||
//------------------------------------------------------------------------
|
||||
void ItemCollection::del(const Common::String &charId, const Common::String &itemId) {
|
||||
if (_item.contains(charId))
|
||||
_item[charId].del(itemId);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
// Purpose: Add an item to a character's inventory
|
||||
//------------------------------------------------------------------------
|
||||
void ItemCollection::add(const Common::String &charId, Item &itemData) {
|
||||
// We might want to give a player character not yet encountered an item before we ever meet them
|
||||
// Which is why we add a new inventory in case the character inventory does not exist yet
|
||||
init(charId);
|
||||
|
||||
_item[charId].equip(itemData);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
// Purpose: Find if a character has an item
|
||||
//------------------------------------------------------------------------
|
||||
bool ItemCollection::has(const Common::String &charId, const Common::String &container, const Common::String &itemId) {
|
||||
if (_item.contains(charId))
|
||||
return _item[charId].has(container, itemId);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
// Purpose: Load items from save file
|
||||
//------------------------------------------------------------------------
|
||||
void ItemCollection::loadState(rapidxml::xml_node<char> *node) {
|
||||
for (auto n = node->first_node(); n != nullptr; n = n->next_sibling()) {
|
||||
// Add all characters in the save file, whether we have them in the inventory or not
|
||||
init(n->name());
|
||||
_item[n->name()].loadState(n);
|
||||
}
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
// Purpose: Write items to save file
|
||||
//------------------------------------------------------------------------
|
||||
void ItemCollection::saveState(rapidxml::xml_document<> &doc, rapidxml::xml_node<char> *root) {
|
||||
for (auto &i : _item) {
|
||||
rapidxml::xml_node<char> *child = doc.allocate_node(rapidxml::node_element, i._key.c_str());
|
||||
i._value.saveState(doc, child);
|
||||
root->append_node(child);
|
||||
}
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
// Purpose: Reset UI elements when resolution changes
|
||||
//------------------------------------------------------------------------
|
||||
void ItemCollection::setUI() {
|
||||
_itemInfo.setUI();
|
||||
|
||||
for (auto &i : _item)
|
||||
i._value.setUI();
|
||||
}
|
||||
|
||||
} // End of namespace Crab
|
||||
98
engines/crab/item/ItemCollection.h
Normal file
98
engines/crab/item/ItemCollection.h
Normal file
@@ -0,0 +1,98 @@
|
||||
/* ScummVM - Graphic Adventure Engine
|
||||
*
|
||||
* ScummVM is the legal property of its developers, whose names
|
||||
* are too numerous to list here. Please refer to the COPYRIGHT
|
||||
* file distributed with this source distribution.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* This code is based on the CRAB engine
|
||||
*
|
||||
* Copyright (c) Arvind Raja Yadav
|
||||
*
|
||||
* Licensed under MIT
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef CRAB_ITEMCOLLECTION_H
|
||||
#define CRAB_ITEMCOLLECTION_H
|
||||
|
||||
#include "crab/item/ItemMenu.h"
|
||||
#include "crab/stat/StatDrawHelper.h"
|
||||
|
||||
namespace Crab {
|
||||
|
||||
namespace pyrodactyl {
|
||||
namespace item {
|
||||
// All the items owned by characters controlled by the player
|
||||
class ItemCollection {
|
||||
// The items for all player characters
|
||||
Common::HashMap<Common::String, ItemMenu> _item;
|
||||
|
||||
// The reference information for these menus used to display these items
|
||||
|
||||
// The reference item slot
|
||||
ItemSlot _ref;
|
||||
|
||||
// This vector stores the increments in x,y for each new slot
|
||||
Vector2i _inc;
|
||||
|
||||
// The dimensions of the menu
|
||||
uint _rows, _cols;
|
||||
|
||||
// Draw item description when user clicks an item to select it
|
||||
pyrodactyl::ui::ItemDesc _itemInfo;
|
||||
|
||||
// Should we enable keyboard for the menus
|
||||
bool _useKeyboard;
|
||||
|
||||
public:
|
||||
ItemCollection() {
|
||||
_rows = 1;
|
||||
_cols = 1;
|
||||
_useKeyboard = true;
|
||||
}
|
||||
|
||||
~ItemCollection() {}
|
||||
|
||||
void load(rapidxml::xml_node<char> *node);
|
||||
|
||||
void handleEvents(const Common::String &charId, const Common::Event &event);
|
||||
|
||||
void init(const Common::String &charId);
|
||||
void draw(const Common::String &charId);
|
||||
|
||||
// Requires: id of the character, the item information
|
||||
void add(const Common::String &charId, Item &itemData);
|
||||
|
||||
// Requires: id of the character from which to remove the item, and id of the item
|
||||
void del(const Common::String &charId, const Common::String &itemId);
|
||||
|
||||
// Requires: id of the character, the name of the container and name of the item
|
||||
bool has(const Common::String &charId, const Common::String &container, const Common::String &itemId);
|
||||
|
||||
void loadState(rapidxml::xml_node<char> *node);
|
||||
void saveState(rapidxml::xml_document<> &doc, rapidxml::xml_node<char> *root);
|
||||
|
||||
void setUI();
|
||||
};
|
||||
} // End of namespace item
|
||||
} // End of namespace pyrodactyl
|
||||
|
||||
} // End of namespace Crab
|
||||
|
||||
#endif // CRAB_ITEMCOLLECTION_H
|
||||
169
engines/crab/item/ItemMenu.cpp
Normal file
169
engines/crab/item/ItemMenu.cpp
Normal file
@@ -0,0 +1,169 @@
|
||||
/* ScummVM - Graphic Adventure Engine
|
||||
*
|
||||
* ScummVM is the legal property of its developers, whose names
|
||||
* are too numerous to list here. Please refer to the COPYRIGHT
|
||||
* file distributed with this source distribution.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* This code is based on the CRAB engine
|
||||
*
|
||||
* Copyright (c) Arvind Raja Yadav
|
||||
*
|
||||
* Licensed under MIT
|
||||
*
|
||||
*/
|
||||
|
||||
#include "crab/item/ItemMenu.h"
|
||||
|
||||
namespace Crab {
|
||||
|
||||
using namespace pyrodactyl::ui;
|
||||
using namespace pyrodactyl::item;
|
||||
using namespace pyrodactyl::people;
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
// Purpose: Load the layout and items
|
||||
//------------------------------------------------------------------------
|
||||
void ItemMenu::init(const ItemSlot &ref, const Vector2i &inc, const uint &rows, const uint &cols,
|
||||
const bool &keyboard) {
|
||||
uint size = rows * cols;
|
||||
for (uint i = 0; i < size; ++i) {
|
||||
ItemSlot b;
|
||||
b.init(ref, inc.x * (i % cols), inc.y * (i / cols));
|
||||
_element.push_back(b);
|
||||
}
|
||||
|
||||
_useKeyboard = keyboard;
|
||||
assignPaths();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
// Purpose: Load items from file
|
||||
//------------------------------------------------------------------------
|
||||
void ItemMenu::loadState(rapidxml::xml_node<char> *node) {
|
||||
uint count = 0;
|
||||
for (auto n = node->first_node(); n != nullptr && count < _element.size(); n = n->next_sibling(), ++count)
|
||||
_element[count].loadState(n);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
// Purpose: Save to file
|
||||
//------------------------------------------------------------------------
|
||||
void ItemMenu::saveState(rapidxml::xml_document<> &doc, rapidxml::xml_node<char> *root) {
|
||||
for (auto &i : _element)
|
||||
i.saveState(doc, root);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
// Purpose: Handles drag n' drop - return type is Boolean because we only need to communicate stat changes
|
||||
//------------------------------------------------------------------------
|
||||
void ItemMenu::handleEvents(const Common::Event &event, const int &xOffset, const int &yOffset) {
|
||||
int result = Menu<ItemSlot>::handleEvents(event);
|
||||
if (result != -1) {
|
||||
_selectIndex = result;
|
||||
for (uint i = 0; i < _element.size(); ++i)
|
||||
_element[i].state(i == (uint)_selectIndex);
|
||||
}
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
// Purpose: Draw the slot backgrounds first, then the items
|
||||
//------------------------------------------------------------------------
|
||||
void ItemMenu::draw(ItemDesc &itemInfo) {
|
||||
if (_selectIndex != -1)
|
||||
itemInfo.draw(_element[_selectIndex]._item);
|
||||
|
||||
for (auto &i : _element)
|
||||
i.draw();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
// Purpose: Equip an item at the first available location (used in events)
|
||||
//------------------------------------------------------------------------
|
||||
bool ItemMenu::equip(Item &item) {
|
||||
for (auto &i : _element)
|
||||
if (i._category == SLOT_STORAGE && i.equip(item))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
// Purpose: Remove all instances of an item
|
||||
//------------------------------------------------------------------------
|
||||
bool ItemMenu::del(const Common::String &id) {
|
||||
bool result = false;
|
||||
|
||||
for (auto &i : _element)
|
||||
if (i._item._id == id) {
|
||||
i._empty = true;
|
||||
i._item.clear();
|
||||
i._unread = false;
|
||||
result = true;
|
||||
break;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
// Purpose: Find out if we have an item with a name
|
||||
//------------------------------------------------------------------------
|
||||
bool ItemMenu::has(const Common::String &container, const Common::String &id) {
|
||||
for (const auto &i : _element)
|
||||
if (i._item._id == id) {
|
||||
if (container == "equip") {
|
||||
if (i._category == SLOT_EQUIP)
|
||||
return true;
|
||||
} else if (container == "storage") {
|
||||
if (i._category == SLOT_STORAGE)
|
||||
return true;
|
||||
} else
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
// Purpose: Swap an item with another item menu
|
||||
//------------------------------------------------------------------------
|
||||
bool ItemMenu::swap(ItemMenu &target, int index) {
|
||||
// We need to scan the slots first for an empty slot to store the item.
|
||||
// If no empty slot is found, then swap with a filled slot of same type
|
||||
// If no slot of type is found, don't swap items at all
|
||||
int foundIndex = -1, curIndex = 0;
|
||||
|
||||
for (auto i = _element.begin(); i != _element.end(); ++i, ++curIndex)
|
||||
if (i->_itemType == target._element[index]._itemType) {
|
||||
if (i->_empty) {
|
||||
i->swap(target._element[index]);
|
||||
return true;
|
||||
} else
|
||||
foundIndex = curIndex;
|
||||
}
|
||||
|
||||
if (foundIndex != -1) {
|
||||
_element[foundIndex].swap(target._element[index]);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
} // End of namespace Crab
|
||||
92
engines/crab/item/ItemMenu.h
Normal file
92
engines/crab/item/ItemMenu.h
Normal file
@@ -0,0 +1,92 @@
|
||||
/* ScummVM - Graphic Adventure Engine
|
||||
*
|
||||
* ScummVM is the legal property of its developers, whose names
|
||||
* are too numerous to list here. Please refer to the COPYRIGHT
|
||||
* file distributed with this source distribution.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* This code is based on the CRAB engine
|
||||
*
|
||||
* Copyright (c) Arvind Raja Yadav
|
||||
*
|
||||
* Licensed under MIT
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef CRAB_ITEMMENU_H
|
||||
#define CRAB_ITEMMENU_H
|
||||
|
||||
#include "crab/item/ItemSlot.h"
|
||||
#include "crab/people/person.h"
|
||||
#include "crab/ui/ItemDesc.h"
|
||||
#include "crab/ui/menu.h"
|
||||
|
||||
namespace Crab {
|
||||
|
||||
namespace pyrodactyl {
|
||||
namespace item {
|
||||
// The menu is used to handle interactions like the player clicking on an item to equip it
|
||||
class ItemMenu : public pyrodactyl::ui::Menu<ItemSlot> {
|
||||
protected:
|
||||
int _previewIndex, _selectIndex;
|
||||
|
||||
// Variable to determine if stats of object need updating
|
||||
bool _update;
|
||||
|
||||
public:
|
||||
// The preview for selected item and hovered item
|
||||
// StatPreview select, hover;
|
||||
|
||||
ItemMenu() {
|
||||
_previewIndex = -1;
|
||||
_selectIndex = -1;
|
||||
_update = false;
|
||||
}
|
||||
~ItemMenu() {}
|
||||
|
||||
int hoverIndex() { return _hoverIndex; }
|
||||
|
||||
void draw(pyrodactyl::ui::ItemDesc &itemInfo);
|
||||
/*pyrodactyl::people::Person &obj, pyrodactyl::stat::StatDrawHelper &helper*/
|
||||
|
||||
void init(const ItemSlot &ref, const Vector2i &inc, const uint &rows,
|
||||
const uint &cols, const bool &keyboard);
|
||||
|
||||
void handleEvents(const Common::Event &event, const int &xOffset = 0, const int &yOffset = 0);
|
||||
#if 0
|
||||
void handleEvents(const SDL_Event &Event, const int &XOffset = 0, const int &YOffset = 0);
|
||||
#endif
|
||||
/* pyrodactyl::people::Person &obj,*/
|
||||
|
||||
bool swap(ItemMenu &target, int index);
|
||||
bool equip(Item &item);
|
||||
bool del(const Common::String &id);
|
||||
bool has(const Common::String &container, const Common::String &id);
|
||||
|
||||
// Used to calculate enabled slots
|
||||
// void SetEnable();
|
||||
|
||||
void saveState(rapidxml::xml_document<> &doc, rapidxml::xml_node<char> *root);
|
||||
void loadState(rapidxml::xml_node<char> *node);
|
||||
};
|
||||
} // End of namespace item
|
||||
} // End of namespace pyrodactyl
|
||||
|
||||
} // End of namespace Crab
|
||||
|
||||
#endif // CRAB_ITEMMENU_H
|
||||
169
engines/crab/item/ItemSlot.cpp
Normal file
169
engines/crab/item/ItemSlot.cpp
Normal file
@@ -0,0 +1,169 @@
|
||||
/* ScummVM - Graphic Adventure Engine
|
||||
*
|
||||
* ScummVM is the legal property of its developers, whose names
|
||||
* are too numerous to list here. Please refer to the COPYRIGHT
|
||||
* file distributed with this source distribution.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* This code is based on the CRAB engine
|
||||
*
|
||||
* Copyright (c) Arvind Raja Yadav
|
||||
*
|
||||
* Licensed under MIT
|
||||
*
|
||||
*/
|
||||
|
||||
#include "crab/crab.h"
|
||||
#include "crab/item/ItemSlot.h"
|
||||
|
||||
namespace Crab {
|
||||
|
||||
using namespace pyrodactyl::ui;
|
||||
using namespace pyrodactyl::item;
|
||||
using namespace pyrodactyl::input;
|
||||
using namespace pyrodactyl::music;
|
||||
using namespace pyrodactyl::people;
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
// Purpose: Load
|
||||
//------------------------------------------------------------------------
|
||||
void ItemSlot::load(rapidxml::xml_node<char> *node) {
|
||||
StateButton::load(node);
|
||||
|
||||
if (node->first_attribute("slot") == nullptr)
|
||||
_noType = true;
|
||||
else {
|
||||
loadStr(_itemType, "slot", node);
|
||||
_noType = false;
|
||||
}
|
||||
|
||||
Common::String name = node->name();
|
||||
if (name == "equip")
|
||||
_category = SLOT_EQUIP;
|
||||
else
|
||||
_category = SLOT_STORAGE;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
// Purpose: Initialize from reference item slot
|
||||
//------------------------------------------------------------------------
|
||||
void ItemSlot::init(const ItemSlot &ref, const int &xOffset, const int &YOffset) {
|
||||
StateButton::init(ref, xOffset, YOffset);
|
||||
_canmove = ref._canmove;
|
||||
_noType = ref._noType;
|
||||
_category = ref._category;
|
||||
_unread = ref._unread;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
// Purpose: Save state to file
|
||||
//------------------------------------------------------------------------
|
||||
void ItemSlot::saveState(rapidxml::xml_document<> &doc, rapidxml::xml_node<char> *root) {
|
||||
rapidxml::xml_node<char> *child;
|
||||
|
||||
if (_category == SLOT_EQUIP)
|
||||
child = doc.allocate_node(rapidxml::node_element, "equip");
|
||||
else
|
||||
child = doc.allocate_node(rapidxml::node_element, "storage");
|
||||
|
||||
_item.saveState(doc, child);
|
||||
saveBool(_unread, "unread", doc, child);
|
||||
|
||||
root->append_node(child);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
// Purpose: Load state from file
|
||||
//------------------------------------------------------------------------
|
||||
void ItemSlot::loadState(rapidxml::xml_node<char> *node) {
|
||||
_item.load(node);
|
||||
loadBool(_unread, "unread", node);
|
||||
|
||||
if (_item._id == "")
|
||||
_empty = true;
|
||||
else
|
||||
_empty = false;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
// Purpose: Draw
|
||||
//------------------------------------------------------------------------
|
||||
void ItemSlot::draw() {
|
||||
StateButton::draw();
|
||||
|
||||
if (!_empty)
|
||||
_item.draw(x, y);
|
||||
|
||||
if (_unread)
|
||||
g_engine->_imageManager->notifyDraw(x + w, y);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
// Purpose: Handle user input
|
||||
//------------------------------------------------------------------------
|
||||
ButtonAction ItemSlot::handleEvents(const Common::Event &event, const int &xOffset, const int &yOffset) {
|
||||
ButtonAction ac = StateButton::handleEvents(event, xOffset, yOffset);
|
||||
if (ac == BUAC_LCLICK || ac == BUAC_RCLICK)
|
||||
_unread = false;
|
||||
|
||||
return ac;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
// Purpose: Exchange items with another slot
|
||||
// this object is the current slot, parameter object is target slot
|
||||
//------------------------------------------------------------------------
|
||||
bool ItemSlot::swap(ItemSlot &target) {
|
||||
if (canSwap(target)) {
|
||||
Item temp = _item;
|
||||
_item = target._item;
|
||||
target._item = temp;
|
||||
|
||||
bool val = _empty;
|
||||
_empty = target._empty;
|
||||
target._empty = val;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
// Purpose: Equip an item
|
||||
//------------------------------------------------------------------------
|
||||
bool ItemSlot::equip(Item &i) {
|
||||
if ((_itemType == i._type || _noType) && _empty) {
|
||||
_item = i;
|
||||
_empty = false;
|
||||
_unread = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
// Purpose: Change stats based on item
|
||||
//------------------------------------------------------------------------
|
||||
void ItemSlot::statChange(pyrodactyl::people::Person &obj, bool increase) {
|
||||
if (_enabled)
|
||||
_item.statChange(obj, increase);
|
||||
}
|
||||
|
||||
} // End of namespace Crab
|
||||
102
engines/crab/item/ItemSlot.h
Normal file
102
engines/crab/item/ItemSlot.h
Normal file
@@ -0,0 +1,102 @@
|
||||
/* ScummVM - Graphic Adventure Engine
|
||||
*
|
||||
* ScummVM is the legal property of its developers, whose names
|
||||
* are too numerous to list here. Please refer to the COPYRIGHT
|
||||
* file distributed with this source distribution.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* This code is based on the CRAB engine
|
||||
*
|
||||
* Copyright (c) Arvind Raja Yadav
|
||||
*
|
||||
* Licensed under MIT
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef CRAB_ITEMSLOT_H
|
||||
#define CRAB_ITEMSLOT_H
|
||||
|
||||
#include "crab/item/Item.h"
|
||||
#include "crab/ui/StateButton.h"
|
||||
|
||||
namespace Crab {
|
||||
|
||||
namespace pyrodactyl {
|
||||
namespace item {
|
||||
enum SlotType {
|
||||
SLOT_EQUIP,
|
||||
SLOT_STORAGE
|
||||
};
|
||||
|
||||
class ItemSlot : public pyrodactyl::ui::StateButton {
|
||||
// Ignore the type of item check
|
||||
bool _noType;
|
||||
|
||||
public:
|
||||
// The type of item allowed in this slot (can be overridden by item_type)
|
||||
Common::String _itemType;
|
||||
|
||||
// Is the slot empty?
|
||||
bool _empty;
|
||||
|
||||
// Is the slot enabled? (used for stat calculation)
|
||||
bool _enabled;
|
||||
|
||||
// Is this a new item? Draw the unread notification icon if so
|
||||
bool _unread;
|
||||
|
||||
// The type of the item slot
|
||||
SlotType _category;
|
||||
|
||||
// The item contained in the slot
|
||||
Item _item;
|
||||
|
||||
ItemSlot() {
|
||||
_empty = true;
|
||||
_enabled = true;
|
||||
_category = SLOT_STORAGE;
|
||||
_noType = false;
|
||||
_unread = false;
|
||||
}
|
||||
~ItemSlot() {}
|
||||
|
||||
void init(const ItemSlot &ref, const int &xOffset = 0, const int &yOffset = 0);
|
||||
void load(rapidxml::xml_node<char> *node);
|
||||
|
||||
void draw();
|
||||
|
||||
pyrodactyl::ui::ButtonAction handleEvents(const Common::Event &event, const int &xOffset = 0, const int &yOffset = 0);
|
||||
|
||||
bool canSwap(ItemSlot &target) {
|
||||
return target._noType || _item._type == target._itemType;
|
||||
}
|
||||
|
||||
bool swap(ItemSlot &target);
|
||||
bool equip(Item &i);
|
||||
|
||||
void statChange(pyrodactyl::people::Person &obj, bool increase);
|
||||
|
||||
void saveState(rapidxml::xml_document<> &doc, rapidxml::xml_node<char> *root);
|
||||
void loadState(rapidxml::xml_node<char> *node);
|
||||
};
|
||||
} // End of namespace item
|
||||
} // End of namespace pyrodactyl
|
||||
|
||||
} // End of namespace Crab
|
||||
|
||||
#endif // CRAB_ITEMSLOT_H
|
||||
67
engines/crab/item/StatPreview.cpp
Normal file
67
engines/crab/item/StatPreview.cpp
Normal file
@@ -0,0 +1,67 @@
|
||||
/* ScummVM - Graphic Adventure Engine
|
||||
*
|
||||
* ScummVM is the legal property of its developers, whose names
|
||||
* are too numerous to list here. Please refer to the COPYRIGHT
|
||||
* file distributed with this source distribution.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* This code is based on the CRAB engine
|
||||
*
|
||||
* Copyright (c) Arvind Raja Yadav
|
||||
*
|
||||
* Licensed under MIT
|
||||
*
|
||||
*/
|
||||
|
||||
#include "crab/crab.h"
|
||||
#include "crab/GameParam.h"
|
||||
#include "crab/item/StatPreview.h"
|
||||
|
||||
namespace Crab {
|
||||
|
||||
using namespace pyrodactyl::item;
|
||||
|
||||
void StatPreview::load(rapidxml::xml_node<char> *node) {
|
||||
loadBool(_enabled, "enabled", node);
|
||||
|
||||
if (nodeValid("stat", node)) {
|
||||
rapidxml::xml_node<char> *snode = node->first_node("stat");
|
||||
stat.load(snode);
|
||||
loadNum(_incS.x, "w", snode);
|
||||
loadNum(_incS.y, "h", snode);
|
||||
}
|
||||
|
||||
if (nodeValid("unit", node)) {
|
||||
rapidxml::xml_node<char> *snode = node->first_node("unit");
|
||||
unit.load(snode);
|
||||
loadNum(_incU.x, "w", snode);
|
||||
loadNum(_incU.y, "h", snode);
|
||||
}
|
||||
}
|
||||
|
||||
void StatPreview::draw(Item &item, pyrodactyl::stat::StatDrawHelper &helper) {
|
||||
if (_enabled) {
|
||||
int count = 0;
|
||||
for (auto i = item._bonus.begin(); i != item._bonus.end(); ++i, ++count) {
|
||||
stat.draw(helper.name(i->_type), _incS.x * count, _incS.y * count);
|
||||
unit.draw(g_engine->_stringPool->get(i->_val), _incU.x * count, _incU.y * count);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // End of namespace Crab
|
||||
62
engines/crab/item/StatPreview.h
Normal file
62
engines/crab/item/StatPreview.h
Normal file
@@ -0,0 +1,62 @@
|
||||
/* ScummVM - Graphic Adventure Engine
|
||||
*
|
||||
* ScummVM is the legal property of its developers, whose names
|
||||
* are too numerous to list here. Please refer to the COPYRIGHT
|
||||
* file distributed with this source distribution.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* This code is based on the CRAB engine
|
||||
*
|
||||
* Copyright (c) Arvind Raja Yadav
|
||||
*
|
||||
* Licensed under MIT
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef CRAB_STATPREVIEW_H
|
||||
#define CRAB_STATPREVIEW_H
|
||||
|
||||
#include "crab/item/Item.h"
|
||||
#include "crab/ui/SectionHeader.h"
|
||||
#include "crab/stat/StatDrawHelper.h"
|
||||
|
||||
namespace Crab {
|
||||
|
||||
namespace pyrodactyl {
|
||||
namespace item {
|
||||
struct StatPreview {
|
||||
pyrodactyl::ui::SectionHeader stat, unit;
|
||||
|
||||
// We increment stat draw position by this much for every new item bonus
|
||||
Vector2i _incS, _incU;
|
||||
|
||||
bool _enabled;
|
||||
|
||||
StatPreview() {
|
||||
_enabled = false;
|
||||
}
|
||||
|
||||
void load(rapidxml::xml_node<char> *node);
|
||||
void draw(Item &item, pyrodactyl::stat::StatDrawHelper &helper);
|
||||
};
|
||||
} // End of namespace item
|
||||
} // End of namespace pyrodactyl
|
||||
|
||||
} // End of namespace Crab
|
||||
|
||||
#endif // CRAB_STATPREVIEW_H
|
||||
Reference in New Issue
Block a user