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"
26 core::map<u16, ClientActiveObject::Factory> ClientActiveObject::m_types;
28 ClientActiveObject::ClientActiveObject(u16 id):
33 ClientActiveObject::~ClientActiveObject()
38 ClientActiveObject* ClientActiveObject::create(u8 type)
40 // Find factory function
41 core::map<u16, Factory>::Node *n;
42 n = m_types.find(type);
45 // If factory is not found, just return.
46 dstream<<"WARNING: ClientActiveObject: No factory for type="
51 Factory f = n->getValue();
52 ClientActiveObject *object = (*f)();
56 void ClientActiveObject::registerType(u16 type, Factory f)
58 core::map<u16, Factory>::Node *n;
59 n = m_types.find(type);
62 m_types.insert(type, f);
70 TestCAO proto_TestCAO;
73 ClientActiveObject(0),
75 m_position(v3f(0,10*BS,0))
77 ClientActiveObject::registerType(getType(), create);
84 ClientActiveObject* TestCAO::create()
89 void TestCAO::addToScene(scene::ISceneManager *smgr)
94 video::IVideoDriver* driver = smgr->getVideoDriver();
96 scene::SMesh *mesh = new scene::SMesh();
97 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
98 video::SColor c(255,255,255,255);
99 video::S3DVertex vertices[4] =
101 video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
102 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
103 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
104 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),
106 u16 indices[] = {0,1,2,2,3,0};
107 buf->append(vertices, 4, indices, 6);
109 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
110 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
111 buf->getMaterial().setTexture
112 (0, driver->getTexture(porting::getDataPath("rat.png").c_str()));
113 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
114 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
115 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
117 mesh->addMeshBuffer(buf);
119 m_node = smgr->addMeshSceneNode(mesh, NULL);
124 void TestCAO::removeFromScene()
133 void TestCAO::updateLight(u8 light_at_pos)
137 v3s16 TestCAO::getLightPosition()
139 return floatToInt(m_position, BS);
142 void TestCAO::updateNodePos()
147 m_node->setPosition(m_position);
148 //m_node->setRotation(v3f(0, 45, 0));
151 void TestCAO::step(float dtime)
155 v3f rot = m_node->getRotation();
156 //dstream<<"dtime="<<dtime<<", rot.Y="<<rot.Y<<std::endl;
157 rot.Y += dtime * 180;
158 m_node->setRotation(rot);
162 void TestCAO::processMessage(const std::string &data)
164 dstream<<"TestCAO: Got data: "<<data<<std::endl;
165 std::istringstream is(data, std::ios::binary);
183 #include "inventory.h"
186 ItemCAO proto_ItemCAO;
189 ClientActiveObject(0),
190 m_selection_box(-BS*0.4,0.0,-BS*0.4, BS*0.4,BS*0.8,BS*0.4),
192 m_position(v3f(0,10*BS,0))
194 ClientActiveObject::registerType(getType(), create);
201 ClientActiveObject* ItemCAO::create()
203 return new ItemCAO();
206 void ItemCAO::addToScene(scene::ISceneManager *smgr)
211 video::IVideoDriver* driver = smgr->getVideoDriver();
213 scene::SMesh *mesh = new scene::SMesh();
214 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
215 video::SColor c(255,255,255,255);
216 video::S3DVertex vertices[4] =
218 /*video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
219 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
220 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
221 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),*/
222 video::S3DVertex(BS/3,0,0, 0,0,0, c, 0,1),
223 video::S3DVertex(-BS/3,0,0, 0,0,0, c, 1,1),
224 video::S3DVertex(-BS/3,0+BS*2/3,0, 0,0,0, c, 1,0),
225 video::S3DVertex(BS/3,0+BS*2/3,0, 0,0,0, c, 0,0),
227 u16 indices[] = {0,1,2,2,3,0};
228 buf->append(vertices, 4, indices, 6);
230 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
231 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
232 //buf->getMaterial().setTexture(0, NULL);
233 buf->getMaterial().setTexture
234 (0, driver->getTexture(porting::getDataPath("rat.png").c_str()));
235 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
236 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
237 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
239 mesh->addMeshBuffer(buf);
241 m_node = smgr->addMeshSceneNode(mesh, NULL);
243 // Set it to use the materials of the meshbuffers directly.
244 // This is needed for changing the texture in the future
245 m_node->setReadOnlyMaterials(true);
249 void ItemCAO::removeFromScene()
258 void ItemCAO::updateLight(u8 light_at_pos)
262 v3s16 ItemCAO::getLightPosition()
264 return floatToInt(m_position, BS);
267 void ItemCAO::updateNodePos()
272 m_node->setPosition(m_position);
275 void ItemCAO::step(float dtime)
279 v3f rot = m_node->getRotation();
280 rot.Y += dtime * 120;
281 m_node->setRotation(rot);
285 void ItemCAO::processMessage(const std::string &data)
287 dstream<<"ItemCAO: Got data: "<<data<<std::endl;
288 std::istringstream is(data, std::ios::binary);
302 void ItemCAO::initialize(const std::string &data)
304 dstream<<"ItemCAO: Got init data: "<<data<<std::endl;
309 newpos.X = stoi(fn.next(","));
310 newpos.Y = stoi(fn.next(","));
311 newpos.Z = stoi(fn.next(":"));
315 m_inventorystring = fn.next("");
320 scene::IMesh *mesh = m_node->getMesh();
325 scene::IMeshBuffer *buf = mesh->getMeshBuffer(0);
331 Create an inventory item to see what is its image
333 std::istringstream is(m_inventorystring, std::ios_base::binary);
334 video::ITexture *texture = NULL;
336 InventoryItem *item = NULL;
337 item = InventoryItem::deSerialize(is);
338 dstream<<__FUNCTION_NAME<<": m_inventorystring=\""
339 <<m_inventorystring<<"\" -> item="<<item
343 texture = item->getImage();
347 catch(SerializationError &e)
349 dstream<<"WARNING: "<<__FUNCTION_NAME
350 <<": error deSerializing inventorystring \""
351 <<m_inventorystring<<"\""<<std::endl;
354 // Set meshbuffer texture
355 buf->getMaterial().setTexture(0, texture);