This repository was archived by the owner on Oct 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrid.cpp
More file actions
168 lines (145 loc) · 5.11 KB
/
grid.cpp
File metadata and controls
168 lines (145 loc) · 5.11 KB
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
//
// Created by lepet on 8/4/2018.
//
#include <random>
#include <cassert>
#include <algorithm>
#include <objects/object.h>
#include <objects/tile.h>
#include <objects/ufo.h>
#include <objects/spaceship.h>
#include <objects/car.h>
#include <system/engine.h>
using namespace std;
grid::grid(const int columns, const int rows) : columns{columns}, rows{rows} {
const int size = columns * rows;
nodes.resize(size);
generate();
}
void grid::generate() {
assert(columns > 3);
assert(rows >= 5);
for (int i = 0; i < rows; ++i) {
switch (i % 5) {
case 0:
generate_park(i, NODE_UP);
break;
case 1:
generate_path(i, NODE_RIGHT);
break;
case 2:
generate_block(i);
break;
case 3:
generate_path(i, NODE_LEFT);
break;
case 4:
generate_park(i, NODE_DOWN);
break;
default:
break;
}
}
// Leave at least one empty seat for car.
nodes[1].type = NODE_EMPTY;
}
void grid::generate_park(int row, node_orientation orientation) {
for (int i = 0; i < columns; ++i) {
int n = i + columns * row;
nodes[n].offset = {i, 0, -row};
nodes[n].type = (i == 0 || i == columns - 1) ?
NODE_PATH : (node_type) (rand() % (NODE_END - NODE_EMPTY) + NODE_EMPTY);
nodes[n].orientation = (i == 0 || i == columns - 1) ? NODE_UP : orientation;
}
}
void grid::generate_path(int row, node_orientation orientation) {
for (int i = 0; i < columns; ++i) {
int n = i + columns * row;
nodes[n].offset = {i, 0, -row};
nodes[n].type = NODE_PATH;
nodes[n].orientation = (i == 0 || i == columns - 1) ? NODE_UP : orientation;
}
}
void grid::generate_block(int row) {
for (int i = 0; i < columns; ++i) {
int n = i + columns * row;
nodes[n].offset = {i, 0, -row};
nodes[n].type = (i == 0 || i == columns - 1) ? NODE_PATH : NODE_BLOCK;
}
}
void grid::create_scene() {
auto& engine = engine::get_instance();
// First pass, create tiles and ufos.
for (auto& node : nodes) {
// Create tiles.
engine.add_object(std::make_unique<tile>(node.type, node.orientation, glm::vec3(node.offset)));
switch (node.type) {
case NODE_UFO:
engine.add_object(std::make_unique<ufo>(glm::vec3(node.offset), rand() % 360, rand() % 360));
break;
case NODE_SPACESHIP:
engine.add_object(std::make_unique<spaceship>(glm::vec3(node.offset), rand() % 360));
break;
default:
break;
}
}
}
// Find the path from origin to an empty node, storing it into a vector.
void grid::find_path(const node& node) {
path_nodes.clear();
if (node.type == NODE_EMPTY) {
glm::vec<2, int> origin{0, 4};
glm::vec<2, int> target{node.offset[0], node.offset[2]};
int region = -target[1] / 5;
int entry = 5 * region;
int primary_path = entry + 1; // The row number of primary path.
int block = entry + 2;
int secondary_path = entry + 3;
path_nodes.push_back(origin);
path_nodes.push_back(glm::vec<2, int>{0, -entry});
path_nodes.push_back(glm::vec<2, int>{1, -primary_path});
if (node.orientation == NODE_UP) {
// This is the easy case.
path_nodes.push_back(target + glm::vec<2, int>{0, -1});
path_nodes.push_back(target + glm::vec<2, int>{1, -1});
path_nodes.push_back(target + glm::vec<2, int>{0, -1});
path_nodes.push_back(target);
} else {
// Otherwise we should go around.
path_nodes.push_back(glm::vec<2, int>{columns - 2, -primary_path});
path_nodes.push_back(glm::vec<2, int>{columns - 1, -block});
path_nodes.push_back(glm::vec<2, int>{columns - 2, -secondary_path});
path_nodes.push_back(target + glm::vec<2, int>{0, 1});
path_nodes.push_back(target + glm::vec<2, int>{-1, 1});
path_nodes.push_back(target + glm::vec<2, int>{0, 1});
path_nodes.push_back(target);
}
auto compare = [](glm::vec<2, int>& u, glm::vec<2, int>& v) {
auto ret = u - v;
return ret[0] == 0 && ret[1] == 0;
};
auto end = unique(path_nodes.begin(), path_nodes.end(), compare);
path_nodes.resize(distance(path_nodes.begin(), end));
}
}
void grid::find_empty() {
vector<node> empty;
for (auto& node : nodes)
if (node.type == NODE_EMPTY)
empty.push_back(node);
int n = rand() % empty.size();
find_path(empty[n]);
}
void grid::generate_car() {
vector<glm::vec3> points;
vector<glm::vec3> tangents;
for (auto& path_node : path_nodes) {
glm::vec3 point;
point[0] = path_node[0];
point[2] = path_node[1];
points.push_back(point);
}
auto& engine = engine::get_instance();
engine.add_object(std::make_unique<car>(points));
}