Display which tool the player is holding
authorJacobF <queatz@gmail.com>
Wed, 7 Sep 2011 14:56:04 +0000 (10:56 -0400)
committerJacobF <queatz@gmail.com>
Wed, 7 Sep 2011 14:56:04 +0000 (10:56 -0400)
And some simple animation when trying to dig...

src/client.cpp
src/client.h
src/game.cpp
src/inventory.h
src/player.cpp
src/player.h

index a5ed6f61bf204807399b9e342d7f330bf83aa754..d491f9d3646b461a40339335d80e38cf1dbb9421 100644 (file)
@@ -2008,6 +2008,14 @@ v3f Client::getPlayerPosition(v3f *eye_position)
        return player->getPosition();
 }
 
+void Client::setPlayerWield(scene::ISceneNode *wield)
+{
+       //JMutexAutoLock envlock(m_env_mutex); //bulk comment-out
+       LocalPlayer *player = m_env.getLocalPlayer();
+       assert(player != NULL);
+       player->wield = wield;
+}
+
 void Client::setPlayerControl(PlayerControl &control)
 {
        //JMutexAutoLock envlock(m_env_mutex); //bulk comment-out
index 1a7ef924ac53d7b9085c4a75e59e480dd60f9761..398a3b849420ea37189f8f78082ff57520435f4a 100644 (file)
@@ -210,6 +210,7 @@ public:
        // eye position in *eye_position
        v3f getPlayerPosition(v3f *eye_position=NULL);
 
+       void setPlayerWield(scene::ISceneNode *wield);
        void setPlayerControl(PlayerControl &control);
 
        void selectPlayerItem(u16 item);
index dc3ed24569cbb56587bd5a5fe449f35fcb69ceb3..86f1939c382922eebb49ce2ee572f69b128a04b7 100644 (file)
@@ -956,7 +956,10 @@ void the_game(
                v3f(BS*100, BS*2, BS*100), // Look from
                v3f(BS*100+1, BS*2, BS*100), // Look to
                -1 // Camera ID
-       );
+       );
+       
+       // Do this so children rotate accordingly (tool)
+       camera->bindTargetAndRotation(true);
 
        if(camera == NULL)
        {
@@ -972,6 +975,44 @@ void the_game(
        f32 camera_yaw = 0; // "right/left"
        f32 camera_pitch = 0; // "up/down"
 
+       /*
+               Tool
+       */
+       
+       v3f tool_wield_position(0.6, -0.6, 1.0);
+       v3f tool_wield_rotation(-25, 180, -25);
+       float tool_wield_animation = 0.0;
+       scene::IMeshSceneNode *tool_wield;
+       {
+               scene::SMesh *mesh = new scene::SMesh();
+               scene::IMeshBuffer *buf = new scene::SMeshBuffer();
+               video::SColor c(255,255,255,255);
+               video::S3DVertex vertices[4] =
+               {
+                       video::S3DVertex(-0.5,0,0, 0,0,0, c, 0,1),
+                       video::S3DVertex(0.5,0,0, 0,0,0, c, 1,1),
+                       video::S3DVertex(0.5,0.5,0, 0,0,0, c, 1,0),
+                       video::S3DVertex(-0.5,0.5,0, 0,0,0, c, 0,0),
+               };
+               u16 indices[] = {0,1,2,2,3,0};
+               buf->append(vertices, 4, indices, 6);
+               // Set material
+               buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
+               buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
+               buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
+               // Add to mesh
+               mesh->addMeshBuffer(buf);
+               buf->drop();
+       
+               tool_wield = smgr->addMeshSceneNode(mesh, camera);
+               mesh->drop();
+       }
+       tool_wield->setVisible(false);
+       tool_wield->setPosition(tool_wield_position);
+       tool_wield->setRotation(tool_wield_rotation);
+       
+       client.setPlayerWield(tool_wield);
+
        /*
                Clouds
        */
@@ -1920,6 +1961,7 @@ void the_game(
                                }
                        }
                        
+                       
                        if(input->getRightClicked())
                        {
                                std::cout<<DTIME<<"Ground right-clicked"<<std::endl;
@@ -1986,6 +2028,14 @@ void the_game(
                else{
                }
 
+               
+               if(input->getLeftState())
+                       // Tool animation loops 0.0 - 1.0 
+                       tool_wield_animation = fmod(tool_wield_animation + dtime * 3.0, 1.0);
+               else
+                        // Return tool to holding position if not digging
+                       tool_wield_animation /= 1.5;
+
                } // selected_object == NULL
                
                input->resetLeftClicked();
@@ -2103,6 +2153,14 @@ void the_game(
                        );
                }
 
+               /*
+                       Animate tool
+               */
+               {
+                       tool_wield->setRotation(tool_wield_rotation - sin(tool_wield_animation * PI) * 40.0);
+                       tool_wield->setPosition(tool_wield_position - sin(tool_wield_animation * PI) / 3.0);
+               }
+
 
                /*
                        Update gui stuff (0ms)
index 44ba6a5adf7cc3e5ebe5c56a943fc3137739a4f3..f70d69b6d71a33116cb575058234fc25aacdfc4e 100644 (file)
@@ -53,6 +53,10 @@ public:
        // Shall make an exact clone of the item
        virtual InventoryItem* clone() = 0;
 #ifndef SERVER
+       // Return the name of the image for this item
+       virtual std::string getBasename() { return ""; }
+       // Shall return an image of the item (or NULL)
+       virtual video::ITexture * getImageRaw() { return NULL; }
        // Shall return an image to show in the GUI (or NULL)
        virtual video::ITexture * getImage() { return NULL; }
 #endif
@@ -353,40 +357,53 @@ public:
                return new ToolItem(m_toolname, m_wear);
        }
 #ifndef SERVER
-       video::ITexture * getImage()
-       {
-               if(g_texturesource == NULL)
-                       return NULL;
-               
-               std::string basename;
+       std::string getBasename() {
                if(m_toolname == "WPick")
-                       basename = "tool_woodpick.png";
+                       return "tool_woodpick.png";
                else if(m_toolname == "STPick")
-                       basename = "tool_stonepick.png";
+                       return "tool_stonepick.png";
                else if(m_toolname == "SteelPick")
-                       basename = "tool_steelpick.png";
+                       return "tool_steelpick.png";
                else if(m_toolname == "MesePick")
-                       basename = "tool_mesepick.png";
+                       return "tool_mesepick.png";
                else if(m_toolname == "WShovel")
-                       basename = "tool_woodshovel.png";
+                       return "tool_woodshovel.png";
                else if(m_toolname == "STShovel")
-                       basename = "tool_stoneshovel.png";
+                       return "tool_stoneshovel.png";
                else if(m_toolname == "SteelShovel")
-                       basename = "tool_steelshovel.png";
+                       return "tool_steelshovel.png";
                else if(m_toolname == "WAxe")
-                       basename = "tool_woodaxe.png";
+                       return "tool_woodaxe.png";
                else if(m_toolname == "STAxe")
-                       basename = "tool_stoneaxe.png";
+                       return "tool_stoneaxe.png";
                else if(m_toolname == "SteelAxe")
-                       basename = "tool_steelaxe.png";
+                       return "tool_steelaxe.png";
                else if(m_toolname == "WSword")
-                       basename = "tool_woodsword.png";
+                       return "tool_woodsword.png";
                else if(m_toolname == "STSword")
-                       basename = "tool_stonesword.png";
+                       return "tool_stonesword.png";
                else if(m_toolname == "SteelSword")
-                       basename = "tool_steelsword.png";
+                       return "tool_steelsword.png";
                else
-                       basename = "cloud.png";
+                       return "cloud.png";
+}
+       
+       video::ITexture * getImageRaw()
+       {
+               if(g_texturesource == NULL)
+                       return NULL;
+               
+               return g_texturesource->getTextureRaw(getBasename());
+       }
+       
+       video::ITexture * getImage()
+       {
+               if(g_texturesource == NULL)
+                       return NULL;
+               
+               std::string basename = getBasename();
+               
+               return g_texturesource->getTextureRaw(basename);
                
                /*
                        Calculate a progress value with sane amount of
index 7cfdfebb69d45ebb739f50c43b51b5bd750b9e5a..03ae24f45b07de8734441a43be5c045082d16d98 100644 (file)
@@ -309,12 +309,31 @@ LocalPlayer::LocalPlayer():
        // Initialize hp to 0, so that no hearts will be shown if server
        // doesn't support health points
        hp = 0;
+       
+       // No tool wielded initially
+       wield = NULL;
 }
 
 LocalPlayer::~LocalPlayer()
 {
 }
 
+void LocalPlayer::wieldItem(u16 item)
+{
+       m_selected_item = item;
+       
+       if(wield) {
+               InventoryItem* i = inventory.getList("main")->getItem(m_selected_item);
+       
+               if(i && strcmp(i->getName(), "ToolItem") == 0) {
+                       wield->getMaterial(0).setTexture(0, i->getImageRaw());
+                       wield->setVisible(true);
+               }
+               else
+                       wield->setVisible(false);
+       }
+}
+
 void LocalPlayer::move(f32 dtime, Map &map, f32 pos_max_d,
                core::list<CollisionInfo> *collision_info)
 {
index 13cffa205a6986f802b77d0bd2fac2f7f486badd..0f2c400114c45508f92269ee2830fc22a520d9ad 100644 (file)
@@ -356,6 +356,8 @@ public:
        {
                return true;
        }
+       
+       void wieldItem(u16 item);
 
        void move(f32 dtime, Map &map, f32 pos_max_d,
                        core::list<CollisionInfo> *collision_info);
@@ -365,6 +367,8 @@ public:
        
        PlayerControl control;
 
+       scene::ISceneNode *wield;
+
 private:
        // This is used for determining the sneaking range
        v3s16 m_sneak_node;