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))
409 ClientActiveObject::registerType(getType(), create);
416 ClientActiveObject* RatCAO::create()
421 void RatCAO::addToScene(scene::ISceneManager *smgr)
426 video::IVideoDriver* driver = smgr->getVideoDriver();
428 scene::SMesh *mesh = new scene::SMesh();
429 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
430 video::SColor c(255,255,255,255);
431 video::S3DVertex vertices[4] =
433 video::S3DVertex(-BS/2,0,0, 0,0,0, c, 0,1),
434 video::S3DVertex(BS/2,0,0, 0,0,0, c, 1,1),
435 video::S3DVertex(BS/2,BS/2,0, 0,0,0, c, 1,0),
436 video::S3DVertex(-BS/2,BS/2,0, 0,0,0, c, 0,0),
438 u16 indices[] = {0,1,2,2,3,0};
439 buf->append(vertices, 4, indices, 6);
441 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
442 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
443 //buf->getMaterial().setTexture(0, NULL);
444 buf->getMaterial().setTexture
445 (0, driver->getTexture(porting::getDataPath("rat.png").c_str()));
446 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
447 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
448 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
450 mesh->addMeshBuffer(buf);
452 m_node = smgr->addMeshSceneNode(mesh, NULL);
454 // Set it to use the materials of the meshbuffers directly.
455 // This is needed for changing the texture in the future
456 m_node->setReadOnlyMaterials(true);
460 void RatCAO::removeFromScene()
469 void RatCAO::updateLight(u8 light_at_pos)
474 u8 li = decode_light(light_at_pos);
475 video::SColor color(255,li,li,li);
477 scene::IMesh *mesh = m_node->getMesh();
481 u16 mc = mesh->getMeshBufferCount();
482 for(u16 j=0; j<mc; j++)
484 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
485 video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
486 u16 vc = buf->getVertexCount();
487 for(u16 i=0; i<vc; i++)
489 vertices[i].Color = color;
494 v3s16 RatCAO::getLightPosition()
496 return floatToInt(m_position, BS);
499 void RatCAO::updateNodePos()
504 //m_node->setPosition(m_position);
505 m_node->setPosition(pos_translator.vect_show);
507 v3f rot = m_node->getRotation();
508 rot.Y = 180.0 - m_yaw;
509 m_node->setRotation(rot);
512 void RatCAO::step(float dtime, ClientEnvironment *env)
514 pos_translator.translate(dtime);
518 void RatCAO::processMessage(const std::string &data)
520 //dstream<<"RatCAO: Got message"<<std::endl;
521 std::istringstream is(data, std::ios::binary);
527 m_position = readV3F1000(is);
528 pos_translator.update(m_position);
530 m_yaw = readF1000(is);
535 void RatCAO::initialize(const std::string &data)
537 //dstream<<"RatCAO: Got init data"<<std::endl;
540 std::istringstream is(data, std::ios::binary);
542 u8 version = readU8(is);
547 m_position = readV3F1000(is);
548 pos_translator.init(m_position);