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)
264 u8 li = decode_light(light_at_pos);
265 video::SColor color(255,li,li,li);
267 scene::IMesh *mesh = m_node->getMesh();
271 u16 mc = mesh->getMeshBufferCount();
272 for(u16 j=0; j<mc; j++)
274 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
275 video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
276 u16 vc = buf->getVertexCount();
277 for(u16 i=0; i<vc; i++)
279 vertices[i].Color = color;
284 v3s16 ItemCAO::getLightPosition()
286 return floatToInt(m_position, BS);
289 void ItemCAO::updateNodePos()
294 m_node->setPosition(m_position);
297 void ItemCAO::step(float dtime, ClientEnvironment *env)
301 /*v3f rot = m_node->getRotation();
302 rot.Y += dtime * 120;
303 m_node->setRotation(rot);*/
304 LocalPlayer *player = env->getLocalPlayer();
306 v3f rot = m_node->getRotation();
307 rot.Y = 180.0 - (player->getYaw());
308 m_node->setRotation(rot);
312 void ItemCAO::processMessage(const std::string &data)
314 dstream<<"ItemCAO: Got message"<<std::endl;
315 std::istringstream is(data, std::ios::binary);
324 m_position.X = (float)readS32((u8*)buf)/1000.0;
326 m_position.Y = (float)readS32((u8*)buf)/1000.0;
328 m_position.Z = (float)readS32((u8*)buf)/1000.0;
333 void ItemCAO::initialize(const std::string &data)
335 dstream<<"ItemCAO: Got init data"<<std::endl;
338 std::istringstream is(data, std::ios::binary);
348 m_position.X = (float)readS32((u8*)buf)/1000.0;
350 m_position.Y = (float)readS32((u8*)buf)/1000.0;
352 m_position.Z = (float)readS32((u8*)buf)/1000.0;
354 m_inventorystring = deSerializeString(is);
366 scene::IMesh *mesh = m_node->getMesh();
371 scene::IMeshBuffer *buf = mesh->getMeshBuffer(0);
376 // Create an inventory item to see what is its image
377 std::istringstream is(m_inventorystring, std::ios_base::binary);
378 video::ITexture *texture = NULL;
380 InventoryItem *item = NULL;
381 item = InventoryItem::deSerialize(is);
382 dstream<<__FUNCTION_NAME<<": m_inventorystring=\""
383 <<m_inventorystring<<"\" -> item="<<item
387 texture = item->getImage();
391 catch(SerializationError &e)
393 dstream<<"WARNING: "<<__FUNCTION_NAME
394 <<": error deSerializing inventorystring \""
395 <<m_inventorystring<<"\""<<std::endl;
398 // Set meshbuffer texture
399 buf->getMaterial().setTexture(0, texture);