new object system
[oweals/minetest.git] / src / clientobject.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 CLIENTOBJECT_HEADER
21 #define CLIENTOBJECT_HEADER
22
23 #include "common_irrlicht.h"
24 #include "activeobject.h"
25
26 /*
27
28 Some planning
29 -------------
30
31 * Client receives a network packet with information of added objects
32   in it
33 * Client supplies the information to its ClientEnvironment
34 * The environment adds the specified objects to itself
35
36 */
37
38 class ClientEnvironment;
39
40 class ClientActiveObject : public ActiveObject
41 {
42 public:
43         ClientActiveObject(u16 id);
44         virtual ~ClientActiveObject();
45
46         virtual void addToScene(scene::ISceneManager *smgr){}
47         virtual void removeFromScene(){}
48         // 0 <= light_at_pos <= LIGHT_SUN
49         virtual void updateLight(u8 light_at_pos){}
50         virtual v3s16 getLightPosition(){return v3s16(0,0,0);}
51         virtual core::aabbox3d<f32>* getSelectionBox(){return NULL;}
52         virtual core::aabbox3d<f32>* getCollisionBox(){return NULL;}
53         virtual v3f getPosition(){return v3f(0,0,0);}
54         
55         // Step object in time
56         virtual void step(float dtime, ClientEnvironment *env){}
57         
58         // Process a message sent by the server side object
59         virtual void processMessage(const std::string &data){}
60
61         virtual std::string infoText() {return "";}
62
63         /*
64                 This takes the return value of
65                 ServerActiveObject::getClientInitializationData
66         */
67         virtual void initialize(const std::string &data){}
68         
69         // Create a certain type of ClientActiveObject
70         static ClientActiveObject* create(u8 type);
71
72 protected:
73         // Used for creating objects based on type
74         typedef ClientActiveObject* (*Factory)();
75         static void registerType(u16 type, Factory f);
76 private:
77         // Used for creating objects based on type
78         static core::map<u16, Factory> m_types;
79 };
80
81 struct DistanceSortedActiveObject
82 {
83         ClientActiveObject *obj;
84         f32 d;
85
86         DistanceSortedActiveObject(ClientActiveObject *a_obj, f32 a_d)
87         {
88                 obj = a_obj;
89                 d = a_d;
90         }
91
92         bool operator < (DistanceSortedActiveObject &other)
93         {
94                 return d < other.d;
95         }
96 };
97
98 /*
99         TestCAO
100 */
101
102 class TestCAO : public ClientActiveObject
103 {
104 public:
105         TestCAO();
106         virtual ~TestCAO();
107         
108         u8 getType() const
109         {
110                 return ACTIVEOBJECT_TYPE_TEST;
111         }
112         
113         static ClientActiveObject* create();
114
115         void addToScene(scene::ISceneManager *smgr);
116         void removeFromScene();
117         void updateLight(u8 light_at_pos);
118         v3s16 getLightPosition();
119         void updateNodePos();
120
121         void step(float dtime, ClientEnvironment *env);
122
123         void processMessage(const std::string &data);
124
125 private:
126         scene::IMeshSceneNode *m_node;
127         v3f m_position;
128 };
129
130 /*
131         ItemCAO
132 */
133
134 class ItemCAO : public ClientActiveObject
135 {
136 public:
137         ItemCAO();
138         virtual ~ItemCAO();
139         
140         u8 getType() const
141         {
142                 return ACTIVEOBJECT_TYPE_ITEM;
143         }
144         
145         static ClientActiveObject* create();
146
147         void addToScene(scene::ISceneManager *smgr);
148         void removeFromScene();
149         void updateLight(u8 light_at_pos);
150         v3s16 getLightPosition();
151         void updateNodePos();
152
153         void step(float dtime, ClientEnvironment *env);
154
155         void processMessage(const std::string &data);
156
157         void initialize(const std::string &data);
158         
159         core::aabbox3d<f32>* getSelectionBox()
160                 {return &m_selection_box;}
161         v3f getPosition()
162                 {return m_position;}
163
164 private:
165         core::aabbox3d<f32> m_selection_box;
166         scene::IMeshSceneNode *m_node;
167         v3f m_position;
168         std::string m_inventorystring;
169 };
170
171 #endif
172