-
Notifications
You must be signed in to change notification settings - Fork 0
/
levelmaker.cpp
156 lines (147 loc) · 5.34 KB
/
levelmaker.cpp
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
#include "include/sista/sista.hpp"
#include <iostream>
#include <fstream>
#ifdef _WIN32
#include <conio.h>
#elif __linux__
#include <unistd.h>
#include <termios.h>
char getch(void) {
char buf = 0;
struct termios old = {0};
fflush(stdout);
if(tcgetattr(0, &old) < 0)
perror("tcsetattr()");
old.c_lflag &= ~ICANON;
old.c_lflag &= ~ECHO;
old.c_cc[VMIN] = 1;
old.c_cc[VTIME] = 0;
if(tcsetattr(0, TCSANOW, &old) < 0)
perror("tcsetattr ICANON");
if(read(0, &buf, 1) < 0)
perror("read()");
old.c_lflag |= ICANON;
old.c_lflag |= ECHO;
if(tcsetattr(0, TCSADRAIN, &old) < 0)
perror("tcsetattr ~ICANON");
// printf("%c\n", buf);
return buf;
}
#endif
#define WIDTH 10
#define HEIGHT 20
ANSI::Settings builder_style(
ANSI::ForegroundColor::F_MAGENTA,
ANSI::BackgroundColor::B_BLACK,
ANSI::Attribute::REVERSE
);
sista::Coordinates up(-1, 0), down(1, 0), left(0, -1), right(0, 1);
int main(int argc, char* argv[]) {
if (argc != 2) {
std::cout << "Usage: " << argv[0] << " <level_name>" << std::endl;
return 1;
}
std::string level_name = argv[1];
std::string level_path = "levels/" + level_name + ".level"; // .level files are pairs of {y, x} coordinates
if (std::ifstream(level_path)) {
std::cout << "Level already exists" << std::endl;
return 1;
}
std::ofstream level_file(level_path);
if (!level_file) {
std::cout << "Could not create level file" << std::endl;
return 1;
}
std::cout << "Level file created" << std::endl;
sista::Cursor cursor_handler;
sista::Field field(WIDTH, HEIGHT);
sista::Pawn* builder = new sista::Pawn('$', sista::Coordinates(1, 5), builder_style);
field.addPawn(builder);
field.print('&');
std::cout << "\nUse {W, A, S, D}+ENTER to move the cursor" << std::endl;
std::cout << "Use {P, R}+ENTER to place and remove a block" << std::endl;
std::cout << "Use {Q}+ENTER to quit" << std::endl;
while (true) {
#if _WIN32 or __linux__
char input = getch();
#elif __APPLE__
cursor_handler.set({20, 30});
char input = std::cin.get();
#else
cursor_handler.set({20, 30});
char input = std::cin.get();
#endif
switch (input) {
case 'w': case 'W':
if (builder->getCoordinates().y > 0) {
try {
field.movePawnBy(builder, up);
} catch (std::invalid_argument& e) {}
}
break;
case 'a': case 'A':
if (builder->getCoordinates().x > 0) {
try {
field.movePawnBy(builder, left);
} catch (std::invalid_argument& e) {}
}
break;
case 's': case 'S':
if (builder->getCoordinates().y < HEIGHT - 1) {
try {
field.movePawnBy(builder, down);
} catch (std::invalid_argument& e) {}
}
break;
case 'd': case 'D':
if (builder->getCoordinates().x < WIDTH - 1) {
try {
field.movePawnBy(builder, right);
} catch (std::invalid_argument& e) {}
}
break;
case 'p': case 'P': {
sista::Coordinates coordinates = builder->getCoordinates();
if (coordinates.y < 1)
continue; // Do not place blocks on the first/second row
coordinates.y++;
if (coordinates.y < HEIGHT) {
field.addPrintPawn(new sista::Pawn('#', coordinates, builder_style));
}
break;
}
case 'r': case 'R': {
sista::Coordinates coordinates = builder->getCoordinates();
coordinates.y++;
if (coordinates.y < HEIGHT) {
sista::Pawn* deleted = field.getPawn(coordinates);
if (deleted != nullptr) {
if (deleted->getSymbol() == '#') {
field.removePawn(deleted);
delete deleted;
// Graphically remove the pawn
cursor_handler.set(coordinates);
ANSI::reset();
std::cout << ' ';
}
}
}
break;
}
case 'q': case 'Q':
for (int row = 0; row < HEIGHT; row++) {
for (int col = 0; col < WIDTH; col++) {
sista::Coordinates coordinates(row, col);
if (field.getPawn(coordinates) != nullptr) {
if (field.getPawn(coordinates)->getSymbol() == '#')
level_file << coordinates.y << " " << coordinates.x << '\n';
}
}
}
level_file.close();
std::cout << "Level saved" << std::endl;
field.reset();
return 0;
}
}
}