home *** CD-ROM | disk | FTP | other *** search
/ 600 Games / 600games.iso / Acao / penguin-copter.swf / scripts / frame_1 / DoAction.as
Encoding:
Text File  |  2005-09-29  |  14.6 KB  |  540 lines

  1. function setTerrainOn(pos)
  2. {
  3.    var floor = floors[pos];
  4.    var ceiling = ceilings[pos];
  5.    floor.clear();
  6.    ceiling.clear();
  7.    floor.beginGradientFill("linear",[16777215,12303291],[100,100],[0,255],{matrixType:"box",x:0,y:hh / 2,w:ww,h:hh / 2,r:1.5707963});
  8.    ceiling.beginFill(15658751);
  9.    var iStart = -1;
  10.    var iEnd = 21;
  11.    floor.moveTo((iStart - 1) * ww / 20,hh);
  12.    ceiling.moveTo((iStart - 1) * ww / 20,0);
  13.    fp = recentFp[0];
  14.    cp = recentCp[0];
  15.    var i = iStart;
  16.    while(i <= iEnd)
  17.    {
  18.       lfp = fp;
  19.       lcp = cp;
  20.       if(i <= 1)
  21.       {
  22.          fp = recentFp[i + 1];
  23.          cp = recentCp[i + 1];
  24.       }
  25.       else
  26.       {
  27.          cp += rand(rt) * 20 - 10;
  28.          fp += rand(rt) * 20 - 10;
  29.          if(rand(rt) < 0.15)
  30.          {
  31.             if(rand(rt) < 0.5)
  32.             {
  33.                cp += rand(rt) * gap / 4;
  34.             }
  35.             else
  36.             {
  37.                fp -= rand(rt) * gap / 4;
  38.             }
  39.          }
  40.          cp += (hh / 2 - gap / 2 - cp) / 12;
  41.          fp += (hh / 2 + gap / 2 - fp) / 12;
  42.       }
  43.       if(i >= 19)
  44.       {
  45.          recentFp[i - 19] = fp;
  46.          recentCp[i - 19] = cp;
  47.       }
  48.       floor.curveTo((i - 1) * ww / 20,lfp,ww / 20 * (i - 0.5),(fp + lfp) / 2);
  49.       ceiling.curveTo((i - 1) * ww / 20,lcp,ww / 20 * (i - 0.5),(cp + lcp) / 2);
  50.       i++;
  51.    }
  52.    floor.lineTo(ww + 20,hh);
  53.    floor.lineTo(0,hh);
  54.    ceiling.lineTo(ww + 20,0);
  55.    ceiling.lineTo(0,0);
  56.    floor.endFill();
  57.    ceiling.endFill();
  58. }
  59. function rotateRadians(x, y, radians)
  60. {
  61.    var pts = new Array();
  62.    var cosrad = Math.cos(radians);
  63.    var sinrad = Math.sin(radians);
  64.    pts[1] = cosrad * x - sinrad * y;
  65.    pts[2] = cosrad * y + sinrad * x;
  66.    return pts;
  67. }
  68. function rand(o)
  69. {
  70.    o.rfr = (o.rfr + 0.1) % 6.283185307179586;
  71.    o.rfvr += Math.cos(o.rfr) * 0.33924 + 2;
  72.    o.rfvr %= 2.718281828459045;
  73.    return o.rfvr * 1.4142135623730951 % 1;
  74. }
  75. function startMain()
  76. {
  77.    _quality = "HIGH";
  78.    high.removeMovieClip();
  79.    main.removeMovieClip();
  80.    this.attachMovie("main","main",++dep);
  81.    main._x = ww / 2;
  82.    main._y = hh / 2;
  83.    main._alpha = 0;
  84. }
  85. function startHigh()
  86. {
  87.    _quality = "HIGH";
  88.    this.attachMovie("high","high",++dep);
  89.    high._x = ww / 2;
  90.    high._y = hh / 2;
  91.    high.ccr = ccr;
  92.    high.gd = gd;
  93.    high._alpha = 0;
  94. }
  95. function startGame()
  96. {
  97.    _quality = "LOW";
  98.    score.text = "";
  99.    recentFp = new Array();
  100.    recentCp = new Array();
  101.    dead = false;
  102.    brnda = 210;
  103.    nbfr = 0;
  104.    furg = 1;
  105.    if(ghosting)
  106.    {
  107.       ghost.removeMovieClip();
  108.       ghost = this.attachMovie("ghost","ghost",++dep);
  109.       gblades = ghost.propellor.blades;
  110.       deadghost = false;
  111.       ghost._x = ghost.x = 100;
  112.       ghost._y = ghost.y = 100;
  113.       ghost._alpha = 25;
  114.       ghost._rotation = ghost.rotation = 25;
  115.       ghost._xscale = ghost._yscale = 25;
  116.       ghost.vy = 0;
  117.       gcr = 0;
  118.       gts = "";
  119.       gdchp = 0;
  120.    }
  121.    gd = "";
  122.    gft = false;
  123.    ccr = 0;
  124.    lcrp = 0;
  125.    bg._x = bg.ox = bg.x = 0;
  126.    copter.vy = 0;
  127.    copter.rx = 0;
  128.    copter._x = copter.x = copter.ox = 100;
  129.    copter._y = copter.y = copter.oy = 100;
  130.    copter._rotation = copter.rotation = 25;
  131.    var i = 0;
  132.    while(i < bd.length)
  133.    {
  134.       bd[i].b.removeMovieClip();
  135.       i++;
  136.    }
  137.    rt = new Object();
  138.    rb = new Object();
  139.    bd = new Array();
  140.    recentCp = [hh / 2 - gap / 2,hh / 2 - gap / 2,hh / 2 - gap / 2];
  141.    recentFp = [hh / 2 + gap / 2,hh / 2 + gap / 2,hh / 2 + gap / 2];
  142.    setTerrainOn(1);
  143.    floorPos = 2;
  144.    floors[1]._x = ceilings[1]._x = 0;
  145.    floors[2]._x = ceilings[2]._x = ww;
  146.    terrain._x = terrain.x = 0;
  147.    setTerrainOn(2);
  148. }
  149. function checkTerrain()
  150. {
  151.    if(terrain.x < - ww)
  152.    {
  153.       terrain.x += ww;
  154.       terrain._x = terrain.x;
  155.       floors[floorPos]._x = ceilings[floorPos]._x = 0;
  156.       floorPos = 1 + floorPos % 2;
  157.       floors[floorPos]._x = ceilings[floorPos]._x = ww;
  158.       var i = 0;
  159.       while(i < bd.length)
  160.       {
  161.          bd[i].x -= ww;
  162.          bd[i].b._x -= ww;
  163.          i++;
  164.       }
  165.       setTerrainOn(floorPos);
  166.    }
  167. }
  168. ww = 750;
  169. hh = 300;
  170. dm = "flash-gear.com";
  171. var dmp = _root._url.split("/");
  172. var di = 0;
  173. while(di < dmp.length)
  174. {
  175.    if(dmp[di].indexOf(dm) >= 0)
  176.    {
  177.       dm = dmp[di];
  178.       break;
  179.    }
  180.    di++;
  181. }
  182. terrainHolder = this.createEmptyMovieClip("terrainHolder",++dep);
  183. bg = terrainHolder.createEmptyMovieClip("bg",++terrainHolder.dep);
  184. terrain = terrainHolder.createEmptyMovieClip("terrain",++terrainHolder.dep);
  185. bdB = terrain.createEmptyMovieClip("bdB",++terrain.dep);
  186. terrainF = terrain.createEmptyMovieClip("terrainF",++terrain.dep);
  187. this.attachMovie("textbox","score",++dep);
  188. score._x = 5;
  189. score._y = hh - score._height - 5;
  190. score.box.selectable = false;
  191. floors = new Array();
  192. ceilings = new Array();
  193. floorsB = new Array();
  194. ceilingsB = new Array();
  195. i = 1;
  196. while(i <= 2)
  197. {
  198.    f = floors[i] = terrainF.createEmptyMovieClip("floor" + i,++terrainF.dep);
  199.    c = ceilings[i] = terrainF.createEmptyMovieClip("ceiling" + i,++terrainF.dep);
  200.    fm = f.createEmptyMovieClip("floormask",++f.dep);
  201.    fm.beginFill(0);
  202.    fm.lineTo(ww + 1,0);
  203.    fm.lineTo(ww + 1,hh);
  204.    fm.lineTo(0,hh);
  205.    fm.lineTo(0,0);
  206.    fm.endFill();
  207.    f.setMask(fm);
  208.    cm = c.createEmptyMovieClip("ceilingmask",++c.dep);
  209.    cm.beginFill(0);
  210.    cm.lineTo(ww + 1,0);
  211.    cm.lineTo(ww + 1,hh);
  212.    cm.lineTo(0,hh);
  213.    cm.lineTo(0,0);
  214.    cm.endFill();
  215.    c.setMask(cm);
  216.    i++;
  217. }
  218. gap = hh - 32;
  219. copter = this.attachMovie("copter","copter",++dep);
  220. blades = copter.propellor.blades;
  221. copter._xscale = copter._yscale = 25;
  222. i = 0;
  223. while(i <= hb)
  224. {
  225.    hpts[i] = rotateRadians(hx[i],hy[i],copter.rotation * 3.1415926 / 180);
  226.    i++;
  227. }
  228. hx = [36,-145,-100,-80,-60,-40,-28,30,-70];
  229. hy = [4,-6.5,-6.8,-6.8,-6.8,-6.8,-33,-34,-34];
  230. hpts = new Array();
  231. hb = hx.length - 1;
  232. i = 0;
  233. while(i <= hb)
  234. {
  235.    hx[i] = hx[i] * copter._xscale / 100;
  236.    hy[i] = hy[i] * copter._yscale / 100;
  237.    i++;
  238. }
  239. this.onMouseDown = function()
  240. {
  241.    this.mouseIsDown = true;
  242. };
  243. this.onMouseUp = function()
  244. {
  245.    this.mouseIsDown = false;
  246. };
  247. pg.y = pg._y;
  248. startGame();
  249. dead = true;
  250. startMain();
  251. this.onEnterFrame = function()
  252. {
  253.    if(main._alpha < 100)
  254.    {
  255.       main._alpha += lcr / 2;
  256.       if(main._alpha > 100)
  257.       {
  258.          main._alpha = 100;
  259.       }
  260.    }
  261.    if(high._alpha < 100)
  262.    {
  263.       high._alpha += lcr / 2;
  264.       if(high._alpha > 100)
  265.       {
  266.          high._alpha = 100;
  267.       }
  268.    }
  269.    t = getTimer();
  270.    if(!gft)
  271.    {
  272.       gft = true;
  273.       lcrt = t;
  274.    }
  275.    else
  276.    {
  277.       lcrp += (t - lcrt) * 0.12;
  278.       lcrt = t;
  279.       lcr = 0;
  280.       if(++furg == 2)
  281.       {
  282.          lcr = Math.min(40,Math.floor(lcrp));
  283.          furg = 0;
  284.          if(!dead)
  285.          {
  286.             ccr += lcr;
  287.             score.text = "Flight length: " + Math.ceil(ccr / 120) + " seconds";
  288.          }
  289.       }
  290.       if(!dead)
  291.       {
  292.          while(ccr > nbfr)
  293.          {
  294.             if(brnda > 80)
  295.             {
  296.                brnda--;
  297.             }
  298.             var tcr = ccr - nbfr;
  299.             nbfr += Math.ceil(rand(rb) * brnda) + brnda;
  300.             var o = new Object();
  301.             o.b = bdB.attachMovie("penguin","bd" + ++bdc,++bdB.dep);
  302.             o.b._height = 30 + rand(rb) * 10;
  303.             o.b._xscale = o.b._yscale;
  304.             o.vx = 1 + Math.min(3,ccr * 0.0005) - o.b._height / 40;
  305.             o.b._x = o.x = ww * 2 + 64 - terrain.x;
  306.             o.b._y = o.y = hh / 2 + (rand(rb) * gap - gap / 2) / 2;
  307.             o.b._rotation = - (40 - o.b._yscale);
  308.             bd[bd.length] = o;
  309.          }
  310.       }
  311.       if(lcr > 0)
  312.       {
  313.          lcrp -= lcr;
  314.          if(!dead)
  315.          {
  316.             if(mouseIsDown)
  317.             {
  318.                var dch = String.fromCharCode(2);
  319.             }
  320.             else
  321.             {
  322.                var dch = String.fromCharCode(1);
  323.             }
  324.             var dlcr = lcr;
  325.             while(dlcr > 0)
  326.             {
  327.                var dmv = Math.min(dlcr,120);
  328.                gd += dch + String.fromCharCode(dmv);
  329.                dlcr -= dmv;
  330.             }
  331.             if(mouseIsDown and copter.vy > -0.75)
  332.             {
  333.                copter.vy -= 0.015 * lcr;
  334.                if(copter.vy < -0.75)
  335.                {
  336.                   copter.vy = -0.75;
  337.                }
  338.             }
  339.             else if(!mouseIsDown and copter.vy < 0.6)
  340.             {
  341.                copter.vy += 0.01 * lcr;
  342.                if(copter.vy > 0.6)
  343.                {
  344.                   copter.vy = 0.6;
  345.                }
  346.             }
  347.             copter.oy = copter.y;
  348.             copter.y += copter.vy * lcr;
  349.             copter._y = copter.y;
  350.             if(ghosting and !deadghost)
  351.             {
  352.                gblades._rotation += -41;
  353.                while(true)
  354.                {
  355.                   if(gts != "")
  356.                   {
  357.                      gmd = gts.charCodeAt(0) == 2;
  358.                      gcl = gts.charCodeAt(1);
  359.                      gts = "";
  360.                   }
  361.                   else
  362.                   {
  363.                      if(gdchp >= gdbp.length)
  364.                      {
  365.                         deadghost = true;
  366.                         break;
  367.                      }
  368.                      gmd = gdbp.charCodeAt(gdchp) == 2;
  369.                      gcl = gdbp.charCodeAt(gdchp + 1);
  370.                      gdchp += 2;
  371.                   }
  372.                   m = Math.min(gcl,ccr - gcr);
  373.                   if(gmd and ghost.vy > -0.75)
  374.                   {
  375.                      ghost.vy -= 0.015 * m;
  376.                      if(ghost.vy < -0.75)
  377.                      {
  378.                         ghost.vy = -0.75;
  379.                      }
  380.                   }
  381.                   else if(!gmd and ghost.vy < 0.6)
  382.                   {
  383.                      ghost.vy += 0.01 * m;
  384.                      if(ghost.vy > 0.6)
  385.                      {
  386.                         ghost.vy = 0.6;
  387.                      }
  388.                   }
  389.                   ghost.y += ghost.vy * m;
  390.                   l = 1;
  391.                   while(l <= m)
  392.                   {
  393.                      ghost.rotation += ((!gmd ? 20 : 0) - ghost.rotation) * 0.08;
  394.                      l++;
  395.                   }
  396.                   gcr += m;
  397.                   if(gcr == ccr)
  398.                   {
  399.                      if(gcl > ccr - (gcr - m))
  400.                      {
  401.                         gts = String.fromCharCode(!gmd ? 1 : 2) + String.fromCharCode(gcl - (ccr - (gcr - m)));
  402.                      }
  403.                      break;
  404.                   }
  405.                }
  406.                ghost._y = ghost.y;
  407.                ghost._rotation = ghost.rotation;
  408.             }
  409.             else if(deadghost)
  410.             {
  411.                ghost.x -= copter.vx;
  412.                ghost._x = ghost.x;
  413.             }
  414.             l = 1;
  415.             while(l <= lcr)
  416.             {
  417.                copter.rotation += ((!mouseIsDown ? 20 : 0) - copter.rotation) * 0.08;
  418.                l++;
  419.             }
  420.             i = 0;
  421.             while(i <= hb)
  422.             {
  423.                hpts[i] = rotateRadians(hx[i],hy[i],copter.rotation * 3.1415926 / 180);
  424.                i++;
  425.             }
  426.             copter._rotation = copter.rotation;
  427.             blades._rotation += -41;
  428.             copter.lrx = copter.rx;
  429.             copter.rx = ccr + 0.00025 * Math.pow(ccr,2);
  430.             copter.vx = copter.rx - copter.lrx;
  431.             bg.ox = bg.x;
  432.             bg.x -= copter.vx;
  433.             bg._x = bg.x;
  434.             terrain.x -= copter.vx;
  435.             terrain._x = terrain.x;
  436.             checkTerrain();
  437.          }
  438.          else
  439.          {
  440.             copter.vx *= Math.pow(0.99,lcr);
  441.             copter.vy *= Math.pow(0.88,lcr);
  442.             copter.x -= copter.vx;
  443.             copter._x = copter.x;
  444.             terrain.x -= copter.vx;
  445.             terrain._x = terrain.x;
  446.             bg.x -= copter.vx;
  447.             bg._x = bg.x;
  448.             i = 0;
  449.             while(i < bd.length)
  450.             {
  451.                if(bd[i].falling)
  452.                {
  453.                   bd[i].vy += lcr / 30;
  454.                   bd[i].b._y += bd[i].vy;
  455.                }
  456.                i++;
  457.             }
  458.             checkTerrain();
  459.          }
  460.          i = bd.length - 1;
  461.          while(i >= 0)
  462.          {
  463.             var o = bd[i];
  464.             o.x += o.vx * lcr;
  465.             o.b._x = o.x;
  466.             if(o.x < 0)
  467.             {
  468.                o.b.removeMovieClip();
  469.                bd.splice(i,1);
  470.             }
  471.             i--;
  472.          }
  473.       }
  474.       if(furg == 1)
  475.       {
  476.          if(!dead)
  477.          {
  478.             var d = Math.sqrt(Math.pow(copter.y - copter.oy,2) + Math.pow(bg.x - bg.ox,2));
  479.             var tpt = new Array();
  480.             j = 0;
  481.             while(j <= d)
  482.             {
  483.                if(j > d)
  484.                {
  485.                   j = d;
  486.                }
  487.                var v = j / d;
  488.                var xv = copter.x - (bg.ox - bg.x) * (1 - v);
  489.                var yv = copter.oy + (copter.y - copter.oy) * v;
  490.                i = 0;
  491.                while(i <= hb)
  492.                {
  493.                   var pts = hpts[i];
  494.                   if(terrainHolder.hitTest(xv + pts[1],yv + pts[2],true) or yv + pts[2] < 0 or yv + pts[2] > hh)
  495.                   {
  496.                      i = 0;
  497.                      while(i < bd.length)
  498.                      {
  499.                         if(bd[i].b.hitTest(xv + pts[1],yv + pts[2],true))
  500.                         {
  501.                            bd[i].falling = true;
  502.                            bd[i].vx *= 1.5;
  503.                         }
  504.                         i++;
  505.                      }
  506.                      dead = true;
  507.                      score.text = "Flight length: " + Math.floor(1000 * ccr / 120) / 1000 + " seconds";
  508.                      if(ghosting)
  509.                      {
  510.                         ghost.removeMovieClip();
  511.                      }
  512.                      vv = new LoadVars();
  513.                      vv.c = "c";
  514.                      ccr -= lcr;
  515.                      vv.l = ccr;
  516.                      pv = new LoadVars();
  517.                      pv.onLoad = function()
  518.                      {
  519.                         if(this.ih == "1")
  520.                         {
  521.                            startHigh();
  522.                         }
  523.                         else
  524.                         {
  525.                            startMain();
  526.                         }
  527.                      };
  528.                      vv.sendAndLoad("http://" + _root.dm + "/copter/copter.php",pv);
  529.                      j = d;
  530.                      break;
  531.                   }
  532.                   i++;
  533.                }
  534.                j += 10;
  535.             }
  536.          }
  537.       }
  538.    }
  539. };
  540.