Refactoring.
This commit is contained in:
parent
692e9de011
commit
647ca6084c
6 changed files with 193 additions and 127 deletions
|
@ -8,18 +8,21 @@
|
|||
|
||||
#ifndef Point
|
||||
|
||||
struct Point {
|
||||
struct Point
|
||||
{
|
||||
double x;
|
||||
double y;
|
||||
|
||||
Point(double x_, double y_) : x(x_), y(y_) { }
|
||||
Point(double x_, double y_) : x(x_), y(y_)
|
||||
{ }
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
#ifndef Orientation
|
||||
|
||||
struct Orientation {
|
||||
struct Orientation
|
||||
{
|
||||
// cubic to point
|
||||
const double f0, f1, f2, f3;
|
||||
// point to cubic
|
||||
|
@ -30,7 +33,8 @@ struct Orientation {
|
|||
Orientation(double f0_, double f1_, double f2_, double f3_, double b0_, double b1_, double b2_, double b3_,
|
||||
double start_angle_)
|
||||
: f0(f0_), f1(f1_), f2(f2_), f3(f3_), b0(b0_), b1(b1_), b2(b2_), b3(b3_),
|
||||
start_angle(start_angle_) { }
|
||||
start_angle(start_angle_)
|
||||
{ }
|
||||
};
|
||||
|
||||
const Orientation pointy_orientation = Orientation(sqrt(3.0), sqrt(3.0) / 2.0, 0.0, 3.0 / 2.0,
|
||||
|
@ -43,23 +47,27 @@ const Orientation flat_orientation = Orientation(3.0 / 2.0, 0.0, sqrt(3.0) / 2.0
|
|||
|
||||
#ifndef Layout
|
||||
|
||||
struct Layout {
|
||||
struct Layout
|
||||
{
|
||||
const Orientation orientation;
|
||||
const double size;
|
||||
const SDL_Point origin;
|
||||
|
||||
Layout(Orientation orientation_, double size_, SDL_Point origin_)
|
||||
: orientation(orientation_), size(size_), origin(origin_) { }
|
||||
: orientation(orientation_), size(size_), origin(origin_)
|
||||
{ }
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
#ifndef Field
|
||||
|
||||
struct Field {
|
||||
struct Field
|
||||
{
|
||||
const Sint16 x, y, z;
|
||||
|
||||
Field(Sint16 x_, Sint16 y_, Sint16 z_) : x(x_), y(y_), z(z_) {
|
||||
Field(Sint16 x_, Sint16 y_, Sint16 z_) : x(x_), y(y_), z(z_)
|
||||
{
|
||||
assert(x + y + z == 0);
|
||||
}
|
||||
};
|
||||
|
@ -69,10 +77,13 @@ const std::vector<Field> hex_directions = {
|
|||
Field(1, 0, -1), Field(0, 1, -1), Field(-1, 1, 0), Field(-1, 0, 1), Field(-1, 1, 0)
|
||||
};
|
||||
|
||||
namespace std {
|
||||
namespace std
|
||||
{
|
||||
template<>
|
||||
struct hash<Field> {
|
||||
size_t operator()(const Field &f) const {
|
||||
struct hash<Field>
|
||||
{
|
||||
size_t operator()(const Field &f) const
|
||||
{
|
||||
hash<Sint16> int_hash;
|
||||
size_t hx = int_hash(f.x);
|
||||
size_t hy = int_hash(f.y);
|
||||
|
@ -99,35 +110,43 @@ Field operator*(Field left, Field right);
|
|||
|
||||
Field operator/(Field left, Field right);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
Point field_to_point(const Field *f, const Layout *layout);
|
||||
Point field_to_point(const Field f, const Layout layout);
|
||||
|
||||
Field point_to_field(Point point, const Layout *layout);
|
||||
Field point_to_field(Point point, const Layout layout);
|
||||
|
||||
Point field_corner_offset(Uint8 corner, const Layout *layout);
|
||||
Point field_corner_offset(Uint8 corner, const Layout layout);
|
||||
|
||||
std::vector<Point> field_to_polygon(const Field *field, const Layout *layout);
|
||||
std::vector<Point> field_to_polygon(const Field field, const Layout layout);
|
||||
|
||||
#ifndef Grid
|
||||
|
||||
class Grid {
|
||||
class Grid
|
||||
{
|
||||
protected:
|
||||
std::unordered_set<Field> fields;
|
||||
SDL_Color color;
|
||||
Layout layout;
|
||||
public:
|
||||
Grid();
|
||||
Grid(Layout layout_, SDL_Color color_) : layout(layout_), color(color_)
|
||||
{
|
||||
this->fields = std::unordered_set<Field>();
|
||||
};
|
||||
|
||||
virtual bool render(SDL_Renderer *renderer) = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
#ifndef HexagonGrid
|
||||
|
||||
class HexagonGrid : public Grid {
|
||||
class HexagonGrid : public Grid
|
||||
{
|
||||
public:
|
||||
HexagonGrid(Sint16 grid_radius);
|
||||
HexagonGrid(Sint16 grid_radius, Layout layout, SDL_Color color);
|
||||
|
||||
bool render(SDL_Renderer *renderer, const SDL_Color color, const Layout *layout);
|
||||
bool render(SDL_Renderer *renderer);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue