3 Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
20 #include "clientobject.h"
23 #include "constants.h"
25 #include "environment.h"
27 core::map<u16, ClientActiveObject::Factory> ClientActiveObject::m_types;
29 ClientActiveObject::ClientActiveObject(u16 id):
34 ClientActiveObject::~ClientActiveObject()
39 ClientActiveObject* ClientActiveObject::create(u8 type)
41 // Find factory function
42 core::map<u16, Factory>::Node *n;
43 n = m_types.find(type);
46 // If factory is not found, just return.
47 dstream<<"WARNING: ClientActiveObject: No factory for type="
52 Factory f = n->getValue();
53 ClientActiveObject *object = (*f)();
57 void ClientActiveObject::registerType(u16 type, Factory f)
59 core::map<u16, Factory>::Node *n;
60 n = m_types.find(type);
63 m_types.insert(type, f);
71 TestCAO proto_TestCAO;
74 ClientActiveObject(0),
76 m_position(v3f(0,10*BS,0))
78 ClientActiveObject::registerType(getType(), create);
85 ClientActiveObject* TestCAO::create()
90 void TestCAO::addToScene(scene::ISceneManager *smgr)
95 video::IVideoDriver* driver = smgr->getVideoDriver();
97 scene::SMesh *mesh = new scene::SMesh();
98 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
99 video::SColor c(255,255,255,255);
100 video::S3DVertex vertices[4] =
102 video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
103 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
104 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
105 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),
107 u16 indices[] = {0,1,2,2,3,0};
108 buf->append(vertices, 4, indices, 6);
110 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
111 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
112 buf->getMaterial().setTexture
113 (0, driver->getTexture(porting::getDataPath("rat.png").c_str()));
114 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
115 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
116 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
118 mesh->addMeshBuffer(buf);
120 m_node = smgr->addMeshSceneNode(mesh, NULL);
125 void TestCAO::removeFromScene()
134 void TestCAO::updateLight(u8 light_at_pos)
138 v3s16 TestCAO::getLightPosition()
140 return floatToInt(m_position, BS);
143 void TestCAO::updateNodePos()
148 m_node->setPosition(m_position);
149 //m_node->setRotation(v3f(0, 45, 0));
152 void TestCAO::step(float dtime, ClientEnvironment *env)
156 v3f rot = m_node->getRotation();
157 //dstream<<"dtime="<<dtime<<", rot.Y="<<rot.Y<<std::endl;
158 rot.Y += dtime * 180;
159 m_node->setRotation(rot);
163 void TestCAO::processMessage(const std::string &data)
165 dstream<<"TestCAO: Got data: "<<data<<std::endl;
166 std::istringstream is(data, std::ios::binary);
184 #include "inventory.h"
187 ItemCAO proto_ItemCAO;
190 ClientActiveObject(0),
191 m_selection_box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.),
193 m_position(v3f(0,10*BS,0))
195 ClientActiveObject::registerType(getType(), create);
202 ClientActiveObject* ItemCAO::create()
204 return new ItemCAO();
207 void ItemCAO::addToScene(scene::ISceneManager *smgr)
212 video::IVideoDriver* driver = smgr->getVideoDriver();
214 scene::SMesh *mesh = new scene::SMesh();
215 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
216 video::SColor c(255,255,255,255);
217 video::S3DVertex vertices[4] =
219 /*video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
220 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
221 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
222 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),*/
223 video::S3DVertex(BS/3.,0,0, 0,0,0, c, 0,1),
224 video::S3DVertex(-BS/3.,0,0, 0,0,0, c, 1,1),
225 video::S3DVertex(-BS/3.,0+BS*2./3.,0, 0,0,0, c, 1,0),
226 video::S3DVertex(BS/3.,0+BS*2./3.,0, 0,0,0, c, 0,0),
228 u16 indices[] = {0,1,2,2,3,0};
229 buf->append(vertices, 4, indices, 6);
231 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
232 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
233 //buf->getMaterial().setTexture(0, NULL);
234 buf->getMaterial().setTexture
235 (0, driver->getTexture(porting::getDataPath("rat.png").c_str()));
236 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
237 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
238 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
240 mesh->addMeshBuffer(buf);
242 m_node = smgr->addMeshSceneNode(mesh, NULL);
244 // Set it to use the materials of the meshbuffers directly.
245 // This is needed for changing the texture in the future
246 m_node->setReadOnlyMaterials(true);
250 void ItemCAO::removeFromScene()
259 void ItemCAO::updateLight(u8 light_at_pos)
263 v3s16 ItemCAO::getLightPosition()
265 return floatToInt(m_position, BS);
268 void ItemCAO::updateNodePos()
273 m_node->setPosition(m_position);
276 void ItemCAO::step(float dtime, ClientEnvironment *env)
280 /*v3f rot = m_node->getRotation();
281 rot.Y += dtime * 120;
282 m_node->setRotation(rot);*/
283 LocalPlayer *player = env->getLocalPlayer();
285 v3f rot = m_node->getRotation();
286 rot.Y = 180.0 - (player->getYaw());
287 m_node->setRotation(rot);
291 void ItemCAO::processMessage(const std::string &data)
293 dstream<<"ItemCAO: Got data: "<<data<<std::endl;
294 std::istringstream is(data, std::ios::binary);
308 void ItemCAO::initialize(const std::string &data)
310 dstream<<"ItemCAO: Got init data: "<<data<<std::endl;
315 newpos.X = stoi(fn.next(","));
316 newpos.Y = stoi(fn.next(","));
317 newpos.Z = stoi(fn.next(":"));
321 m_inventorystring = fn.next("");
326 scene::IMesh *mesh = m_node->getMesh();
331 scene::IMeshBuffer *buf = mesh->getMeshBuffer(0);
337 Create an inventory item to see what is its image
339 std::istringstream is(m_inventorystring, std::ios_base::binary);
340 video::ITexture *texture = NULL;
342 InventoryItem *item = NULL;
343 item = InventoryItem::deSerialize(is);
344 dstream<<__FUNCTION_NAME<<": m_inventorystring=\""
345 <<m_inventorystring<<"\" -> item="<<item
349 texture = item->getImage();
353 catch(SerializationError &e)
355 dstream<<"WARNING: "<<__FUNCTION_NAME
356 <<": error deSerializing inventorystring \""
357 <<m_inventorystring<<"\""<<std::endl;
360 // Set meshbuffer texture
361 buf->getMaterial().setTexture(0, texture);