some APi changes in Container

This commit is contained in:
Eduardo Bart 2012-06-05 13:59:32 -03:00
parent 3f18ffa475
commit 8ef4716d71
4 changed files with 33 additions and 34 deletions

View File

@ -31,17 +31,24 @@ Container::Container()
m_hasParent = false; m_hasParent = false;
} }
void Container::open(const ContainerPtr& previousContainer) ItemPtr Container::getItem(int slot)
{
if(slot < 0 || slot >= (int)m_items.size())
return nullptr;
return m_items[slot];
}
void Container::onOpen(const ContainerPtr& previousContainer)
{ {
callLuaField("onOpen", previousContainer); callLuaField("onOpen", previousContainer);
} }
void Container::close() void Container::onClose()
{ {
callLuaField("onClose"); callLuaField("onClose");
} }
void Container::addItem(const ItemPtr& item) void Container::onAddItem(const ItemPtr& item)
{ {
m_items.push_front(item); m_items.push_front(item);
updateItemsPositions(); updateItemsPositions();
@ -49,14 +56,14 @@ void Container::addItem(const ItemPtr& item)
callLuaField("onAddItem", 0, item); callLuaField("onAddItem", 0, item);
} }
void Container::addItems(const std::vector<ItemPtr>& items) void Container::onAddItems(const std::vector<ItemPtr>& items)
{ {
for(const ItemPtr& item : items) for(const ItemPtr& item : items)
m_items.push_back(item); m_items.push_back(item);
updateItemsPositions(); updateItemsPositions();
} }
void Container::updateItem(int slot, const ItemPtr& item) void Container::onUpdateItem(int slot, const ItemPtr& item)
{ {
if(slot < 0 || slot >= (int)m_items.size()) { if(slot < 0 || slot >= (int)m_items.size()) {
g_logger.traceError("slot not found"); g_logger.traceError("slot not found");
@ -70,7 +77,7 @@ void Container::updateItem(int slot, const ItemPtr& item)
callLuaField("onUpdateItem", slot, item, oldItem); callLuaField("onUpdateItem", slot, item, oldItem);
} }
void Container::removeItem(int slot) void Container::onRemoveItem(int slot)
{ {
if(slot < 0 || slot >= (int)m_items.size()) { if(slot < 0 || slot >= (int)m_items.size()) {
g_logger.traceError("slot not found"); g_logger.traceError("slot not found");
@ -85,13 +92,6 @@ void Container::removeItem(int slot)
callLuaField("onRemoveItem", slot, item); callLuaField("onRemoveItem", slot, item);
} }
ItemPtr Container::getItem(int slot)
{
if(slot < 0 || slot >= (int)m_items.size())
return nullptr;
return m_items[slot];
}
void Container::updateItemsPositions() void Container::updateItemsPositions()
{ {
for(int slot = 0; slot < (int)m_items.size(); ++slot) for(int slot = 0; slot < (int)m_items.size(); ++slot)

View File

@ -32,14 +32,8 @@ class Container : public LuaObject
public: public:
Container(); Container();
void open(const ContainerPtr& previousContainer);
void close();
void addItem(const ItemPtr& item);
void addItems(const std::vector<ItemPtr>& items);
void updateItem(int slot, const ItemPtr& item);
void removeItem(int slot);
ItemPtr getItem(int slot); ItemPtr getItem(int slot);
std::deque<ItemPtr> getItems() { return m_items; }
Position getSlotPosition(int slot) { return Position(0xffff, m_id | 0x40, slot); } Position getSlotPosition(int slot) { return Position(0xffff, m_id | 0x40, slot); }
@ -56,6 +50,16 @@ public:
ItemPtr getContainerItem() { return m_containerItem; } ItemPtr getContainerItem() { return m_containerItem; }
bool hasParent() { return m_hasParent; } bool hasParent() { return m_hasParent; }
protected:
void onOpen(const ContainerPtr& previousContainer);
void onClose();
void onAddItem(const ItemPtr& item);
void onAddItems(const std::vector<ItemPtr>& items);
void onUpdateItem(int slot, const ItemPtr& item);
void onRemoveItem(int slot);
friend class Game;
private: private:
void updateItemsPositions(); void updateItemsPositions();

View File

@ -57,7 +57,7 @@ void Game::resetGameStates()
for(auto& it : m_containers) { for(auto& it : m_containers) {
const ContainerPtr& container = it.second; const ContainerPtr& container = it.second;
if(container) if(container)
container->close(); container->onClose();
} }
m_containers.clear(); m_containers.clear();
m_vips.clear(); m_vips.clear();
@ -180,11 +180,11 @@ void Game::processOpenContainer(int containerId, const ItemPtr& containerItem, c
container->setContainerItem(containerItem); container->setContainerItem(containerItem);
container->setHasParent(hasParent); container->setHasParent(hasParent);
m_containers[containerId] = container; m_containers[containerId] = container;
container->addItems(items); container->onAddItems(items);
container->open(previousContainer); container->onOpen(previousContainer);
if(previousContainer) if(previousContainer)
previousContainer->close(); previousContainer->onClose();
} }
void Game::processCloseContainer(int containerId) void Game::processCloseContainer(int containerId)
@ -196,7 +196,7 @@ void Game::processCloseContainer(int containerId)
} }
m_containers[containerId] = nullptr; m_containers[containerId] = nullptr;
container->close(); container->onClose();
} }
void Game::processContainerAddItem(int containerId, const ItemPtr& item) void Game::processContainerAddItem(int containerId, const ItemPtr& item)
@ -207,7 +207,7 @@ void Game::processContainerAddItem(int containerId, const ItemPtr& item)
return; return;
} }
container->addItem(item); container->onAddItem(item);
} }
void Game::processContainerUpdateItem(int containerId, int slot, const ItemPtr& item) void Game::processContainerUpdateItem(int containerId, int slot, const ItemPtr& item)
@ -218,7 +218,7 @@ void Game::processContainerUpdateItem(int containerId, int slot, const ItemPtr&
return; return;
} }
container->updateItem(slot, item); container->onUpdateItem(slot, item);
} }
void Game::processContainerRemoveItem(int containerId, int slot) void Game::processContainerRemoveItem(int containerId, int slot)
@ -229,7 +229,7 @@ void Game::processContainerRemoveItem(int containerId, int slot)
return; return;
} }
container->removeItem(slot); container->onRemoveItem(slot);
} }
void Game::processInventoryChange(int slot, const ItemPtr& item) void Game::processInventoryChange(int slot, const ItemPtr& item)

