summaryrefslogtreecommitdiff
path: root/index.html
blob: 569f1a366f4e7d23c633cec4bdb1e7740e3fd38d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Rotatio clone</title>
<script>
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)

function init(){
	W=480;
	H=640;
	R=15;
	fwd=1;
	speed=1;
	rot=Math.random()*2*Math.PI;
	podx=W/2;
	pody=H/2;

	walldir=0;
	walladv=0;
	wallspeed=1;
	wallgate=0.5;

	gatewidth=100;

	cvs=document.getElementById("cvs");
	ctx=cvs.getContext("2d");
	cvs.width=W;
	cvs.height=H;

	ctx.fillStyle="#fff";
	ctx.beginPath();
	ctx.rect(0,0,W,H);
	ctx.fill();
	// ctx.clearRect(0,0,W,H);

	document.addEventListener("keydown",keydown);
	document.addEventListener("keyup",keyup);
	document.addEventListener("mousedown",keydown);
	document.addEventListener("mouseup",keyup);

	runloop();
}


function runloop(){
	var id=requestAnimationFrame(runloop);

	if(checks()){
		cancelAnimationFrame(id);
		return;
	}
	increment();
	draw();
}


function draw(){
	ctx.clearRect(0,0,W,H);
	ctx.strokeStyle="#000";
	ctx.lineWidth=4;

	var d=Math.sqrt(W*W+H*H),dx=d*Math.cos(rot),dy=d*Math.sin(rot);
	ctx.beginPath();
	ctx.moveTo(-dx+podx,-dy+pody);
	ctx.lineTo(dx+podx,dy+pody);
	ctx.stroke();

	ctx.beginPath();
	ctx.arc(podx,pody,R,0,2*Math.PI,true);
	ctx.stroke();

	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]);
	ctx.moveTo(pgate2[0],pgate2[1]);
	ctx.lineTo(p1[0],p1[1]);
	ctx.stroke();
}

function increment(){
	rot+=0.015;
	podx+=Math.cos(rot)*fwd*speed*2.5;
	pody+=Math.sin(rot)*fwd*speed*2.5;
	walladv+=wallspeed*0.5;
}


function checks(){ //returns true if runloop should stop
	if(checklose()){
		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(){
	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: 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];
	}
	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;
}


function keydown(ev){
	fwd=-1;
}

function keyup(ev){
	fwd=1;
}

function modb(v,m){ //modbound
	if(v<0)return m-(-v%m);
	if(v>=m)return v%m;
}

//shim
var requestAnimationFrame=requestAnimationFrame||webkitRequestAnimationFrame||function(c){return setTimeout(c,1000/60);};
var cancelAnimationFrame=cancelAnimationFrame||webkitCancelAnimationFrame||webkitCancelRequestAnimationFrame||function(i){return clearTimeout(i);};

window.addEventListener("load",init);
</script>
<style>
body{
	background-color:#eee;
}
canvas#cvs{
	border-radius:7px;
	background-color:#fff;
}
</style>
</head>
<body>
<div style="text-align:center">
	<canvas id="cvs"></canvas>
</div>
</body>
</html>