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

📄 tilemap.h

📁 游戏开发数据结构Data Structures for Game Programmers
💻 H
📖 第 1 页 / 共 2 页
字号:
#ifndef TILEMAP_H
#define TILEMAP_H

#include "Map.h"
#include "SDLHelpers.h"
#include "Heap.h"


const int DIRECTIONTABLE[4][2] = { { 0, -1 }, 
                                   { 1, 0 }, 
                                   { 0, 1 }, 
                                   { -1, 0 } };

// ============================================================================
//  New Pathfinding Information
// ============================================================================
class Coordinate
{
public:
    int x;
    int y;
    float heuristic;
};

int CompareCoordinates( Coordinate left, Coordinate right )
{
    if( left.heuristic < right.heuristic )
        return 1;
    if( left.heuristic > right.heuristic )
        return -1;
    return 0;
}



// ============================================================================
//  TileCell Class
// ============================================================================
class TileCell
{
public:
// =======================================================
//  Functions
// =======================================================

// -------------------------------------------------------
// Name:        TileCell
// Description: constructs the cell
// -------------------------------------------------------
    TileCell()
    {
        m_blocked = false;
        m_item = 0;
        m_person = 0;
    }



// =======================================================
//  Data
// =======================================================

// -------------------------------------------------------
// Name:        m_blocked
// Description: determines if the cell is blocked by
//              a feature of the geography, but not
//              by items or people.
// -------------------------------------------------------
    bool m_blocked;

// -------------------------------------------------------
// Name:        m_item
// Description: The item in the cell
// -------------------------------------------------------
    Item* m_item;

// -------------------------------------------------------
// Name:        m_person
// Description: The person in the cell
// -------------------------------------------------------
    Person* m_person;


// =======================================================
//  Pathfinding Data
// =======================================================

// -------------------------------------------------------
// Name:        m_marked
// Description: determines if the cell is marked
// -------------------------------------------------------
    bool m_marked;

// -------------------------------------------------------
// Name:        m_distance
// Description: The distance from this cell to the 
//              starting cell
// -------------------------------------------------------
    float m_distance;

// -------------------------------------------------------
// Name:        m_lastx, m_lasty
// Description: The coordinates of the last cell in the
//              path
// -------------------------------------------------------
    int m_lastx;
    int m_lasty;

};




// ============================================================================
//  TileCell Class
// ============================================================================
class TileMap : public Map
{

protected:
// =======================================================
//  Data
// =======================================================

// -------------------------------------------------------
// Name:        m_tiles
// Description: a 3d array, representing the tilemap
//              graphics
// -------------------------------------------------------
    Array3D<int> m_tiles;

// -------------------------------------------------------
// Name:        m_tilemap
// Description: a 2d array, representing the tilemap
//              cells
// -------------------------------------------------------
    Array2D<TileCell> m_tilemap;

// -------------------------------------------------------
// Name:        m_tilebmps
// Description: an array of bitmaps that the tiles will
//              be drawn from
// -------------------------------------------------------
    SDL_Surface** m_tilebmps;


// =======================================================
//  New Functions
// =======================================================
public:
// -------------------------------------------------------
// Name:        TileMap
// Description: Constructor, constructs the map with the
//              given size, and a filename. If the
//              filename is 0, then empty map is created
// -------------------------------------------------------
    TileMap( int p_x, int p_y, int p_z, SDL_Surface** p_tilebmps )
             : m_tiles( p_x, p_y, p_z ), m_tilemap( p_x, p_y )
    {
        m_tilebmps = p_tilebmps;
    }


// -------------------------------------------------------
// Name:        ~TileMap
// Description: destructor, deletes everything on the map
// -------------------------------------------------------
    ~TileMap()
    {
        int x, y;

        // go through all of the tiles and delete the items
        // and persons.
        for( y = 0; y < m_tilemap.Width(); y++ )
        {
            for( x = 0; x < m_tilemap.Height(); x++ )
            {
                if( m_tilemap.Get( x, y ).m_item != 0 )
                    delete m_tilemap.Get( x, y ).m_item;
                if( m_tilemap.Get( x, y ).m_person != 0 )
                    delete m_tilemap.Get( x, y ).m_person;
                m_tilemap.Get( x, y ).m_item = 0;
                m_tilemap.Get( x, y ).m_person = 0;
            }
        }  
    }

// -------------------------------------------------------
// Name:        SetTile
// Description: Set the tile at the index
// -------------------------------------------------------
    void SetTile( int p_x, int p_y, int p_z, int p_tile )
    {
        m_tiles.Get( p_x, p_y, p_z ) = p_tile;
    }
    

// -------------------------------------------------------
// Name:        Getcell
// Description: Given an X and Y coordinate, calculate
//              the cell number.
// -------------------------------------------------------
    int GetCell( int p_x, int p_y )
    {
        return p_y * m_tiles.Width() + p_x;
    }

// -------------------------------------------------------
// Name:        LoadFromFile
// Description: Loads the map from a file
// -------------------------------------------------------
    void LoadFromFile( char* p_filename )
    {
        int x, y;
        int item;
        int person;
        Array2D<int> items( 64, 64 );
        Array2D<int> people( 64, 64 );
        int maptype;

        // open the file
        FILE* f = fopen( p_filename, "rb" );

        // file cant be opened, return
        if( f == 0 )
            return;

        // read in the map type.
        fread( &maptype, 1, sizeof(int), f );

        // quit out if type isn't 0
        if( maptype != 0 )
        {
            fclose( f );
            return;
        }
        
        // read in the tile data
        fread( m_tiles.m_array, 64 * 64 * 2, sizeof(int), f );

        // read the items and the people into the temporary arrays
        fread( items.m_array, 64 * 64, sizeof(int), f );
        fread( people.m_array, 64 * 64, sizeof(int), f );

        // read the exits in
        fread( m_exits[0], 64, sizeof(char), f );
        fread( m_exits[1], 64, sizeof(char), f );
        fread( m_exits[2], 64, sizeof(char), f );

        // close the file
        fclose( f );

        // loop through each cell, converting the items and people.
        for( y = 0; y < 64; y++ )
        {
            for( x = 0; x < 64; x++ )
            {
                // get item type
                item = items.Get( x, y );

                // if there is an item at that cell, convert it.
                if( item != -1 )
                {
                    m_tilemap.Get( x, y ).m_item = 
                        MakeItem( item, x, y, GetCell( x, y ) );

                }

                // get the person type
                person = people.Get( x, y );

                // item is a person
                if( person != -1 )
                {
                    m_tilemap.Get( x, y ).m_person = 
                        MakePerson( person, x, y, GetCell( x, y ) );

                    // set the viewer if the person is type 0, the 
                    // default player
                    if( person == 0 )
                    {
                        SetViewer( m_tilemap.Get( x, y ).m_person );
                    }
                }
            }
        }
    }

// =======================================================
//  New Functions
// =======================================================

// -------------------------------------------------------
// Name:        Draw
// Description: Draws the map onto the given surface, 
//              using the viewers coordinates as the
//              midpoint of the screen.
// -------------------------------------------------------
    void Draw( SDL_Surface* p_surface, int p_midx, int p_midy )
    {
        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 )
    {
        return CanMove( p_person->GetX(), p_person->GetY(), p_direction );
    }


    bool CanMove( int p_x, int p_y, int p_direction )

⌨️ 快捷键说明

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