summaryrefslogtreecommitdiff
path: root/index.html
blob: 073c73cab69a13ffd65a2e4308226c1c7beca142 (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
<!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;
//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;

	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);

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


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 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);
	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;
}

function checks(){
	if(checklose()){
		alert("You lost!");
		return true;
	}
}

function checklose(){
	if(podx-R<=0||podx+R>=W-1||pody-R<=0||pody+R>=H-1)return true;
	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;
	}
	return false;
}


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:4px;
	background-color:#fff;
}
</style>
</head>
<body>
<div style="text-align:center">
	<canvas id="cvs"></canvas>
</div>
</body>
</html>