3 <title>Tilerender</title>
5 </head><body onload="script()" style="width: 100%; max-height: 600px; overflow: hidden; padding: 0 auto;" >
6 <canvas id="view" width="1280" height="600" style="padding: 0 auto; max-height: 600px;">
7 Canvas not supported<br/>
8 <img id="maptiles" src="tileset.png" width="64" style="overflow: hidden;" />
10 <tile position="2" animated="1"></tile>
11 <tile position="3" elevation=".125"></tile>
12 <tile position="4" blocking="yes"></tile>
14 <img id="character" src="character.png" speed="4"/>
15 <div id="map" columns="18" rows="36">
16 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
17 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
18 5, 1, 1, 4, 4, 4, 4, 4, 4, 4, 2, 2, 2, 2, 2, 2, 2, 5,
19 5, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,5,
20 5, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 5,
21 5, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,5,
22 5, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 5,
23 5, 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 2, 2,5,
24 5, 2, 2, 2, 2, 1, 2, 4, 2, 2, 2, 2, 2, 2, 2, 2, 2, 5,
25 5, 2, 2, 2, 1, 1, 4, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,5,
26 5, 2, 2, 2, 1, 4, 4, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 5,
27 5, 2, 2, 1, 4, 4, 1, 2, 2, 2, 2, 2, 2, 4, 2, 2, 2,5,
28 5, 2, 2, 2, 1, 4, 1, 2, 2, 2, 4, 2, 2, 4, 4, 2, 2, 5,
29 5, 2, 2, 2, 1, 1, 2, 2, 2, 2, 4, 2, 4, 3, 4, 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0, 0,
86 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
87 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
88 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
92 var gravity = 9.81, fps = 30;
93 view = document.getElementById("view");
94 canvas = document.getElementById("view").getContext("2d");
97 rows: document.getElementById("map").getAttribute("rows") * 1 || 1,
98 cols: document.getElementById("map").getAttribute("columns") * 1 || 1,
99 data: document.getElementById("map").textContent.split(',') || [],
100 tiles: document.getElementById("maptiles"),
102 default_props: {animated: false, elevation: 0, blocking: false},
104 tw: 32, th: 16, sw: 16, sh: 8,
105 offset_x: 0, offset_y: 0,
108 this.tw = this.tiles.getAttribute("width") * 1 || 32;
109 this.th = this.tiles.getAttribute("height") * 1 || this.tw / 2;
110 this.sw = this.tw / 2;
111 this.sh = this.th / 2;
113 this.gravity = gravity / fps / Math.sqrt(this.sw * this.sw + this.sh * this.sh) * this.sh;
117 for (i in this.data) this.data[i] = this.data[i] * 1 || 0; // make numeric
119 for (prop of document.querySelectorAll("tiles > tile")) {
120 i = prop.getAttribute("position") * 1;
121 if (i) this.tileprops[i] = {
122 animated: prop.getAttribute("animated") || this.default_props.animated,
123 elevation: prop.getAttribute("elevation") * 1 || this.default_props.elevation,
124 blocking: prop.getAttribute("blocking") || this.default_props.blocking
129 props_of: function(n) { return this.tileprops[this.data[n]] || this.default_props; },
131 tile_at: function(x, y, z) {
132 var my = (y / this.sh |0);
133 var mx = (x / this.sw |0);
135 switch(((my % 2) << 1) + mx % 2) {
136 case 0: my += o = (y % this.sh * 2 > this.sw - x % this.sw); break;
137 case 1: my += o = (y % this.sh * 2 > x % this.sw); mx -= o; break;
138 case 2: my += o = (y % this.sh * 2 > x % this.sw); break;
139 case 3: my += o = (y % this.sh * 2 > this.sw - x % this.sw); mx += o - 1; break;
141 mx -= x / this.tw |0;
143 return this.cols * my + mx + (z |0) * this.rows * this.cols;
146 draw: function(){ // map.draw
147 var py = player.n / this.cols % this.rows |0;
148 var px = player.n % this.cols;
149 var x, y, z, n, tile, props;
151 for (y = 0; y < this.rows; ++y) {
152 for (z = 0; z < this.data.length / (this.rows * this.cols); ++z)
153 for (x = 0; x < this.cols; ++x) {
154 n = x + y * map.cols + z * map.rows * map.cols;
155 props = this.props_of(n);
156 tile = ( n == ptr ) ? 5 : this.data[n];
157 canvas.globalAlpha = ( y > py &&
158 (z > player.z || z == (player.z |0) && props.blocking) &&
164 canvas.drawImage( this.tiles,
165 this.tw * tile, 2 * this.th * ((frame / 16 |0) % 8) * props.animated,
166 this.tw, 2 * this.th,
167 x * this.tw + y % 2 * this.sw - this.sw - this.offset_x,
168 y * this.sh - z * this.th - 3 * this.sh - this.offset_y,
172 if (py == y) player.draw();
180 sprite: document.getElementById("character"),
183 this.w = this.sprite.getAttribute("width") * 1 || map.tw || 32;
184 this.h = this.sprite.getAttribute("height") * 1 || map.sh * 6 || this.w * 1.5;
185 this.o = this.sprite.getAttribute("offset") * 1 || map.sh || this.w / 4;
186 this.ospeed = this.speed = this.sprite.getAttribute("speed") * 1|| this.w / 16;
187 this.n = map.tile_at(this.x, this.y, this.z);
188 map.offset_x = this.x - view.clientWidth / 2 |0;
189 map.offset_y = this.y - view.clientHeight / 2 |0;
192 move: function(){ // player.move
196 case 'n': dy = -this.speed; break;
197 case 's': dy = this.speed; break;
198 case 'w': dx = -this.speed; break;
199 case 'e': dx = this.speed; break;
200 case 'nw': dy = -.45 * this.speed; dx = -.9 * this.speed; break;
201 case 'ne': dy = -.45 * this.speed; dx = .9 * this.speed; break;
202 case 'sw': dy = .45 * this.speed; dx = -.9 * this.speed; break;
203 case 'se': dy = .45 * this.speed; dx = .9 * this.speed; break;
206 if (player.frame % 6 > 2) {
214 var n = map.tile_at(this.x + dx, this.y + dy, this.z);
215 if (!map.props_of(n).blocking &&
216 !map.data[n + map.rows * map.cols]
217 ){ this.x += dx; this.y += dy; this.n = n;}
219 this.frame += this.frame < 255 ? 1 : -127;
221 map.offset_x = this.x - view.clientWidth / 2 |0;
222 map.offset_y = this.y - view.clientHeight / 2 |0;
223 }, // end player.move
227 var zbase = (this.n / (map.rows * map.cols) |0) + map.props_of(this.n).elevation;
229 this.z += this.zspeed -= map.gravity;
231 if (this.z > zbase) {
232 this.speed = this.ospeed * 2;
233 } else if (!map.data[this.n] && !map.props_of(this.n - map.rows * map.cols).blocking) {
234 this.n -= map.rows * map.cols;
236 this.speed = this.ospeed;
241 }, // end player.fall
244 draw: function(){ // player.draw
245 var face = 0, state = 0;
248 case 's': face = 0; break;
249 case 'sw': face = 1; break;
250 case 'w': face = 2; break;
251 case 'nw': face = 3; break;
252 case 'n': face = 4; break;
253 case 'ne': face = 5; break;
254 case 'e': face = 6; break;
255 case 'se': face = 7; break;
259 (this.frame > 11) ? frame / 16 % 2 |0 :
260 (this.frame < 12) ? this.frame / 3 + 2 |0 :
263 canvas.drawImage( this.sprite, this.w * face, this.h * state,
265 this.x - this.w / 2 - map.offset_x,
266 this.y - this.z * map.th - this.h + this.o - map.offset_y,
277 function get_input(){
279 if ( keys[87] || keys[38] ) d = 'n';
280 if ( keys[83] || keys[40] ) d = 's';
281 if ( keys[65] || keys[37] ) d += 'w';
282 if ( keys[68] || keys[39] ) d += 'e';
284 if ( d == '' ) player.frame = 12
287 if ( player.frame > 11 ) player.frame = 0;
292 if (keys[32] && player.zspeed == 0) {player.zspeed += .75; player.z +=.0001;}
299 setInterval(function(){
303 frame += frame < 255 ? 1 : -255;
306 window.addEventListener('keydown', function(x){ keys[x.keyCode] = true; }, false );
307 window.addEventListener('keyup', function(x){ keys[x.keyCode] = false;}, false );
309 window.addEventListener('mousemove', function(e){
310 px = view.width * (e.clientX - view.offsetLeft) / view.clientWidth |0;
311 py = view.height * (e.clientY - view.offsetTop) / view.clientHeight |0;
313 ptr = map.tile_at(px + map.offset_x, py + map.offset_y, 0);