]> git.lizzy.rs Git - minetest.git/blob - src/camera.h
Fix and tune things, add tool "recharge" animation, add dummyball
[minetest.git] / src / camera.h
1 /*
2 Minetest-c55
3 Copyright (C) 2010-2011 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 General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
14
15 You should have received a copy of the GNU 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 #ifndef CAMERA_HEADER
21 #define CAMERA_HEADER
22
23 #include "common_irrlicht.h"
24 #include "inventory.h"
25 #include "mesh.h"
26 #include "tile.h"
27 #include "utility.h"
28 #include <ICameraSceneNode.h>
29
30 class LocalPlayer;
31 struct MapDrawControl;
32 class IGameDef;
33
34 /*
35         Client camera class, manages the player and camera scene nodes, the viewing distance
36         and performs view bobbing etc. It also displays the wielded tool in front of the
37         first-person camera.
38 */
39 class Camera
40 {
41 public:
42         Camera(scene::ISceneManager* smgr, MapDrawControl& draw_control);
43         ~Camera();
44
45         // Get player scene node.
46         // This node is positioned at the player's torso (without any view bobbing),
47         // as given by Player::m_position. Yaw is applied but not pitch.
48         inline scene::ISceneNode* getPlayerNode() const
49         {
50                 return m_playernode;
51         }
52
53         // Get head scene node.
54         // It has the eye transformation and pitch applied,
55         // but no view bobbing.
56         inline scene::ISceneNode* getHeadNode() const
57         {
58                 return m_headnode;
59         }
60
61         // Get camera scene node.
62         // It has the eye transformation, pitch and view bobbing applied.
63         inline scene::ICameraSceneNode* getCameraNode() const
64         {
65                 return m_cameranode;
66         }
67
68         // Get the camera position (in absolute scene coordinates).
69         // This has view bobbing applied.
70         inline v3f getPosition() const
71         {
72                 return m_camera_position;
73         }
74
75         // Get the camera direction (in absolute camera coordinates).
76         // This has view bobbing applied.
77         inline v3f getDirection() const
78         {
79                 return m_camera_direction;
80         }
81
82         // Horizontal field of view
83         inline f32 getFovX() const
84         {
85                 return m_fov_x;
86         }
87
88         // Vertical field of view
89         inline f32 getFovY() const
90         {
91                 return m_fov_y;
92         }
93
94         // Get maximum of getFovX() and getFovY()
95         inline f32 getFovMax() const
96         {
97                 return MYMAX(m_fov_x, m_fov_y);
98         }
99
100         // Checks if the constructor was able to create the scene nodes
101         bool successfullyCreated(std::wstring& error_message);
102
103         // Step the camera: updates the viewing range and view bobbing.
104         void step(f32 dtime);
105
106         // Update the camera from the local player's position.
107         // frametime is used to adjust the viewing range.
108         void update(LocalPlayer* player, f32 frametime, v2u32 screensize,
109                         f32 tool_reload_ratio);
110
111         // Render distance feedback loop
112         void updateViewingRange(f32 frametime_in);
113
114         // Start digging animation
115         // Pass 0 for left click, 1 for right click
116         void setDigging(s32 button);
117
118         // Replace the wielded item mesh
119         void wield(const ItemStack &item, IGameDef *gamedef);
120
121         // Draw the wielded tool.
122         // This has to happen *after* the main scene is drawn.
123         // Warning: This clears the Z buffer.
124         void drawWieldedTool();
125
126 private:
127         // Scene manager and nodes
128         scene::ISceneManager* m_smgr;
129         scene::ISceneNode* m_playernode;
130         scene::ISceneNode* m_headnode;
131         scene::ICameraSceneNode* m_cameranode;
132
133         scene::ISceneManager* m_wieldmgr;
134         scene::IMeshSceneNode* m_wieldnode;
135         u8 m_wieldlight;
136
137         // draw control
138         MapDrawControl& m_draw_control;
139
140         // Absolute camera position
141         v3f m_camera_position;
142         // Absolute camera direction
143         v3f m_camera_direction;
144
145         // Field of view and aspect ratio stuff
146         f32 m_aspect;
147         f32 m_fov_x;
148         f32 m_fov_y;
149
150         // Stuff for viewing range calculations
151         f32 m_added_frametime;
152         s16 m_added_frames;
153         f32 m_range_old;
154         f32 m_frametime_old;
155         f32 m_frametime_counter;
156         f32 m_time_per_range;
157
158         // View bobbing animation frame (0 <= m_view_bobbing_anim < 1)
159         f32 m_view_bobbing_anim;
160         // If 0, view bobbing is off (e.g. player is standing).
161         // If 1, view bobbing is on (player is walking).
162         // If 2, view bobbing is getting switched off.
163         s32 m_view_bobbing_state;
164         // Speed of view bobbing animation
165         f32 m_view_bobbing_speed;
166
167         // Digging animation frame (0 <= m_digging_anim < 1)
168         f32 m_digging_anim;
169         // If -1, no digging animation
170         // If 0, left-click digging animation
171         // If 1, right-click digging animation
172         s32 m_digging_button;
173 };
174
175 #endif