add HexTile class
This commit is contained in:
parent
f072eec36a
commit
19b3a79f33
@ -243,6 +243,8 @@ void Client::draw_map()
|
|||||||
for (int y = 0; y < height; y++)
|
for (int y = 0; y < height; y++)
|
||||||
{
|
{
|
||||||
for (int x = 0; x < width; x++)
|
for (int x = 0; x < width; x++)
|
||||||
|
{
|
||||||
|
if (m_map.tile_present(x, y))
|
||||||
{
|
{
|
||||||
glPushMatrix();
|
glPushMatrix();
|
||||||
glTranslatef(span_x * x, span_y * y, 0);
|
glTranslatef(span_x * x, span_y * y, 0);
|
||||||
@ -265,6 +267,7 @@ void Client::draw_map()
|
|||||||
glPopMatrix();
|
glPopMatrix();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
glPopMatrix();
|
glPopMatrix();
|
||||||
glPopAttrib();
|
glPopAttrib();
|
||||||
}
|
}
|
||||||
|
51
src/common/HexTile.cc
Normal file
51
src/common/HexTile.cc
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
|
||||||
|
#include "HexTile.h"
|
||||||
|
|
||||||
|
#define COS_60 0.5
|
||||||
|
#define SIN_60 0.8660254037844386
|
||||||
|
|
||||||
|
/* points of a horizontal hexagon 2.0 units high */
|
||||||
|
static const float hex_points[][2] = {
|
||||||
|
{1.1547005383792517, 0.0},
|
||||||
|
{0.577350269189626, 1.0},
|
||||||
|
{-0.5773502691896256, 1.0},
|
||||||
|
{-1.1547005383792517, 0.0},
|
||||||
|
{-0.5773502691896264, -1.0},
|
||||||
|
{0.577350269189626, -1.0}
|
||||||
|
};
|
||||||
|
|
||||||
|
HexTile::HexTile(float x, float y, float size)
|
||||||
|
{
|
||||||
|
m_x = x;
|
||||||
|
m_y = y;
|
||||||
|
m_size = size;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool HexTile::point_within(float x, float y)
|
||||||
|
{
|
||||||
|
x = 2.0 * (x - m_x) / m_size;
|
||||||
|
y = 2.0 * (y - m_y) / m_size;
|
||||||
|
/* a point (x, y) at angle q rotates to (x', y') by f deg around the origin
|
||||||
|
* according to:
|
||||||
|
* x' = r*cos(q+f) = r*cos(q)*cos(f)-r*sin(q)*sin(f)
|
||||||
|
* y' = r*sin(q+f) = r*sin(q)*cos(f)+r*cos(q)*sin(f)
|
||||||
|
* x' = x*cos(f) - y*sin(f)
|
||||||
|
* y' = y*cos(f) + x*sin(f)
|
||||||
|
* cos(60 deg) = 0.5
|
||||||
|
* sin(60 deg) = 0.8660254
|
||||||
|
* so:
|
||||||
|
* x' = x * 0.5 - y * 0.8660254
|
||||||
|
* y' = y * 0.5 + x * 0.8660254
|
||||||
|
*/
|
||||||
|
if ((y > 1.0) || (y < -1.0))
|
||||||
|
return false;
|
||||||
|
float rx = x * COS_60 - y * SIN_60;
|
||||||
|
float ry = y * COS_60 + x * SIN_60;
|
||||||
|
if ((ry > 1.0) || (ry < -1.0))
|
||||||
|
return false;
|
||||||
|
x = rx * COS_60 - ry * SIN_60;
|
||||||
|
y = ry * COS_60 + rx * SIN_60;
|
||||||
|
if ((y > 1.0) || (y < -1.0))
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
16
src/common/HexTile.h
Normal file
16
src/common/HexTile.h
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
|
||||||
|
#ifndef HEXTILE_H
|
||||||
|
#define HEXTILE_H
|
||||||
|
|
||||||
|
class HexTile
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
HexTile(float x, float y, float size);
|
||||||
|
bool point_within(float x, float y);
|
||||||
|
protected:
|
||||||
|
float m_x;
|
||||||
|
float m_y;
|
||||||
|
float m_size;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
@ -1,5 +1,6 @@
|
|||||||
|
|
||||||
#include "Map.h"
|
#include "Map.h"
|
||||||
|
#include "HexTile.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
@ -7,7 +8,13 @@ Map::Map(int width, int height)
|
|||||||
{
|
{
|
||||||
m_width = width;
|
m_width = width;
|
||||||
m_height = height;
|
m_height = height;
|
||||||
m_grid = vector< vector< bool > >(height, vector<bool>(width, true));
|
m_grid = vector< vector< bool > >(height, vector<bool>(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 j = 0; j < width; j++)
|
||||||
|
m_grid[i][j] = ht.point_within(j + 0.5, i + 0.5);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Map::tile_present(int x, int y)
|
bool Map::tile_present(int x, int y)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user