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(); } }
// // CreateActor - Chapter 22, page 761 // int CreateActor( BSTR bstrActorXMLFile ) { std::string actorResource = ws2s(std::wstring(bstrActorXMLFile, SysStringLen(bstrActorXMLFile))); StrongActorPtr pActor = g_pApp->m_pGame->VCreateActor(actorResource, NULL); if (!pActor) return INVALID_ACTOR_ID; // fire an event letting everyone else know that we created a new actor shared_ptr<EvtData_New_Actor> pNewActorEvent(GCC_NEW EvtData_New_Actor(pActor->GetId())); IEventManager::Get()->VQueueEvent(pNewActorEvent); return pActor->GetId(); }
void RemoteNetworkView::GetActor(IEventDataPtr pEventData) { char response[4]; std::string httpinmsg; IEventDataPtr pResponseHttpEvent(CREATE_EVENT(EventData_ResponseHTTP::sk_EventType)); std::shared_ptr<EventData_GetActor> pCastEventData = std::static_pointer_cast<EventData_GetActor>(pEventData); std::string name = pCastEventData->VGetActorName(); unsigned int ip = pCastEventData->VGetIp(); pResponseHttpEvent->VSetSocketId(pEventData->VGetSocketId()); pResponseHttpEvent->VSetIp(ip); //look for actor data in actor manager by ip / actor name StrongActorPtr pActor = m_ActorManager->GetActorByName(ip, name); if (pActor == NULL) { _itoa_s(http_response_code_t::NOTFOUND, response,10); httpinmsg.append(response); std::istrstream in(httpinmsg.c_str(),httpinmsg.size()); pResponseHttpEvent->VDeserialize(in); IEventManager::Get()->VTriggerEvent(pResponseHttpEvent); } else { // Better (de-)serialize Actor with streams std::string buffer; _itoa_s(http_response_code_t::OK, response, 10); buffer.append(response); buffer.append(" "); char* id = new char[10]; sprintf_s(id, 10, "%d", pActor->GetId()); buffer.append(id); buffer.append("#"); buffer.append(pActor->GetName()); buffer.append("#"); Vec3 position = pActor->GetPosition(); char *xpos = new char[10]; sprintf_s(xpos, 10, "%.3f", position.x); buffer.append(xpos); buffer.append("#"); char *ypos = new char[10]; sprintf_s(ypos, 10, "%.3f", position.y); buffer.append(ypos); buffer.append("#"); char *zpos = new char[10]; sprintf_s(zpos, 10, "%.3f", position.z); buffer.append(zpos); buffer.push_back('\0'); // when found, send answer std::istrstream in(buffer.c_str(), buffer.size()); pResponseHttpEvent->VDeserialize(in); IEventManager::Get()->VTriggerEvent(pResponseHttpEvent); } }
ActorId ActorManager::AddActor(StrongActorPtr pActor) { ActorId id; // if an actor already exists, do not create StrongActorPtr actorTest = GetActorByName(pActor->GetIp(),pActor->GetName()); if (actorTest == NULL) { id = pActor->GetId(); // No id affected yet if (id == -1) { id = GetNextLastId(); pActor->SetId(id); m_ActorMap[id] = pActor; //alternative //m_ActorMap.insert(std::pair<ActorId, Actor>(id, actor)); // add the actor for this Ip m_ActorsByIpMap[pActor->GetIp()].push_back(pActor); } } else { id = actorTest->GetId(); } return id; }
// // TeapotWarsLogic::VChangeState // void TeapotWarsLogic::VChangeState(BaseGameState newState) { BaseGameLogic::VChangeState(newState); switch(newState) { case BGS_WaitingForPlayers: { // spawn all local players (should only be one, though we might support more in the future) GCC_ASSERT(m_ExpectedPlayers == 1); for (int i = 0; i < m_ExpectedPlayers; ++i) { shared_ptr<IGameView> playersView(GCC_NEW TeapotWarsHumanView(g_pApp->m_Renderer)); VAddView(playersView); if (m_bProxy) { // if we are a remote player, all we have to do is spawn our view - the server will do the rest. return; } } // spawn all remote player's views on the game for (int i = 0; i < m_ExpectedRemotePlayers; ++i) { shared_ptr<IGameView> remoteGameView(GCC_NEW NetworkGameView); VAddView(remoteGameView); } // spawn all AI's views on the game for (int i = 0; i < m_ExpectedAI; ++i) { shared_ptr<IGameView> aiView(GCC_NEW AITeapotView(m_pPathingGraph)); VAddView(aiView); } break; } case BGS_SpawningPlayersActors: { if (m_bProxy) { // only the server needs to do this. return; } for (auto it = m_gameViews.begin(); it != m_gameViews.end(); ++it) { shared_ptr<IGameView> pView = *it; if (pView->VGetType() == GameView_Human) { StrongActorPtr pActor = VCreateActor("actors\\player_teapot.xml", NULL); if (pActor) { shared_ptr<EvtData_New_Actor> pNewActorEvent(GCC_NEW EvtData_New_Actor(pActor->GetId(), pView->VGetId())); IEventManager::Get()->VTriggerEvent(pNewActorEvent); // [rez] This needs to happen asap because the constructor function for Lua (which is called in through VCreateActor()) queues an event that expects this event to have been handled } } else if (pView->VGetType() == GameView_Remote) { shared_ptr<NetworkGameView> pNetworkGameView = static_pointer_cast<NetworkGameView, IGameView>(pView); StrongActorPtr pActor = VCreateActor("actors\\remote_teapot.xml", NULL); if (pActor) { shared_ptr<EvtData_New_Actor> pNewActorEvent(GCC_NEW EvtData_New_Actor(pActor->GetId(), pNetworkGameView->VGetId())); IEventManager::Get()->VQueueEvent(pNewActorEvent); } } else if (pView->VGetType() == GameView_AI) { shared_ptr<AITeapotView> pAiView = static_pointer_cast<AITeapotView, IGameView>(pView); StrongActorPtr pActor = VCreateActor("actors\\ai_teapot.xml", NULL); if (pActor) { shared_ptr<EvtData_New_Actor> pNewActorEvent(GCC_NEW EvtData_New_Actor(pActor->GetId(), pAiView->VGetId())); IEventManager::Get()->VQueueEvent(pNewActorEvent); } } } break; } } }
int InternalScriptExports::CreateActor(const char* actorArchetype, LuaPlus::LuaObject luaPosition, LuaPlus::LuaObject luaYawPitchRoll) { if (!luaPosition.IsTable()) { GCC_ERROR("Invalid object passed to CreateActor(); type = " + std::string(luaPosition.TypeName())); return INVALID_ACTOR_ID; } if (!luaYawPitchRoll.IsTable()) { GCC_ERROR("Invalid object passed to CreateActor(); type = " + std::string(luaYawPitchRoll.TypeName())); return INVALID_ACTOR_ID; } Vec3 pos(luaPosition["x"].GetFloat(), luaPosition["y"].GetFloat(), luaPosition["z"].GetFloat()); Vec3 ypr(luaYawPitchRoll["x"].GetFloat(), luaYawPitchRoll["y"].GetFloat(), luaYawPitchRoll["z"].GetFloat()); Mat4x4 initialTransform; initialTransform.BuildYawPitchRoll(ypr.x, ypr.y, ypr.z); initialTransform.SetPosition(pos); TiXmlElement *overloads = NULL; StrongActorPtr pActor = g_pApp->m_pGame->VCreateActor(actorArchetype, overloads, &initialTransform); if (pActor) { shared_ptr<EvtData_New_Actor> pNewActorEvent(GCC_NEW EvtData_New_Actor(pActor->GetId())); IEventManager::Get()->VQueueEvent(pNewActorEvent); return pActor->GetId(); } return INVALID_ACTOR_ID; }