summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--index.html102
1 files changed, 81 insertions, 21 deletions
diff --git a/index.html b/index.html
index 073c73c..569f1a3 100644
--- a/index.html
+++ b/index.html
@@ -8,6 +8,7 @@ var W,H,R;
var cvs,ctx;
var fwd,speed,rot,podx,pody;
var walldir,walladv,wallspeed,wallgate;
+var gatewidth;
//dir in [0..3] (0 right, 1 up etc)
//adv in pixels (distance the wall has advanced)
//gate in [0.0..1.0] (0 at start, 1 at end of line)
@@ -27,6 +28,8 @@ function init(){
wallspeed=1;
wallgate=0.5;
+ gatewidth=100;
+
cvs=document.getElementById("cvs");
ctx=cvs.getContext("2d");
cvs.width=W;
@@ -50,11 +53,12 @@ function init(){
function runloop(){
var id=requestAnimationFrame(runloop);
- increment();
- draw();
if(checks()){
cancelAnimationFrame(id);
+ return;
}
+ increment();
+ draw();
}
@@ -73,15 +77,7 @@ function draw(){
ctx.arc(podx,pody,R,0,2*Math.PI,true);
ctx.stroke();
- var wall2pos=function(perc){
- switch(walldir){
- case 0: return [walladv,perc*H];
- case 1: return [perc*W,H-1-walladv];
- case 2: return [W-1-walladv,perc*H];
- case 3: return [perc*W,walladv];
- }
- };
- var p0=wall2pos(0),pgate1=wall2pos(wallgate-.1),pgate2=wall2pos(wallgate+.1),p1=wall2pos(1);
+ var p0=wall2pos(0),pgate1=wall2pos(wallgate-gatewidthf()/2),pgate2=wall2pos(wallgate+gatewidthf()/2),p1=wall2pos(1);
ctx.beginPath();
ctx.moveTo(p0[0],p0[1]);
ctx.lineTo(pgate1[0],pgate1[1]);
@@ -94,25 +90,89 @@ function increment(){
rot+=0.015;
podx+=Math.cos(rot)*fwd*speed*2.5;
pody+=Math.sin(rot)*fwd*speed*2.5;
- walladv+=wallspeed;
+ walladv+=wallspeed*0.5;
}
-function checks(){
+
+function checks(){ //returns true if runloop should stop
if(checklose()){
- alert("You lost!");
+ playerlose();
return true;
}
+ if(checkwin()){
+ playerwin();
+ return false;
+ }
+ return false;
+}
+
+function playerlose(){
+ setTimeout(function(){alert("You lost!");},300);
+}
+
+function playerwin(){
+ walldir=~~(Math.random()*4);
+ walladv=0;
+ wallspeed+=0.08;
+ wallgate=gatewidthf()*0.75+Math.random()*(1-1.5*gatewidthf());
+}
+
+function checkplayeronedge(){
+ return podx-R<=0||podx+R>=W-1||pody-R<=0||pody+R>=H-1;
+}
+
+function checkplayeronbar(){
+ return Math.abs(walladvfrom0()-(wallisvertical()?podx:pody))<=R;
}
function checklose(){
- if(podx-R<=0||podx+R>=W-1||pody-R<=0||pody+R>=H-1)return true;
+ return (checkplayeronbar()&&!checkwin())||checkplayeronedge();
+}
+
+function checkwin(debug){
+ if(!checkplayeronbar())return false;
+ var heightonbar,vert=wallisvertical();
+ if(vert){
+ heightonbar=Math.sqrt((R+3)*(R+3)-(walladvfrom0()-podx)*(walladvfrom0()-podx));
+ } else {
+ heightonbar=Math.sqrt((R+3)*(R+3)-(walladvfrom0()-pody)*(walladvfrom0()-pody));
+ }
+ if(debug)console.log("heightonbar="+heightonbar);
+ if(isNaN(heightonbar))return false; //not really on bar you suckers
+
+ if(debug){
+ console.log("up: "+Math.abs((wallgate-gatewidthf()/2)*(vert?H:W)-(vert?pody:podx)));
+ console.log("down: "+Math.abs((wallgate+gatewidthf()/2)*(vert?H:W)-(vert?pody:podx)));
+ }
+
+ return Math.max(
+ Math.abs((wallgate-gatewidthf()/2)*(vert?H:W)-(vert?pody:podx)),
+ Math.abs((wallgate+gatewidthf()/2)*(vert?H:W)-(vert?pody:podx))
+ )<=gatewidth;
+}
+
+
+function wall2pos(perc){
switch(walldir){
- case 0: if(abs(walladv- podx)<R+4)return true; break;
- case 1: if(abs(walladv-H+pody)<R+4)return true; break;
- case 2: if(abs(walladv-W+podx)<R+4)return true; break;
- case 3: if(abs(walladv- pody)<R+4)return true; break;
+ case 0: return [walladv,perc*H];
+ case 1: return [perc*W,H-1-walladv];
+ case 2: return [W-1-walladv,perc*H];
+ case 3: return [perc*W,walladv];
}
- return false;
+ throw new Error("wall2pos argument perc="+perc);
+}
+
+function wallisvertical(){
+ return walldir%2==0;
+}
+function wallisrev(){
+ return ~~((walldir+1)/2)==1;
+}
+function gatewidthf(){
+ return gatewidth/(wallisvertical()?H:W);
+}
+function walladvfrom0(){
+ return wallisrev()?(wallisvertical()?W-walladv:H-walladv):walladv;
}
@@ -140,7 +200,7 @@ body{
background-color:#eee;
}
canvas#cvs{
- border-radius:4px;
+ border-radius:7px;
background-color:#fff;
}
</style>