2480724986bd2e627bbdd3ef9422c739da240069
[blender.git] / source / gameengine / Ketsji / KX_SCA_ReplaceMeshActuator.cpp
1 //
2 // Replace the mesh for this actuator's parent
3 //
4 // $Id$
5 //
6 // ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
7 //
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License
10 // as published by the Free Software Foundation; either version 2
11 // of the License, or (at your option) any later version. The Blender
12 // Foundation also sells licenses for use in proprietary software under
13 // the Blender License.  See http://www.blender.org/BL/ for information
14 // about this.
15 //
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software Foundation,
23 // Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24 //
25 // The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
26 // All rights reserved.
27 //
28 // The Original Code is: all of this file.
29 //
30 // Contributor(s): none yet.
31 //
32 // ***** END GPL/BL DUAL LICENSE BLOCK *****
33
34 //
35 // Previously existed as:
36
37 // \source\gameengine\GameLogic\SCA_ReplaceMeshActuator.cpp
38
39 // Please look here for revision history.
40
41 #include "KX_SCA_ReplaceMeshActuator.h"
42
43 #ifdef HAVE_CONFIG_H
44 #include <config.h>
45 #endif
46
47 /* ------------------------------------------------------------------------- */
48 /* Python functions                                                          */
49 /* ------------------------------------------------------------------------- */
50
51 /* Integration hooks ------------------------------------------------------- */
52
53         PyTypeObject 
54
55 KX_SCA_ReplaceMeshActuator::
56
57 Type = {
58         PyObject_HEAD_INIT(&PyType_Type)
59         0,
60         "KX_SCA_ReplaceMeshActuator",
61         sizeof(KX_SCA_ReplaceMeshActuator),
62         0,
63         PyDestructor,
64         0,
65         __getattr,
66         __setattr,
67         0, 
68         __repr,
69         0, 
70         0,
71         0,
72         0,
73         0
74 };
75
76 PyParentObject KX_SCA_ReplaceMeshActuator::Parents[] = {
77         &SCA_IActuator::Type,
78         &SCA_ILogicBrick::Type,
79         &CValue::Type,
80         NULL
81 };
82
83
84
85 PyMethodDef KX_SCA_ReplaceMeshActuator::Methods[] = {
86   {"setMesh", (PyCFunction) KX_SCA_ReplaceMeshActuator::sPySetMesh, METH_VARARGS, SetMesh_doc},
87   {NULL,NULL} //Sentinel
88 };
89
90
91
92 PyObject* KX_SCA_ReplaceMeshActuator::_getattr(const STR_String& attr)
93 {
94   _getattr_up(SCA_IActuator);
95 }
96
97
98
99 /* 1. setMesh */
100 char KX_SCA_ReplaceMeshActuator::SetMesh_doc[] = 
101         "setMesh(name)\n"
102         "\t- name: string\n"
103         "\tSet the mesh that will be substituted for the current one.\n";
104
105 PyObject* KX_SCA_ReplaceMeshActuator::PySetMesh(PyObject* self,
106                                                                           PyObject* args, 
107                                                                           PyObject* kwds)
108 {
109         char* meshname;
110         
111         if (!PyArg_ParseTuple(args, "s", &meshname))
112         {
113                 return NULL;    
114         }
115
116         void* mesh = SCA_ILogicBrick::m_sCurrentLogicManager->GetMeshByName(STR_String(meshname));
117         
118         if (m_mesh) {
119                 m_mesh= (class RAS_MeshObject*)mesh;
120                 Py_Return;
121         }
122         
123         return NULL;
124 }
125
126
127
128 /* ------------------------------------------------------------------------- */
129 /* Native functions                                                          */
130 /* ------------------------------------------------------------------------- */
131
132 KX_SCA_ReplaceMeshActuator::KX_SCA_ReplaceMeshActuator(SCA_IObject *gameobj,
133                                                                                                            class RAS_MeshObject *mesh,
134                                                                                                            SCA_IScene* scene,
135                                                                                                            PyTypeObject* T) : 
136
137         SCA_IActuator(gameobj, T),
138         m_mesh(mesh),
139         m_scene(scene)
140 {
141 } /* End of constructor */
142
143
144
145 KX_SCA_ReplaceMeshActuator::~KX_SCA_ReplaceMeshActuator()
146
147         // there's nothing to be done here, really....
148 } /* end of destructor */
149
150
151
152 bool KX_SCA_ReplaceMeshActuator::Update(double curtime,
153                                                                                 double deltatime)
154 {
155         bool result = false;    
156         bool bNegativeEvent = IsNegativeEvent();
157         RemoveAllEvents();
158
159         if (bNegativeEvent)
160                 return false; // do nothing on negative events
161
162         if (m_mesh) m_scene->ReplaceMesh(GetParent(),m_mesh);
163
164         return false;
165 }
166
167
168
169 CValue* KX_SCA_ReplaceMeshActuator::GetReplica()
170 {
171         KX_SCA_ReplaceMeshActuator* replica = 
172                 new KX_SCA_ReplaceMeshActuator(*this);
173
174         if (replica == NULL)
175                 return NULL;
176
177         replica->ProcessReplica();
178
179         // this will copy properties and so on...
180         CValue::AddDataToReplica(replica);
181
182         return replica;
183 };
184
185 /* eof */