Advertisement
Guest User

PolyGravity JS - level loader

a guest
Oct 18th, 2012
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /** loadLevel(data:Array): Level
  2.  * Loads level (set of shapes with bound Canvases) from an array.
  3.  */
  4. function loadLevel(d) {
  5.     var L, o, q, i, n, p = 5, l = d.length, vx, vy, qx, qy;
  6.     L = { };
  7.     L.c = d.slice(0, 5); // get level colours from data
  8.     L.o = []; // shapes in level
  9.     L.l = []; // lines in level
  10.     // sample player values:
  11.     P = { x: 100, y: -200, r: 16, vd: hpi, vv: 0, vx: 0, vy: 0, va: 0.7, vf: 0.3, vm: 5 };
  12.     while (p < l) {
  13.         switch (v()) {
  14.         case 1:
  15.             o = {
  16.                 c: v(), // color index
  17.                 d: v(), // depth
  18.                 t: v(), // type
  19.                 n: v(), x: [], y: [], d: [] // vertexes
  20.             };
  21.             // skip first two values (not used)
  22.             v(); v();
  23.             // initial bounds (set to min/max integer values):
  24.             o.L = o.T = 0x7FFFFFFF;
  25.             o.R = o.B = -0x7FFFFFFF;
  26.             // load points into shape:
  27.             n = o.n;
  28.             for (i = 1; i < n; i++) {
  29.                 // bounds:
  30.                 o.L = min(o.L, vx = v());
  31.                 o.T = min(o.T, vy = v());
  32.                 o.R = max(o.R, vx);
  33.                 o.B = max(o.B, vy);
  34.                 o.x.push(vx);
  35.                 o.y.push(vy);
  36.             }
  37.             // re-add first points for easier iterating:
  38.             o.x.push(o.x[0]);
  39.             o.y.push(o.y[0]);
  40.             // extend bounding box a bit:
  41.             o.L -= 5; o.R += 5; o.T -= 5; o.B += 5;
  42.             // create canvas for shape:
  43.             q = (o.g = cn(o.R - o.L, o.B - o.T)).c;
  44.             // debugging: draw bounding box of shape
  45.             q.strokeStyle = o.t == 1 ? 'blue' : o.t == 2 ? 'red' : o.t == 3 ? 'yellow' : 'white';
  46.             q.strokeRect(0, 0, o.g.width, o.g.height);
  47.             // draw shape in canvas:
  48.             q.fillStyle = hexc(L.c[o.c]);
  49.             // draw shape into it's canvas:
  50.             q.moveTo(o.x[0], o.y[0]);
  51.             q.beginPath();
  52.             for (i = 0; i < n; i++) {
  53.                 vx = o.x[i]; qx = o.x[i + 1];
  54.                 vy = o.y[i]; qy = o.y[i + 1];
  55.                 if (i) q.lineTo(vx - o.L, vy - o.T);
  56.                 o.d.push([vx, vy, qx, qy, atan2(qy - vy, qx - vx)]);
  57.             }
  58.             q.closePath();
  59.             q.fill();
  60.             q.stroke();
  61.             // push shape into level list
  62.             L.o.push(o);
  63.             break;
  64.         }
  65.     }
  66.     return L;
  67.     // inner function, returns next data element
  68.     function v() { return d[p++] }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement