Map is an array of HexTiles

This commit is contained in:
Josh Holtrop 2012-09-11 23:13:33 -04:00
parent 3bcf26fc20
commit 013f0f30ab
4 changed files with 41 additions and 16 deletions

View File

@ -51,8 +51,8 @@ Client::Client(bool fullscreen, bool compatibility_context,
initgl(); initgl();
resize_window(m_window->getSize().x, m_window->getSize().y); resize_window(m_window->getSize().x, m_window->getSize().y);
m_player = new Player(); m_player = new Player();
m_player->x = 1250; m_player->x = 0;
m_player->y = 1000; m_player->y = 0;
m_player->direction = M_PI_2; m_player->direction = M_PI_2;
GLProgram::AttributeBinding obj_attrib_bindings[] = { GLProgram::AttributeBinding obj_attrib_bindings[] = {
{0, "pos"}, {0, "pos"},
@ -262,8 +262,8 @@ void Client::draw_map()
if (m_map.tile_present(x, y)) if (m_map.tile_present(x, y))
{ {
m_modelview.push(); m_modelview.push();
float cx = x * tile_size * HEX_WIDTH_TO_HEIGHT * 0.75; float cx = m_map.get_tile(x, y)->get_x();
float cy = (y + ((x & 1) ? 0.5 : 0.0)) * tile_size; float cy = m_map.get_tile(x, y)->get_y();
m_modelview.translate(cx, cy, 0); m_modelview.translate(cx, cy, 0);
m_modelview.to_uniform(uniform_locations[6]); m_modelview.to_uniform(uniform_locations[6]);
for (map<string, WFObj::Material>::iterator it = for (map<string, WFObj::Material>::iterator it =

View File

@ -9,6 +9,9 @@ class HexTile
public: public:
HexTile(float x, float y, float size); HexTile(float x, float y, float size);
bool point_within(float x, float y); bool point_within(float x, float y);
float get_x() { return m_x; }
float get_y() { return m_y; }
float get_size() { return m_size; }
protected: protected:
float m_x; float m_x;
float m_y; float m_y;

View File

@ -4,20 +4,38 @@
using namespace std; using namespace std;
Map::Map(int width, int height) Map::Map(int width, int height, float tile_size)
{ {
m_width = width; m_width = width;
m_height = height; m_height = height;
m_grid = vector< vector< bool > >(height, vector<bool>(width)); m_tile_size = tile_size;
/* construction of default map */
float span_x = HEX_WIDTH_TO_HEIGHT * 0.75 * tile_size;
float offset_x = -span_x * (width / 2.0);
float offset_y = -tile_size * (height / 2.0);
HexTile outer_out(0, 0, 0.85 * span_x * width);
HexTile outer_in(0, 0, 0.5 * span_x * width);
HexTile inner_out(0, 0, 0.30 * span_x * width);
/* construction of default map - temporary */
HexTile ht(width / 2.0, height / 2.0, 0.8 * height / 2.0);
for (int i = 0; i < height; i++) for (int i = 0; i < height; i++)
{
m_grid.push_back(vector< refptr< HexTile > >());
for (int j = 0; j < width; j++) for (int j = 0; j < width; j++)
m_grid[i][j] = ht.point_within(j + 0.5, i + 0.5); {
} refptr<HexTile> ht;
float x = j * span_x + offset_x;
float y = (i + ((j & 1) ? 0.5 : 0.0)) * tile_size + offset_y;
bool Map::tile_present(int x, int y) if (inner_out.point_within(y, x)
{ || (outer_out.point_within(y, x)
return m_grid[y][x]; && !outer_in.point_within(y, x)))
{
ht = new HexTile(x, y, tile_size);
}
m_grid[i].push_back(ht);
}
}
} }

View File

@ -3,18 +3,22 @@
#define MAP_H #define MAP_H
#include <vector> #include <vector>
#include "refptr.h"
#include "HexTile.h"
class Map class Map
{ {
public: public:
Map(int width=50, int height=50); Map(int width=21, int height=21, float tile_size=50);
bool tile_present(int x, int y); bool tile_present(int x, int y) { return !m_grid[y][x].isNull(); }
refptr<HexTile> get_tile(int x, int y) { return m_grid[y][x]; }
int get_width() { return m_width; } int get_width() { return m_width; }
int get_height() { return m_height; } int get_height() { return m_height; }
protected: protected:
int m_width; int m_width;
int m_height; int m_height;
std::vector< std::vector< bool > > m_grid; float m_tile_size;
std::vector< std::vector< refptr< HexTile > > > m_grid;
}; };
#endif #endif