]> git.lizzy.rs Git - minetest.git/blob - src/player.h
82ed92649d767baf763c754a0f7c7f8369d891a4
[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
42         v3f getSpeed()
43         {
44                 return m_speed;
45         }
46
47         void setSpeed(v3f speed)
48         {
49                 m_speed = speed;
50         }
51         
52         // Y direction is ignored
53         void accelerate(v3f target_speed, f32 max_increase);
54
55         v3f getPosition()
56         {
57                 return m_position;
58         }
59
60         virtual void setPosition(v3f position)
61         {
62                 m_position = position;
63         }
64
65         void setPitch(f32 pitch)
66         {
67                 m_pitch = pitch;
68         }
69
70         virtual void setYaw(f32 yaw)
71         {
72                 m_yaw = yaw;
73         }
74
75         f32 getPitch()
76         {
77                 return m_pitch;
78         }
79
80         f32 getYaw()
81         {
82                 return m_yaw;
83         }
84
85         virtual void updateName(const char *name)
86         {
87                 snprintf(m_name, PLAYERNAME_SIZE, "%s", name);
88         }
89
90         const char * getName()
91         {
92                 return m_name;
93         }
94
95         virtual bool isLocal() const = 0;
96
97         bool touching_ground;
98         
99         Inventory inventory;
100
101         u16 peer_id;
102
103 protected:
104         char m_name[PLAYERNAME_SIZE];
105         f32 m_pitch;
106         f32 m_yaw;
107         v3f m_speed;
108         v3f m_position;
109 };
110
111 class RemotePlayer : public Player, public scene::ISceneNode
112 {
113 public:
114         RemotePlayer(
115                 scene::ISceneNode* parent=NULL,
116                 IrrlichtDevice *device=NULL,
117                 s32 id=0);
118         
119         virtual ~RemotePlayer();
120
121         /*
122                 ISceneNode methods
123         */
124
125         virtual void OnRegisterSceneNode()
126         {
127                 if (IsVisible)
128                         SceneManager->registerNodeForRendering(this);
129
130                 ISceneNode::OnRegisterSceneNode();
131         }
132
133         virtual void render()
134         {
135                 // Do nothing
136         }
137         
138         virtual const core::aabbox3d<f32>& getBoundingBox() const
139         {
140                 return m_box;
141         }
142
143         void setPosition(v3f position)
144         {
145                 Player::setPosition(position);
146                 ISceneNode::setPosition(position);
147         }
148
149         virtual void setYaw(f32 yaw)
150         {
151                 Player::setYaw(yaw);
152                 ISceneNode::setRotation(v3f(0, -yaw, 0));
153         }
154
155         bool isLocal() const
156         {
157                 return false;
158         }
159
160         void updateName(const char *name);
161
162 private:
163         scene::ITextSceneNode* m_text;
164         core::aabbox3d<f32> m_box;
165 };
166
167 struct PlayerControl
168 {
169         PlayerControl()
170         {
171                 up = false;
172                 down = false;
173                 left = false;
174                 right = false;
175                 jump = false;
176                 superspeed = false;
177                 pitch = 0;
178                 yaw = 0;
179         }
180         PlayerControl(
181                 bool a_up,
182                 bool a_down,
183                 bool a_left,
184                 bool a_right,
185                 bool a_jump,
186                 bool a_superspeed,
187                 float a_pitch,
188                 float a_yaw
189         )
190         {
191                 up = a_up;
192                 down = a_down;
193                 left = a_left;
194                 right = a_right;
195                 jump = a_jump;
196                 superspeed = a_superspeed;
197                 pitch = a_pitch;
198                 yaw = a_yaw;
199         }
200         bool up;
201         bool down;
202         bool left;
203         bool right;
204         bool jump;
205         bool superspeed;
206         float pitch;
207         float yaw;
208 };
209
210 class LocalPlayer : public Player
211 {
212 public:
213         LocalPlayer();
214         virtual ~LocalPlayer();
215
216         bool isLocal() const
217         {
218                 return true;
219         }
220
221         void applyControl(float dtime);
222         
223         PlayerControl control;
224
225 private:
226 };
227
228 #endif
229