Add a little animation when changing the wielded item
authorPilzAdam <pilzadam@minetest.net>
Sun, 19 May 2013 22:20:42 +0000 (00:20 +0200)
committerPilzAdam <pilzadam@minetest.net>
Sun, 19 May 2013 23:09:41 +0000 (01:09 +0200)
src/camera.cpp
src/camera.h
src/game.cpp

index 0dd0a767bce056cf37412ec169afc68dd674e62d..f227bd98ae1b7169c98a3da28d330634215dbbfc 100644 (file)
@@ -71,7 +71,12 @@ Camera::Camera(scene::ISceneManager* smgr, MapDrawControl& draw_control,
 
        m_digging_anim(0),
        m_digging_button(-1),
-       m_dummymesh(createCubeMesh(v3f(1,1,1)))
+       m_dummymesh(createCubeMesh(v3f(1,1,1))),
+
+       m_wield_change_timer(0.125),
+       m_wield_mesh_next(NULL),
+       m_previous_playeritem(-1),
+       m_previous_itemname("")
 {
        //dstream<<__FUNCTION_NAME<<std::endl;
 
@@ -142,6 +147,21 @@ void Camera::step(f32 dtime)
                        m_view_bobbing_fall = -1; // Mark the effect as finished
        }
 
+       if(m_wield_change_timer < 0.125)
+               m_wield_change_timer += dtime;
+       if(m_wield_change_timer > 0.125)
+               m_wield_change_timer = 0.125;
+
+       if(m_wield_change_timer >= 0 && m_wield_change_timer - dtime < 0) {
+               if(m_wield_mesh_next) {
+                       m_wieldnode->setMesh(m_wield_mesh_next);
+                       m_wieldnode->setVisible(true);
+               } else {
+                       m_wieldnode->setVisible(false);
+               }
+               m_wield_mesh_next = NULL;
+       }
+
        if (m_view_bobbing_state != 0)
        {
                //f32 offset = dtime * m_view_bobbing_speed * 0.035;
@@ -351,6 +371,10 @@ void Camera::update(LocalPlayer* player, f32 frametime, v2u32 screensize,
        v3f wield_position = v3f(55, -35, 65);
        //v3f wield_rotation = v3f(-100, 120, -100);
        v3f wield_rotation = v3f(-100, 120, -100);
+       if(m_wield_change_timer < 0)
+               wield_position.Y -= 40 + m_wield_change_timer*320;
+       else
+               wield_position.Y -= 40 - m_wield_change_timer*320;
        if(m_digging_anim < 0.05 || m_digging_anim > 0.5){
                f32 frac = 1.0;
                if(m_digging_anim > 0.5)
@@ -559,18 +583,33 @@ void Camera::setDigging(s32 button)
                m_digging_button = button;
 }
 
-void Camera::wield(const ItemStack &item)
+void Camera::wield(const ItemStack &item, u16 playeritem)
 {
        IItemDefManager *idef = m_gamedef->idef();
-       scene::IMesh *wield_mesh = idef->getWieldMesh(item.getDefinition(idef).name, m_gamedef);
-       if(wield_mesh)
-       {
-               m_wieldnode->setMesh(wield_mesh);
-               m_wieldnode->setVisible(true);
-       }
-       else
-       {
-               m_wieldnode->setVisible(false);
+       std::string itemname = item.getDefinition(idef).name;
+       m_wield_mesh_next = idef->getWieldMesh(itemname, m_gamedef);
+       if(playeritem != m_previous_playeritem) {
+               m_previous_playeritem = playeritem;
+               m_previous_itemname = itemname;
+               if(m_wield_change_timer >= 0.125)
+                       m_wield_change_timer = -0.125;
+               else if(m_wield_change_timer > 0) {
+                       m_wield_change_timer = -m_wield_change_timer;
+               }
+       } else {
+               if(m_wield_mesh_next) {
+                       m_wieldnode->setMesh(m_wield_mesh_next);
+                       m_wieldnode->setVisible(true);
+               } else {
+                       m_wieldnode->setVisible(false);
+               }
+               m_wield_mesh_next = NULL;
+               if(m_previous_itemname != itemname) {
+                       m_previous_itemname = itemname;
+                       m_wield_change_timer = 0;
+               }
+               else
+                       m_wield_change_timer = 0.125;
        }
 }
 
index 0ed5c20d292096833afe890bdfb08f0fc4d302e1..c8a6e0bb80092b1817baca3cf8d9d18d2b2c4f47 100644 (file)
@@ -117,7 +117,7 @@ public:
        void setDigging(s32 button);
 
        // Replace the wielded item mesh
-       void wield(const ItemStack &item);
+       void wield(const ItemStack &item, u16 playeritem);
 
        // Draw the wielded tool.
        // This has to happen *after* the main scene is drawn.
@@ -178,6 +178,12 @@ private:
 
        //dummymesh for camera
        irr::scene::IAnimatedMesh* m_dummymesh;
+
+       // Animation when changing wielded item
+       f32 m_wield_change_timer;
+       scene::IMesh *m_wield_mesh_next;
+       u16 m_previous_playeritem;
+       std::string m_previous_itemname;
 };
 
 #endif
index 746769aee2673dab140d5cde2d3f706b72ea6e8a..185f42eae02c8d9bfd4f374adadc7d2ed5be722f 100644 (file)
@@ -3084,7 +3084,7 @@ void the_game(
                        ItemStack item;
                        if(mlist != NULL)
                                item = mlist->getItem(client.getPlayerItem());
-                       camera.wield(item);
+                       camera.wield(item, client.getPlayerItem());
                }
 
                /*