Example #1
0
void GameInst::LoadLevel()
{
    sf::Vector2f windowDim = m_GUIMgr.GetWindowDim();

    //-------------------------------------- chipmunk physics
    // cpVect is a 2D vector and cpv() is a shortcut for initializing them.
    cpVect gravity = cpv(0, 980);

    // Create a physworld
    m_pSpace = cpSpaceNew();
    cpSpaceSetGravity(m_pSpace, gravity);

    cpFloat offSet = -16;
    //top
    m_WorldBounds.Top = cpSegmentShapeNew(m_pSpace->staticBody, cpv(offSet,offSet), cpv(cpFloat(windowDim.x)+offSet,offSet), 1);
    cpShapeSetFriction(m_WorldBounds.Top, 0.5);
    cpSpaceAddShape(m_pSpace, m_WorldBounds.Top);
    cpShapeSetCollisionType(m_WorldBounds.Top, SURFACE_BOTTOM);
    //bottom
    m_WorldBounds.Bottom = cpSegmentShapeNew(m_pSpace->staticBody, cpv(offSet,cpFloat(windowDim.y)+offSet), cpv(cpFloat(windowDim.x)+offSet,cpFloat(windowDim.y)+offSet), 1);
    cpShapeSetFriction(m_WorldBounds.Bottom, 0.5);
    cpSpaceAddShape(m_pSpace, m_WorldBounds.Bottom);
    cpShapeSetCollisionType(m_WorldBounds.Bottom, SURFACE_TOP);
    //left
    m_WorldBounds.Left = cpSegmentShapeNew(m_pSpace->staticBody, cpv(offSet,offSet), cpv(offSet,cpFloat(windowDim.y)+offSet), 1);
    cpShapeSetFriction(m_WorldBounds.Left, 0.5);
    cpSpaceAddShape(m_pSpace, m_WorldBounds.Left);
    cpShapeSetCollisionType(m_WorldBounds.Left, SURFACE_RIGHT);
    //right
    m_WorldBounds.Right = cpSegmentShapeNew(m_pSpace->staticBody, cpv(cpFloat(windowDim.x)+offSet,offSet), cpv(cpFloat(windowDim.x)+offSet,cpFloat(windowDim.y)+offSet), 1);
    cpShapeSetFriction(m_WorldBounds.Right, 0.5);
    cpSpaceAddShape(m_pSpace, m_WorldBounds.Right);
    cpShapeSetCollisionType(m_WorldBounds.Right, SURFACE_LEFT);

    //player-surface collision callbacks
    cpSpaceAddCollisionHandler(m_pSpace,PLAYER, SURFACE_TOP,cpCollisionBeginFunc(PlayerSurfaceCollision),NULL,NULL,NULL,NULL);
    cpSpaceAddCollisionHandler(m_pSpace,PLAYER, SURFACE_BOTTOM,cpCollisionBeginFunc(PlayerSurfaceCollision),NULL,NULL,NULL,NULL);
    cpSpaceAddCollisionHandler(m_pSpace,PLAYER, SURFACE_LEFT,cpCollisionBeginFunc(PlayerSurfaceCollision),NULL,NULL,NULL,NULL);
    cpSpaceAddCollisionHandler(m_pSpace,PLAYER, SURFACE_RIGHT,cpCollisionBeginFunc(PlayerSurfaceCollision),NULL,NULL,NULL,NULL);

    //create player
    m_pPlayer = new Player(m_ResMgr, *m_pSpace);
    m_pPlayer->SetPosition(900, 600);
    m_Renderer.AddDrawableSprite(m_pPlayer->GetSprite());

    //load level data from file
    std::fstream file;
    file.open(("media/level1.txt"));
    std::string line;
    int curLine = 0;
    while( std::getline(file,line) )
    {
        //
        for(unsigned int i = 0; i < line.size(); i++)
        {
            char c = line[i];
            switch(c)
            {
            case('#'):
            {
                Block *block = new Block(m_ResMgr, *m_pSpace, Block::BLOCK_SOLID, sf::Vector2f(float(i)*32,float(curLine)*32) );
                m_blocks.push_back(block);
                //block->SetPosition();
                m_Renderer.AddDrawableSprite(block->GetSprite());
                break;
            }
            case('>'):
            {
                Emitters.push_back(new Emitter(m_ResMgr, *m_pSpace, sf::Vector2f(float(i * 32), float(curLine * 32)) ));
                Emitters.back()->Show();
                break;
            }
            case('<'):
            {
                /*Emitters.push_back(new Emitter(m_ResMgr, *m_pSpace));
                Emitters.back()->SetPosition(i * 32, curLine * 32);
                m_Renderer.AddDrawableSprite(Emitters.back()->GetSprite());*/
                break;
            }
            }
            //
        }
        curLine++;
    }
    file.close();

    file.open(("media/level1_binds.txt"));
    while( std::getline(file,line) )
    {
        char buffer[32];
        //char* tokenBuffer;
        sprintf_s(buffer, 32, line.c_str());

        // Get the block IDs to link them
        //int block1 = atoi(strtok_s(buffer,",", &tokenBuffer)) + atoi(strtok_s(NULL ,"=", &tokenBuffer)) * 32;
        //int block2 = atoi(strtok_s(NULL,",", &tokenBuffer)) + atoi(strtok_s(NULL,",", &tokenBuffer)) * 32;

        // Link them
        //m_blocks[block1]->SetOutput(block2);
    }
    file.close();

    //rebuild static level geometry
    //cpSpaceRehashStatic();
}
void GameInst::LoadLevel()
{
	sf::Vector2f windowDim = m_GUIMgr.GetWindowDim();

	//-------------------------------------- chipmunk physics
	// cpVect is a 2D vector and cpv() is a shortcut for initializing them.
	cpVect gravity = cpv(0, 980);
  
	// Create a physworld
	m_pSpace = cpSpaceNew();
	cpSpaceSetGravity(m_pSpace, gravity);

	cpFloat offSet = 0;
	//top
	m_WorldBounds.Top = cpSegmentShapeNew(m_pSpace->staticBody, cpv(offSet,offSet), cpv(cpFloat(windowDim.x)+offSet,offSet), 1);
	cpShapeSetFriction(m_WorldBounds.Top, 0.5);
	cpSpaceAddShape(m_pSpace, m_WorldBounds.Top);
	cpShapeSetCollisionType(m_WorldBounds.Top, SURFACE_BOTTOM);
	//bottom
	m_WorldBounds.Bottom = cpSegmentShapeNew(m_pSpace->staticBody, cpv(offSet,cpFloat(windowDim.y)+offSet), cpv(cpFloat(windowDim.x)+offSet,cpFloat(windowDim.y)+offSet), 1);
	cpShapeSetFriction(m_WorldBounds.Bottom, 0.5);
	cpSpaceAddShape(m_pSpace, m_WorldBounds.Bottom);
	cpShapeSetCollisionType(m_WorldBounds.Bottom, SURFACE_TOP);
	//left
	m_WorldBounds.Left = cpSegmentShapeNew(m_pSpace->staticBody, cpv(offSet,offSet), cpv(offSet,cpFloat(windowDim.y)+offSet), 1);
	cpShapeSetFriction(m_WorldBounds.Left, 0.5);
	cpSpaceAddShape(m_pSpace, m_WorldBounds.Left);
	cpShapeSetCollisionType(m_WorldBounds.Left, SURFACE_RIGHT);
	//right
	m_WorldBounds.Right = cpSegmentShapeNew(m_pSpace->staticBody, cpv(cpFloat(windowDim.x)+offSet,offSet), cpv(cpFloat(windowDim.x)+offSet,cpFloat(windowDim.y)+offSet), 1);
	cpShapeSetFriction(m_WorldBounds.Right, 0.5);
	cpSpaceAddShape(m_pSpace, m_WorldBounds.Right);
	cpShapeSetCollisionType(m_WorldBounds.Right, SURFACE_LEFT);
	
	//player-surface collision callbacks
	cpSpaceAddCollisionHandler(m_pSpace,PLAYER, SURFACE_TOP,cpCollisionBeginFunc(PlayerSurfaceCollision),NULL,NULL,NULL,NULL);
	cpSpaceAddCollisionHandler(m_pSpace,PLAYER, SURFACE_BOTTOM,cpCollisionBeginFunc(PlayerSurfaceCollision),NULL,NULL,NULL,NULL);
	cpSpaceAddCollisionHandler(m_pSpace,PLAYER, SURFACE_LEFT,cpCollisionBeginFunc(PlayerSurfaceCollision),NULL,NULL,NULL,NULL);
	cpSpaceAddCollisionHandler(m_pSpace,PLAYER, SURFACE_RIGHT,cpCollisionBeginFunc(PlayerSurfaceCollision),NULL,NULL,NULL,NULL);

	//load level data from file
	std::fstream file;
	file.open(("media/level1.txt"));
	std::string line;
	int curLine = 0;

	while( std::getline(file,line) )
	{
		//
		for(unsigned int i = 0; i < line.size();i++)
		{
			bool playerMade = false;
			char c = line[i];
			switch(c)
			{
			case('p'):
				{
					//create player
					if (!playerMade) {
						m_pPlayer = new Player(m_ResMgr, *m_pSpace);
						m_pPlayer->SetPosition(float(i)*32, float(curLine)*32);
						m_Renderer.AddDrawableSprite(m_pPlayer->GetSprite());
						playerMade = true;
					}
					break;
				}
			case('#'):
				{
					Block *block = new Block(m_ResMgr, *m_pSpace, Block::BLOCK_SOLID, sf::Vector2f(float(i)*32,float(curLine)*32) );
					m_blocks.push_back(block);
					m_Renderer.AddDrawableSprite(block->GetSprite());
					break;
				}
			case('>'):
				{
					Emitters.push_back(new Emitter(m_ResMgr, *m_pSpace, sf::Vector2f(float(i * 32), float(curLine * 32)) ));
					Emitters.back()->Show();
					break;
				}
			case('<'):
				{
					/*Emitters.push_back(new Emitter(m_ResMgr, *m_pSpace));
					Emitters.back()->SetPosition(i * 32, curLine * 32);
					m_Renderer.AddDrawableSprite(Emitters.back()->GetSprite());*/
					Block *block = new Block(m_ResMgr, *m_pSpace, Block::BLOCK_END, sf::Vector2f(float(i)*32,float(curLine)*32) );
					m_blocks.push_back(block);
					m_Renderer.AddDrawableSprite(block->GetSprite());
					catcherPositions.push_back(block->GetSprite()->sprite->getPosition());
					break;
				}
			}
			//
		}
		curLine++;
	}
	file.close();

	//enable user input to the player
	m_pPlayer->SetInputHandler(m_pInputHandler);

	//rebuild static level geometry
	//cpSpaceRehashStatic();
}