var nx = r + c |0; var ny = r - c |0;
return ny * w + nx;
}
+ this.tileX = (x,y) => this.tileAt(x,y) % w;
+ this.tileY = (x,y) => this.tileAt(x,y) / w |0;
// vice versa the pixel coordinates of a given map tile
this.positionOf = (tl, ty = -1) => (~ty) ? [dx[ty * w + tl], dy[ty * w + tl]] : [ dx[tl], dy[tl] ];
this.xOf = (tl, ty = -1) => (~ty) ? dx[ty * w + tl] : dx[tl];
this.yOf = (tl, ty = -1) => (~ty) ? dy[ty * w + tl] : dy[tl];
+ // map pixel for given screen pixel
+ this.mapX = x => x - posx;
+ this.mapY = y => y - posy;
+
// center map to pixel position (by setting drawing offsets)
this.center = function(x, y) {
posx = canvas.canvas.width / 2 - x;
const bg = this.info.layer.find(l => l.type == "background").data;
const ob = this.info.layer.find(l => l.type == "object").data;
var i, mm = [];
- //var is = (posx > w * tw - canvas.canvas.width || posy < 0) ? 0 : this.tileAt(posx + canvas.canvas.width, posy);
- // var ie = this.tileAt(posx, + canvas.canvas.height - posy);
// mobs only have x/y pixel positions,
// to draw them in order with map tiles we set up
// draw background layer first
for ( i = 0; i < h * w; i++ ) draw_tile(bg[i], posx + dx[i], posy + dy[i]);
- // for ( i = is; i < ie; i++ ) draw_tile(bg[i], posx + dx[i], posy + dy[i]);
// draw object layer and mobs
for ( i = 0; i < h * w; i++ ) {
- // for ( i = is; i < ie; i++ ) {
draw_tile(ob[i], posx + dx[i], posy + dy[i]);
if (mm[i]) mm[i].forEach(m => m.draw(posx + m.position[0], posy + m.position[1]));
if (npcs[i]) npcs[i].draw(posx + dx[i], posy + dy[i]);
left: 65, altleft: 37,
right: 68, altright: 39,
}
- var keys = [];
+ var keys = [], click = [];
window.addEventListener("keydown", e => keys[e.keyCode] = true );
window.addEventListener("keyup" , e => keys[e.keyCode] = false);
+ window.addEventListener("click", m => click = [ map.mapX(m.clientX), map.mapY(m.clientY) ] );
setInterval(() => input(), 33.33)
// cause player to walk, processes blocked terrain and player speed
events(hero.position[0], hero.position[1]);
}
+ // Process on_trigger events at pixel position x/y
function events(x, y) {
const i = map.tileAt(x,y); let ev, e, n;
if (map.events[i]) {
hero.direct(dir[k]).run();
translate(trans[dir[k]][0], trans[dir[k]][1]);
} else hero.stance();
+
+ if (click[0]) {
+ let hx = map.tileX(hero.position[0], hero.position[1]);
+ let hy = map.tileY(hero.position[0], hero.position[1]);
+ let cx = map.tileX(click[0], click[1]);
+ let cy = map.tileY(click[0], click[1]);
+ if ( (cx == hx - 1 || cx == hx + 1 || cx == hx)
+ && (cy == hy - 1 || cy == hy + 1 || cy == hy))
+ events(click[0], click[1]);
+ click = [];
+ }
}
}