Removed lua 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
51         {
52                 dstream<<"ClientActiveObject::create(): passed "
53                                 <<"unknown type="<<type<<std::endl;
54                 return NULL;
55         }
56 }
57
58 /*
59         TestCAO
60 */
61
62 TestCAO::TestCAO(u16 id):
63         ClientActiveObject(id),
64         m_node(NULL),
65         m_position(v3f(0,10*BS,0))
66 {
67 }
68
69 TestCAO::~TestCAO()
70 {
71 }
72
73 void TestCAO::addToScene(scene::ISceneManager *smgr)
74 {
75         if(m_node != NULL)
76                 return;
77         
78         video::IVideoDriver* driver = smgr->getVideoDriver();
79         
80         scene::SMesh *mesh = new scene::SMesh();
81         scene::IMeshBuffer *buf = new scene::SMeshBuffer();
82         video::SColor c(255,255,255,255);
83         video::S3DVertex vertices[4] =
84         {
85                 video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
86                 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
87                 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
88                 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),
89         };
90         u16 indices[] = {0,1,2,2,3,0};
91         buf->append(vertices, 4, indices, 6);
92         // Set material
93         buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
94         buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
95         buf->getMaterial().setTexture
96                         (0, driver->getTexture(porting::getDataPath("rat.png").c_str()));
97         buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
98         buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
99         buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
100         // Add to mesh
101         mesh->addMeshBuffer(buf);
102         buf->drop();
103         m_node = smgr->addMeshSceneNode(mesh, NULL);
104         mesh->drop();
105         updateNodePos();
106 }
107
108 void TestCAO::removeFromScene()
109 {
110         if(m_node == NULL)
111                 return;
112
113         m_node->remove();
114         m_node = NULL;
115 }
116
117 void TestCAO::updateLight(u8 light_at_pos)
118 {
119 }
120
121 v3s16 TestCAO::getLightPosition()
122 {
123         return floatToInt(m_position, BS);
124 }
125
126 void TestCAO::updateNodePos()
127 {
128         if(m_node == NULL)
129                 return;
130
131         m_node->setPosition(m_position);
132         //m_node->setRotation(v3f(0, 45, 0));
133 }
134
135 void TestCAO::step(float dtime)
136 {
137         if(m_node)
138         {
139                 v3f rot = m_node->getRotation();
140                 //dstream<<"dtime="<<dtime<<", rot.Y="<<rot.Y<<std::endl;
141                 rot.Y += dtime * 180;
142                 m_node->setRotation(rot);
143         }
144 }
145
146 void TestCAO::processMessage(const std::string &data)
147 {
148         dstream<<"TestCAO: Got data: "<<data<<std::endl;
149         std::istringstream is(data, std::ios::binary);
150         u16 cmd;
151         is>>cmd;
152         if(cmd == 0)
153         {
154                 v3f newpos;
155                 is>>newpos.X;
156                 is>>newpos.Y;
157                 is>>newpos.Z;
158                 m_position = newpos;
159                 updateNodePos();
160         }
161 }
162
163