Exemple #1
0
gkGameObjectInstance* gkGameObjectGroup::createGroupInstance(gkScene* scene, const gkResourceName& name)
{
	GK_ASSERT(m_instanceManager);

	if (m_instanceManager->exists(name))
	{
		gkLogMessage("GameObjectGroup: Duplicate instance '" << name.getName() << "(" << name.getGroup() << ")'");
		return 0;
	}


	gkGameObjectInstance* newInst = m_instanceManager->create<gkGameObjectInstance>(name);
	newInst->_updateFromGroup(this);


	gkGameObject* obj = newInst->getRoot();
	GK_ASSERT(obj);
	if (obj)
		obj->setOwner(scene);



	Objects::Iterator iter = m_objects.iterator();
	while (iter.hasMoreElements())
		newInst->addObject(iter.getNext().second);

	return newInst;
}
void gkGameObjectInstance::createInstanceImpl(void)
{
	if (!m_owner || !m_owner->getOwner())
	{
		m_instanceState = ST_ERROR;
		m_instanceError = "Root object is not in any scene!";
		return;
	}


	gkScene* scene = m_owner->getOwner();

	m_owner->createInstance();
#ifdef OGREKIT_USE_PHYSICS	
	gkGameObjectSet parentingPhysicsObjs;
	Objects::Iterator iter = m_objects.iterator();
	while (iter.hasMoreElements())
	{
		gkGameObject* gobj = iter.getNext().second;

		gobj->setOwner(scene);
		gobj->createInstance();
		parentingPhysicsObjs.insert(gobj);
	}

	
	scene->_applyBuiltinParents(parentingPhysicsObjs);
	scene->_applyBuiltinPhysics(parentingPhysicsObjs);
#endif
}
void gkGameObjectInstance::applyTransform(const gkTransformState& trans)
{
	if (!isInstanced())
		return;


	const gkMatrix4 plocal = trans.toMatrix();

	Objects::Iterator iter = m_objects.iterator();
	while (iter.hasMoreElements())
	{
		gkGameObject* obj = iter.getNext().second;

		if (obj->hasParent())
		{
			continue;
		}

		gkTransformState* initalTransformState = m_objInitialTransformstates.get(obj);

		// Update transform relative to owner
		gkMatrix4 clocal;
		initalTransformState->toMatrix(clocal);
		obj->setTransform(plocal * clocal);
	}
}
void gkGameObjectInstance::cloneObjects(gkScene* scene, const gkTransformState& from, int time,
                                        const gkVector3& linearVelocity, bool tsLinLocal,
                                        const gkVector3& angularVelocity, bool tsAngLocal)
{
	if (!isInstanced())
		return;

	GK_ASSERT(scene);


	const gkMatrix4 plocal = from.toMatrix();


	Objects::Iterator iter = m_objects.iterator();
	while (iter.hasMoreElements())
	{
		gkGameObject* oobj = iter.getNext().second;
		gkGameObject* nobj = scene->cloneObject(oobj, time);



		// be sure this info was not cloned!
		GK_ASSERT(!nobj->isGroupInstance());


		// Update transform relative to owner
		gkGameObjectProperties& props = nobj->getProperties();

		gkMatrix4 clocal;
		props.m_transform.toMatrix(clocal);

		// merge back to transform state
		props.m_transform = gkTransformState(plocal * clocal);


		nobj->createInstance();


		if (props.isRigidOrDynamic() || props.isGhost())
		{
			if (!linearVelocity.isZeroLength())
				nobj->setLinearVelocity(linearVelocity, tsLinLocal ? TRANSFORM_LOCAL : TRANSFORM_PARENT);
		}

		if (props.isRigid())
		{
			if (!angularVelocity.isZeroLength())
				nobj->setAngularVelocity(angularVelocity, tsAngLocal ? TRANSFORM_LOCAL : TRANSFORM_PARENT);
		}
	}

}
gkGameObjectInstance::~gkGameObjectInstance()
{
	if (m_owner != 0)
		gkGameObjectManager::getSingleton().destroy(m_owner->getResourceHandle());

	m_owner = 0;


	Objects::Iterator iter = m_objects.iterator();
	while (iter.hasMoreElements())
	{
		gkGameObject* gobj = iter.getNext().second;
		m_objInitialTransformstates.remove(gobj);
		GK_ASSERT(!gobj->isInstanced());
		delete gobj;
	}

	m_objects.clear();
}
void gkGameObjectInstance::destroyInstanceImpl(void)
{
	if (!m_owner || !m_owner->getOwner())
	{
		m_instanceState = ST_ERROR;
		m_instanceError = "Root object is not in any scene!";
		return;
	}


	Objects::Iterator iter = m_objects.iterator();
	while (iter.hasMoreElements())
	{
		gkGameObject* gobj = iter.getNext().second;

		gobj->destroyInstance();
		gobj->setOwner(0);
		gobj->setParent(0);
	}
}