This repository has been archived by the owner on May 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.d
218 lines (191 loc) · 5.18 KB
/
main.d
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
* My entry to the 1st Virbox coding challenge.
* It draws a couple of windows onto the terminal
* using curses, and then draws the player and the
* map onto the screen. Very incomplete, at least
* the movement system works. There is also a basic
* mapping "system" (basically just a function in
* the Box class) which allows an array of strings
* to be placed over the game window. Very handy
* and I like it. I might reuse this code some
* other time but for now this is all I got. o7
*
* Use HJKL, WASD or the arrow keys to move the
* character around the screen. The character
* will not move if there is an obstacle in the
* way, such as another character or a window
* border. A "window" in curses terms means a
* container which you can draw text into.
*
* Licensed under the GNU General Public License,
* version 2. See the COPYING file included for
* the full license.
*/
import core.sys.posix.unistd : isatty;
import std.algorithm.comparison : clamp;
import std.math.rounding : ceil;
import std.conv : to;
import std.file : remove;
import std.format : format;
import std.meta : Alias;
import std.traits : isSomeString;
/* Import curses into the "cs" namespace */
import cs = deimos.curses;
import box; /* Box class */
import player; /* Player class */
import util; /* utility functions, ColourPair alias and stdscr alias */
enum Key
{
RESIZE = /*0632*/ 0x19a, /* Terminal resize event */
DOWN = /*0402*/ 0x102, /* down-arrow key */
UP = /*0403*/ 0x103, /* up-arrow key */
LEFT = /*0404*/ 0x104, /* left-arrow key */
RIGHT = /*0405*/ 0x105 /* right-arrow key */
}
enum Colour
{
BLACK,
RED,
GREEN,
YELLOW,
BLUE,
MAGENTA,
CYAN,
WHITE
}
/* Minimum required terminal size */
ushort MINROWS = 28;
ushort MINCOLS = 82;
/* Size and position of UI elements */
ushort GAMEBOX_H = 15;
ushort GAMEBOX_W = 65;
ushort GAMEBOX_Y = 5;
ushort GAMEBOX_X = 8;
ushort TEXTBOX_H = 8;
ushort TEXTBOX_W = 65;
ushort TEXTBOX_Y = 20;
ushort TEXTBOX_X = 8;
/* File to dump screen contents to */
const char *DUMPFILE = cast(char *)".vscrdump";
int main(string[] args)
{
bool dumped;
int ch, tmp;
Box game;
Box text;
Player player;
void panic(R, A...)(R fmt, A a)
if (isSomeString!R)
{
import core.runtime : Runtime;
import core.stdc.stdlib : exit;
import std.path : baseName;
import std.stdio : stderr;
game.destroy();
text.destroy();
endCurses();
stderr.writefln("%s: %s", baseName(args[0]), fmt.format(a));
scope (exit) {
Runtime.terminate();
exit(1);
}
}
if (!isatty(1)) {
panic("output is not to a terminal; cannot continue");
}
cs.initscr(); /* initialise curses */
if (!cs.has_colors()) {
panic("terminal does not support color; cannot continue");
}
cs.start_color(); /* enable color */
cs.use_default_colors(); /* take terminal transparency into account */
cs.raw(); /* disable line buffering */
cs.noecho(); /* don't echo characters when we run getch() */
cs.curs_set(0); /* don't show the cursor */
cs.keypad(stdscr, 1); /* catch F* and arrow key characters */
/* colours */
cs.init_color(Colour.RED, 1000, 0, 0);
/* colour pairs */
cs.init_pair(1, Colour.WHITE, Colour.RED);
checkDimensions(MINCOLS, MINROWS, DUMPFILE, dumped);
game = new Box(GAMEBOX_H, GAMEBOX_W, GAMEBOX_Y, GAMEBOX_X);
text = new Box(TEXTBOX_H, TEXTBOX_W, TEXTBOX_Y, TEXTBOX_X);
player = new Player(game.win, 12, 60, '*');
game.map([
` `,
` _______ `,
` /virbox \ 0 0 0 0 0`,
` | farms | | | | | |`,
` |_______| 0 0 0 0 0`,
` |_| | | | | | `
]);
text.quotew(
"Welcome to virboxquest! We'll give you a quick tutorial.
First, move your character around using h, j, k and l,
or you can use w, a, s, and d.
Remove this text by pressing [Enter].
");
do {
ch = cs.wgetch(game.win);
/*
* if we're waiting for the player to resize the terminal,
* we want to check the dimensions and then continue with
* the next run of the loop immediately, we don't want any
* of the other keybinds to be detected until the player
* resizes their terminal dimensions properly
*/
if (dumped) {
if (ch == Key.RESIZE) {
checkDimensions(MINCOLS, MINROWS, DUMPFILE, dumped);
}
continue;
}
switch (ch) {
case Key.RESIZE: /* on window resize */
checkDimensions(MINCOLS, MINROWS, DUMPFILE, dumped);
break;
case Key.UP:
case 'w':
case 'k':
player.moveUp();
break;
case Key.DOWN:
case 's':
case 'j':
player.moveDown();
break;
case Key.LEFT:
case 'a':
case 'h':
player.moveLeft();
break;
case Key.RIGHT:
case 'd':
case 'l':
player.moveRight();
break;
case 'q': /* quit key */
text.clear();
text.quote("Are you sure you want to quit? ", 20);
cs.curs_set(1); /* show cursor */
tmp = cs.wgetch(text.win);
if (tmp == 'y' || tmp == 'Y') {
game.destroy();
text.destroy();
endCurses();
return 0;
} else {
cs.curs_set(0);
text.clear();
break;
}
default:
break;
}
cs.wrefresh(game.win);
cs.wrefresh(text.win);
} while (ch);
game.destroy();
endCurses();
return 0;
}