-
Notifications
You must be signed in to change notification settings - Fork 1
/
Hexagon.h
156 lines (136 loc) · 3.23 KB
/
Hexagon.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#pragma once
// Created by ZK on 2014/5/23.
#include <algorithm>
#include <vector>
#include <iostream>
#include <cmath>
#include <float.h>
#include "cocos2d.h"
using namespace cocos2d;
using namespace std;
class HexCoordinate;
#define HexZero Hexagon()
#define HexC Hexagon::s_directions[Hexagon::C]
#define HexR Hexagon::s_directions[Hexagon::R]
#define HexRT Hexagon::s_directions[Hexagon::RT]
#define HexLT Hexagon::s_directions[Hexagon::LT]
#define HexL Hexagon::s_directions[Hexagon::L]
#define HexLB Hexagon::s_directions[Hexagon::LB]
#define HexRB Hexagon::s_directions[Hexagon::RB]
class Hexagon
{
public:
float q; //x in cub coordinate
float r; //z in cub coordinate
ccColor4F color;
#ifdef _DEBUG
CCLabelTTF * ttf;
bool _init;
#endif // DEBUG
enum eDirection
{
C,
R,
RT,
LT,
L,
LB,
RB,
COUNT
};
static Hexagon s_directions[eDirection::COUNT];
Hexagon (const Hexagon& h);
Hexagon (float q_ = 0, float r_ = 0, ccColor4F color = ccc4f (1, 1, 1, 1));
inline float y()const
{
return -q - r;
};
void Draw (HexCoordinate* coord_);
float Distance (const Hexagon& h) const;
Hexagon Towards (const Hexagon& h)const;
Hexagon Mirror (const Hexagon& mirror_hex)const;
Hexagon Round() const;
void Integerlize();
void Normalize();
Hexagon& Move (Hexagon::eDirection dir, int times = 1);
Hexagon& Color (const ccColor4F& color);
inline Hexagon operator- (const Hexagon& h_) const
{
Hexagon p;
p.q = q - h_.q;
p.r = r - h_.r;
return p;
}
inline Hexagon operator+ (const Hexagon& h_) const
{
Hexagon p;
p.q = q + h_.q;
p.r = r + h_.r;
return p;
}
inline Hexagon& operator+= (const Hexagon& h_)
{
q += h_.q;
r += h_.r;
return *this;
}
inline Hexagon& operator/= (const float f_)
{
assert (f_ != 0);
if (f_ != 0)
{
q /= f_;
r /= f_;
}
return *this;
}
inline Hexagon& operator*= (const float f_)
{
q *= f_;
r *= f_;
return *this;
}
inline Hexagon& operator-= (const Hexagon& h_)
{
q -= h_.q;
r -= h_.r;
return *this;
}
inline Hexagon& operator= (const Hexagon& h_)
{
q = h_.q;
r = h_.r;
color = h_.color;
return *this;
}
inline Hexagon operator* (float f_) const
{
Hexagon p;
p.q = q * f_;
p.r = r * f_;
return p;
}
inline Hexagon operator/ (float f_) const
{
assert (f_ != 0);
Hexagon p;
if (f_ != 0)
{
p.q = q / f_;
p.r = r / f_;
}
return p;
}
inline bool operator != (const Hexagon& h_) const
{
return ! (*this == h_);
}
inline bool operator == (const Hexagon& h_) const
{
return (abs (h_.q - this->q) < FLT_EPSILON) && (abs (h_.r - this->r) < FLT_EPSILON);
}
inline bool operator < (const Hexagon& h_) const
{
return this->q < h_.q || ((this->q == h_.q) && this->r < h_.r);
}
};