CEntity * CEntityFactory::CreateEntity(EntityId id){ String buffer = String::Read(m_entitiesInfo); //Analizamos el XML xml_document<> doc; doc.parse<0>((char*)buffer.ToCString()); //Dependiendo del id buscamos la entidad adecuada String entityName; switch (id){ case EDREADNOUGHT: entityName = "dreadnought"; break; case EAVATAR: entityName = "avatar"; break; case EDREADNOUGHTSHOT1: entityName = "dreadnoughtshot1"; break; case EDREADNOUGHTSHOT2: entityName = "dreadnoughtshot2"; break; case EAVATARSHOT1: entityName = "avatarshot1"; break; case EAVATARTRACTOR: entityName = "avatartractor"; break; case EFIGHTERSHOT: entityName = "fightershot"; break; } //Accedemos al nodo de la entidad xml_node<>* entityNode = doc.first_node()->first_node(entityName.ToCString()); CEntity * entity = new CEntity(id); //Accedemos al nodo componentes xml_node<>* componentsNode = entityNode->first_node("components"); //Accedemos al primer componente xml_node<>* componentNode = componentsNode->first_node(); //Buscamos todas las componentes while (componentNode != nullptr){ String name = componentNode->name(); //Creamos el nuevo componente Component * component = NewComponent(name, entity, entityName); //Lo añadimos a la entidad if (component){ entity->AddComponent(component); } componentNode = componentNode->next_sibling(); } m_entities.Add(entity); return entity; }
CEntity * CEntitiesFactory::SpawnEntity(const SEntityParams * params) { CEntity * et = new CEntity(params->GetSide(), m_world); if(params->GetType() == EET_SHIP) { //hotfix: create controls and register to InputManager if not AI AddComponents(et, params); //if (!static_cast<const SShipParams *>(params)->IsAI()) { InitEntityControls(et); /* LOOKING INSIDE init_world.json and setting position/rotation */ CCompTransform * transform = new CCompTransform(et, 0, 0, 0); et->AddComponent(transform); //will be set when init_world.json is parsed FILE * wFile = fopen("data/conf/init_world.json", "rb"); char buffer[65536]; assert(wFile != nullptr && "CEntitiesFactory::SpawnEntity()"); rapidjson::FileReadStream is(wFile, buffer, sizeof(buffer)); rapidjson::Document cDoc; cDoc.ParseStream<0, rapidjson::UTF8<>, rapidjson::FileReadStream>(is); assert(!cDoc.HasParseError()); rapidjson::Value player; if (et->GetSide() == EGS_PLAYER_1) { player = cDoc["player1"]; } else if (et->GetSide() == EGS_PLAYER_2) { player = cDoc["player2"]; } for (rapidjson::Value::ConstMemberIterator itr = player.MemberBegin(); itr != player.MemberEnd(); ++itr) { if (!strcmp(itr->name.GetString(), "position")) { SSetPosMsg posMsg(itr->value["x"].GetFloat(), itr->value["y"].GetFloat()); et->ReceiveMessage(posMsg); } else if (!strcmp(itr->name.GetString(), "rotation")) { SSetRotMsg rotMsg(itr->value.GetFloat()); et->ReceiveMessage(rotMsg); } } fclose(wFile); } else if(params->GetType() == EET_PROJECTILE) { AddComponents(et, params); } else if (params->GetType() == EET_EXPLOSION) { AddComponents(et, params); } else if (params->GetType() == EET_DECOY) { AddComponents(et, params); } else if (params->GetType() == EET_BOT) { AddComponents(et, params); } return et; }