EntityCollection<EntityType>& IScene::GetEntities() { static const std::type_index type = typeid(std::decay_t<EntityType>); EntityCollectionBase* entities = GetEntities(type); if (!entities) { std::unique_ptr<EntityCollectionBase> newCollection(std::make_unique<EntityCollection<EntityType>>()); NewCollection(std::move(newCollection), type); entities = GetEntities(type); assert(entities != nullptr); } return static_cast<EntityCollection<EntityType>&>(*entities); }
/// get a triangle selector for all the objects matching the types mask IMetaTriangleSelector_IPtr Simulation::GetCollisionTriangleSelector( size_t types ) { IMetaTriangleSelector_IPtr meta_selector; // first, lookup the type mask in our table hash_map<uint32_t, IMetaTriangleSelector_IPtr>::const_iterator needle; needle = mCollisionSelectors.find(types); if (needle != mCollisionSelectors.end()) { // if found, return the selector meta_selector = needle->second; } else { // if not found, create the selector meta_selector = mIrr.getSceneManager()->createMetaTriangleSelector(); SimEntitySet ents = GetEntities(types); // iterate over all entities of that type and add them to the selector for (SimEntitySet::const_iterator iter = ents.begin(); iter != ents.end(); ++iter) { SimEntityPtr ent = *iter; ITriangleSelector_IPtr tri_selector = ent->GetSceneObject()->GetTriangleSelector(); meta_selector->addTriangleSelector(tri_selector.get()); } // remember for future reuse mCollisionSelectors[types] = meta_selector; LOG_F_DEBUG("collision", "created triangle selector for mask " << types << " with: " << meta_selector->getTriangleCount() << " triangles"); } return meta_selector; }
Room* RoomLoader::LoadRoom( const std::string& aRoomName ) { Tmx::Map map; map.ParseFile(aRoomName); if (map.HasError()) { printf("error code: %d\n", map.GetErrorCode()); printf("error text: %s\n", map.GetErrorText().c_str()); return NULL; } Room* room = new Room(GetLayer(map, "background"), GetLayer(map, "middle"), GetLayer(map, "foreground")); std::vector<Entity*> entities = GetEntities(map, "entities"); float2 topleft(0,0); float2 bottomright((map.GetWidth()) * map.GetTileWidth(), (map.GetHeight()) * map.GetTileHeight()); std::vector<std::pair<float2, float2>> rects = GetCameraRects(map, "camera"); room->setCameraRect(topleft, bottomright); room->setCamera(new Camera(rects)); for (unsigned int i = 0; i < entities.size(); i++) { room->addEntity(entities[i]); } return room; }
void ListenerSystem::OnUpdate(float /*elapsedTime*/) { std::size_t activeListenerCount = 0; for (const Ndk::EntityHandle& entity : GetEntities()) { // Is the listener actif ? const ListenerComponent& listener = entity->GetComponent<ListenerComponent>(); if (!listener.IsActive()) continue; // We get the position and the rotation to affect these to the listener const NodeComponent& node = entity->GetComponent<NodeComponent>(); Nz::Audio::SetListenerPosition(node.GetPosition(Nz::CoordSys_Global)); Nz::Audio::SetListenerRotation(node.GetRotation(Nz::CoordSys_Global)); // We verify the presence of a component of velocity // (The listener'speed does not move it, but disturbs the sound like Doppler effect) if (entity->HasComponent<VelocityComponent>()) { const VelocityComponent& velocity = entity->GetComponent<VelocityComponent>(); Nz::Audio::SetListenerVelocity(velocity.linearVelocity); } activeListenerCount++; } if (activeListenerCount > 1) NazaraWarning(Nz::String::Number(activeListenerCount) + " listeners were active in the same update loop"); }
void ListenerSystem::OnUpdate(float elapsedTime) { std::size_t activeListenerCount = 0; for (const Ndk::EntityHandle& entity : GetEntities()) { // Is the listener actif ? const ListenerComponent& listener = entity->GetComponent<ListenerComponent>(); if (!listener.IsActive()) continue; Nz::Vector3f oldPos = Nz::Audio::GetListenerPosition(); // We get the position and the rotation to affect these to the listener const NodeComponent& node = entity->GetComponent<NodeComponent>(); Nz::Vector3f newPos = node.GetPosition(Nz::CoordSys_Global); Nz::Audio::SetListenerPosition(newPos); Nz::Audio::SetListenerRotation(node.GetRotation(Nz::CoordSys_Global)); // Compute listener velocity based on their old/new position Nz::Vector3f velocity = (newPos - oldPos) / elapsedTime; Nz::Audio::SetListenerVelocity(velocity); activeListenerCount++; } if (activeListenerCount > 1) NazaraWarning(Nz::String::Number(activeListenerCount) + " listeners were active in the same update loop"); }
// Creates a serializable object from the given parameters. lean::scoped_ptr<Entity, lean::critical_ref> EntitySerializer::Create(const beCore::Parameters &creationParameters, const beCore::ParameterSet ¶meters) const { lean::scoped_ptr<Entity> entity( GetEntities(parameters)->AddEntity() ); entity->SetName( creationParameters.GetValueDefault<beCore::Exchange::utf8_string>("Name") ); return entity.transfer(); }
std::vector<std::string> BSPFile::RequiredFilesInEntities(void) const { std::vector<std::string> result; std::vector<std::vector<std::pair<std::string, std::string> > > entities = GetEntities(); for (size_t x = 0; x < entities.size(); x++) { for (size_t y = 0; y < entities.at(x).size(); y++) { if (!entities.at(x).at(y).first.compare("requiredfiles")) //eg. classname worldspawn: "requiredfiles" "scripts/flame1.txt pics/mapshots/mapshot.jpg" { size_t pos = 0; size_t oldpos = 0; result.reserve(result.size() + std::count(entities.at(x).at(y).second.begin(), entities.at(x).at(y).second.end(), ' ')); while ((pos = entities.at(x).at(y).second.find(' ', oldpos)) != std::string::npos) { std::string currentFile = entities.at(x).at(y).second.substr(oldpos, pos - oldpos); ReplaceFileEnding(¤tFile); result.push_back(currentFile); oldpos = pos + 1; } std::string currentFile = entities.at(x).at(y).second.substr(oldpos); ReplaceFileEnding(¤tFile); result.push_back(entities.at(x).at(y).second.substr(oldpos)); } else if (!entities.at(x).at(y).first.compare("model")) //eg. classname func_model: "model" "models/props/keyboard/keyboard1.md2" { if (entities.at(x).at(y).second.compare(0, 1, "*")) //theres an internal refenrece possible with *X, so this does not relate to an actual model file { std::string currentFile = entities.at(x).at(y).second; ReplaceFileEnding(¤tFile); result.push_back(currentFile); } } else if (!entities.at(x).at(y).first.compare("sky")) { result.reserve(result.size() + 6); std::string path = entities.at(x).at(y).second.substr(0, entities.at(x).at(y).second.find(" ")); result.push_back("env/" + path + "up.img"); result.push_back("env/" + path + "dn.img"); result.push_back("env/" + path + "lf.img"); result.push_back("env/" + path + "ft.img"); result.push_back("env/" + path + "rt.img"); result.push_back("env/" + path + "bk.img"); } else if (!entities.at(x).at(y).first.compare("noise")) // e.g. classname target_speaker: "noise" "kurwa/mozart" { std::string currentFile = "sound/" + entities.at(x).at(y).second + ".wav"; result.push_back(currentFile); } } } return result; }
// Loads an entity from the given xml node. lean::scoped_ptr<Entity, lean::critical_ref> EntitySerializer::Load(const rapidxml::xml_node<lean::utf8_t> &node, beCore::ParameterSet ¶meters, beCore::SerializationQueue<beCore::LoadJob> &queue) const { lean::scoped_ptr<Entity> entity( GetEntities(parameters)->AddEntity() ); entity->SetName( GetName(node) ); Load(entity.get(), node, parameters, queue); return entity.transfer(); }
const EntityCollection<EntityType>& IScene::GetEntities() const { static const std::type_index type = typeid(std::decay_t<EntityType>); static const EntityCollection<EntityType> emptyCollection; const EntityCollectionBase* entities = GetEntities(type); if (entities) { return static_cast<const EntityCollection<EntityType>&>(*entities); } else { return emptyCollection; } }
void test::TestSpeed() { auto c1 = new stoked::ComponentPool<ComponentA>(1024); auto c2 = new stoked::ComponentPool<ComponentB>(1024); auto c3 = new stoked::ComponentPool<ComponentC>(1024); ComponentTypeValue c1ValueType = stoked::ComponentType::value<ComponentA>(); ComponentTypeValue c2ValueType = stoked::ComponentType::value<ComponentB>(); ComponentTypeValue c3ValueType = stoked::ComponentType::value<ComponentC>(); assert(c1ValueType == stoked::ComponentType::value<ComponentA>()); assert(c2ValueType == stoked::ComponentType::value<ComponentB>()); assert(c3ValueType == stoked::ComponentType::value<ComponentC>()); auto components = new stoked::ComponentPool<ComponentA>(1024); auto entities = new stoked::EntityPool(4096); auto start = std::chrono::system_clock::now().time_since_epoch() / std::chrono::milliseconds(1); unsigned int totalComponentsAdded = 0; const unsigned int ITERATIONS = 10000; for (int i = 0; i < ITERATIONS; ++i) { unsigned int j = 0; while (j < entities->GetEntities()->size()) { auto ent = entities->Create(); assert(ent != nullptr); auto testComponent = components->Get(); if (testComponent) { totalComponentsAdded++; ent->AddComponent(testComponent); assert(ent->ContainsComponent<ComponentA>()); if (ent->ContainsComponent<ComponentA>()) { auto com = ent->GetComponent<ComponentA>(); assert(com != nullptr); } } ++j; } entities->FreeAll(); } auto end = std::chrono::system_clock::now().time_since_epoch() / std::chrono::milliseconds(1); fprintf(stderr, "Took %lu ms for %lu components\n", end - start, totalComponentsAdded); delete entities; delete components; PASSED(); }
int GameClient::Deinitialize() { bIsInitialized = false; TotalPlayers = 0; GetEntities()->DeleteEntities(); closesocket(ServerSocket); ServerSocket = INVALID_SOCKET; WSACleanup(); DeinitialiseUI(); //D3DHookDeInit(); return 1; }
void StartResolve() { clib::recursive_mutex::scoped_lock proc(m_mutex); if (m_resolved) { proc.unlock(); on_refresh(this, EVENT_RESOLVED); return; } m_resolved = true; Analysis::IEntityVector entities; GetEntities(&entities); m_resolveThreads.Append(__FUNCTION__, boost::bind(&threadResolve, this, entities)); }
bool GameClient::Connect() { char IP[15] = ""; SOCKADDR_IN ServerAddr; unsigned short ClientPort = 41805; long rc = 0; if(!bIsConnected) { IOStream::Instance() << BootMessage << "Initializing Connection" <<endl; Initialize(); FILE *Realmlist = fopen("realmlist.wth","r"); if(!Realmlist) { IOStream::Instance() << Error << "File realmlist.wth could not be found" << endl; return false; } while(!feof(Realmlist)) { fscanf(Realmlist,"%14s",IP); if(!fscanf(Realmlist,"%hu",&ClientPort)) ClientPort = 41805; IOStream::Instance() << BootMessage << "Trying to connect to "<<IP << " : "<<ClientPort <<endl; memset(&ServerAddr,NULL,sizeof(SOCKADDR_IN)); ServerAddr.sin_addr.s_addr = inet_addr(IP); ServerAddr.sin_port = htons(ClientPort); ServerAddr.sin_family = AF_INET; rc = connect(ServerSocket,(SOCKADDR *)&ServerAddr,sizeof(SOCKADDR)); if(rc == SOCKET_ERROR) { IOStream::Instance() << "Error" << WSAGetLastError() << " establishing connection " <<endl; continue; } else { ChunkPermissions all(MATCH_ALL); IOStream::Instance() << "Successfully connected" << endl; conn = new NetworkConnection(GetEntities(),ServerSocket,boost::bind<>(&GameClient::Disconnect,boost::ref(*this)) ); conn->SetPermissions(all); IOStream::Instance() << BootMessage << "Waiting for Player ID" <<endl; bIsConnected = true; break; } } fclose(Realmlist); } return true; }
Room* RoomLoader::LoadRoom( int x, int y ) { const Tmx::ObjectGroup *roomObjectGroup = map.GetObjectGroup(0); int roomX = -1; int roomY = -1; int roomW = -1; int roomH = -1; for (int i = 0; i < roomObjectGroup->GetNumObjects(); i++) { const Tmx::Object *roomObject = roomObjectGroup->GetObject(i); int rX = roomObject->GetX() / 10; int rY = roomObject->GetY() / 10; int rW = roomObject->GetWidth() / 10; int rH = roomObject->GetHeight() / 10; if (x >= rX && y >= rY && x < rX + rW && y < rY + rH) { roomX = rX; roomY = rY; roomW = rW; roomH = rH; break; } } if (roomX < 0) { //throw std::exception("Found no room at these coordinates"); throw std::runtime_error("Found no room at these coordinates"); } Room* room = new Room(GetLayer(map, roomX, roomY, roomW, roomH, "tiles"), roomX, roomY); std::vector<Entity*> entities = GetEntities(map, roomX, roomY, roomW, roomH, "entities"); room->setCamera(new Camera()); for (unsigned int i = 0; i < entities.size(); i++) { room->addEntity(entities[i]); } return room; }
vector<UserEntity*>* Session::GetEntities() { return GetEntities(""); }