1 ///////////////////////////////////////////////////////////////////////////////
2 //            Copyright (C) 2004-2010 by The Allacrost Project
3 //                         All Rights Reserved
4 //
5 // This code is licensed under the GNU GPL version 2. It is free software
6 // and you may modify it and/or redistribute it under the terms of this license.
7 // See http://www.gnu.org/copyleft/gpl.html for details.
8 ///////////////////////////////////////////////////////////////////////////////
9 
10 /** ****************************************************************************
11 *** \file    map_tiles.h
12 *** \author  Tyler Olsen, roots@allacrost.org
13 *** \brief   Header file for map mode tile management.
14 ***
15 *** This code encapsulates everything related to tiles and tile management in
16 *** map mode.
17 *** ***************************************************************************/
18 
19 #ifndef __MAP_TILES_HEADER__
20 #define __MAP_TILES_HEADER__
21 
22 // Allacrost utilities
23 #include "defs.h"
24 #include "utils.h"
25 
26 // Local map mode headers
27 #include "map_utils.h"
28 
29 namespace hoa_map {
30 
31 namespace private_map {
32 
33 /** ****************************************************************************
34 *** \brief Represents a single image tile on the map.
35 ***
36 *** The images that a tile uses are not stored within this class. This class
37 *** only holds indices to the container class holding those images. This class
38 *** also does not contain any information about walkability or the collision grid.
39 *** That information is maintained in the map object manager.
40 ***
41 *** \note The reason that tiles do not contain walkability information is that
42 *** each tile is 32x32 pixels, but walkability is defined on a 16x16 granularity,
43 *** meaning that there are four "walkable" sections to each tile. Certain code
44 *** such as pathfinding is more simple if all walkability information is kept in
45 *** in another form of container.
46 *** ***************************************************************************/
47 class MapTile {
48 public:
49 	/** \name Tile Layer Indeces
50 	*** \brief Indeces to the tile image container, mapping the three tile layers.
51 	*** \note A negative value means that no image is registered to that tile layer.
52 	**/
53 	//@{
54 	int16 lower_layer, middle_layer, upper_layer;
55 	//@}
56 
MapTile()57 	MapTile()
58 		{ lower_layer = -1; middle_layer = -1; upper_layer = -1; }
59 
MapTile(int16 lower,int16 middle,int16 upper)60 	MapTile(int16 lower, int16 middle, int16 upper)
61 		{ lower_layer = lower; middle_layer = middle; upper_layer = upper; }
62 }; // class MapTile
63 
64 
65 /** ****************************************************************************
66 *** \brief A helper class to MapMode responsible for all tile data and operations
67 ***
68 *** This class is responsible for loading, updating, and drawing all tile images
69 *** and managing the tile grid. The TileSupervisor does <b>not</b> manage the map
70 *** collision grid, which is used by map objects and sprites.
71 ***
72 *** Maps have a minimum size of 24 rows and 32 columns of tiles. Theoretically
73 *** there is no upper limit on size.
74 *** ***************************************************************************/
75 class TileSupervisor {
76 	friend class hoa_map::MapMode;
77 
78 public:
79 	TileSupervisor();
80 
81 	~TileSupervisor();
82 
83 	/** \brief Handles all operations on loading tilesets and tile images from the map data file
84 	*** \param map_file A reference to the Lua file containing the map data
85 	*** \param map_instance A pointer to the MapMode object which invoked this function
86 	*** \note The map file should already be opened with no Lua tables open
87 	**/
88 	void Load(hoa_script::ReadScriptDescriptor& map_file, const MapMode* map_instance);
89 
90 	//! \brief Updates all animated tile images
91 	void Update();
92 
93 	/** \brief Draws the various tile layers to the screen
94 	*** \param frame A pointer to the computed information required to draw this frame
95 	***
96 	*** The implementation of these functions are nearly identical except for using
97 	*** a different layer index to reference the tile image and some minor
98 	*** differences in draw flags. We do not attempt to apply code reuse to these
99 	*** functions because we need them to be as fast as possible since they are
100 	*** each executed for every frame.
101 	***
102 	*** \note These functions do not reset the coordinate system and hence require
103 	*** that the proper coordinate system is already set prior to these function
104 	*** calls (0.0f, SCREEN_COLS, SCREEN_ROWS, 0.0f). These functions do make
105 	*** modifications to the blending draw flag and the draw cursor position
106 	*** which are not restored by the function upon its return, so take measures
107 	*** to retain this information before calling these functions if necessary.
108 	**/
109 	//@{
110 	void DrawLowerLayer(const MapFrame* const frame);
111 	void DrawMiddleLayer(const MapFrame* const frame);
112 	void DrawUpperLayer(const MapFrame* const frame);
113 	//@}
114 
115 private:
116 	/** \brief The number of rows of tiles in the map.
117 	*** This number must be greater than or equal to 24 for the map to be valid.
118 	**/
119 	uint16 _num_tile_rows;
120 
121 	/** \brief The number of columns of tiles in the map.
122 	*** This number must be greater than or equal to 32 for the map to be valid.
123 	**/
124 	uint16 _num_tile_cols;
125 
126 	/** \brief A map of 2D vectors that contains all of the map's tile objects.
127 	*** Each key-value pair in the std::map represents a map context, thus the size of the std::map is equal to
128 	*** number of contexts in the game map (up to 32). The 2D vector represents the rows and columns of tiles,
129 	*** respectively, for the given map context.
130 	**/
131 	std::map<MAP_CONTEXT, std::vector<std::vector<MapTile> > > _tile_grid;
132 
133 	//! \brief Contains the image objects for all map tiles, both still and animated.
134 	std::vector<hoa_video::ImageDescriptor*> _tile_images;
135 
136 	/** \brief Contains all of the animated tile images used on the map.
137 	*** The purpose of this vector is to easily update all tile animations without stepping through the
138 	*** _tile_images vector, which contains both still and animated images.
139 	**/
140 	std::vector<hoa_video::AnimatedImage*> _animated_tile_images;
141 }; // class TileSupervisor
142 
143 } // namespace private_map
144 
145 } // namespace hoa_map
146 
147 #endif // __MAP_TILES_HEADER__
148