Esempio n. 1
0
Node* Node::CloneRecursive(Node* parent, SceneResolver& resolver, CreateMode mode)
{
    // Create clone node
    Node* cloneNode = parent->CreateChild(0, (mode == REPLICATED && id_ < FIRST_LOCAL_ID) ? REPLICATED : LOCAL);
    resolver.AddNode(id_, cloneNode);

    // Copy attributes
    const Vector<AttributeInfo>* attributes = GetAttributes();
    for (unsigned j = 0; j < attributes->Size(); ++j)
    {
        const AttributeInfo& attr = attributes->At(j);
        // Do not copy network-only attributes, as they may have unintended side effects
        if (attr.mode_ & AM_FILE)
            cloneNode->SetAttribute(j, GetAttribute(j));
    }

    // Clone components
    for (Vector<SharedPtr<Component> >::ConstIterator i = components_.Begin(); i != components_.End(); ++i)
    {
        Component* component = *i;
        Component* cloneComponent = cloneNode->SafeCreateComponent(component->GetTypeName(), component->GetType(), 
            (mode == REPLICATED && component->GetID() < FIRST_LOCAL_ID) ? REPLICATED : LOCAL, 0);
        if (!cloneComponent)
        {
            LOGERROR("Could not clone component " + component->GetTypeName());
            continue;
        }
        resolver.AddComponent(component->GetID(), cloneComponent);

        const Vector<AttributeInfo>* compAttributes = component->GetAttributes();
        if (compAttributes)
        {
            for (unsigned j = 0; j < compAttributes->Size(); ++j)
            {
                const AttributeInfo& attr = compAttributes->At(j);
                if (attr.mode_ & AM_FILE)
                    cloneComponent->SetAttribute(j, component->GetAttribute(j));
            }
        }
    }

    // Clone child nodes recursively
    for (Vector<SharedPtr<Node> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
    {
        Node* node = *i;
        node->CloneRecursive(cloneNode, resolver, mode);
    }

    return cloneNode;
}
Component::Component( Component& c){
    this->id = c.GetID();
    this->type = c.GetType();
    this->name = c.GetName();
}
Esempio n. 3
0
//------------------------------------------------------------------------
// 
//------------------------------------------------------------------------
void game::ObjectFactory::Serialize( const Object * pObj_p, text::GfigElementA * pGFig_p )
{

	// NOTE:
	// saving an object that uses a prefab should optmize out data that equals the prefab.
	// currently, data discarded on the gfigs consider only the defaults..
	// so, for example:
	// default color = rgba(1,1,1,1)
	// prefab color = rgba(1,1,0.5,0.5)
	// obj color = rgba(1,1,0.5,1)
	// 
	// prefab gfig = ba(0.5,0.5)
	// obj gfig = a(1)
	// the only way is create a new factory function for components, that receives a prefab compo for reference
	// TODO

	
	int nCompos = (int)pObj_p->m_components.Size();

	if( pObj_p->m_prefab == -1 ){

		pGFig_p->m_subElements.emplace_back(
			GfigElementA(pObj_p->m_szName,  "" )
			);

		for( int itCompo = 0 ; itCompo < nCompos; ++itCompo ){

			Component * pCompo = pObj_p->m_components[itCompo].Get();

			m_pLayerOwner->m_componentFactory.Serialize( pCompo, &pGFig_p->m_subElements.back() );
		}
	}
	else{

		pGFig_p->m_subElements.emplace_back(
			GfigElementA(pObj_p->m_szName,   pObj_p->m_prefab )
			);

		Object * pPrefab = m_prefabs[pObj_p->m_prefab].pObj.Get();
		//int nPrefCompos = (int)pPrefab->m_components.Size();

		for( int itCompo = 0 ; itCompo < nCompos; ++itCompo ){

			Component * pCompo = pObj_p->m_components[itCompo].Get();
			Component * pPrefCompo = pPrefab->GetFirstOfComponent(pCompo->GetType()).Get();				

			// how to solve multiple of the same compo type? new int on compo?

			if( pCompo->GetType() == COMPONENT_TYPE(AABB2DColliderComponent)
				||
				pCompo->GetType() == COMPONENT_TYPE(TransformComponent)
				||
				pCompo->GetType() == COMPONENT_TYPE(SpriteComponent_)
				||
				pCompo->GetType() == COMPONENT_TYPE(SpriteAnimCompo_)){ // testing just with AABB2d

				if( pPrefCompo != nullptr ){

					m_pLayerOwner->m_componentFactory.Serialize( pCompo, pPrefCompo, &pGFig_p->m_subElements.back() );
				}
				else{

					m_pLayerOwner->m_componentFactory.Serialize(
						pCompo,
						m_pLayerOwner->m_componentFactory.GetDefaultCompo(pCompo->GetType()),
						&pGFig_p->m_subElements.back() );
				}
			}
			else{

				m_pLayerOwner->m_componentFactory.Serialize( pCompo, &pGFig_p->m_subElements.back() );
			}
		}
	}
}