Exemple #1
0
//---------------------------------------
void Map::SpawnGameObjectEvent( Dictionary& params )
{
    std::string name;
    Vec2f position;

    // Get name
    if ( params.Get( "ObjectName", name ) != Dictionary::DErr_SUCCESS )
    {
        ConsolePrintf( CONSOLE_ERROR, "Failed to spawn GameObject: missing ObjectName\n" );
        return;
    }

    // Get position
    if ( params.Get( "Position", position ) != Dictionary::DErr_SUCCESS )
    {
        ConsolePrintf( CONSOLE_ERROR, "Failed to spawn GameObject: missing Position\n" );
        return;
    }

    // Spawn entity
    GameObject* gameObject = NULL;

    GameObjectDefinition* def = GameObjectDefinition::GetDefinitionByName( name );
    if ( def )
    {
        gameObject = def->Create();
        gameObject->Position = position;
        gameObject->GameMap = this;
        gameObject->SetBoundsFromSprite();
        gameObject->ObjectId = mObjects.size();

        // Call OnCreate for all logic nodes on the object
        gameObject->FinalizeNodes();

        mObjects.push_back( gameObject );
    }
    else
    {
        ConsolePrintf( CONSOLE_ERROR, "Failed to spawn GameObject: No GameObject found '%s'\n", name.c_str() );
    }
    params.Set( "GameObject", gameObject );
}
Exemple #2
0
//---------------------------------------
void Map::LoadObjectGroup( const XmlReader::XmlReaderIterator& itr )
{
    ObjLayer* layer = new ObjLayer;
    layer->Type = LT_OBJ;

    LoadLayerBaseInfo( layer, itr );

    for ( XmlReader::XmlReaderIterator objItr = itr.NextChild( "object" );
            objItr.IsValid(); objItr = objItr.NextSibling( "object" ) )
    {
        objItr.ValidateXMLAttributes( "x,y","name,width,height,type,visible" );

        std::string name = objItr.GetAttributeAsString( "name", "" );
        std::string type = objItr.GetAttributeAsString( "type", "" );
        int x = objItr.GetAttributeAsInt( "x" );
        int y = objItr.GetAttributeAsInt( "y" );
        int w = objItr.GetAttributeAsInt( "width", 0 );
        int h = objItr.GetAttributeAsInt( "height", 0 );
        bool visible = objItr.GetAttributeAsInt( "visible", 1 ) == 0 ? false : true;

        GameObject* gameObject;
        GameObjectDefinition* def = GameObjectDefinition::GetDefinitionByName( type );
        if ( def )
        {
            gameObject = def->Create();

            // Set attributes
            gameObject->Visible = layer->Visible;//visible;
            gameObject->Position.x = (float) x;
            gameObject->Position.y = (float) y;
            gameObject->BoundingRect.Set( x, y, x + w, y + h );
            gameObject->GameMap = this;
            gameObject->ObjectId = mObjects.size();

            // Load editor values for properties
            XmlReader::XmlReaderIterator propItr = objItr.NextChild( "properties" );
            if ( propItr.IsValid() )
            {
                LoadProperties( gameObject, propItr );
            }

            // Call OnCreate for all logic nodes on the object
            gameObject->FinalizeNodes();
        }
        else
        {
            ConsolePrintf( CONSOLE_WARNING, "Unknown object type: '%s'\n", type.c_str() );
            continue;
        }

        layer->Objs.push_back( gameObject );
        mObjects.push_back( gameObject );

        /*
        MapObject* mapObject = new MapObject(
        	name, type, RectI( x, y, x + w, y + h ), visible );

        // Read properties
        XmlReader::XmlReaderIterator propItr = objItr.NextChild( "properties" );
        if ( propItr.IsValid() )
        {
        	LoadProperties( mapObject->Properties, propItr );
        	mapObject->EvalProperties();
        }

        // Check for polyline
        XmlReader::XmlReaderIterator polylineItr = objItr.NextChild( "polyline" );
        if ( polylineItr.IsValid() )
        {
        	polylineItr.ValidateXMLAttributes( "points","" );

        	std::string pointCSV = polylineItr.GetAttributeAsString( "points" );
        	std::vector< std::string > csvTokens;
        	std::vector< std::string > csvTokenElements;
        	StringUtil::Tokenize( pointCSV, csvTokens, " " );
        	int xmin=0, xmax=0, ymin=0, ymax=0;
        	for ( auto csvItr = csvTokens.begin(); csvItr != csvTokens.end(); ++csvItr )
        	{
        		StringUtil::Tokenize( *csvItr, csvTokenElements, "," );

        		if ( csvTokenElements.size() == 2U )
        		{
        			int vx, vy;

        			if ( StringUtil::StringToType( csvTokenElements[0], &vx ) &&
        				 StringUtil::StringToType( csvTokenElements[1], &vy ) )
        			{
        				mapObject->Verticies.push_back( Vec2i( vx, vy ) );

        				if ( vx > xmax ) xmax = vx;
        				if ( vx < xmin ) xmin = vx;
        				if ( vy > ymax ) ymax = vy;
        				if ( vy < ymin ) ymin = vy;
        			}
        		}

        		csvTokenElements.clear();
        	}

        	mapObject->Shape = MapObject::Obj_POLYLINE;
        	mapObject->BoundingRect.Set( x+xmin, y+ymin, x+xmax, y+ymax );
        }
        // Rect
        else
        {
        	mapObject->Shape = MapObject::Obj_RECT;
        	mapObject->Verticies.push_back( Vec2i( x, y ) );
        	mapObject->Verticies.push_back( Vec2i( x+w, y ) );
        	mapObject->Verticies.push_back( Vec2i( x+w, y+h ) );
        	mapObject->Verticies.push_back( Vec2i( x, y+h ) );
        }

        layer->Objs.push_back( mapObject );
        */
    }
    mLayers.push_back( layer );
}