#include <chunk.h>
Public Member Functions | |
Chunk () | |
Chunk (MapTile tile_type, int _world_row, int _world_col) | |
void | init (MapTile tile_type, int _world_row, int _world_col) |
bool | build_chunk_with_dungeons () |
void | build_land_chunk () |
void | build_forest_chunk () |
void | build_water_chunk () |
void | build_beach_chunk () |
IntPoint | get_world_loc () const |
IntPoint | get_up_stair (int depth) const |
IntPoint | get_down_stair (int depth) const |
std::vector< Item * > & | get_items (int depth) |
void | remove_item (Item *item, int depth) |
void | add_item (Item *item, int depth) |
const std::vector< std::vector < Tile > > & | get_floor (int depth) |
Tile * | get_tile (int depth, int row, int col) |
void | set_tile (int depth, int row, int col, Tile tile) |
int | get_depth () const |
bool | out_of_bounds (int depth, int row, int col) const |
Spawner | get_spawner (int depth) |
void | dungeon_dump (int depth) |
MapTile | get_type () |
void | serialize () |
Private Types | |
typedef std::vector < std::vector< Tile > > | TileMatrix |
Private Member Functions | |
bool | find_serialized_chunk () |
int | calculate_file_size (int bytes_per_tile) |
void | deserialize (string file_name) |
Private Attributes | |
chunk_meta | cm |
MapTile | chunk_type |
std::vector< ChunkLayer > | layers |
The Chunk class is the largest data model in the game.
Each Chunk contains a vector of dungeons and an overworld. Chunks are in charge of their own loading method: When a new chunk is constructed or its init() method is called (the first implies the second), it will look in the chunk data folder to see if the chunk at its coordinates has previously been serialized. If not, the chunk controls its own creation, and if so, the chunk calls its deserialize() method, loading it with the appropriate data.
Chunk::Chunk | ( | ) |
Empty constructor.
Chunk::Chunk | ( | MapTile | tile_type, |
int | _world_row, | ||
int | _world_col | ||
) |
Typical use case constructor. Will handle file deserialization by itself: When provided with a location, the function will decide whether or not it should deserialize an offloaded chunk.
tile_type | - the type of the map tile on which this chunk lies. |
_world_col | - The row of this chunk's location on the world map. |
_world_col | - The column of this chunk's location on the world map. |
void Chunk::add_item | ( | Item * | item, |
int | depth | ||
) |
Will add the given item to the chunk's item list.
item | - a pointer to the item to add |
depth | - the depth at which to add the item. |
void Chunk::build_beach_chunk | ( | ) |
Builds a chunk with only a sandy overworld.
bool Chunk::build_chunk_with_dungeons | ( | ) |
void Chunk::build_forest_chunk | ( | ) |
Builds a chunk with grass, trees, groves, and dungeons.
void Chunk::build_land_chunk | ( | ) |
Builds a chunk with dirt, trees, and dungeons. Behaves as the default overworld.
void Chunk::build_water_chunk | ( | ) |
Builds a chunk with only a watery overworld.
|
private |
Determines the size of the file that will be created for this chunk.
|
private |
Loads this chunk's information from a file. Essentially does a simple reverse of the serialization process. The "header" bytes, which always must exist in the file, are processed one-by-one and loaded into the class member variables to restore the data. Then, data which may or may not exist (such as stair/spawner locations) is loaded based on header information. Next, the bytes of the dungeon and overworld are loaded in a triple-nested for-loop.
file_name | - The name of the file from which the chunk will be loaded. |
world_row | - This chunk's row on the world map. |
world_col | - This chunk's column on the world map. |
TODO still differentiating between overworld and dungeons here.
void Chunk::dungeon_dump | ( | int | depth | ) |
Prints a graphical representation of the given layer to stdout.
depth | - the depth of the layer to print. |
|
private |
The entry point for deserialization. Will attempt to find an offloaded chunk with the given coordinates. If an offloaded chunk is found in the filesystem, calls the deserialize() function and returns true. Otherwise, returns false.
row | - The world row of the chunk to find. |
col | - The world column of the chunk to find. |
int Chunk::get_depth | ( | ) | const |
Determine how many layers are beneath the overworld in this chunk.
IntPoint Chunk::get_down_stair | ( | int | depth | ) | const |
Returns the location of the down stair in the chunk as an IntPoint.
depth | - the depth of this chunk. |
const std::vector< std::vector< Tile > > & Chunk::get_floor | ( | int | depth | ) |
depth | - the depth to get (-1 is the overworld) |
vector< Item * > & Chunk::get_items | ( | int | depth | ) |
Returns a vector of all the items on the ground in this chunk.
depth | - the depth of this chunk. |
Spawner Chunk::get_spawner | ( | int | depth | ) |
Returns the spawner object at the given depth.
depth |
Tile * Chunk::get_tile | ( | int | depth, |
int | row, | ||
int | col | ||
) |
depth | |
row | |
col |
MapTile Chunk::get_type | ( | ) |
IntPoint Chunk::get_up_stair | ( | int | depth | ) | const |
Returns the location of the up stair in the chunk as an IntPoint.
depth | - the depth of this chunk. |
IntPoint Chunk::get_world_loc | ( | ) | const |
void Chunk::init | ( | MapTile | tile_type, |
int | _world_row, | ||
int | _world_col | ||
) |
Resets all important information in the chunk, deserializing as needed.
tile_type | - The type of the map tile on which this chunk lies. |
_world_row | - The row of this chunk's location on the world map. |
_world_col | - The column of this chunk's location on the world map. |
bool Chunk::out_of_bounds | ( | int | depth, |
int | row, | ||
int | col | ||
) | const |
Given a point, will determine whether the point is out of bounds.
depth | |
row | |
col |
void Chunk::remove_item | ( | Item * | item, |
int | depth | ||
) |
Will remove the given item from the chunk.
item | - a pointer to the item to remove |
depth | - the depth at which to remove the item. |
void Chunk::serialize | ( | ) |
Saves all of the important information for this chunk in a file. The file size is first carefully calculated, then a char array is created with that size and populated with the dungeon data. The file is saved in data/chunk.
Some bitwise arithmetic is used to pack ints and bools together in a single byte. As we get over 128 tile IDs, we will have to change this functionality.
if((cm.depth < 0) || (cm.depth > 10)) { cout<<"CHUNK DEPTH: "<<cm.depth<<endl; }
void Chunk::set_tile | ( | int | depth, |
int | row, | ||
int | col, | ||
Tile | tile | ||
) |
Sets a tile at the given position to the given tile type. If the depth is -1, the tile will be set at the overworld.
depth | - the depth of this chunk. |
row | - the row of the tile to set. |
col | - the column of the tile to set. |
tile | - the tile type. |
|
private |
A vector containing the layers of this chunk. Generally this is the main data chokepoint in the Chunk class.