-
Notifications
You must be signed in to change notification settings - Fork 2
/
console.js
240 lines (215 loc) · 7.42 KB
/
console.js
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
var Options = require('options');
var Game = require('./lib/Game');
var TileTypes = require('./lib/TileTypes');
var GameOptions = require('./lib/GameOptions');
var escapeString = '\u001B';
var screenBuffer = {
statusArea: {
top: 1,
left: 1,
bottom: process.stdout.rows - 4,
right: 20,
buffer: []
},
mapArea: {
top: 1,
left: 22,
right: process.stdout.columns - 1,
bottom: process.stdout.rows - 4,
buffer: []
},
messageArea: {
top: process.stdout.rows - 2,
left: 1,
right: process.stdout.columns - 1,
buffer: []
},
toPrint: ''
}
function getPrintable(tile) {
var retString = ' ';
if (tile.entity !== null) {
retString = 'r';
} else if (tile.tileType === TileTypes.WALL) {
retString = '#';
} else if (tile.tileType === TileTypes.FLOOR) {
retString = '.';
}
return {
string: retString,
color: GameOptions.ColorCodes.Default
};
}
function clearScreen() {
process.stdout.write(escapeString + '[2J');
// TODO: make 24 configurable
var bottom = process.stdout.rows > 24 ? 24 : process.stdout.rows;
// TODO: make 4 configurable
screenBuffer.statusArea.buffer = [];
screenBuffer.statusArea.bottom = bottom;
// TODO: make 30 configurable
screenBuffer.statusArea.right = 30;
screenBuffer.mapArea.buffer = [];
// TODO: make 32 configurable
screenBuffer.mapArea.left = 32;
screenBuffer.mapArea.bottom = bottom;
// TODO: make 80 configurable
screenBuffer.mapArea.right = process.stdout.columns > 80
? 80
: process.stdout.columns;
screenBuffer.messageArea.buffer = [];
screenBuffer.messageArea.top = bottom + 2;
screenBuffer.messageArea.right = process.stdout.columns > 80
? 80
: process.stdout.columns;
}
function drawString(options) {
options = new Options({
x: 1,
y: 1,
str: '',
color: GameOptions.ColorCodes.Default
}).merge(options);
var outputString = escapeString + '[' + options.value.y + ';' + options.value.x + 'H';
outputString += escapeString + options.value.color;
outputString += options.value.str;
screenBuffer.toPrint += outputString;
//process.stdout.write(outputString);
}
function flushScreenBuffer() {
process.stdout.write(screenBuffer.toPrint);
screenBuffer.toPrint = '';
}
function updateStatusArea() {
drawString({y:1, x:1, str:'Gnasher'});
drawString({y:2, x:1, str:'Knight Templar'});
drawString({y:3, x:4, str:Array(11).join('\u00bb'), color:GameOptions.ColorCodes.BrightGreen});
drawString({y:4, x:4, str:'30/30', color:GameOptions.ColorCodes.Green});
drawString({y:6, x:4, str:Array(11).join('\u00bb'), color:GameOptions.ColorCodes.BrightBlue});
drawString({y:7, x:4, str:'10/10', color:GameOptions.ColorCodes.Blue});
for (var i = 1; i <= screenBuffer.statusArea.bottom; i++) {
drawString({y:i, x:screenBuffer.statusArea.right + 1, str:'\u2502', color:GameOptions.ColorCodes.Gray});
}
flushScreenBuffer();
}
function updateMapArea() {
var playerX = screenBuffer.mapArea.left +
Math.floor((screenBuffer.mapArea.right - screenBuffer.mapArea.left) / 2);
var playerY = screenBuffer.mapArea.top +
Math.floor((screenBuffer.mapArea.bottom - screenBuffer.mapArea.top) / 2);
for(var x = screenBuffer.mapArea.left; x <= screenBuffer.mapArea.right; x++) {
for(var y = screenBuffer.mapArea.top; y <= screenBuffer.mapArea.bottom; y++) {
var tile = game.getTile((playerY - y),-1*(playerX - x));
var printable = getPrintable(tile);
if(typeof screenBuffer.mapArea.buffer[y] === 'undefined') {
screenBuffer.mapArea.buffer[y] = [];
}
if(typeof screenBuffer.mapArea.buffer[y][x] === 'undefined') {
screenBuffer.mapArea.buffer[y][x] = {
string:'',
color:GameOptions.ColorCodes.Default
};
}
if (screenBuffer.mapArea.buffer[y][x].string !== printable.string
|| screenBuffer.mapArea.buffer[y][x].color !== printable.color) {
screenBuffer.mapArea.buffer[y][x] = {
string:printable.string,
color:printable.color
};
drawString({y:y, x:x, str:printable.string, color:printable.color});
}
}
}
drawString({
x:playerX,
y:playerY,
str:'@',
color:GameOptions.ColorCodes[GameOptions.Colors.Player]
});
// TODO: remove this
drawString({
x: 1,
y: 30,
str:'('+game.playerLocation.x+', '+game.playerLocation.y + ') '
});
flushScreenBuffer();
}
function updateMessageArea(message) {
for(var i = screenBuffer.messageArea.left; i <= screenBuffer.messageArea.right; i++) {
drawString({
y: screenBuffer.messageArea.top -1,
x: i,
str: '\u2500',
color: GameOptions.ColorCodes.Gray
});
}
// add tee to join lines between status and message areas
drawString({
y: screenBuffer.statusArea.bottom + 1,
x: screenBuffer.statusArea.right + 1,
str: '\u2534',
color:GameOptions.ColorCodes.Gray
});
if (message && message.length > 0) {
drawString({
y: screenBuffer.messageArea.top,
x: screenBuffer.messageArea.left,
str: message + ' ',
color: GameOptions.ColorCodes.Red
});
}
flushScreenBuffer();
}
function redrawScreen() {
clearScreen();
updateScreen();
}
function updateScreen() {
updateStatusArea();
updateMapArea();
updateMessageArea();
}
function handleInput(character) {
try {
switch(character.toString()) {
case GameOptions.Keybindings.Quit:
var quitMessage = escapeString + '[2J';
quitMessage += escapeString + '[H';
quitMessage += escapeString + GameOptions.ColorCodes.Default;
quitMessage += escapeString + '[?25h';
quitMessage += 'Thanks for playing!';
console.log(quitMessage);
process.exit(0);
break;
case GameOptions.Keybindings.MoveDown:
game.moveDown();
break;
case GameOptions.Keybindings.MoveUp:
game.moveUp();
break;
case GameOptions.Keybindings.MoveLeft:
game.moveLeft();
break;
case GameOptions.Keybindings.MoveRight:
game.moveRight();
break;
}
} catch (err) {
var cleanupMsg = escapeString + '[2J';
cleanupMsg += escapeString + '[H';
cleanupMsg += escapeString + GameOptions.ColorCodes.Default;
cleanupMsg += escapeString + '[?25h';
console.log(cleanupMsg);
console.log(err);
process.exit(1);
}
}
var game = new Game();
game.on('map_updated', updateMapArea);
game.on('message', updateMessageArea);
game.start();
process.stdout.on('resize', redrawScreen);
process.stdout.write('\033[?25l');
process.stdin.setRawMode(true);
process.stdin.on('data', handleInput);
redrawScreen();