This repository has been archived by the owner on May 16, 2023. It is now read-only.
/
buildtowers-html5.js
executable file
·211 lines (186 loc) · 8.44 KB
/
buildtowers-html5.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
var BuildTowers = require('./buildtowers');
// for node.js, not for CommonJS
module.exports = BuildTowers;
BuildTowers.prototype.initialCanvas = function (element) {
this.canvasElement = document.createElement('canvas');
element.appendChild(this.canvasElement);
this.resizeCanvas();
BuildTowers.ins = this;
var game = this;
window.addEventListener("keydown", function (e) {
e.preventDefault();
var key = String.fromCharCode(e.keyCode);
game.keyNow = key;
}, false);
window.addEventListener("keyup", function (e) {
e.preventDefault();
var key = String.fromCharCode(e.keyCode);
if (game.keyNow === key) {
game.keyNow = '_';
}
}, false);
window.addEventListener('resize', function() {
if (game.resizeTimer) {
clearTimeout(game.resizeTimer);
}
game.resizeTimer = setTimeout(function () {
game.resizeCanvas();
}, 100);
});
this.mainInterval = window.setInterval(function () {
game.key((game.keyNow || '').toLowerCase());
game.turn();
game.draw();
}, 50);
};
BuildTowers.FONT_MAP_SIZE = 50; // font map is for pre-rendering area, 50 x 50 is reserved in the default
BuildTowers.prototype.resizeCanvas = function () {
if (this.maxWidth && this.maxWidth === window.innerWidth &&
this.maxHeight && this.maxHeight === window.innerHeight) {
return; // nothing to do
}
var device_pixel_ratio = window.devicePixelRatio || 1;
this.maxWidth = window.innerWidth;
this.maxHeight = window.innerHeight;
var font_size = Math.min(Math.floor(this.maxWidth * device_pixel_ratio / 96), Math.floor(this.maxHeight * device_pixel_ratio / 27 / 2));
if (this.fontX === font_size && this.fontY === font_size * 2) {
return; // nothing to do
}
this.fontX = font_size; this.fontY = font_size * 2;
this.devicePixelRatio = device_pixel_ratio;
this.canvasElement.setAttribute('width', this.fontX * 96);
this.canvasElement.setAttribute('height', this.fontY * 27);
this.canvasElement.parentElement.style.width = Math.round(this.fontX * 96 / device_pixel_ratio) + 'px';
this.canvasElement.parentElement.style.height = Math.round(this.fontY * 27 / device_pixel_ratio) + 'px';
this.canvasElement.style.width = Math.round(this.fontX * 96 / device_pixel_ratio) + 'px';
this.canvasElement.style.height = Math.round(this.fontY * 27 / device_pixel_ratio) + 'px';
this.canvasContext = this.canvasElement.getContext("2d");
this.canvasContext.fillStyle = 'white';
this.fontCanvasElement = document.createElement('canvas');
this.fontCanvasElement.setAttribute('width', this.fontX);
this.fontCanvasElement.setAttribute('height', this.fontY);
this.fontCanvasContext = this.fontCanvasElement.getContext("2d");
this.fontCanvasContext.fillStyle = this.fillStyle = 'black';
this.fontCanvasContext.font = this.fontY + 'px "Courier New",Courier,monospace';
this.fontCanvasContext.textAlign = 'center';
this.fontCanvasContext.textBaseline = 'middle';
this.fontMap = {}; // str + ' ' + color : [ dx, dy ]
this.fontLength = 0;
this.fontMapCanvasElement = document.createElement('canvas');
this.fontMapCanvasElement.setAttribute('width', this.fontX * BuildTowers.FONT_MAP_SIZE);
this.fontMapCanvasElement.setAttribute('height', this.fontY * BuildTowers.FONT_MAP_SIZE);
this.fontMapCanvasContext = this.fontMapCanvasElement.getContext("2d");
this.fontMapCanvasContext.fillStyle = 'white';
this.fontMapCanvasContext.fillRect(0, 0, this.fontX * BuildTowers.FONT_MAP_SIZE, this.fontY * BuildTowers.FONT_MAP_SIZE);
// for full width
this.fontFWCanvasElement = document.createElement('canvas');
this.fontFWCanvasElement.setAttribute('width', this.fontX * 2);
this.fontFWCanvasElement.setAttribute('height', this.fontY);
this.fontFWCanvasContext = this.fontFWCanvasElement.getContext("2d");
this.fontFWCanvasContext.fillStyle = this.fillStyle = 'black';
this.fontFWCanvasContext.font = this.fontY + 'px "Courier New",Courier,monospace';
this.fontFWCanvasContext.textAlign = 'center';
this.fontFWCanvasContext.textBaseline = 'middle';
this.fontFWMap = {}; // str + ' ' + color : [ dx, dy ]
this.fontFWLength = 0;
this.fontFWMapCanvasElement = document.createElement('canvas');
this.fontFWMapCanvasElement.setAttribute('width', this.fontX * BuildTowers.FONT_MAP_SIZE * 2);
this.fontFWMapCanvasElement.setAttribute('height', this.fontY * BuildTowers.FONT_MAP_SIZE);
this.fontFWMapCanvasContext = this.fontFWMapCanvasElement.getContext("2d");
this.fontFWMapCanvasContext.fillStyle = 'white';
this.fontFWMapCanvasContext.fillRect(0, 0, this.fontX * BuildTowers.FONT_MAP_SIZE * 2, this.fontY * BuildTowers.FONT_MAP_SIZE);
// initial drawing
this.draw(true);
};
BuildTowers.prototype.getPointFromHTML = function (x, y) {
var px = x, py = y;
var mx = Math.floor(px * this.devicePixelRatio / this.fontX), my = Math.floor(py * this.devicePixelRatio / this.fontY);
return [ mx, my ];
};
BuildTowers.COLOR_REGEXP = /^\{([^-]+)-fg\}(.*)\{\/\1-fg\}$/;
BuildTowers.UNDERLINE_REGEXP = /^\{underline\}(.*)\{\/underline\}$/;
BuildTowers.prototype.draw = function (initial) {
var screen = this.getScreen();
var context = this.canvasContext;
// for half width
var font_element = this.fontCanvasElement;
var font_context = this.fontCanvasContext;
var font_map = this.fontMap;
var font_map_element = this.fontMapCanvasElement;
var font_map_context = this.fontMapCanvasContext;
// for full width
var fontfw_element = this.fontFWCanvasElement;
var fontfw_context = this.fontFWCanvasContext;
var fontfw_map = this.fontFWMap;
var fontfw_map_element = this.fontFWMapCanvasElement;
var fontfw_map_context = this.fontFWMapCanvasContext;
var old_screen = initial ? null : this.oldScreen;
var dw = this.fontX, dh = this.fontY;
var get_str_pos = function (str, color, full_width, underline) {
if (font_map[str + ' ' + color + ' ' + underline]) {
return font_map[str + ' ' + color + ' ' + underline];
} else if (fontfw_map[str + ' ' + color + ' ' + underline]) {
return fontfw_map[str + ' ' + color + ' ' + underline];
}
var dx, dy;
if (full_width) {
++this.fontFWLength;
dx = (this.fontFWLength % BuildTowers.FONT_MAP_SIZE) * dw * 2; dy = Math.floor(this.fontFWLength / BuildTowers.FONT_MAP_SIZE) * dh;
fontfw_context.clearRect(0, 0, dw * 2, dh);
fontfw_context.fillStyle = color;
fontfw_context.fillText(str, dw, dh * 0.5);
if (underline) {
fontfw_context.fillRect(0, dh - 2, dw * 2, 2);
}
fontfw_map_context.drawImage(fontfw_element, dx, dy);
fontfw_map[str + ' ' + color + ' ' + underline] = [ dx, dy ];
return fontfw_map[str + ' ' + color + ' ' + underline];
} else {
++this.fontLength;
dx = (this.fontLength % BuildTowers.FONT_MAP_SIZE) * dw; dy = Math.floor(this.fontLength / BuildTowers.FONT_MAP_SIZE) * dh;
font_context.clearRect(0, 0, dw, dh);
font_context.fillStyle = color;
font_context.fillText(str, dw * 0.5, dh * 0.5);
if (underline) {
font_context.fillRect(0, dh - 2, dw, 2);
}
font_map_context.drawImage(font_element, dx, dy);
font_map[str + ' ' + color + ' ' + underline] = [ dx, dy ];
return font_map[str + ' ' + color + ' ' + underline];
}
};
var before_full_width = false;
for (var y = 0; y < 27; ++y) {
for (var x = 0; x < 96; ++x) {
var str = screen[y][x];
if (!str) { // null is blank
str = screen[y][x] = ' ';
}
var full_width = before_full_width;
before_full_width = (str.indexOf("\0") !== -1); // if str have null-str, next str is full-width
if (old_screen && str === old_screen[y][x]) { // no-updated
continue;
}
var colors = BuildTowers.COLOR_REGEXP.exec(str);
if (colors) {
if (this.fillStyle !== colors[1]) {
this.fillStyle = colors[1];
}
str = colors[2];
} else {
if (this.fillStyle !== 'black') {
this.fillStyle = 'black';
}
}
var underlines = BuildTowers.UNDERLINE_REGEXP.exec(str);
if (underlines) {
str = underlines[1];
}
var dx = dw * (full_width ? x - 1 : x), dy = dh * y;
var s = get_str_pos.call(this, str, this.fillStyle, full_width, !!underlines);
var sx = s[0], sy = s[1], sw = (full_width ? dw * 2 : dw ), sh = dh;
context.drawImage((full_width ? fontfw_map_element : font_map_element), sx, sy, sw, sh, dx, dy, (full_width ? dw * 2 : dw), dh);
}
}
this.oldScreen = screen.map(function (row) { return row.concat(); });
};