add HexTile class
This commit is contained in:
parent
f072eec36a
commit
19b3a79f33
@ -244,25 +244,28 @@ void Client::draw_map()
|
||||
{
|
||||
for (int x = 0; x < width; x++)
|
||||
{
|
||||
glPushMatrix();
|
||||
glTranslatef(span_x * x, span_y * y, 0);
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
||||
glBegin(GL_QUADS);
|
||||
glColor3f(0.4, 0.4, 0.4);
|
||||
glVertex2f(span_x, span_y);
|
||||
glVertex2f(0, span_y);
|
||||
glVertex2f(0, 0);
|
||||
glVertex2f(span_x, 0);
|
||||
glEnd();
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||
glBegin(GL_QUADS);
|
||||
glColor3f(1, 1, 1);
|
||||
glVertex2f(span_x, span_y);
|
||||
glVertex2f(0, span_y);
|
||||
glVertex2f(0, 0);
|
||||
glVertex2f(span_x, 0);
|
||||
glEnd();
|
||||
glPopMatrix();
|
||||
if (m_map.tile_present(x, y))
|
||||
{
|
||||
glPushMatrix();
|
||||
glTranslatef(span_x * x, span_y * y, 0);
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
||||
glBegin(GL_QUADS);
|
||||
glColor3f(0.4, 0.4, 0.4);
|
||||
glVertex2f(span_x, span_y);
|
||||
glVertex2f(0, span_y);
|
||||
glVertex2f(0, 0);
|
||||
glVertex2f(span_x, 0);
|
||||
glEnd();
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||
glBegin(GL_QUADS);
|
||||
glColor3f(1, 1, 1);
|
||||
glVertex2f(span_x, span_y);
|
||||
glVertex2f(0, span_y);
|
||||
glVertex2f(0, 0);
|
||||
glVertex2f(span_x, 0);
|
||||
glEnd();
|
||||
glPopMatrix();
|
||||
}
|
||||
}
|
||||
}
|
||||
glPopMatrix();
|
||||
|
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 "HexTile.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
@ -7,7 +8,13 @@ Map::Map(int width, int height)
|
||||
{
|
||||
m_width = width;
|
||||
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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user