StrongActorPtr BaseGameLogic::VCreateActor(const std::string &_ActorResource, TiXmlElement* _Overrides, const glm::mat4x4* _InitialTransform, const ActorId _ServersActorId) { if (m_pActorFactory == NULL) return StrongActorPtr(); //if (!m_bProxy && serversActorId != INVALID_ACTOR_ID) // return StrongActorPtr(); //if (m_bProxy && serversActorId == INVALID_ACTOR_ID) //return StrongActorPtr(); StrongActorPtr pActor = m_pActorFactory->CreateActor(_ActorResource.c_str(), _Overrides, _InitialTransform, _ServersActorId); if (pActor) { m_Actors.insert(std::make_pair(pActor->GetId(), pActor)); m_LastActorId = pActor->GetId(); //if (!m_bProxy && (m_State==BGS_SpawningPlayersActors || m_State==BGS_Running)) if (m_State==BGS_SpawningPlayersActors || m_State==BGS_Running) { //shared_ptr<EvtData_Request_New_Actor> pNewActor(GCC_NEW EvtData_Request_New_Actor(actorResource, initialTransform, pActor->GetId())); //IEventManager::Get()->VTriggerEvent(pNewActor); } return pActor; } else { // FUTURE WORK: Log error: couldn't create actor return StrongActorPtr(); } }
/** Creates an actor and all its components based on XML configuration ** resource: filename for xml ** state: current game state **/ StrongActorPtr ActorFactory::CreateActor(const char* resource, int* state) { //Holds referenced to loaded XML file pugi::xml_document doc; //Error check to see if file was loaded corectly pugi::xml_parse_result result; std::string resource_str(resource); if (!(result = doc.load_file(("./assets/actors/" + resource_str + ".xml").c_str()))) { std::cout << "ActorFactory::CreateActor(...): Failed to load" << std::endl; std::cout << "Filename: " << resource << " Load result: " << result.description() << std::endl; return StrongActorPtr(); } // Constructs a default actor and gives in the current game state StrongActorPtr actor(new Actor()); actor->resetGameState(state); //Used to iterate over XML file to get attributes pugi::xml_node tools = doc.child(resource); //Initializes the specified actor with chosen XML attributes if(!actor->Init(&tools)) { std::cout << "ActorFactory::CreateActor(...): Failed to initialize actor: " << resource << std::endl; return StrongActorPtr(); } //Iterates over XML to get components to add for (pugi::xml_node tool = tools.first_child(); tool; tool = tool.next_sibling()) { //Creates each component given the XML attribute StrongActorComponentPtr component(CreateComponent(&tool)); if (component) { //Adds the component to the actor actor->AddComponent(component); //Gives the component a reference to its owner component->SetOwner(actor); //Completes any post-initialization of the components component->PostInit(NULL); } else { std::cout << "ActorFactory::CreateActor(...): Failed to create component" << std::endl; return StrongActorPtr(); } } return actor; }
StrongActorPtr BaseGameLogic::createActor(const std::string& resource_name) { LOGI << "Creating a new actor from resource '" << resource_name << "'." << endl; ActorFactory& factory = ActorFactory::getInstance(); StrongActorPtr actor = factory.createActor(resource_name); if (actor) { LOGI << "Created a new actor with id " << actor->getId() << "." << endl; actors_[actor->getId()] = actor; return actor; } else { LOGE << "Failed to create a new actor from resource '" << resource_name << "'." << endl; return StrongActorPtr(); } }