View File

@ -192,13 +192,8 @@ void OTClient::registerLuaFunctions()
g_lua.registerClass<Container>(); g_lua.registerClass<Container>();
g_lua.bindClassStaticFunction<Container>("create", []{ return ContainerPtr(new Container); }); g_lua.bindClassStaticFunction<Container>("create", []{ return ContainerPtr(new Container); });
g_lua.bindClassMemberFunction<Container>("open", &Container::open);
g_lua.bindClassMemberFunction<Container>("close", &Container::close);
g_lua.bindClassMemberFunction<Container>("addItem", &Container::addItem);
g_lua.bindClassMemberFunction<Container>("addItems", &Container::addItems);
g_lua.bindClassMemberFunction<Container>("updateItem", &Container::updateItem);
g_lua.bindClassMemberFunction<Container>("removeItem", &Container::removeItem);
g_lua.bindClassMemberFunction<Container>("getItem", &Container::getItem); g_lua.bindClassMemberFunction<Container>("getItem", &Container::getItem);
g_lua.bindClassMemberFunction<Container>("getItems", &Container::getItems);
g_lua.bindClassMemberFunction<Container>("getSlotPosition", &Container::getSlotPosition); g_lua.bindClassMemberFunction<Container>("getSlotPosition", &Container::getSlotPosition);
g_lua.bindClassMemberFunction<Container>("setId", &Container::setId); g_lua.bindClassMemberFunction<Container>("setId", &Container::setId);
g_lua.bindClassMemberFunction<Container>("setCapacity", &Container::setCapacity); g_lua.bindClassMemberFunction<Container>("setCapacity", &Container::setCapacity);