]> git.lizzy.rs Git - dragonfireclient.git/blob - src/client/camera.h
Localize error messages in mainmenu (#11495)
[dragonfireclient.git] / src / client / camera.h
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #pragma once
21
22 #include "irrlichttypes_extrabloated.h"
23 #include "inventory.h"
24 #include "client/tile.h"
25 #include <ICameraSceneNode.h>
26 #include <ISceneNode.h>
27 #include <list>
28 #include "util/Optional.h"
29
30 class LocalPlayer;
31 struct MapDrawControl;
32 class Client;
33 class RenderingEngine;
34 class WieldMeshSceneNode;
35
36 struct Nametag
37 {
38         scene::ISceneNode *parent_node;
39         std::string text;
40         video::SColor textcolor;
41         Optional<video::SColor> bgcolor;
42         v3f pos;
43
44         Nametag(scene::ISceneNode *a_parent_node,
45                         const std::string &text,
46                         const video::SColor &textcolor,
47                         const Optional<video::SColor> &bgcolor,
48                         const v3f &pos):
49                 parent_node(a_parent_node),
50                 text(text),
51                 textcolor(textcolor),
52                 bgcolor(bgcolor),
53                 pos(pos)
54         {
55         }
56
57         video::SColor getBgColor(bool use_fallback) const
58         {
59                 if (bgcolor)
60                         return bgcolor.value();
61                 else if (!use_fallback)
62                         return video::SColor(0, 0, 0, 0);
63                 else if (textcolor.getLuminance() > 186)
64                         // Dark background for light text
65                         return video::SColor(50, 50, 50, 50);
66                 else
67                         // Light background for dark text
68                         return video::SColor(50, 255, 255, 255);
69         }
70 };
71
72 enum CameraMode {CAMERA_MODE_FIRST, CAMERA_MODE_THIRD, CAMERA_MODE_THIRD_FRONT};
73
74 /*
75         Client camera class, manages the player and camera scene nodes, the viewing distance
76         and performs view bobbing etc. It also displays the wielded tool in front of the
77         first-person camera.
78 */
79 class Camera
80 {
81 public:
82         Camera(MapDrawControl &draw_control, Client *client, RenderingEngine *rendering_engine);
83         ~Camera();
84
85         // Get camera scene node.
86         // It has the eye transformation, pitch and view bobbing applied.
87         inline scene::ICameraSceneNode* getCameraNode() const
88         {
89                 return m_cameranode;
90         }
91
92         // Get the camera position (in absolute scene coordinates).
93         // This has view bobbing applied.
94         inline v3f getPosition() const
95         {
96                 return m_camera_position;
97         }
98
99         // Returns the absolute position of the head SceneNode in the world
100         inline v3f getHeadPosition() const
101         {
102                 return m_headnode->getAbsolutePosition();
103         }
104
105         // Get the camera direction (in absolute camera coordinates).
106         // This has view bobbing applied.
107         inline v3f getDirection() const
108         {
109                 return m_camera_direction;
110         }
111
112         // Get the camera offset
113         inline v3s16 getOffset() const
114         {
115                 return m_camera_offset;
116         }
117
118         // Horizontal field of view
119         inline f32 getFovX() const
120         {
121                 return m_fov_x;
122         }
123
124         // Vertical field of view
125         inline f32 getFovY() const
126         {
127                 return m_fov_y;
128         }
129
130         // Get maximum of getFovX() and getFovY()
131         inline f32 getFovMax() const
132         {
133                 return MYMAX(m_fov_x, m_fov_y);
134         }
135
136         // Notify about new server-sent FOV and initialize smooth FOV transition
137         void notifyFovChange();
138
139         // Step the camera: updates the viewing range and view bobbing.
140         void step(f32 dtime);
141
142         // Update the camera from the local player's position.
143         // busytime is used to adjust the viewing range.
144         void update(LocalPlayer* player, f32 frametime, f32 busytime,
145                         f32 tool_reload_ratio);
146
147         // Update render distance
148         void updateViewingRange();
149
150         // Start digging animation
151         // Pass 0 for left click, 1 for right click
152         void setDigging(s32 button);
153
154         // Replace the wielded item mesh
155         void wield(const ItemStack &item);
156
157         // Draw the wielded tool.
158         // This has to happen *after* the main scene is drawn.
159         // Warning: This clears the Z buffer.
160         void drawWieldedTool(irr::core::matrix4* translation=NULL);
161
162         // Toggle the current camera mode
163         void toggleCameraMode() {
164                 if (m_camera_mode == CAMERA_MODE_FIRST)
165                         m_camera_mode = CAMERA_MODE_THIRD;
166                 else if (m_camera_mode == CAMERA_MODE_THIRD)
167                         m_camera_mode = CAMERA_MODE_THIRD_FRONT;
168                 else
169                         m_camera_mode = CAMERA_MODE_FIRST;
170         }
171
172         // Set the current camera mode
173         inline void setCameraMode(CameraMode mode)
174         {
175                 m_camera_mode = mode;
176         }
177
178         //read the current camera mode
179         inline CameraMode getCameraMode()
180         {
181                 return m_camera_mode;
182         }
183
184         Nametag *addNametag(scene::ISceneNode *parent_node,
185                 const std::string &text, video::SColor textcolor,
186                 Optional<video::SColor> bgcolor, const v3f &pos);
187
188         void removeNametag(Nametag *nametag);
189
190         void drawNametags();
191
192         inline void addArmInertia(f32 player_yaw);
193
194 private:
195         // Nodes
196         scene::ISceneNode *m_playernode = nullptr;
197         scene::ISceneNode *m_headnode = nullptr;
198         scene::ICameraSceneNode *m_cameranode = nullptr;
199
200         scene::ISceneManager *m_wieldmgr = nullptr;
201         WieldMeshSceneNode *m_wieldnode = nullptr;
202
203         // draw control
204         MapDrawControl& m_draw_control;
205
206         Client *m_client;
207
208         // Default Client FOV (as defined by the "fov" setting)
209         f32 m_cache_fov;
210
211         // Absolute camera position
212         v3f m_camera_position;
213         // Absolute camera direction
214         v3f m_camera_direction;
215         // Camera offset
216         v3s16 m_camera_offset;
217
218         bool m_stepheight_smooth_active = false;
219
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;
223
224         // FOV transition variables
225         bool m_fov_transition_active = false;
226         f32 m_fov_diff, m_transition_time;
227
228         v2f m_wieldmesh_offset = v2f(55.0f, -35.0f);
229         v2f m_arm_dir;
230         v2f m_cam_vel;
231         v2f m_cam_vel_old;
232         v2f m_last_cam_pos;
233
234         // Field of view and aspect ratio stuff
235         f32 m_aspect = 1.0f;
236         f32 m_fov_x = 1.0f;
237         f32 m_fov_y = 1.0f;
238
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;
247         // Fall view bobbing
248         f32 m_view_bobbing_fall = 0.0f;
249
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;
256
257         // Animation when changing wielded item
258         f32 m_wield_change_timer = 0.125f;
259         ItemStack m_wield_item_next;
260
261         CameraMode m_camera_mode = CAMERA_MODE_FIRST;
262
263         f32 m_cache_fall_bobbing_amount;
264         f32 m_cache_view_bobbing_amount;
265         bool m_arm_inertia;
266
267         std::list<Nametag *> m_nametags;
268         bool m_show_nametag_backgrounds;
269 };