コード例 #1
0
ファイル: LevelParser.cpp プロジェクト: paubereng/BaseCode
void LevelParser::parseObjectLayer(TiXmlElement * pObjectElement, std::vector<Layer*>* pLayers, Level* pLevel)
{
	ObjectLayer* pObjectLayer = new ObjectLayer();
	std::cout << pObjectElement->FirstChildElement()->Value();
	for (TiXmlElement* e = pObjectElement->FirstChildElement(); e !=
		NULL; e = e->NextSiblingElement())
	{
		std::cout << e->Value();
		if (e->Value() == std::string("object"))
		{
			int x, y, width, height, numSprites, speedX, speedY, maxSpeed, callbackId, collitionmargin;
			double friction;
			char* textureID;

			e->Attribute("x", &x);
			e->Attribute("y", &y);
			GameObject* pGameObject = GameObjectFactory::Instance()->CreateGameObject(e->Attribute("type"));
			

			for (TiXmlElement* properties = e->FirstChildElement();
			properties != NULL; properties = properties->NextSiblingElement())
			{
				if (properties->Value() == std::string("properties"))
				{
					for (TiXmlElement* property = properties->FirstChildElement(); property != NULL; property = property->NextSiblingElement())
					{
						if (property->Value() == std::string("property"))
						{
							if (property->Attribute("name") == std::string("textureWidth"))
							{
								property->Attribute("value", &width);
							}
							else if (property->Attribute("name") == std::string("textureHeight"))
							{
								property->Attribute("value", &height);
							}
							else if (property->Attribute("name") == std::string("speedY"))
							{
								property->Attribute("value", &speedY);
							}
							else if (property->Attribute("name") == std::string("textureID"))
							{
								const size_t len = strlen(property->Attribute("value"));
								textureID = new char[len + 1];
								strncpy(textureID, property->Attribute("value"), len);
								textureID[len] = '\0';
								
							}
							else if (property->Attribute("name") == std::string("speedX"))
							{
								property->Attribute("value", &speedX);
							}
							else if (property->Attribute("name") == std::string("numFrames"))
							{
								property->Attribute("value", &numSprites);
							}
							else if (property->Attribute("name") == std::string("maxSpeed"))
							{
								property->Attribute("value", &maxSpeed);
							}
							else if (property->Attribute("name") == std::string("callbackId"))
							{
								property->Attribute("value", &callbackId);
							}
							else if (property->Attribute("name") == std::string("friction"))
							{
								property->Attribute("value", &friction);
							}
							else if (property->Attribute("name") == std::string("collitionmargin"))
							{
								property->Attribute("value", &collitionmargin);
							}
						}
					}
				}
			}
			if (dynamic_cast<CollisionObject*>(pGameObject))
			{
				CollisionObject* pCollisionObj = dynamic_cast< CollisionObject* >(pGameObject);
				pCollisionObj->setCollisionLayers(pLevel->getCollisionLayers());
				pCollisionObj->setCollisionMargin(collitionmargin);
				TheCamera::Instance()->setTarget(dynamic_cast<Player*>(pGameObject));
				TheCamera::Instance()->setPosition(dynamic_cast<Player*>(pGameObject)->getPosition());
			}
			pGameObject->load(new LoaderParams(x, y, width, height, textureID, numSprites, speedX, speedY, maxSpeed, friction, callbackId));
			if (textureID == "Player")
			{
				pLevel->setPlayer(dynamic_cast<Player*>(pGameObject));
			}
			pObjectLayer->getGameObjects()->push_back(pGameObject);
		}
	}
	pLayers->push_back(pObjectLayer);
}
コード例 #2
0
ファイル: LevelParser.cpp プロジェクト: carretero4/BaseCode
void LevelParser::parseObjectLayer(TiXmlElement* pObjectElement, std::vector<Layer*> *pLayers, Level *pLevel)
{
	ObjectLayer* pObjectLayer = new ObjectLayer();

	for (TiXmlElement* e = pObjectElement->FirstChildElement(); e != NULL; e = e->NextSiblingElement())
	{
		if (e->Value() == std::string("object"))
		{

			int x, y, width, height, spriteNum, rowNum, callbackID;
			std::string textureID, fileName;
			// get the initial node values type, x and y
			e->Attribute("x", &x);
			e->Attribute("y", &y);
			e->Attribute("width", &m_width);
			e->Attribute("height", &m_height);
			
			GameObject* pGameObject = GameObjectFactory::Instance()->CreateGameObject(e->Attribute("type"));
			// get the property values
			for (TiXmlElement* properties = e->FirstChildElement(); properties != NULL; properties = properties->NextSiblingElement())
			{
				if (properties->Value() == std::string("properties"))
				{
					for (TiXmlElement* property = properties->FirstChildElement(); property != NULL; property = property->NextSiblingElement())
					{
						if (property->Value() == std::string("property"))
						{
							if (property->Attribute("name") == std::string("callbackID"))
							{
								property->Attribute("value", &callbackID);
							}
							else if (property->Attribute("name") == std::string("rowNum"))
							{
								property->Attribute("value", &rowNum);
							}
							else if (property->Attribute("name") == std::string("spriteNum"))
							{
								property->Attribute("value", &m_spriteNum);
							}
							else if (property->Attribute("name") == std::string("fileName"))
							{
								property->Attribute("value", &m_fileName);
							}
							else if (property->Attribute("name") == std::string("textureID")) {
								textureID = property->Attribute("value");
							}
							
						}
					}
				}
			}
			if (m_fileName == 1) { file = "gorda.png"; }
			else if (m_fileName == 2) { file = "cine.png"; }

			
			pObjectLayer->getGameObjects()->push_back(pGameObject);

			if (pGameObject != NULL) {
				pGameObject->load(new LoaderParams(x, y - m_height, m_width, m_height, textureID, file, m_spriteNum, rowNum, callbackID));
			    pGameObject->setCollisionLayers(pLevel->getCollisionLayers());
				if (textureID == "player") // check if it's the player
				{
					//Camera::Instance()->setTarget(pGameObject);
				}
				pObjectLayer->getGameObjects()->push_back(pGameObject);
			}
		}
	}
	pLayers->push_back(pObjectLayer);
}
コード例 #3
0
ファイル: LevelParser.cpp プロジェクト: dscravag/Prehisto
void LevelParser::parseObjectLayer(TiXmlElement* pObjectElement, std::vector<Layer*> *pLayers, Level* pLevel)
{
    // create an object layer
    ObjectLayer* pObjectLayer = new ObjectLayer();
    
    for(TiXmlElement* e = pObjectElement->FirstChildElement(); e != NULL; e = e->NextSiblingElement())
    {
        if(e->Value() == std::string("object"))
        {
            int x, y, width, height, numFrames, callbackID = 0, animSpeed = 0;
            std::string textureID;
            std::string type;
            
            // get the initial node values type, x and y
            e->Attribute("x", &x);
            e->Attribute("y", &y);
            
            type = e->Attribute("type");
            GameObject* pGameObject = TheGameObjectFactory::Instance()->create(type);
            
            // get the property values
            for(TiXmlElement* properties = e->FirstChildElement(); properties != NULL; properties = properties->NextSiblingElement())
            {
                if(properties->Value() == std::string("properties"))
                {
                    for(TiXmlElement* property = properties->FirstChildElement(); property != NULL; property = property->NextSiblingElement())
                    {
                        if(property->Value() == std::string("property"))
                        {
                            if(property->Attribute("name") == std::string("numFrames"))
                            {
                                property->Attribute("value", &numFrames);
                            }
                            else if(property->Attribute("name") == std::string("textureHeight"))
                            {
                                property->Attribute("value", &height);
                            }
                            else if(property->Attribute("name") == std::string("textureID"))
                            {
                                textureID = property->Attribute("value");
                            }
                            else if(property->Attribute("name") == std::string("textureWidth"))
                            {
                                property->Attribute("value", &width);
                            }
                            else if(property->Attribute("name") == std::string("callbackID"))
                            {
                                property->Attribute("value", &callbackID);
                            }
                            else if(e->Attribute("name") == std::string("animSpeed"))
                            {
                                property->Attribute("value", &animSpeed);
                            }
                        }
                    }
                }
            }
            // load the object
            pGameObject->load(std::unique_ptr<LoaderParams>(new LoaderParams(x, y, width, height, textureID, numFrames,callbackID, animSpeed)));
            // set the collision layers
            pGameObject->setCollisionLayers(pLevel->getCollisionLayers());
            
            if(type == "Player")
            {
                pLevel->setPlayer(dynamic_cast<Player*>(pGameObject));
            }
            
            pObjectLayer->getGameObjects()->push_back(pGameObject);
        }
    }
    
    pLayers->push_back(pObjectLayer);
}
コード例 #4
0
ファイル: LevelParser.cpp プロジェクト: iiechapman/Sandman2D
//For extracting objects from object layers
void LevelParser::parseObjectLayer
(TiXmlElement *pObjectElement, vector<ILayer *> *pLayers,
 string layerType, Level* pLevel ,PlayState* newState){
    
    //Create an object layer
    ObjectLayer* pObjectLayer = new ObjectLayer();
    pObjectLayer->setType(layerType);
    cout << "Created new " << pObjectLayer->getType() << " layer\n";
    //cout << "Current Value: ";
    //cout << pObjectElement->FirstChildElement()->Value() << "\n";
    
    for (TiXmlElement* e = pObjectElement->FirstChildElement();
         e != NULL; e = e->NextSiblingElement()){
        cout << "Checking " << e->Value() << "\n";
        if (e->Value() == string("object")){
            int x(0), y(0), width(0), height(0), numFrames(1), callbackID(0), animSpeed(1);
            
            SDL_Color color;
            SDL_BlendMode blendMode = SDL_BLENDMODE_NONE;
            
            string textureID(""),name(""),lockTo(""),scrollLock(""),ai("");
            
            
            //Get initial values
            e->Attribute("x", &x);
            e->Attribute("y", &y);
            //cout << "Y: " << x << "\nY: " << y << "\n";
            name = e->Attribute("name");
            
            int GID = 0;
            e->Attribute("gid",&GID);
            
            
            //Check if object exists in element library
            IGameObject* pGameObject;
            if ((*newState->getElements())[name]){
                GameObjectParams* elementParams = (*newState->getElements())[name];
                cout << "Loaded from library: " << elementParams->getName() << "\n";
                pGameObject = GameObjectFactory::Instance()->create(elementParams->getType());
            } else {
                cout << "Object \"" << e->Attribute("name") << "\" not found in library!\n";
                pGameObject = GameObjectFactory::Instance()->create(e->Attribute("type"));
            }
            
            
            //Prep game object params
            pGameObject->load
            (*new GameObjectParams
             (name, (x), (y-height),width, height, textureID,callbackID,animSpeed));
            
            //Load default params per element library
            if ((*newState->getElements())[name]){
                pGameObject->GetParams() = *(*newState->getElements())[name];
            }
            
            
            pGameObject->GetParams().getPosition().setX(x);
            pGameObject->GetParams().getPosition().setY(y);
            
            //Overload properties based on unique object params IN TILED
            for (TiXmlElement* properties = e->FirstChildElement();
                 properties != NULL; properties = properties->NextSiblingElement()){
                if (properties->Value() == string("properties")){
                    
                    for (TiXmlElement* property = properties->FirstChildElement();
                         property!=NULL; property = property->NextSiblingElement()){
                        
                        if (property->Value() == string("property")){
                            
                            if (property->Attribute("name") == string("numFrames")){
                                property->Attribute("value",&numFrames);
                                pGameObject->GetParams().setMaxFrames(numFrames);
                                cout << "Set frames to " << numFrames << "\n";
                                
                            } else if (property->Attribute("name") == string("lockTo")){
                                lockTo = property->Attribute("value");
                                pGameObject->GetParams().setLockTo(lockTo);
                                
                            } else if (property->Attribute("name") == string("ai")){
                                ai = property->Attribute("value");
                                
                                cout << "checking ai flag\n";
                                
                                if (ai == string("true")){
                                    pGameObject->GetParams().setAI(true);
                                } else {
                                    pGameObject->GetParams().setAI(false);
                                }
                                
                            } else if (property->Attribute("name") == string("textureHeight")){
                                property->Attribute("value",&height);
                                pGameObject->GetParams().setHeight(height);
                                
                            } else if (property->Attribute("name") == string("scrollLock")){
                                scrollLock = property->Attribute("value");
                                pGameObject->GetParams().setScrolling(scrollLock != string("true"));
                                
                            } else if (property->Attribute("name") == string("textureWidth")){
                                property->Attribute("value",&width);
                                pGameObject->GetParams().setWidth(width);
                                
                            } else if (property->Attribute("name") == string("animSpeed")){
                                property->Attribute("value",&animSpeed);
                                pGameObject->GetParams().setAnimSpeed(animSpeed);
                                
                            } else if (property->Attribute("name") == string("callbackID")){
                                property->Attribute("value",&callbackID);
                                pGameObject->GetParams().setCallBackID(callbackID);
                                
                            } else if (property->Attribute("name") == string("textureID")){
                                textureID = property->Attribute("value");
                                pGameObject->GetParams().setTextureID(textureID);
                                
                            } else if (property->Attribute("name") == string("blendMode")){
                                if (property->Attribute("value") == string("add")){
                                    blendMode = SDL_BLENDMODE_ADD;
                                } else if (property->Attribute("value") == string("blend")){
                                    blendMode = SDL_BLENDMODE_BLEND;
                                } else if (property->Attribute("value") == string("mod")){
                                    blendMode = SDL_BLENDMODE_MOD;
                                }
                                pGameObject->GetParams().setBlendMode(blendMode);
                                
                            } else if (property->Attribute("name") == string("alpha")){
                                int temp;
                                property->Attribute("value",&temp);
                                color.a = temp;
                                
                            }  else if (property->Attribute("name") == string("red")){
                                int temp;
                                property->Attribute("value",&temp);
                                color.r = temp;
                                
                            }  else if (property->Attribute("name") == string("green")){
                                int temp;
                                property->Attribute("value",&temp);
                                color.g = temp;
                                
                            }  else if (property->Attribute("name") == string("blue")){
                                int temp;
                                property->Attribute("value",&temp);
                                color.b = temp;
                            }
                            pGameObject->GetParams().setColor(color);
                            
                        }
                    }
                }
            }
            

            //If object is player set game player pointer accordingly
            if (pObjectLayer->getType() == string("player")){
                cout << "Adding player to object spot "
                << pObjectLayer->getGameObjects()->size() << "\n";
                
                if (Game::Instance()->isLiveModeOn()){
                    pGameObject = 0;
                } else {
                    pLevel->setPlayer(dynamic_cast<Player*>(pGameObject));
                    Game::Instance()->setPlayer(dynamic_cast<Player*>(pGameObject));
                }
            }
            
            if (pGameObject){
                if(pGameObject->GetParams().getType() == string("Light")){
                    pGameObject->GetParams().getPosition().setX
                    (pGameObject->GetParams().getPosition().getX() - pGameObject->GetParams().getWidth()/2);
                    
                    pGameObject->GetParams().getPosition().setY
                    (pGameObject->GetParams().getPosition().getY() - pGameObject->GetParams().getHeight()/2);
                }
            }
            
            if (pGameObject){
                pObjectLayer->getGameObjects()->push_back(pGameObject);
                cout << "Created new " << pGameObject->GetParams().getType() << "\n";
            }
        }
    }
    pLayers->push_back(pObjectLayer);
}