📄 tilemap.h
字号:
#ifndef TILEMAP_H
#define TILEMAP_H
#include "Map.h"
#include "SDLHelpers.h"
const int DIRECTIONTABLE[4][2] = { { 0, -1 },
{ 1, 0 },
{ 0, 1 },
{ -1, 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;
};
// ============================================================================
// 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 );
// open the file
FILE* f = fopen( p_filename, "rb" );
// file cant be opened, return
if( f == 0 )
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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -