Temporary commit; lots of test code and stuff
[oweals/minetest.git] / src / clientobject.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #include "clientobject.h"
21 #include "debug.h"
22 #include "porting.h"
23 #include "constants.h"
24 #include "utility.h"
25
26 ClientActiveObject::ClientActiveObject(u16 id):
27         ActiveObject(id)
28 {
29 }
30
31 ClientActiveObject::~ClientActiveObject()
32 {
33         removeFromScene();
34 }
35
36 ClientActiveObject* ClientActiveObject::create(u8 type)
37 {
38         if(type == ACTIVEOBJECT_TYPE_INVALID)
39         {
40                 dstream<<"ClientActiveObject::create(): passed "
41                                 <<"ACTIVEOBJECT_TYPE_INVALID"<<std::endl;
42                 return NULL;
43         }
44         else if(type == ACTIVEOBJECT_TYPE_TEST)
45         {
46                 dstream<<"ClientActiveObject::create(): passed "
47                                 <<"ACTIVEOBJECT_TYPE_TEST"<<std::endl;
48                 return new TestCAO(0);
49         }
50         else if(type == ACTIVEOBJECT_TYPE_LUA)
51         {
52                 dstream<<"ClientActiveObject::create(): passed "
53                                 <<"ACTIVEOBJECT_TYPE_LUA"<<std::endl;
54                 return NULL;
55         }
56         else
57         {
58                 dstream<<"ClientActiveObject::create(): passed "
59                                 <<"unknown type="<<type<<std::endl;
60                 return NULL;
61         }
62 }
63
64 /*
65         TestCAO
66 */
67
68 TestCAO::TestCAO(u16 id):
69         ClientActiveObject(id),
70         m_node(NULL),
71         m_position(v3f(0,10*BS,0))
72 {
73 }
74
75 TestCAO::~TestCAO()
76 {
77 }
78
79 void TestCAO::addToScene(scene::ISceneManager *smgr)
80 {
81         if(m_node != NULL)
82                 return;
83         
84         video::IVideoDriver* driver = smgr->getVideoDriver();
85         
86         scene::SMesh *mesh = new scene::SMesh();
87         scene::IMeshBuffer *buf = new scene::SMeshBuffer();
88         video::SColor c(255,255,255,255);
89         video::S3DVertex vertices[4] =
90         {
91                 video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
92                 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
93                 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
94                 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),
95         };
96         u16 indices[] = {0,1,2,2,3,0};
97         buf->append(vertices, 4, indices, 6);
98         // Set material
99         buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
100         buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
101         buf->getMaterial().setTexture
102                         (0, driver->getTexture(porting::getDataPath("rat.png").c_str()));
103         buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
104         buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
105         buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
106         // Add to mesh
107         mesh->addMeshBuffer(buf);
108         buf->drop();
109         m_node = smgr->addMeshSceneNode(mesh, NULL);
110         mesh->drop();
111         updateNodePos();
112 }
113
114 void TestCAO::removeFromScene()
115 {
116         if(m_node == NULL)
117                 return;
118
119         m_node->remove();
120         m_node = NULL;
121 }
122
123 void TestCAO::updateLight(u8 light_at_pos)
124 {
125 }
126
127 v3s16 TestCAO::getLightPosition()
128 {
129         return floatToInt(m_position, BS);
130 }
131
132 void TestCAO::updateNodePos()
133 {
134         if(m_node == NULL)
135                 return;
136
137         m_node->setPosition(m_position);
138         //m_node->setRotation(v3f(0, 45, 0));
139 }
140
141 void TestCAO::step(float dtime)
142 {
143         if(m_node)
144         {
145                 v3f rot = m_node->getRotation();
146                 //dstream<<"dtime="<<dtime<<", rot.Y="<<rot.Y<<std::endl;
147                 rot.Y += dtime * 180;
148                 m_node->setRotation(rot);
149         }
150 }
151
152 void TestCAO::processMessage(const std::string &data)
153 {
154         //dstream<<"TestCAO: Got data: "<<data<<std::endl;
155         std::istringstream is(data, std::ios::binary);
156         u16 cmd;
157         is>>cmd;
158         if(cmd == 0)
159         {
160                 v3f newpos;
161                 is>>newpos.X;
162                 is>>newpos.Y;
163                 is>>newpos.Z;
164                 m_position = newpos;
165                 updateNodePos();
166         }
167 }
168
169