/** loadLevel(data:Array): Level
* Loads level (set of shapes with bound Canvases) from an array.
*/
function loadLevel(d) {
var L, o, q, i, n, p = 5, l = d.length, vx, vy, qx, qy;
L = { };
L.c = d.slice(0, 5); // get level colours from data
L.o = []; // shapes in level
L.l = []; // lines in level
// sample player values:
P = { x: 100, y: -200, r: 16, vd: hpi, vv: 0, vx: 0, vy: 0, va: 0.7, vf: 0.3, vm: 5 };
while (p < l) {
switch (v()) {
case 1:
o = {
c: v(), // color index
d: v(), // depth
t: v(), // type
n: v(), x: [], y: [], d: [] // vertexes
};
// skip first two values (not used)
v(); v();
// initial bounds (set to min/max integer values):
o.L = o.T = 0x7FFFFFFF;
o.R = o.B = -0x7FFFFFFF;
// load points into shape:
n = o.n;
for (i = 1; i < n; i++) {
// bounds:
o.L = min(o.L, vx = v());
o.T = min(o.T, vy = v());
o.R = max(o.R, vx);
o.B = max(o.B, vy);
o.x.push(vx);
o.y.push(vy);
}
// re-add first points for easier iterating:
o.x.push(o.x[0]);
o.y.push(o.y[0]);
// extend bounding box a bit:
o.L -= 5; o.R += 5; o.T -= 5; o.B += 5;
// create canvas for shape:
q = (o.g = cn(o.R - o.L, o.B - o.T)).c;
// debugging: draw bounding box of shape
q.strokeStyle = o.t == 1 ? 'blue' : o.t == 2 ? 'red' : o.t == 3 ? 'yellow' : 'white';
q.strokeRect(0, 0, o.g.width, o.g.height);
// draw shape in canvas:
q.fillStyle = hexc(L.c[o.c]);
// draw shape into it's canvas:
q.moveTo(o.x[0], o.y[0]);
q.beginPath();
for (i = 0; i < n; i++) {
vx = o.x[i]; qx = o.x[i + 1];
vy = o.y[i]; qy = o.y[i + 1];
if (i) q.lineTo(vx - o.L, vy - o.T);
o.d.push([vx, vy, qx, qy, atan2(qy - vy, qx - vx)]);
}
q.closePath();
q.fill();
q.stroke();
// push shape into level list
L.o.push(o);
break;
}
}
return L;
// inner function, returns next data element
function v() { return d[p++] }
}