2011-02-25 13:35:59 +00:00
|
|
|
/** \file gameengine/Ketsji/KX_SCA_AddObjectActuator.cpp
|
|
|
|
* \ingroup ketsji
|
|
|
|
*/
|
2002-10-12 11:37:38 +00:00
|
|
|
//
|
|
|
|
// Add an object when this actuator is triggered
|
|
|
|
//
|
|
|
|
// $Id$
|
|
|
|
//
|
2008-04-16 22:40:48 +00:00
|
|
|
// ***** BEGIN GPL LICENSE BLOCK *****
|
2002-10-12 11:37:38 +00:00
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License
|
|
|
|
// as published by the Free Software Foundation; either version 2
|
2008-04-16 22:40:48 +00:00
|
|
|
// of the License, or (at your option) any later version.
|
2002-10-12 11:37:38 +00:00
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software Foundation,
|
2010-02-12 13:34:04 +00:00
|
|
|
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2002-10-12 11:37:38 +00:00
|
|
|
//
|
|
|
|
// The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// The Original Code is: all of this file.
|
|
|
|
//
|
|
|
|
// Contributor(s): none yet.
|
|
|
|
//
|
2008-04-16 22:40:48 +00:00
|
|
|
// ***** END GPL LICENSE BLOCK *****
|
2002-10-12 11:37:38 +00:00
|
|
|
// Previously existed as:
|
|
|
|
|
|
|
|
// \source\gameengine\GameLogic\SCA_AddObjectActuator.cpp
|
|
|
|
|
|
|
|
// Please look here for revision history.
|
|
|
|
|
|
|
|
|
|
|
|
#include "KX_SCA_AddObjectActuator.h"
|
|
|
|
#include "SCA_IScene.h"
|
|
|
|
#include "KX_GameObject.h"
|
|
|
|
#include "KX_IPhysicsController.h"
|
2008-09-06 14:13:31 +00:00
|
|
|
#include "PyObjectPlus.h"
|
2008-09-06 02:46:11 +00:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
/* Native functions */
|
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
KX_SCA_AddObjectActuator::KX_SCA_AddObjectActuator(SCA_IObject *gameobj,
|
2008-07-12 12:10:27 +00:00
|
|
|
SCA_IObject *original,
|
2002-10-12 11:37:38 +00:00
|
|
|
int time,
|
|
|
|
SCA_IScene* scene,
|
2009-04-20 15:06:46 +00:00
|
|
|
const float *linvel,
|
2008-08-27 03:34:53 +00:00
|
|
|
bool linv_local,
|
2009-04-20 15:06:46 +00:00
|
|
|
const float *angvel,
|
2009-06-28 11:22:26 +00:00
|
|
|
bool angv_local)
|
2002-10-12 11:37:38 +00:00
|
|
|
:
|
2009-09-24 21:22:24 +00:00
|
|
|
SCA_IActuator(gameobj, KX_ACT_ADD_OBJECT),
|
2002-10-12 11:37:38 +00:00
|
|
|
m_OriginalObject(original),
|
|
|
|
m_scene(scene),
|
2008-08-27 03:34:53 +00:00
|
|
|
|
|
|
|
m_localLinvFlag(linv_local),
|
|
|
|
m_localAngvFlag(angv_local)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2009-04-20 15:06:46 +00:00
|
|
|
m_linear_velocity[0] = linvel[0];
|
|
|
|
m_linear_velocity[1] = linvel[1];
|
|
|
|
m_linear_velocity[2] = linvel[2];
|
|
|
|
m_angular_velocity[0] = angvel[0];
|
|
|
|
m_angular_velocity[1] = angvel[1];
|
|
|
|
m_angular_velocity[2] = angvel[2];
|
|
|
|
|
2008-07-12 12:10:27 +00:00
|
|
|
if (m_OriginalObject)
|
|
|
|
m_OriginalObject->RegisterActuator(this);
|
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
m_lastCreatedObject = NULL;
|
|
|
|
m_timeProp = time;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
KX_SCA_AddObjectActuator::~KX_SCA_AddObjectActuator()
|
|
|
|
{
|
2008-07-12 12:10:27 +00:00
|
|
|
if (m_OriginalObject)
|
|
|
|
m_OriginalObject->UnregisterActuator(this);
|
2002-10-12 11:37:38 +00:00
|
|
|
if (m_lastCreatedObject)
|
2009-06-08 20:08:19 +00:00
|
|
|
m_lastCreatedObject->UnregisterActuator(this);
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2004-10-16 11:41:50 +00:00
|
|
|
bool KX_SCA_AddObjectActuator::Update()
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2005-03-09 19:45:59 +00:00
|
|
|
//bool result = false; /*unused*/
|
2002-10-12 11:37:38 +00:00
|
|
|
bool bNegativeEvent = IsNegativeEvent();
|
|
|
|
RemoveAllEvents();
|
|
|
|
|
|
|
|
if (bNegativeEvent) return false; // do nothing on negative events
|
|
|
|
|
2006-04-01 03:30:15 +00:00
|
|
|
InstantAddObject();
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2006-04-01 03:30:15 +00:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-04-01 03:30:15 +00:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
SCA_IObject* KX_SCA_AddObjectActuator::GetLastCreatedObject() const
|
|
|
|
{
|
|
|
|
return m_lastCreatedObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CValue* KX_SCA_AddObjectActuator::GetReplica()
|
|
|
|
{
|
|
|
|
KX_SCA_AddObjectActuator* replica = new KX_SCA_AddObjectActuator(*this);
|
|
|
|
|
|
|
|
if (replica == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
// this will copy properties and so on...
|
|
|
|
replica->ProcessReplica();
|
|
|
|
|
|
|
|
return replica;
|
|
|
|
}
|
|
|
|
|
2008-07-12 12:10:27 +00:00
|
|
|
void KX_SCA_AddObjectActuator::ProcessReplica()
|
|
|
|
{
|
|
|
|
if (m_OriginalObject)
|
|
|
|
m_OriginalObject->RegisterActuator(this);
|
|
|
|
m_lastCreatedObject=NULL;
|
|
|
|
SCA_IActuator::ProcessReplica();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool KX_SCA_AddObjectActuator::UnlinkObject(SCA_IObject* clientobj)
|
|
|
|
{
|
|
|
|
if (clientobj == m_OriginalObject)
|
|
|
|
{
|
|
|
|
// this object is being deleted, we cannot continue to track it.
|
|
|
|
m_OriginalObject = NULL;
|
|
|
|
return true;
|
|
|
|
}
|
2009-06-08 20:08:19 +00:00
|
|
|
if (clientobj == m_lastCreatedObject)
|
|
|
|
{
|
|
|
|
// this object is being deleted, we cannot continue to track it.
|
|
|
|
m_lastCreatedObject = NULL;
|
|
|
|
return true;
|
|
|
|
}
|
2008-07-12 12:10:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2011-05-06 20:18:42 +00:00
|
|
|
void KX_SCA_AddObjectActuator::Relink(CTR_Map<CTR_HashedPtr, void*> *obj_map)
|
2008-07-19 07:45:19 +00:00
|
|
|
{
|
|
|
|
void **h_obj = (*obj_map)[m_OriginalObject];
|
|
|
|
if (h_obj) {
|
|
|
|
if (m_OriginalObject)
|
|
|
|
m_OriginalObject->UnregisterActuator(this);
|
|
|
|
m_OriginalObject = (SCA_IObject*)(*h_obj);
|
|
|
|
m_OriginalObject->RegisterActuator(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-31 04:11:39 +00:00
|
|
|
#ifdef WITH_PYTHON
|
2002-10-12 11:37:38 +00:00
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
/* Python functions */
|
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/* Integration hooks ------------------------------------------------------- */
|
|
|
|
PyTypeObject KX_SCA_AddObjectActuator::Type = {
|
2009-06-08 20:08:19 +00:00
|
|
|
PyVarObject_HEAD_INIT(NULL, 0)
|
2002-10-12 11:37:38 +00:00
|
|
|
"KX_SCA_AddObjectActuator",
|
2009-04-20 15:06:46 +00:00
|
|
|
sizeof(PyObjectPlus_Proxy),
|
2002-10-12 11:37:38 +00:00
|
|
|
0,
|
2009-04-20 15:06:46 +00:00
|
|
|
py_base_dealloc,
|
2002-10-12 11:37:38 +00:00
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
2009-04-20 15:06:46 +00:00
|
|
|
py_base_repr,
|
2009-06-29 12:06:46 +00:00
|
|
|
0,0,0,0,0,0,0,0,0,
|
2009-06-28 11:22:26 +00:00
|
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
|
|
|
|
0,0,0,0,0,0,0,
|
|
|
|
Methods,
|
|
|
|
0,
|
|
|
|
0,
|
2009-06-29 12:06:46 +00:00
|
|
|
&SCA_IActuator::Type,
|
|
|
|
0,0,0,0,0,0,
|
|
|
|
py_base_new
|
2002-10-12 11:37:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
PyMethodDef KX_SCA_AddObjectActuator::Methods[] = {
|
2008-08-27 03:34:53 +00:00
|
|
|
{"instantAddObject", (PyCFunction) KX_SCA_AddObjectActuator::sPyInstantAddObject, METH_NOARGS,"instantAddObject() : immediately add object without delay\n"},
|
2002-10-12 11:37:38 +00:00
|
|
|
{NULL,NULL} //Sentinel
|
|
|
|
};
|
|
|
|
|
2009-02-26 09:04:06 +00:00
|
|
|
PyAttributeDef KX_SCA_AddObjectActuator::Attributes[] = {
|
2009-04-20 15:06:46 +00:00
|
|
|
KX_PYATTRIBUTE_RW_FUNCTION("object",KX_SCA_AddObjectActuator,pyattr_get_object,pyattr_set_object),
|
|
|
|
KX_PYATTRIBUTE_RO_FUNCTION("objectLastCreated",KX_SCA_AddObjectActuator,pyattr_get_objectLastCreated),
|
|
|
|
KX_PYATTRIBUTE_INT_RW("time",0,2000,true,KX_SCA_AddObjectActuator,m_timeProp),
|
|
|
|
KX_PYATTRIBUTE_FLOAT_ARRAY_RW("linearVelocity",-FLT_MAX,FLT_MAX,KX_SCA_AddObjectActuator,m_linear_velocity,3),
|
|
|
|
KX_PYATTRIBUTE_FLOAT_ARRAY_RW("angularVelocity",-FLT_MAX,FLT_MAX,KX_SCA_AddObjectActuator,m_angular_velocity,3),
|
2009-02-26 09:04:06 +00:00
|
|
|
{ NULL } //Sentinel
|
|
|
|
};
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2009-04-20 15:06:46 +00:00
|
|
|
PyObject* KX_SCA_AddObjectActuator::pyattr_get_object(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2009-04-20 15:06:46 +00:00
|
|
|
KX_SCA_AddObjectActuator* actuator = static_cast<KX_SCA_AddObjectActuator*>(self);
|
|
|
|
if (!actuator->m_OriginalObject)
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
else
|
|
|
|
return actuator->m_OriginalObject->GetProxy();
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
2009-04-20 15:06:46 +00:00
|
|
|
int KX_SCA_AddObjectActuator::pyattr_set_object(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
|
|
|
|
{
|
|
|
|
KX_SCA_AddObjectActuator* actuator = static_cast<KX_SCA_AddObjectActuator*>(self);
|
|
|
|
KX_GameObject *gameobj;
|
2009-02-19 10:34:51 +00:00
|
|
|
|
2009-04-20 15:06:46 +00:00
|
|
|
if (!ConvertPythonToGameObject(value, &gameobj, true, "actuator.object = value: KX_SCA_AddObjectActuator"))
|
2009-06-08 20:08:19 +00:00
|
|
|
return PY_SET_ATTR_FAIL; // ConvertPythonToGameObject sets the error
|
2009-02-19 10:34:51 +00:00
|
|
|
|
2009-04-20 15:06:46 +00:00
|
|
|
if (actuator->m_OriginalObject != NULL)
|
|
|
|
actuator->m_OriginalObject->UnregisterActuator(actuator);
|
2009-02-19 10:34:51 +00:00
|
|
|
|
2009-04-20 15:06:46 +00:00
|
|
|
actuator->m_OriginalObject = (SCA_IObject*)gameobj;
|
2009-02-19 10:34:51 +00:00
|
|
|
|
2009-04-20 15:06:46 +00:00
|
|
|
if (actuator->m_OriginalObject)
|
|
|
|
actuator->m_OriginalObject->RegisterActuator(actuator);
|
2009-02-19 10:34:51 +00:00
|
|
|
|
2009-06-08 20:08:19 +00:00
|
|
|
return PY_SET_ATTR_SUCCESS;
|
2009-04-20 15:06:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PyObject* KX_SCA_AddObjectActuator::pyattr_get_objectLastCreated(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef)
|
|
|
|
{
|
|
|
|
KX_SCA_AddObjectActuator* actuator = static_cast<KX_SCA_AddObjectActuator*>(self);
|
|
|
|
if (!actuator->m_lastCreatedObject)
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
else
|
|
|
|
return actuator->m_lastCreatedObject->GetProxy();
|
|
|
|
}
|
|
|
|
|
2009-09-29 21:42:40 +00:00
|
|
|
PyObject* KX_SCA_AddObjectActuator::PyInstantAddObject()
|
|
|
|
{
|
|
|
|
InstantAddObject();
|
|
|
|
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
2010-10-31 04:11:39 +00:00
|
|
|
#endif // WITH_PYTHON
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2006-04-01 03:30:15 +00:00
|
|
|
void KX_SCA_AddObjectActuator::InstantAddObject()
|
|
|
|
{
|
|
|
|
if (m_OriginalObject)
|
|
|
|
{
|
|
|
|
// Add an identical object, with properties inherited from the original object
|
|
|
|
// Now it needs to be added to the current scene.
|
|
|
|
SCA_IObject* replica = m_scene->AddReplicaObject(m_OriginalObject,GetParent(),m_timeProp );
|
|
|
|
KX_GameObject * game_obj = static_cast<KX_GameObject *>(replica);
|
2009-04-20 15:06:46 +00:00
|
|
|
game_obj->setLinearVelocity(m_linear_velocity ,m_localLinvFlag);
|
2008-08-27 03:34:53 +00:00
|
|
|
game_obj->setAngularVelocity(m_angular_velocity,m_localAngvFlag);
|
|
|
|
game_obj->ResolveCombinedVelocities(m_linear_velocity, m_angular_velocity, m_localLinvFlag, m_localAngvFlag);
|
2006-04-01 03:30:15 +00:00
|
|
|
|
|
|
|
// keep a copy of the last object, to allow python scripters to change it
|
|
|
|
if (m_lastCreatedObject)
|
2006-05-22 21:03:43 +00:00
|
|
|
{
|
2009-06-08 20:08:19 +00:00
|
|
|
//Let's not keep a reference to the object: it's bad, if the object is deleted
|
|
|
|
//this will force to keep a "zombie" in the game for no good reason.
|
|
|
|
//m_scene->DelayedReleaseObject(m_lastCreatedObject);
|
|
|
|
//m_lastCreatedObject->Release();
|
|
|
|
|
|
|
|
//Instead we use the registration mechanism
|
|
|
|
m_lastCreatedObject->UnregisterActuator(this);
|
|
|
|
m_lastCreatedObject = NULL;
|
2006-05-22 21:03:43 +00:00
|
|
|
}
|
2006-04-01 03:30:15 +00:00
|
|
|
|
|
|
|
m_lastCreatedObject = replica;
|
2009-06-08 20:08:19 +00:00
|
|
|
// no reference
|
|
|
|
//m_lastCreatedObject->AddRef();
|
|
|
|
// but registration
|
|
|
|
m_lastCreatedObject->RegisterActuator(this);
|
2008-03-01 19:46:50 +00:00
|
|
|
// finished using replica? then release it
|
|
|
|
replica->Release();
|
2006-04-01 03:30:15 +00:00
|
|
|
}
|
|
|
|
}
|