3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "irrlichttypes_extrabloated.h"
23 #include "inventory.h"
24 #include "client/tile.h"
25 #include <ICameraSceneNode.h>
26 #include <ISceneNode.h>
28 #include "util/Optional.h"
31 struct MapDrawControl;
33 class WieldMeshSceneNode;
37 scene::ISceneNode *parent_node;
39 video::SColor textcolor;
40 Optional<video::SColor> bgcolor;
43 Nametag(scene::ISceneNode *a_parent_node,
44 const std::string &text,
45 const video::SColor &textcolor,
46 const Optional<video::SColor> &bgcolor,
48 parent_node(a_parent_node),
56 video::SColor getBgColor(bool use_fallback) const
59 return bgcolor.value();
60 else if (!use_fallback)
61 return video::SColor(0, 0, 0, 0);
62 else if (textcolor.getLuminance() > 186)
63 // Dark background for light text
64 return video::SColor(50, 50, 50, 50);
66 // Light background for dark text
67 return video::SColor(50, 255, 255, 255);
71 enum CameraMode {CAMERA_MODE_FIRST, CAMERA_MODE_THIRD, CAMERA_MODE_THIRD_FRONT};
74 Client camera class, manages the player and camera scene nodes, the viewing distance
75 and performs view bobbing etc. It also displays the wielded tool in front of the
81 Camera(MapDrawControl &draw_control, Client *client);
84 // Get camera scene node.
85 // It has the eye transformation, pitch and view bobbing applied.
86 inline scene::ICameraSceneNode* getCameraNode() const
91 // Get the camera position (in absolute scene coordinates).
92 // This has view bobbing applied.
93 inline v3f getPosition() const
95 return m_camera_position;
98 // Returns the absolute position of the head SceneNode in the world
99 inline v3f getHeadPosition() const
101 return m_headnode->getAbsolutePosition();
104 // Get the camera direction (in absolute camera coordinates).
105 // This has view bobbing applied.
106 inline v3f getDirection() const
108 return m_camera_direction;
111 // Get the camera offset
112 inline v3s16 getOffset() const
114 return m_camera_offset;
117 // Horizontal field of view
118 inline f32 getFovX() const
123 // Vertical field of view
124 inline f32 getFovY() const
129 // Get maximum of getFovX() and getFovY()
130 inline f32 getFovMax() const
132 return MYMAX(m_fov_x, m_fov_y);
135 // Notify about new server-sent FOV and initialize smooth FOV transition
136 void notifyFovChange();
138 // Checks if the constructor was able to create the scene nodes
139 bool successfullyCreated(std::string &error_message);
141 // Step the camera: updates the viewing range and view bobbing.
142 void step(f32 dtime);
144 // Update the camera from the local player's position.
145 // busytime is used to adjust the viewing range.
146 void update(LocalPlayer* player, f32 frametime, f32 busytime,
147 f32 tool_reload_ratio);
149 // Update render distance
150 void updateViewingRange();
152 // Start digging animation
153 // Pass 0 for left click, 1 for right click
154 void setDigging(s32 button);
156 // Replace the wielded item mesh
157 void wield(const ItemStack &item);
159 // Draw the wielded tool.
160 // This has to happen *after* the main scene is drawn.
161 // Warning: This clears the Z buffer.
162 void drawWieldedTool(irr::core::matrix4* translation=NULL);
164 // Toggle the current camera mode
165 void toggleCameraMode() {
166 if (m_camera_mode == CAMERA_MODE_FIRST)
167 m_camera_mode = CAMERA_MODE_THIRD;
168 else if (m_camera_mode == CAMERA_MODE_THIRD)
169 m_camera_mode = CAMERA_MODE_THIRD_FRONT;
171 m_camera_mode = CAMERA_MODE_FIRST;
174 // Set the current camera mode
175 inline void setCameraMode(CameraMode mode)
177 m_camera_mode = mode;
180 //read the current camera mode
181 inline CameraMode getCameraMode()
183 return m_camera_mode;
186 Nametag *addNametag(scene::ISceneNode *parent_node,
187 const std::string &text, video::SColor textcolor,
188 Optional<video::SColor> bgcolor, const v3f &pos);
190 void removeNametag(Nametag *nametag);
194 inline void addArmInertia(f32 player_yaw);
198 scene::ISceneNode *m_playernode = nullptr;
199 scene::ISceneNode *m_headnode = nullptr;
200 scene::ICameraSceneNode *m_cameranode = nullptr;
202 scene::ISceneManager *m_wieldmgr = nullptr;
203 WieldMeshSceneNode *m_wieldnode = nullptr;
206 MapDrawControl& m_draw_control;
210 // Default Client FOV (as defined by the "fov" setting)
213 // Absolute camera position
214 v3f m_camera_position;
215 // Absolute camera direction
216 v3f m_camera_direction;
218 v3s16 m_camera_offset;
220 // Server-sent FOV variables
221 bool m_server_sent_fov = false;
222 f32 m_curr_fov_degrees, m_old_fov_degrees, m_target_fov_degrees;
224 // FOV transition variables
225 bool m_fov_transition_active = false;
226 f32 m_fov_diff, m_transition_time;
228 v2f m_wieldmesh_offset = v2f(55.0f, -35.0f);
234 // Field of view and aspect ratio stuff
239 // View bobbing animation frame (0 <= m_view_bobbing_anim < 1)
240 f32 m_view_bobbing_anim = 0.0f;
241 // If 0, view bobbing is off (e.g. player is standing).
242 // If 1, view bobbing is on (player is walking).
243 // If 2, view bobbing is getting switched off.
244 s32 m_view_bobbing_state = 0;
245 // Speed of view bobbing animation
246 f32 m_view_bobbing_speed = 0.0f;
248 f32 m_view_bobbing_fall = 0.0f;
250 // Digging animation frame (0 <= m_digging_anim < 1)
251 f32 m_digging_anim = 0.0f;
252 // If -1, no digging animation
253 // If 0, left-click digging animation
254 // If 1, right-click digging animation
255 s32 m_digging_button = -1;
257 // Animation when changing wielded item
258 f32 m_wield_change_timer = 0.125f;
259 ItemStack m_wield_item_next;
261 CameraMode m_camera_mode = CAMERA_MODE_FIRST;
263 f32 m_cache_fall_bobbing_amount;
264 f32 m_cache_view_bobbing_amount;
267 std::list<Nametag *> m_nametags;
268 bool m_show_nametag_backgrounds;