-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbattle-map-explorer.js
18 lines (15 loc) · 4.15 KB
/
battle-map-explorer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
battle-map-explorer version 1.1
This code is released into the public domain - attribution is appreciated but not required.
Made by Byron Knoll.
https://github.com/byronknoll/battle-map-explorer
*/
window.requestAnimFrame=function(){return window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||window.oRequestAnimationFrame||window.msRequestAnimationFrame||function(H,x){window.setTimeout(H,1E3/60)}}();function BattleMapExplorer(){}
BattleMapExplorer.run=function(H,x,M,g,y){function N(a){return 87==a||38==a||104==a?!0:!1}function O(a){return 65==a||37==a||100==a?!0:!1}function P(a){return 68==a||39==a||102==a?!0:!1}function Q(a){return 83==a||40==a||98==a?!0:!1}function A(a,d){for(var b=0;b<k.length;++b)if(VisibilityPolygon.doLineSegmentsIntersect(p,q,a,d,k[b][0][0],k[b][0][1],k[b][1][0],k[b][1][1]))return;p=a;q=d;h=!0}function R(){var a=canvas.getContext("2d");if(!S){if(T){S=!0;var d=w.width,b=w.height;M.push([[-1,-1],[d+1,
-1],[d+1,b+1],[-1,b+1]]);k=VisibilityPolygon.convertToSegments(M);k=VisibilityPolygon.breakIntersections(k);for(d=0;d<g.length;++d)B.push(!1)}a.save()}else if(h){h=!1;I||(C&&(A(p-z,q),h=!0),D&&(A(p+z,q),h=!0),E&&(A(p,q-z),h=!0),F&&(A(p,q+z),h=!0));J&&(C||D||E||F)&&(I=!0);var d=u/2,b=v/2,e=d-p,l=b-q,m=Math.min(u,v)/2;a.restore();a.save();a.clearRect(0,0,u,v);y&&(a.beginPath(),a.arc(d,b,m,0,2*Math.PI,!0),a.clip());for(var f=[],c=0;c<k.length;++c)f.push(k[c]);for(c=0;c<g.length;++c)B[c]||f.push(g[c]);
f=VisibilityPolygon.computeViewport([p,q],f,[-e,-l],[u-e,v-l]);a.beginPath();a.moveTo(f[0][0]+e,f[0][1]+l);for(c=1;c<f.length;++c)a.lineTo(f[c][0]+e,f[c][1]+l);a.clip();var c=p-u/2,f=q-v/2,r=u,n=v,t=d-u/2,G=b-v/2;y&&(c=p-m,f=q-m,r=2*m,n=2*m,t=d-m,G=b-m);0>c&&(t-=c,r+=c,c=0);0>f&&(G-=f,n+=f,f=0);c+r>=w.width&&(r=w.width-c-1);f+n>=w.height&&(n=w.height-f-1);a.drawImage(w,c,f,r,n,t,G,r,n);for(c=0;c<g.length;++c)B[c]||(a.beginPath(),a.lineWidth=10,a.strokeStyle="black",a.moveTo(g[c][0][0]+e,g[c][0][1]+
l),a.lineTo(g[c][1][0]+e,g[c][1][1]+l),a.stroke(),a.beginPath(),a.lineWidth=6,a.strokeStyle="white",a.moveTo(g[c][0][0]+e,g[c][0][1]+l),a.lineTo(g[c][1][0]+e,g[c][1][1]+l),a.stroke());a.beginPath();a.lineWidth=2;a.strokeStyle="black";a.fillStyle="white";a.fillRect(d-2,b-25,4,20);a.rect(d-2,b-25,4,20);a.stroke();a.fillRect(d-2,b+5,4,20);a.rect(d-2,b+5,4,20);a.stroke();a.fillRect(d-25,b-2,20,4);a.rect(d-25,b-2,20,4);a.stroke();a.fillRect(d+5,b-2,20,4);a.rect(d+5,b-2,20,4);a.stroke();y&&(e=a.createRadialGradient(d,
b,10,d,b,m),e.addColorStop(0,"rgba(0,0,0,0)"),e.addColorStop(.7,"rgba(0,0,0,0)"),e.addColorStop(1,"rgba(0,0,0,1)"),a.fillStyle=e,a.fillRect(d-m,b-m,2*m,2*m))}requestAnimFrame(R)}y="undefined"!==typeof y?y:!0;var u=window.innerWidth,v=window.innerHeight;canvas.getContext("2d").canvas.width=u;canvas.getContext("2d").canvas.height=v;var h=!0,S=!1,p=x[0],q=x[1],C=!1,D=!1,E=!1,F=!1,J=!1,I=!1,U=0,V=0,K=0,L=0,z=5,k=[],B=[];x=new Hammer(document.getElementById("canvas"));x.get("pan").set({direction:Hammer.DIRECTION_ALL});
var w=new Image,T=!1;w.onload=function(){T=!0};w.src=H;requestAnimFrame(R);window.onresize=function(a){u=window.innerWidth;v=window.innerHeight;canvas.getContext("2d").canvas.width=u;canvas.getContext("2d").canvas.height=v;h=!0};document.addEventListener("touchmove",function(a){a.preventDefault()},!1);document.onkeydown=function(a){N(a.keyCode)?h=E=!0:O(a.keyCode)?h=C=!0:P(a.keyCode)?h=D=!0:Q(a.keyCode)?h=F=!0:16==a.keyCode&&(J=!0)};document.onkeyup=function(a){N(a.keyCode)?(E=!1,h=!0):O(a.keyCode)?
(C=!1,h=!0):P(a.keyCode)?(D=!1,h=!0):Q(a.keyCode)?(F=!1,h=!0):32==a.keyCode?console.log("["+Math.round(p)+","+Math.round(q)+"],"):191==a.keyCode||96==a.keyCode?z=Number(prompt("Enter movement speed:",z)):16==a.keyCode&&(J=!1);I=!1};x.on("pan",function(a){var d=a.center.x,b=a.center.y,e=a.deltaX;a=a.deltaY;d-=e;b-=a;if(d!=U||b!=V)L=K=0;U=d;V=b;d=e;b=a;e-=K;a-=L;K=d;L=b;A(p-e,q-a)});x.on("tap",function(a){var d=a.center.x-(u/2-p);a=a.center.y-(v/2-q);for(var b=0;b<g.length;++b){var e=g[b][0][0],l=g[b][0][1],
m=g[b][1][0],f=g[b][1][1],c=m-e,r=f-l,n=(d-e)*c+(a-l)*r,t=c*c+r*r,k=-1;0!=t&&(k=n/t);n=t=void 0;0>k?(t=e,n=l):1<k?(t=m,n=f):(t=e+k*c,n=l+k*r);e=d-t;l=a-n;10>Math.sqrt(e*e+l*l)&&(B[b]=!B[b],h=!0)}})};