]> git.lizzy.rs Git - dragonfireclient.git/blob - src/player.h
.
[dragonfireclient.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         // NOTE: Use peer_id == 0 for disconnected
101         /*virtual bool isClientConnected() { return false; }
102         virtual void setClientConnected(bool) {}*/
103
104         bool touching_ground;
105         bool in_water;
106         
107         Inventory inventory;
108
109         u16 peer_id;
110
111 protected:
112         char m_name[PLAYERNAME_SIZE];
113         f32 m_pitch;
114         f32 m_yaw;
115         v3f m_speed;
116         v3f m_position;
117 };
118
119 class ServerRemotePlayer : public Player
120 {
121 public:
122         /*ServerRemotePlayer(bool client_connected):
123                 m_client_connected(client_connected)*/
124         ServerRemotePlayer()
125         {
126         }
127         virtual ~ServerRemotePlayer()
128         {
129         }
130
131         virtual bool isLocal() const
132         {
133                 return false;
134         }
135
136         virtual void move(f32 dtime, Map &map)
137         {
138         }
139
140         /*virtual bool isClientConnected()
141         {
142                 return m_client_connected;
143         }
144         virtual void setClientConnected(bool client_connected)
145         {
146                 m_client_connected = client_connected;
147         }
148
149         // This 
150         bool m_client_connected;*/
151
152 private:
153 };
154
155 #ifndef SERVER
156
157 class RemotePlayer : public Player, public scene::ISceneNode
158 {
159 public:
160         RemotePlayer(
161                 scene::ISceneNode* parent=NULL,
162                 IrrlichtDevice *device=NULL,
163                 s32 id=0);
164         
165         virtual ~RemotePlayer();
166
167         /*
168                 ISceneNode methods
169         */
170
171         virtual void OnRegisterSceneNode()
172         {
173                 if (IsVisible)
174                         SceneManager->registerNodeForRendering(this);
175
176                 ISceneNode::OnRegisterSceneNode();
177         }
178
179         virtual void render()
180         {
181                 // Do nothing
182         }
183         
184         virtual const core::aabbox3d<f32>& getBoundingBox() const
185         {
186                 return m_box;
187         }
188
189         void setPosition(v3f position)
190         {
191                 m_oldpos = m_showpos;
192                 
193                 if(m_pos_animation_time < 0.001 || m_pos_animation_time > 1.0)
194                         m_pos_animation_time = m_pos_animation_time_counter;
195                 else
196                         m_pos_animation_time = m_pos_animation_time * 0.9
197                                         + m_pos_animation_time_counter * 0.1;
198                 m_pos_animation_time_counter = 0;
199                 m_pos_animation_counter = 0;
200                 
201                 Player::setPosition(position);
202                 //ISceneNode::setPosition(position);
203         }
204
205         virtual void setYaw(f32 yaw)
206         {
207                 Player::setYaw(yaw);
208                 ISceneNode::setRotation(v3f(0, -yaw, 0));
209         }
210
211         bool isLocal() const
212         {
213                 return false;
214         }
215
216         void updateName(const char *name);
217
218         virtual void updateLight(u8 light_at_pos)
219         {
220                 if(m_node == NULL)
221                         return;
222
223                 u8 li = decode_light(light_at_pos);
224                 video::SColor color(255,li,li,li);
225
226                 scene::IMesh *mesh = m_node->getMesh();
227                 
228                 u16 mc = mesh->getMeshBufferCount();
229                 for(u16 j=0; j<mc; j++)
230                 {
231                         scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
232                         video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
233                         u16 vc = buf->getVertexCount();
234                         for(u16 i=0; i<vc; i++)
235                         {
236                                 vertices[i].Color = color;
237                         }
238                 }
239         }
240         
241         void move(f32 dtime, Map &map);
242
243 private:
244         scene::IMeshSceneNode *m_node;
245         scene::ITextSceneNode* m_text;
246         core::aabbox3d<f32> m_box;
247
248         v3f m_oldpos;
249         f32 m_pos_animation_counter;
250         f32 m_pos_animation_time;
251         f32 m_pos_animation_time_counter;
252         v3f m_showpos;
253 };
254
255 #endif
256
257 #ifndef SERVER
258 struct PlayerControl
259 {
260         PlayerControl()
261         {
262                 up = false;
263                 down = false;
264                 left = false;
265                 right = false;
266                 jump = false;
267                 superspeed = false;
268                 pitch = 0;
269                 yaw = 0;
270         }
271         PlayerControl(
272                 bool a_up,
273                 bool a_down,
274                 bool a_left,
275                 bool a_right,
276                 bool a_jump,
277                 bool a_superspeed,
278                 float a_pitch,
279                 float a_yaw
280         )
281         {
282                 up = a_up;
283                 down = a_down;
284                 left = a_left;
285                 right = a_right;
286                 jump = a_jump;
287                 superspeed = a_superspeed;
288                 pitch = a_pitch;
289                 yaw = a_yaw;
290         }
291         bool up;
292         bool down;
293         bool left;
294         bool right;
295         bool jump;
296         bool superspeed;
297         float pitch;
298         float yaw;
299 };
300
301 class LocalPlayer : public Player
302 {
303 public:
304         LocalPlayer();
305         virtual ~LocalPlayer();
306
307         bool isLocal() const
308         {
309                 return true;
310         }
311
312         void move(f32 dtime, Map &map);
313
314         void applyControl(float dtime);
315         
316         PlayerControl control;
317
318 private:
319 };
320 #endif // !SERVER
321
322 #endif
323