using items, just test, almost nothing done yet

This commit is contained in:
Henrique 2011-11-10 04:53:16 -02:00
parent 578078007b
commit 3f4ad7977c
7 changed files with 70 additions and 3 deletions

View File

@ -105,7 +105,7 @@ void Game::processTextMessage(int type, const std::string& message)
void Game::processInventoryChange(int slot, const ItemPtr& item)
{
g_dispatcher.addEvent([=] {
g_dispatcher.addEvent([slot, item] {
g_lua.callGlobalField("Game","onInventoryChange", slot, item);
});
}

View File

@ -28,6 +28,7 @@
class Map
{
public:
enum {
PLAYER_OFFSET_X = 8,
PLAYER_OFFSET_Y = 6,
@ -40,7 +41,6 @@ class Map
NUM_TILE_PIXELS = 32
};
public:
void draw(const Rect& rect);
void clean();

View File

@ -27,6 +27,7 @@
#include "game.h"
#include "localplayer.h"
#include "effect.h"
#include <otclient/net/protocolgame.h>
#include <framework/graphics/fontmanager.h>
Tile::Tile(const Position& position)
@ -228,3 +229,31 @@ bool Tile::isLookPossible()
}
return true;
}
// TODO:
/*
Get menu options
if invited to party
if creature, attack and follow
if item, use or use with
*/
void Tile::useItem()
{
// Get top item of stack priority 2 (do a function to do this later)
ThingPtr thing;
int lastStackpos = -1;
for(int stackPos = 0; stackPos < (int)m_things.size(); ++stackPos) {
int otherPriority = m_things[stackPos]->getStackPriority();
if(otherPriority == 2) {
thing = m_things[stackPos];
lastStackpos = stackPos;
}
}
if(lastStackpos != -1) {
// use this
g_game.getProtocolGame()->sendUseItem(m_position, thing->getId(), lastStackpos, 0); // 0 has something to do with container
}
}

View File

@ -53,6 +53,8 @@ public:
bool isFullyOpaque();
bool isLookPossible();
void useItem();
TilePtr asTile() { return std::static_pointer_cast<Tile>(shared_from_this()); }
private:

View File

@ -54,6 +54,7 @@ public:
void sendTurnEast();
void sendTurnSouth();
void sendTurnWest();
void sendUseItem(const Position& position, int itemId, int stackpos, int index);
void sendTalk(int channelType, int channelId, const std::string& receiver, const std::string& message);
void sendAddVip(const std::string& name);
void sendRemoveVip(int id);
@ -140,6 +141,7 @@ private:
ThingPtr internalGetThing(InputMessage& msg);
ItemPtr internalGetItem(InputMessage& msg, uint16 id);
void addPosition(OutputMessage& msg, const Position& position);
Position parsePosition(InputMessage& msg);
private:

View File

@ -158,6 +158,17 @@ void ProtocolGame::sendTurnWest()
send(oMsg);
}
void ProtocolGame::sendUseItem(const Position& position, int itemId, int stackpos, int index)
{
OutputMessage oMsg;
oMsg.addU8(Otc::ClientUseObject);
addPosition(oMsg, position);
oMsg.addU16(itemId);
oMsg.addU8(stackpos);
oMsg.addU8(index);
send(oMsg);
}
void ProtocolGame::sendTalk(int channelType, int channelId, const std::string& receiver, const std::string& message)
{
if(message.length() > 255 || message.length() <= 0)
@ -199,3 +210,10 @@ void ProtocolGame::sendRemoveVip(int id)
oMsg.addU32(id);
send(oMsg);
}
void ProtocolGame::addPosition(OutputMessage& msg, const Position& position)
{
msg.addU16(position.x);
msg.addU16(position.y);
msg.addU8(position.z);
}

View File

@ -26,6 +26,8 @@
#include <framework/otml/otml.h>
#include <framework/graphics/graphics.h>
#include <framework/ui/uilineedit.h>
#include <otclient/core/localplayer.h>
#include <otclient/core/tile.h>
void UIMap::setup()
{
@ -141,13 +143,27 @@ void UIMap::onStyleApply(const OTMLNodePtr& styleNode)
bool UIMap::onMousePress(const Point& mousePos, Fw::MouseButton button)
{
if(m_mapRect.contains(mousePos)) {
Point relativeStretchMousePos = mousePos - m_mapRect.topLeft();
Size mapSize(Map::MAP_VISIBLE_WIDTH * Map::NUM_TILE_PIXELS, Map::MAP_VISIBLE_HEIGHT * Map::NUM_TILE_PIXELS);
PointF stretchFactor(m_mapRect.width() / (float)mapSize.width(), m_mapRect.height() / (float)mapSize.height());
PointF relativeMousePos = PointF(relativeStretchMousePos.x, relativeStretchMousePos.y) / stretchFactor;
PointF tilePosF = relativeMousePos / Map::NUM_TILE_PIXELS;
Position tilePos = Position(1 + (int)tilePosF.x - Map::PLAYER_OFFSET_X, 1 + (int)tilePosF.y - Map::PLAYER_OFFSET_Y, 0) + g_game.getLocalPlayer()->getPosition();
TilePtr tile = g_map.getTile(tilePos);
tile->useItem();
}
return UIWidget::onMousePress(mousePos, button);
}
void UIMap::onGeometryUpdate(const Rect& oldRect, const Rect& newRect)
{
Rect mapRect = newRect.expanded(-m_mapMargin-1);
Size mapSize(15*32, 11*32);
Size mapSize(Map::MAP_VISIBLE_WIDTH * Map::NUM_TILE_PIXELS, Map::MAP_VISIBLE_HEIGHT * Map::NUM_TILE_PIXELS);
mapSize.scale(mapRect.size(), Fw::KeepAspectRatio);
m_mapRect.setSize(mapSize);
m_mapRect.moveCenter(newRect.center());