Пример #1
0
Actor*Level::createActorComponentWithBody(b2Body* tbody, QString& actorQmlComponent, QQmlEngine* qmlEngine, bool initLogics)
{
    Body* body = checkBodyOrInit(tbody);
    QQmlComponent actorComponent(qmlEngine,actorQmlComponent);
    QObject* actorObject = actorComponent.create();
    if(actorComponent.isError()){
        qDebug() << " QML Actor Component Error ";
        for (QQmlError error : actorComponent.errors()) {
                qDebug() << " Error " << error.toString();
        }
    }

    Actor* actor = static_cast<Actor*>(actorObject);
    if(actor){
       actor-> setBody(body);
       actor->setPosition(body->getPosition());
       actor->setParent(this);
       actor->setParentItem(this);

       if(initLogics){
           // Perform any initialization of the new scene graph components
           // TODO: Refactor into queue inside of engine to avoid possible double initialization.
           QList<Logic*> logics = actor->findChildren<Logic*>();
           for (Logic* logic : logics) {
               logic->init();
           }
       }
    }else{
        qDebug() << " Qml component was not of Actor Type ";
    }
    return actor;
}
	bool GameActorNode::createActorSpriteWithDictionary( CCDictionary* dictionary )
	{
		if(dictionary == nullptr)	{
			SL_PROCESS_APP()->log(ELogType_Error, "%s - dictionary can not be null", __FUNCTION__);
			return false;	
		}

		// remove previously created ActorSprites if they exist
		CCArray* removeChildren(CCArray::create());
		CCArray* children(getChildren());
		CCObject* child;
		CCARRAY_FOREACH(children, child)
		{
			if(dynamic_cast<ActorSprite*>(child) != nullptr)	{
				removeChildren->addObject(child);
			}	
		}

		CCARRAY_FOREACH(removeChildren, child)
		{
			dynamic_cast<ActorSprite*>(child)->removeFromParent();
		}

		_actorSprite = ActorSprite::createWithDictionary(dictionary);
		if(_actorSprite != nullptr)	{
			addChild(_actorSprite);
		}

		// note: components need to be updated now
		SLSize idx(0);
		IComponent* component(_components->componentAt(idx));
		while(component != nullptr)	{
			// for actor components the actor sprite changes
			GameActorComponent* actorComponent(dynamic_cast<GameActorComponent*>(component));
			if(actorComponent != nullptr)	{
				actorComponent->setActorSprite(_actorSprite);
			}

			// for replica components the construction dictionary changes
			ReplicaComponent* replicaComponent(dynamic_cast<ReplicaComponent*>(component));
			if(replicaComponent != nullptr)	{
				replicaComponent->setConstructionDictionary(dictionary);
			}

			// TODO @CP : add a virtual onActorSpriteChanged

			++idx;
			component = _components->componentAt(idx);
		}

		return true;
	}