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"
31 core::map<u16, ClientActiveObject::Factory> ClientActiveObject::m_types;
33 ClientActiveObject::ClientActiveObject(u16 id):
38 ClientActiveObject::~ClientActiveObject()
43 ClientActiveObject* ClientActiveObject::create(u8 type)
45 // Find factory function
46 core::map<u16, Factory>::Node *n;
47 n = m_types.find(type);
50 // If factory is not found, just return.
51 dstream<<"WARNING: ClientActiveObject: No factory for type="
56 Factory f = n->getValue();
57 ClientActiveObject *object = (*f)();
61 void ClientActiveObject::registerType(u16 type, Factory f)
63 core::map<u16, Factory>::Node *n;
64 n = m_types.find(type);
67 m_types.insert(type, f);
75 TestCAO proto_TestCAO;
78 ClientActiveObject(0),
80 m_position(v3f(0,10*BS,0))
82 ClientActiveObject::registerType(getType(), create);
89 ClientActiveObject* TestCAO::create()
94 void TestCAO::addToScene(scene::ISceneManager *smgr)
99 video::IVideoDriver* driver = smgr->getVideoDriver();
101 scene::SMesh *mesh = new scene::SMesh();
102 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
103 video::SColor c(255,255,255,255);
104 video::S3DVertex vertices[4] =
106 video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
107 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
108 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
109 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),
111 u16 indices[] = {0,1,2,2,3,0};
112 buf->append(vertices, 4, indices, 6);
114 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
115 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
116 buf->getMaterial().setTexture
117 (0, driver->getTexture(porting::getDataPath("rat.png").c_str()));
118 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
119 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
120 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
122 mesh->addMeshBuffer(buf);
124 m_node = smgr->addMeshSceneNode(mesh, NULL);
129 void TestCAO::removeFromScene()
138 void TestCAO::updateLight(u8 light_at_pos)
142 v3s16 TestCAO::getLightPosition()
144 return floatToInt(m_position, BS);
147 void TestCAO::updateNodePos()
152 m_node->setPosition(m_position);
153 //m_node->setRotation(v3f(0, 45, 0));
156 void TestCAO::step(float dtime, ClientEnvironment *env)
160 v3f rot = m_node->getRotation();
161 //dstream<<"dtime="<<dtime<<", rot.Y="<<rot.Y<<std::endl;
162 rot.Y += dtime * 180;
163 m_node->setRotation(rot);
167 void TestCAO::processMessage(const std::string &data)
169 dstream<<"TestCAO: Got data: "<<data<<std::endl;
170 std::istringstream is(data, std::ios::binary);
188 #include "inventory.h"
191 ItemCAO proto_ItemCAO;
194 ClientActiveObject(0),
195 m_selection_box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.),
197 m_position(v3f(0,10*BS,0))
199 ClientActiveObject::registerType(getType(), create);
206 ClientActiveObject* ItemCAO::create()
208 return new ItemCAO();
211 void ItemCAO::addToScene(scene::ISceneManager *smgr)
216 video::IVideoDriver* driver = smgr->getVideoDriver();
218 scene::SMesh *mesh = new scene::SMesh();
219 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
220 video::SColor c(255,255,255,255);
221 video::S3DVertex vertices[4] =
223 /*video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
224 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
225 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
226 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),*/
227 video::S3DVertex(BS/3.,0,0, 0,0,0, c, 0,1),
228 video::S3DVertex(-BS/3.,0,0, 0,0,0, c, 1,1),
229 video::S3DVertex(-BS/3.,0+BS*2./3.,0, 0,0,0, c, 1,0),
230 video::S3DVertex(BS/3.,0+BS*2./3.,0, 0,0,0, c, 0,0),
232 u16 indices[] = {0,1,2,2,3,0};
233 buf->append(vertices, 4, indices, 6);
235 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
236 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
237 //buf->getMaterial().setTexture(0, NULL);
238 // Initialize with the stick texture
239 buf->getMaterial().setTexture
240 (0, driver->getTexture(porting::getDataPath("stick.png").c_str()));
241 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
242 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
243 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
245 mesh->addMeshBuffer(buf);
247 m_node = smgr->addMeshSceneNode(mesh, NULL);
249 // Set it to use the materials of the meshbuffers directly.
250 // This is needed for changing the texture in the future
251 m_node->setReadOnlyMaterials(true);
255 void ItemCAO::removeFromScene()
264 void ItemCAO::updateLight(u8 light_at_pos)
269 u8 li = decode_light(light_at_pos);
270 video::SColor color(255,li,li,li);
272 scene::IMesh *mesh = m_node->getMesh();
276 u16 mc = mesh->getMeshBufferCount();
277 for(u16 j=0; j<mc; j++)
279 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
280 video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
281 u16 vc = buf->getVertexCount();
282 for(u16 i=0; i<vc; i++)
284 vertices[i].Color = color;
289 v3s16 ItemCAO::getLightPosition()
291 return floatToInt(m_position, BS);
294 void ItemCAO::updateNodePos()
299 m_node->setPosition(m_position);
302 void ItemCAO::step(float dtime, ClientEnvironment *env)
306 /*v3f rot = m_node->getRotation();
307 rot.Y += dtime * 120;
308 m_node->setRotation(rot);*/
309 LocalPlayer *player = env->getLocalPlayer();
311 v3f rot = m_node->getRotation();
312 rot.Y = 180.0 - (player->getYaw());
313 m_node->setRotation(rot);
317 void ItemCAO::processMessage(const std::string &data)
319 dstream<<"ItemCAO: Got message"<<std::endl;
320 std::istringstream is(data, std::ios::binary);
326 m_position = readV3F1000(is);
331 void ItemCAO::initialize(const std::string &data)
333 dstream<<"ItemCAO: Got init data"<<std::endl;
336 std::istringstream is(data, std::ios::binary);
338 u8 version = readU8(is);
343 m_position = readV3F1000(is);
345 m_inventorystring = deSerializeString(is);
357 scene::IMesh *mesh = m_node->getMesh();
362 scene::IMeshBuffer *buf = mesh->getMeshBuffer(0);
367 // Create an inventory item to see what is its image
368 std::istringstream is(m_inventorystring, std::ios_base::binary);
369 video::ITexture *texture = NULL;
371 InventoryItem *item = NULL;
372 item = InventoryItem::deSerialize(is);
373 dstream<<__FUNCTION_NAME<<": m_inventorystring=\""
374 <<m_inventorystring<<"\" -> item="<<item
378 texture = item->getImage();
382 catch(SerializationError &e)
384 dstream<<"WARNING: "<<__FUNCTION_NAME
385 <<": error deSerializing inventorystring \""
386 <<m_inventorystring<<"\""<<std::endl;
389 // Set meshbuffer texture
390 buf->getMaterial().setTexture(0, texture);
398 #include "inventory.h"
404 ClientActiveObject(0),
405 m_selection_box(-BS/3.,0.0,-BS/3., BS/3.,BS/2.,BS/3.),
407 m_position(v3f(0,10*BS,0)),
410 ClientActiveObject::registerType(getType(), create);
417 ClientActiveObject* RatCAO::create()
422 void RatCAO::addToScene(scene::ISceneManager *smgr)
427 video::IVideoDriver* driver = smgr->getVideoDriver();
429 scene::SMesh *mesh = new scene::SMesh();
430 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
431 video::SColor c(255,255,255,255);
432 video::S3DVertex vertices[4] =
434 video::S3DVertex(-BS/2,0,0, 0,0,0, c, 0,1),
435 video::S3DVertex(BS/2,0,0, 0,0,0, c, 1,1),
436 video::S3DVertex(BS/2,BS/2,0, 0,0,0, c, 1,0),
437 video::S3DVertex(-BS/2,BS/2,0, 0,0,0, c, 0,0),
439 u16 indices[] = {0,1,2,2,3,0};
440 buf->append(vertices, 4, indices, 6);
442 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
443 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
444 //buf->getMaterial().setTexture(0, NULL);
445 buf->getMaterial().setTexture
446 (0, driver->getTexture(porting::getDataPath("rat.png").c_str()));
447 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
448 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
449 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
451 mesh->addMeshBuffer(buf);
453 m_node = smgr->addMeshSceneNode(mesh, NULL);
455 // Set it to use the materials of the meshbuffers directly.
456 // This is needed for changing the texture in the future
457 m_node->setReadOnlyMaterials(true);
461 void RatCAO::removeFromScene()
470 void RatCAO::updateLight(u8 light_at_pos)
475 u8 li = decode_light(light_at_pos);
476 video::SColor color(255,li,li,li);
478 scene::IMesh *mesh = m_node->getMesh();
482 u16 mc = mesh->getMeshBufferCount();
483 for(u16 j=0; j<mc; j++)
485 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
486 video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
487 u16 vc = buf->getVertexCount();
488 for(u16 i=0; i<vc; i++)
490 vertices[i].Color = color;
495 v3s16 RatCAO::getLightPosition()
497 return floatToInt(m_position, BS);
500 void RatCAO::updateNodePos()
505 //m_node->setPosition(m_position);
506 m_node->setPosition(pos_translator.vect_show);
508 v3f rot = m_node->getRotation();
509 rot.Y = 180.0 - m_yaw;
510 m_node->setRotation(rot);
513 void RatCAO::step(float dtime, ClientEnvironment *env)
515 pos_translator.translate(dtime);
519 void RatCAO::processMessage(const std::string &data)
521 //dstream<<"RatCAO: Got message"<<std::endl;
522 std::istringstream is(data, std::ios::binary);
528 m_position = readV3F1000(is);
529 pos_translator.update(m_position);
531 m_yaw = readF1000(is);
536 void RatCAO::initialize(const std::string &data)
538 //dstream<<"RatCAO: Got init data"<<std::endl;
541 std::istringstream is(data, std::ios::binary);
543 u8 version = readU8(is);
548 m_position = readV3F1000(is);
549 pos_translator.init(m_position);