]> git.lizzy.rs Git - minetest.git/blobdiff - src/client/camera.h
Dual wielding
[minetest.git] / src / client / camera.h
index 403d6024c9e6aedf4ad11d68bdcf4480e7fab5ef..ae27a5e423c423be3c55b5e8559a2ea1c68406c4 100644 (file)
@@ -24,6 +24,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "client/tile.h"
 #include <ICameraSceneNode.h>
 #include <ISceneNode.h>
+#include <plane3d.h>
+#include <array>
 #include <list>
 #include "util/Optional.h"
 
@@ -69,6 +71,49 @@ struct Nametag
        }
 };
 
+enum HandIndex { MAINHAND = 0, OFFHAND = 1 };
+
+class WieldNode
+{
+public:
+       WieldNode(HandIndex index, Client *client, scene::ISceneManager *mgr);
+       void step(f32 dtime);
+       void addArmInertia(f32 player_yaw, v3f camera_direction);
+       void update(video::SColor player_light_color, f32 view_bobbing_anim, f32 tool_reload_ratio);
+       void setDigging(s32 button);
+       void wield(const ItemStack &item);
+
+private:
+       HandIndex m_index;
+       int m_direction;
+
+       Client *m_client;
+       WieldMeshSceneNode *m_meshnode = nullptr;
+
+       // Digging animation frame (0 <= m_digging_anim < 1)
+       f32 m_digging_anim = 0.0f;
+
+       // If -1, no digging animation
+       // If 0, left-click digging animation
+       // If 1, right-click digging animation
+       s32 m_digging_button = -1;
+
+       // Animation when changing wielded item
+       f32 m_change_timer = 0.125f;
+       ItemStack m_item_next;
+       bool m_item_old = false;
+
+       // Last known light color of the player
+       video::SColor m_player_light_color;
+
+       // Arm inertia
+       v2f m_offset = v2f(55.0f, -35.0f);
+       v2f m_arm_dir;
+       v2f m_cam_vel;
+       v2f m_cam_vel_old;
+       v2f m_last_cam_pos;
+};
+
 enum CameraMode {CAMERA_MODE_FIRST, CAMERA_MODE_THIRD, CAMERA_MODE_THIRD_FRONT};
 
 /*
@@ -133,6 +178,23 @@ class Camera
                return MYMAX(m_fov_x, m_fov_y);
        }
 
+       // Returns a lambda that when called with an object's position and bounding-sphere
+       // radius (both in BS space) returns true if, and only if the object should be
+       // frustum-culled.
+       auto getFrustumCuller() const
+       {
+               return [planes = getFrustumCullPlanes(),
+                               camera_offset = intToFloat(m_camera_offset, BS)
+                               ](v3f position, f32 radius) {
+                       v3f pos_camspace = position - camera_offset;
+                       for (auto &plane : planes) {
+                               if (plane.getDistanceTo(pos_camspace) > radius)
+                                       return true;
+                       }
+                       return false;
+               };
+       }
+
        // Notify about new server-sent FOV and initialize smooth FOV transition
        void notifyFovChange();
 
@@ -146,11 +208,11 @@ class Camera
        void updateViewingRange();
 
        // Start digging animation
-       // Pass 0 for left click, 1 for right click
-       void setDigging(s32 button);
+       // button: Pass 0 for left click, 1 for right click
+       void setDigging(s32 button, HandIndex hand);
 
        // Replace the wielded item mesh
-       void wield(const ItemStack &item);
+       void wield(const ItemStack &item, HandIndex hand);
 
        // Draw the wielded tool.
        // This has to happen *after* the main scene is drawn.
@@ -190,13 +252,18 @@ class Camera
        inline void addArmInertia(f32 player_yaw);
 
 private:
+       // Use getFrustumCuller().
+       // This helper just exists to decrease the header's number of includes.
+       std::array<core::plane3d<f32>, 4> getFrustumCullPlanes() const;
+
        // Nodes
        scene::ISceneNode *m_playernode = nullptr;
        scene::ISceneNode *m_headnode = nullptr;
        scene::ICameraSceneNode *m_cameranode = nullptr;
 
+       WieldNode *m_wieldnodes[2];
+
        scene::ISceneManager *m_wieldmgr = nullptr;
-       WieldMeshSceneNode *m_wieldnode = nullptr;
 
        // draw control
        MapDrawControl& m_draw_control;
@@ -223,12 +290,6 @@ class Camera
        bool m_fov_transition_active = false;
        f32 m_fov_diff, m_transition_time;
 
-       v2f m_wieldmesh_offset = v2f(55.0f, -35.0f);
-       v2f m_arm_dir;
-       v2f m_cam_vel;
-       v2f m_cam_vel_old;
-       v2f m_last_cam_pos;
-
        // Field of view and aspect ratio stuff
        f32 m_aspect = 1.0f;
        f32 m_fov_x = 1.0f;
@@ -245,17 +306,6 @@ class Camera
        // Fall view bobbing
        f32 m_view_bobbing_fall = 0.0f;
 
-       // Digging animation frame (0 <= m_digging_anim < 1)
-       f32 m_digging_anim = 0.0f;
-       // If -1, no digging animation
-       // If 0, left-click digging animation
-       // If 1, right-click digging animation
-       s32 m_digging_button = -1;
-
-       // Animation when changing wielded item
-       f32 m_wield_change_timer = 0.125f;
-       ItemStack m_wield_item_next;
-
        CameraMode m_camera_mode = CAMERA_MODE_FIRST;
 
        f32 m_cache_fall_bobbing_amount;
@@ -264,4 +314,7 @@ class Camera
 
        std::list<Nametag *> m_nametags;
        bool m_show_nametag_backgrounds;
+
+       // Last known light color of the player
+       video::SColor m_player_light_color;
 };