Keep track of player item
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Wed, 10 Aug 2011 16:31:44 +0000 (18:31 +0200)
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Thu, 11 Aug 2011 13:22:36 +0000 (15:22 +0200)
src/client.cpp
src/client.h
src/game.cpp
src/player.cpp
src/player.h

index 30bd101984fa7e6e3c62a8a434d1c075f90b01fc..2687d01e0daf209bb51dc31eb6d397027ec17b6f 100644 (file)
@@ -1953,6 +1953,14 @@ void Client::setPlayerControl(PlayerControl &control)
        player->control = control;
 }
 
+void Client::selectPlayerItem(u16 item)
+{
+       LocalPlayer *player = m_env.getLocalPlayer();
+       assert(player != NULL);
+
+       player->wieldItem(item);
+}
+
 // Returns true if the inventory of the local player has been
 // updated from the server. If it is true, it is set to false.
 bool Client::getLocalInventoryUpdated()
index 1c71b2c2c3e4b7635a52aa813715b32f53c65288..d21764f751c3fc310c6be09bff16dadba5cdf7fc 100644 (file)
@@ -211,7 +211,9 @@ public:
        v3f getPlayerPosition(v3f *eye_position=NULL);
 
        void setPlayerControl(PlayerControl &control);
-       
+
+       void selectPlayerItem(u16 item);
+
        // Returns true if the inventory of the local player has been
        // updated from the server. If it is true, it is set to false.
        bool getLocalInventoryUpdated();
index 65f160f7bdd0e906952f3ba99b3bbc536ffccf68..147a1fa07b8680e8ad12ffee4bf83749b6cc7cf3 100644 (file)
@@ -2200,6 +2200,7 @@ void the_game(
                if(client.getLocalInventoryUpdated()
                                || g_selected_item != old_selected_item)
                {
+                       client.selectPlayerItem(g_selected_item);
                        old_selected_item = g_selected_item;
                        //std::cout<<"Updating local inventory"<<std::endl;
                        client.getLocalInventory(local_inventory);
index be478e8693efce74dfe42d21cb2a5bb1403e71db..d59ae7049d3a561357942f9cf04f7b11cd2b34a9 100644 (file)
@@ -33,6 +33,7 @@ Player::Player():
        craftresult_is_preview(true),
        hp(20),
        peer_id(PEER_ID_INEXISTENT),
+       m_selected_item(0),
        m_pitch(0),
        m_yaw(0),
        m_speed(0,0,0),
@@ -47,6 +48,11 @@ Player::~Player()
        delete inventory_backup;
 }
 
+void Player::wieldItem(u16 item)
+{
+       m_selected_item = item;
+}
+
 void Player::resetInventory()
 {
        inventory.clear();
index 45454e9e1e6c728c0da5e1bcf3b47e947fdcecf3..9a6ad93bfacad954121dda8964f27c749cdf8cc7 100644 (file)
@@ -105,6 +105,15 @@ public:
                snprintf(m_name, PLAYERNAME_SIZE, "%s", name);
        }
 
+       virtual void wieldItem(u16 item);
+       virtual const InventoryItem *getWieldItem() const
+       {
+               const InventoryList *list = inventory.getList("main");
+               if (list)
+                       return list->getItem(m_selected_item);
+               return NULL;
+       }
+
        const char * getName()
        {
                return m_name;
@@ -146,6 +155,7 @@ public:
 
 protected:
        char m_name[PLAYERNAME_SIZE];
+       u16 m_selected_item;
        f32 m_pitch;
        f32 m_yaw;
        v3f m_speed;