From: Paul Hänsch Date: Sat, 15 Feb 2020 01:16:49 +0000 (+0100) Subject: trigger events by mouse X-Git-Url: https://git.plutz.net/?a=commitdiff_plain;h=5ee37d7f9e046c3ac7d3532e94b9173900e62299;p=flarejs trigger events by mouse --- diff --git a/engine.js b/engine.js index 52cf869..3b36331 100644 --- a/engine.js +++ b/engine.js @@ -153,12 +153,18 @@ function Map(textdef) { 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; @@ -172,8 +178,6 @@ function Map(textdef) { 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 @@ -186,11 +190,9 @@ function Map(textdef) { // 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]); @@ -395,10 +397,11 @@ function Controls(hero){ 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 @@ -429,6 +432,7 @@ function Controls(hero){ 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]) { @@ -471,6 +475,17 @@ function Controls(hero){ 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 = []; + } } }