From 3e2efdf18ab95d3c88f66c7a1f6caf6b1c7231e5 Mon Sep 17 00:00:00 2001 From: PilzAdam Date: Mon, 20 May 2013 00:20:42 +0200 Subject: Add a little animation when changing the wielded item --- src/camera.cpp | 61 +++++++++++++++++++++++++++++++++++++++++++++++----------- src/camera.h | 8 +++++++- src/game.cpp | 2 +- 3 files changed, 58 insertions(+), 13 deletions(-) diff --git a/src/camera.cpp b/src/camera.cpp index 0dd0a767b..f227bd98a 100644 --- a/src/camera.cpp +++ b/src/camera.cpp @@ -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< 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; } } diff --git a/src/camera.h b/src/camera.h index 0ed5c20d2..c8a6e0bb8 100644 --- a/src/camera.h +++ b/src/camera.h @@ -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 diff --git a/src/game.cpp b/src/game.cpp index 746769aee..185f42eae 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -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()); } /* -- cgit v1.2.3