⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tilemap.h

📁 游戏开发数据结构Data Structures for Game Programmers
💻 H
📖 第 1 页 / 共 2 页
字号:
    {
        int x, y, z;            // counting variables
        int px, py;             // pixel coordinates
        int ox, oy;             // offset coordinates
        int current;
        Item* i;
        Person* p;

        // these are the drawing boundaries
        // figure out the maximum number of tiles to draw in each
        // direction, based on the width/height of the screen.
        int minx = m_viewer->GetX() - (p_midx / 64) - 1;
        int maxx = m_viewer->GetX() + (p_midx / 64) + 1;
        int miny = m_viewer->GetY() - (p_midy / 64) - 1;
        int maxy = m_viewer->GetY() + (p_midy / 64) + 1;

        // make sure the coordinates are in bounds.
        if( minx < 0 )                  { minx = 0; }
        if( maxx >= m_tiles.Width() )   { maxx = m_tiles.Width() - 1; }
        if( miny < 0 )                  { miny = 0; }
        if( maxy >= m_tiles.Height() )  { maxy = m_tiles.Height() - 1; }
        

        // calculate the offsetsfrom the viewer
        ox = (-m_viewer->GetX() * 64) + p_midx - 32;
        oy = (-m_viewer->GetY() * 64) + p_midy - 32;

        for( y = miny; y <= maxy; y++ )
        {
            for( x = minx; x <= maxx; x++ )
            {
                // calculate the coordinates of the current tile
                px = x * 64 + ox;
                py = y * 64 + oy;

                for( z = 0; z < m_tiles.Depth(); z++ )
                {
                    current = m_tiles.Get( x, y, z );
                    if( current != -1 )
                    {
                        // draw the tile
                        SDLBlit( m_tilebmps[current], p_surface, px, py );
                    }
                }

                i = m_tilemap.Get( x, y ).m_item;
                p = m_tilemap.Get( x, y ).m_person;
                if( i != 0 )
                    SDLBlit( i->GetGraphic(), p_surface, px, py );
                if( p != 0 )
                    SDLBlit( p->GetGraphic(), p_surface, px, py );
            }
        }
    }

// -------------------------------------------------------
// Name:        CanMove
// Description: Determines if the given person can move
//              in the given direction
// -------------------------------------------------------
    bool CanMove( Person* p_person, int p_direction )
    {
        // get the new coordinates
        int newx = p_person->GetX() + DIRECTIONTABLE[p_direction][0]; 
        int newy = p_person->GetY() + DIRECTIONTABLE[p_direction][1];

        // check to see if the cell is in bounds.
        if( newx < 0 || newx >= m_tiles.Width() ||
            newy < 0 || newy >= m_tiles.Height() )
            return false;

        // check to see if the path is blocked or not.
        if( m_tilemap.Get( newx, newy ).m_blocked == true )
            return false;

        // check to see if there is a person blocking the way
        if( m_tilemap.Get( newx, newy ).m_person != 0 )
            return false;

        // check to see if there in an item blocking the way
        if( m_tilemap.Get( newx, newy ).m_item != 0 )
        {
            // some items can block, others can't
            if( m_tilemap.Get( newx, newy ).m_item->CanBlock() == true )
                return false;
        }

        return true;
    }


// -------------------------------------------------------
// Name:        Move
// Description: Moves the given person in the given
//              direction
// -------------------------------------------------------
    void Move( Person* p_person, int p_direction )
    {
        // get the new coordinates
        int newx = p_person->GetX() + DIRECTIONTABLE[p_direction][0]; 
        int newy = p_person->GetY() + DIRECTIONTABLE[p_direction][1];

        // make sure the player can move
        if( CanMove( p_person, p_direction ) == true )
        {
            // move the person to the new cell
            m_tilemap.Get( newx, newy ).m_person = p_person;

            // clear the person pointer in the old cell
            m_tilemap.Get( p_person->GetX(), p_person->GetY() ).m_person = 0;

            // set the new x, y, and cell variables
            p_person->SetX( newx );
            p_person->SetY( newy );
            p_person->SetCell( GetCell( newx, newy ) );
        }
    }


// -------------------------------------------------------
// Name:        GetItem
// Description: Gets a pointer to the item in the given
//              cell
// -------------------------------------------------------
    Item* GetItem( int p_cell )
    {
        // check to see if the cell is in bounds first
        if( p_cell >= GetNumberOfCells() || p_cell < 0 )
            return 0;

        return m_tilemap.m_array[p_cell].m_item;
    }


// -------------------------------------------------------
// Name:        SetItem
// Description: Sets the item in the given cell.
// -------------------------------------------------------
    void SetItem( int p_cell, Item* p_item )
    {
        // check to see if the cell is in bounds first
        if( p_cell >= GetNumberOfCells() || p_cell < 0 )
            return;

        m_tilemap.m_array[p_cell].m_item = p_item;
    }

    
// -------------------------------------------------------
// Name:        GetPerson
// Description: Gets a pointer to the person in the given
//              cell
// -------------------------------------------------------
    Person* GetPerson( int p_cell )
    {
        // check to see if the cell is in bounds first
        if( p_cell >= GetNumberOfCells() || p_cell < 0 )
            return 0;

        return m_tilemap.m_array[p_cell].m_person;
    }


// -------------------------------------------------------
// Name:        SetPerson
// Description: sets the person in the given cell.
// -------------------------------------------------------
    void SetPerson( int p_cell, Person* p_person )
    {
        // check to see if the cell is in bounds first
        if( p_cell >= GetNumberOfCells() || p_cell < 0 )
            return;

        m_tilemap.m_array[p_cell].m_person = p_person;
    }


// -------------------------------------------------------
// Name:        GetCellNumber
// Description: Gets the number of the cell in the
//              given direction
// -------------------------------------------------------
    int GetCellNumber( int p_cell, int p_direction )
    {
        int x, y;
    
        // calculate the x and y coords of the current cell first
        y = p_cell / m_tiles.Width();
        x = p_cell - (y * m_tiles.Width());
    
        // now calculate the new coordinates
        x = x + DIRECTIONTABLE[p_direction][0];
        y = y + DIRECTIONTABLE[p_direction][1];

        // check to see if the cell is out of bounds
        if( x < 0 || x >= m_tiles.Width() ||
            y < 0 || y >= m_tiles.Height() )
        {
            return -1;
        }

        // return the cell number
        return GetCell( x, y );
    }

    
// -------------------------------------------------------
// Name:        GetNumberOfCells
// Description: Gets the number of cells in the map
// -------------------------------------------------------
    int GetNumberOfCells()
    {
        return m_tilemap.Size();
    }


// -------------------------------------------------------
// Name:        GetClosestDirection
// Description: Gets the direction that will move the
//              first person closer to the second person.
// -------------------------------------------------------
    int GetClosestDirection( Person* p_one, Person* p_two )
    {
        int direction = -1;

        // if person 1 is above person 2, then go north
        if( p_one->GetY() > p_two->GetY() )
            direction = 0;
        // if person 1 is left of person 2, then go east
        else if( p_one->GetX() < p_two->GetX() )
            direction = 1;
        // if person 1 is below person 2, then go south
        else if( p_one->GetY() < p_two->GetY() )
            direction = 2;
        // if person 1 is right of person 2, then go west
        else if( p_one->GetX() > p_two->GetX() )
            direction = 3;

        return direction;
    }

};







#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -