コード例 #1
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);
}