/
SquareGrid.h
174 lines (141 loc) · 3.71 KB
/
SquareGrid.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#pragma once
#include "Types.h"
namespace ProceduralGeneration
{
struct Node
{
Vector2 position;
int vertexIndex;
Node() {
position = Vector2::Zero();
vertexIndex = -1;
}
Node(Vector2 pos)
{
position = pos;
vertexIndex = -1;
}
};
struct ControlNode : Node
{
bool active;
Node above, right; // The positions above and to the right of the control node
ControlNode()
{
active = false;
//above = nullptr;
//right = nullptr;
}
ControlNode(Vector2 pos, bool active, float squareSize) : Node(pos), active(active)
{
Vector2 forwardVector = Vector2::Up() * ((float)squareSize / 2.0f);
Vector2 rightVector = Vector2::Right() * ((float)squareSize / 2.0f);
// set the position to the distance above
above = Node(position + forwardVector);
// set the position to the distance to the right
right = Node(position + rightVector);
}
~ControlNode()
{
//delete above;
//delete right;
}
};
struct Square
{
ControlNode topLeft, topRight, bottomRight, bottomLeft;
Node centreTop, centreRight, centreBottom, centreLeft;
short configuration; // value is between 0000 and 1111
vector<Node*> nodesUsed;
Square()
{
//topLeft = nullptr;
//topRight = nullptr;
//bottomLeft = nullptr;
//bottomRight = nullptr;
//centreTop = nullptr;
//centreRight = nullptr;
//centreBottom = nullptr;
//centreLeft = nullptr;
configuration = 0000;
}
Square(ControlNode topLeft, ControlNode topRight, ControlNode bottomRight, ControlNode bottomLeft)
{
// set the cornor nodes
this->topLeft = topLeft;
this->topRight = topRight;
this->bottomRight = bottomRight;
this->bottomLeft = bottomLeft;
// set center nodes to the nodes connected to the corner nodes
centreTop = topLeft.right;
centreRight = bottomRight.above;
centreBottom = bottomLeft.right;
centreLeft = bottomLeft.above;
configuration = 0;
// turns on the appropriate bit in the four bit number base on
// which control nodes are active
if (topLeft.active)
configuration += 8; // turns on the first bit
if (topRight.active)
configuration += 4; // turns on the second bit
if (bottomRight.active)
configuration += 2; // turns on the thrid bit
if (bottomLeft.active)
configuration += 1; // turns on the fourth bit
}
void Draw()
{
//bool controlNodes[4];
//char characters[4];
//controlNodes[0] = topLeft.active;
//controlNodes[1] = topRight.active;
//controlNodes[2] = bottomLeft.active;
//controlNodes[3] = bottomRight.active;
if (topLeft.active)
cout<< '+';
else
cout << 'o';
cout << "--.--";
if (topRight.active)
cout << '+';
else
cout << 'o';
cout << endl;
cout << "| |" << endl
<< ". ." << endl
<< "| |" << endl;
if (bottomLeft.active)
cout << '+';
else
cout << 'o';
cout << "--.--";
if (bottomRight.active)
cout << '+';
else
cout << 'o';
}
};
/// <summary>
/// Storse a 2D array of squares
/// </summary>
class SquareGrid
{
Array2D<Square>* squares;
int m_squareCountX, m_squareCountY;
public:
/// <summary>
/// Fills the 2D array of squares using parameters
/// </summary>
/// <param name="map">2D array of integers to be passed in from the map generator</param>
/// <param name="squareSize">The scale of the squares inside the grid</param>
SquareGrid(Array2D<int>* map, float squareSize);
void TriangulateSquare(Square& square);
void PrintConfigurations();
void PrintConfigurationsWithGrid();
Array2D<Square>* GetSquares() { return squares; }
~SquareGrid()
{
delete squares;
}
};
}