fc87a5edd33a750804746bff0dac017b94717814
[oweals/minetest.git] / src / player.h
1 /*
2 Minetest-c55
3 Copyright (C) 2010 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 /*
21 (c) 2010 Perttu Ahola <celeron55@gmail.com>
22 */
23
24 #ifndef PLAYER_HEADER
25 #define PLAYER_HEADER
26
27 #include "common_irrlicht.h"
28 #include "inventory.h"
29
30 #define PLAYERNAME_SIZE 20
31
32 class Map;
33
34 class Player
35 {
36 public:
37         Player();
38         virtual ~Player();
39
40         //void move(f32 dtime, Map &map);
41         virtual void move(f32 dtime, Map &map) = 0;
42
43         v3f getSpeed()
44         {
45                 return m_speed;
46         }
47
48         void setSpeed(v3f speed)
49         {
50                 m_speed = speed;
51         }
52         
53         // Y direction is ignored
54         void accelerate(v3f target_speed, f32 max_increase);
55
56         v3f getPosition()
57         {
58                 return m_position;
59         }
60
61         virtual void setPosition(v3f position)
62         {
63                 m_position = position;
64         }
65
66         void setPitch(f32 pitch)
67         {
68                 m_pitch = pitch;
69         }
70
71         virtual void setYaw(f32 yaw)
72         {
73                 m_yaw = yaw;
74         }
75
76         f32 getPitch()
77         {
78                 return m_pitch;
79         }
80
81         f32 getYaw()
82         {
83                 return m_yaw;
84         }
85
86         virtual void updateName(const char *name)
87         {
88                 snprintf(m_name, PLAYERNAME_SIZE, "%s", name);
89         }
90
91         const char * getName()
92         {
93                 return m_name;
94         }
95
96         virtual bool isLocal() const = 0;
97
98         virtual void updateLight(u8 light_at_pos) {};
99
100         virtual bool isClientConnected() { return false; }
101         virtual void setClientConnected(bool) {}
102
103         bool touching_ground;
104         bool in_water;
105         
106         Inventory inventory;
107
108         u16 peer_id;
109
110 protected:
111         char m_name[PLAYERNAME_SIZE];
112         f32 m_pitch;
113         f32 m_yaw;
114         v3f m_speed;
115         v3f m_position;
116 };
117
118 class ServerRemotePlayer : public Player
119 {
120 public:
121         ServerRemotePlayer(bool client_connected):
122                 m_client_connected(client_connected)
123         {
124         }
125         virtual ~ServerRemotePlayer()
126         {
127         }
128
129         virtual bool isLocal() const
130         {
131                 return false;
132         }
133
134         virtual void move(f32 dtime, Map &map)
135         {
136         }
137
138         virtual bool isClientConnected()
139         {
140                 return m_client_connected;
141         }
142         virtual void setClientConnected(bool client_connected)
143         {
144                 m_client_connected = client_connected;
145         }
146
147         // This 
148         bool m_client_connected;
149
150 private:
151 };
152
153 #ifndef SERVER
154
155 class RemotePlayer : public Player, public scene::ISceneNode
156 {
157 public:
158         RemotePlayer(
159                 scene::ISceneNode* parent=NULL,
160                 IrrlichtDevice *device=NULL,
161                 s32 id=0);
162         
163         virtual ~RemotePlayer();
164
165         /*
166                 ISceneNode methods
167         */
168
169         virtual void OnRegisterSceneNode()
170         {
171                 if (IsVisible)
172                         SceneManager->registerNodeForRendering(this);
173
174                 ISceneNode::OnRegisterSceneNode();
175         }
176
177         virtual void render()
178         {
179                 // Do nothing
180         }
181         
182         virtual const core::aabbox3d<f32>& getBoundingBox() const
183         {
184                 return m_box;
185         }
186
187         void setPosition(v3f position)
188         {
189                 m_oldpos = m_showpos;
190                 
191                 if(m_pos_animation_time < 0.001 || m_pos_animation_time > 1.0)
192                         m_pos_animation_time = m_pos_animation_time_counter;
193                 else
194                         m_pos_animation_time = m_pos_animation_time * 0.9
195                                         + m_pos_animation_time_counter * 0.1;
196                 m_pos_animation_time_counter = 0;
197                 m_pos_animation_counter = 0;
198                 
199                 Player::setPosition(position);
200                 //ISceneNode::setPosition(position);
201         }
202
203         virtual void setYaw(f32 yaw)
204         {
205                 Player::setYaw(yaw);
206                 ISceneNode::setRotation(v3f(0, -yaw, 0));
207         }
208
209         bool isLocal() const
210         {
211                 return false;
212         }
213
214         void updateName(const char *name);
215
216         virtual void updateLight(u8 light_at_pos)
217         {
218                 if(m_node == NULL)
219                         return;
220
221                 u8 li = decode_light(light_at_pos);
222                 video::SColor color(255,li,li,li);
223
224                 scene::IMesh *mesh = m_node->getMesh();
225                 
226                 u16 mc = mesh->getMeshBufferCount();
227                 for(u16 j=0; j<mc; j++)
228                 {
229                         scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
230                         video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
231                         u16 vc = buf->getVertexCount();
232                         for(u16 i=0; i<vc; i++)
233                         {
234                                 vertices[i].Color = color;
235                         }
236                 }
237         }
238         
239         void move(f32 dtime, Map &map);
240
241 private:
242         scene::IMeshSceneNode *m_node;
243         scene::ITextSceneNode* m_text;
244         core::aabbox3d<f32> m_box;
245
246         v3f m_oldpos;
247         f32 m_pos_animation_counter;
248         f32 m_pos_animation_time;
249         f32 m_pos_animation_time_counter;
250         v3f m_showpos;
251 };
252
253 #endif
254
255 #ifndef SERVER
256 struct PlayerControl
257 {
258         PlayerControl()
259         {
260                 up = false;
261                 down = false;
262                 left = false;
263                 right = false;
264                 jump = false;
265                 superspeed = false;
266                 pitch = 0;
267                 yaw = 0;
268         }
269         PlayerControl(
270                 bool a_up,
271                 bool a_down,
272                 bool a_left,
273                 bool a_right,
274                 bool a_jump,
275                 bool a_superspeed,
276                 float a_pitch,
277                 float a_yaw
278         )
279         {
280                 up = a_up;
281                 down = a_down;
282                 left = a_left;
283                 right = a_right;
284                 jump = a_jump;
285                 superspeed = a_superspeed;
286                 pitch = a_pitch;
287                 yaw = a_yaw;
288         }
289         bool up;
290         bool down;
291         bool left;
292         bool right;
293         bool jump;
294         bool superspeed;
295         float pitch;
296         float yaw;
297 };
298
299 class LocalPlayer : public Player
300 {
301 public:
302         LocalPlayer();
303         virtual ~LocalPlayer();
304
305         bool isLocal() const
306         {
307                 return true;
308         }
309
310         void move(f32 dtime, Map &map);
311
312         void applyControl(float dtime);
313         
314         PlayerControl control;
315
316 private:
317 };
318 #endif // !SERVER
319
320 #endif
321