home *** CD-ROM | disk | FTP | other *** search
/ 600 Games / 600games.iso / Acao / germ_roundup.swf / scripts / __Packages / Player.as < prev    next >
Encoding:
Text File  |  2007-03-20  |  11.2 KB  |  433 lines

  1. class Player extends smashing.Renderable
  2. {
  3.    var oBundle;
  4.    var mcInvul;
  5.    var upThrust = 1700;
  6.    var sideThrust = 300;
  7.    var r = 25;
  8.    var xm = 0;
  9.    var ym = 0;
  10.    var ySpeedLimit = 500;
  11.    var onSurface = false;
  12.    var nHurtFlash = 0.1;
  13.    var nHurtTime = 2;
  14.    var bHurt = false;
  15.    var cGerms = 0;
  16.    var facing = 1;
  17.    var floorMod = 50;
  18.    var germScore = 100;
  19.    var nInvul = 5;
  20.    var cInvul = 0;
  21.    var bInvul = false;
  22.    var nSpeed = 0;
  23.    var nSpeedBoost = 150;
  24.    var nSpeedTime = 5;
  25.    var cSpeedTime = 0;
  26.    var nCrouchMod = 0;
  27.    var nIdle = 3;
  28.    var cIdle = 0;
  29.    var sIdle = "";
  30.    function Player()
  31.    {
  32.       var _loc1_ = this;
  33.       super();
  34.       _loc1_.update = _loc1_.updateGround;
  35.       _loc1_.floorMod += _loc1_.r;
  36.       smashing.Menu.watchProp(_loc1_,"cGerms","text");
  37.       _loc1_.cGerms = 0;
  38.       _loc1_.aIdle = ["wink","dance","foot tap"];
  39.    }
  40.    function get germs()
  41.    {
  42.       return this.cGerms;
  43.    }
  44.    function updateGround(nElapsed)
  45.    {
  46.       var _loc1_ = this;
  47.       var nXm;
  48.       var sState = "stand";
  49.       var bCrouch = false;
  50.       if(_loc1_.nSpeed == 1)
  51.       {
  52.          _loc1_.cSpeedTime += nElapsed;
  53.          nXm = 0;
  54.          if(Key.isDown(39))
  55.          {
  56.             nXm += nElapsed * (_loc1_.sideThrust + _loc1_.nSpeedBoost);
  57.             _loc1_.faceRight();
  58.             sState = "run";
  59.          }
  60.          if(Key.isDown(37))
  61.          {
  62.             nXm -= nElapsed * (_loc1_.sideThrust + _loc1_.nSpeedBoost);
  63.             _loc1_.faceLeft();
  64.             sState = "run";
  65.          }
  66.          if(_loc1_.cSpeedTime >= _loc1_.nSpeedTime)
  67.          {
  68.             _loc1_.unSpeed();
  69.          }
  70.       }
  71.       else
  72.       {
  73.          nXm = 0;
  74.          if(Key.isDown(39))
  75.          {
  76.             nXm += nElapsed * _loc1_.sideThrust;
  77.             _loc1_.faceRight();
  78.             sState = "run";
  79.          }
  80.          if(Key.isDown(37))
  81.          {
  82.             nXm -= nElapsed * _loc1_.sideThrust;
  83.             _loc1_.faceLeft();
  84.             sState = "run";
  85.          }
  86.       }
  87.       if(Key.isDown(38))
  88.       {
  89.          _loc1_.ym -= _loc1_.upThrust * nElapsed;
  90.          _loc1_.onSurface = false;
  91.       }
  92.       if(Key.isDown(40))
  93.       {
  94.          _loc1_.ym += _loc1_.upThrust * nElapsed;
  95.          _loc1_.onSurface = false;
  96.          bCrouch = true;
  97.       }
  98.       _loc1_.ym += smashing.Phys.gr * nElapsed;
  99.       if(Math.abs(_loc1_.ym) > _loc1_.ySpeedLimit)
  100.       {
  101.          _loc1_.ym = _loc1_.ySpeedLimit * (_loc1_.ym / Math.abs(_loc1_.ym));
  102.       }
  103.       var clips = _loc1_.oGerms.oHandler.clips;
  104.       var l = clips.length;
  105.       var mcC;
  106.       var r2;
  107.       var dist = new smashing.Point(0,0);
  108.       var i = 0;
  109.       while(i < l)
  110.       {
  111.          mcC = clips[i];
  112.          if(mcC != null)
  113.          {
  114.             if(mcC.grabbable == true)
  115.             {
  116.                dist.x = mcC.x - _loc1_.x;
  117.                dist.y = mcC.y - _loc1_.y;
  118.                r2 = Math.pow(_loc1_.r + mcC.r,2);
  119.                if(dist.dot(dist) < r2)
  120.                {
  121.                   mcC.react(_loc1_);
  122.                }
  123.             }
  124.          }
  125.          i++;
  126.       }
  127.       if(_loc1_.bInvul == false)
  128.       {
  129.          if(_loc1_.bHurt == false)
  130.          {
  131.             clips = _loc1_.oCitizens.oHandler.clips;
  132.             l = clips.length;
  133.             var mcC;
  134.             i = 0;
  135.             while(i < l)
  136.             {
  137.                mcC = clips[i];
  138.                if(mcC != null)
  139.                {
  140.                   if(mcC.bDead == false)
  141.                   {
  142.                      if(mcC.collisionTest(_loc1_) == true)
  143.                      {
  144.                         mcC.react(_loc1_,nElapsed);
  145.                      }
  146.                   }
  147.                }
  148.                i++;
  149.             }
  150.          }
  151.       }
  152.       else
  153.       {
  154.          _loc1_.cInvul += nElapsed;
  155.          if(_loc1_.cInvul >= _loc1_.nInvul)
  156.          {
  157.             _loc1_.unInvulnerable();
  158.          }
  159.       }
  160.       _loc1_.xm -= smashing.Phys.drag(_loc1_.xm,nElapsed);
  161.       if(_loc1_.xm > -20)
  162.       {
  163.          _loc1_.xm = 0;
  164.       }
  165.       nXm += _loc1_.xm * nElapsed;
  166.       var nYm = _loc1_.ym * nElapsed;
  167.       _loc1_.shadow._x = _loc1_._x + nXm;
  168.       _loc1_.shadow._y = _loc1_._y + nYm;
  169.       var m = new smashing.Point(nXm,nYm);
  170.       var p = new smashing.Point(_loc1_.x,_loc1_.y);
  171.       clips = _loc1_.oBuildings.oHandler.clips;
  172.       l = clips.length;
  173.       var l2;
  174.       var aLines;
  175.       var aC = [];
  176.       var _loc2_ = undefined;
  177.       var bestResult = null;
  178.       var aDebug = [];
  179.       i = 0;
  180.       while(i < l)
  181.       {
  182.          mcC = clips[i];
  183.          if(mcC.hasObstacle == true)
  184.          {
  185.             if(mcC.hitTest(_loc1_) == true || mcC.hitTest(_loc1_.shadow) == true)
  186.             {
  187.                aLines = mcC.aLines;
  188.                l2 = aLines.length;
  189.                var _loc3_ = 0;
  190.                while(_loc3_ < l2)
  191.                {
  192.                   if(bCrouch == false)
  193.                   {
  194.                      _loc2_ = aLines[_loc3_].collisionPointSwept(p,m,_loc1_.r);
  195.                   }
  196.                   else if(!smashing.Misc.isHoriz(aLines[_loc3_]))
  197.                   {
  198.                      _loc2_ = aLines[_loc3_].collisionPointSwept(p,m,_loc1_.r);
  199.                   }
  200.                   aDebug.push(_loc2_);
  201.                   if(bestResult == null && _loc2_ != null)
  202.                   {
  203.                      bestResult = _loc2_;
  204.                   }
  205.                   else if(_loc2_.distSqu < bestResult.distSqu && _loc2_ != null)
  206.                   {
  207.                      bestResult = _loc2_;
  208.                   }
  209.                   _loc3_ = _loc3_ + 1;
  210.                }
  211.             }
  212.          }
  213.          i++;
  214.       }
  215.       var bSurface = false;
  216.       if(bestResult != null)
  217.       {
  218.          var actDist = Math.sqrt(bestResult.distSqu);
  219.          m.length -= actDist;
  220.          var normal = bestResult.gizmo.lineNormal;
  221.          _loc1_.onSurface = true;
  222.          bSurface = true;
  223.          var pHit = new smashing.Point(bestResult.hit.x,bestResult.hit.y);
  224.          _loc1_.x = pHit.x;
  225.          _loc1_.y = pHit.y;
  226.          p = new smashing.Point(_loc1_.x,_loc1_.y);
  227.          m = normal.multiply(m.dot(normal));
  228.          if(m.y == 0)
  229.          {
  230.             _loc1_.ym = 0;
  231.          }
  232.          if(m.x == 0)
  233.          {
  234.             _loc1_.xm = 0;
  235.          }
  236.       }
  237.       p = new smashing.Point(_loc1_.x,_loc1_.y);
  238.       var myFloor = BuildingHandler.floor - _loc1_.floorMod;
  239.       if(_loc1_.y + m.y > myFloor)
  240.       {
  241.          _loc1_.y = myFloor;
  242.          _loc1_.ym = 0;
  243.          m.y = 0;
  244.          _loc1_.onSurface = true;
  245.          bSurface = true;
  246.          if(bCrouch == true)
  247.          {
  248.             sState = "crouch";
  249.          }
  250.       }
  251.       if(bSurface == false)
  252.       {
  253.          sState = "fly";
  254.       }
  255.       var myCeil = BuildingHandler.ceiling + _loc1_.r;
  256.       if(_loc1_.y + m.y < myCeil)
  257.       {
  258.          _loc1_.ym = 0;
  259.          m.y = 0;
  260.          _loc1_.y = myCeil;
  261.       }
  262.       var myLeft = smashing.Viewport.halfWidth - _loc1_.r;
  263.       if(_loc1_.x + m.x - smashing.Viewport.x < - myLeft)
  264.       {
  265.          _loc1_.x = smashing.Viewport.x - myLeft;
  266.          m.x = 0;
  267.          _loc1_.xm = 0;
  268.       }
  269.       if(_loc1_.x + m.x - smashing.Viewport.x > myLeft)
  270.       {
  271.          _loc1_.x = smashing.Viewport.x + myLeft;
  272.          m.x = 0;
  273.          _loc1_.xm = 0;
  274.       }
  275.       _loc1_.y += m.y;
  276.       _loc1_.x += m.x;
  277.       if(_loc1_.bHurt == true)
  278.       {
  279.          if(_loc1_.cHurt < _loc1_.nHurtTime / 2)
  280.          {
  281.             sState = "hit";
  282.          }
  283.       }
  284.       if(sState == "stand")
  285.       {
  286.          _loc1_.cIdle += nElapsed;
  287.          if(_loc1_.cIdle >= _loc1_.nIdle)
  288.          {
  289.             if(_loc1_.sIdle == "")
  290.             {
  291.                _loc1_.sIdle = _loc1_.aIdle[Math.floor(Math.random() * _loc1_.aIdle.length)];
  292.             }
  293.             sState = _loc1_.sIdle;
  294.          }
  295.       }
  296.       else
  297.       {
  298.          _loc1_.cIdle = 0;
  299.          _loc1_.sIdle = "";
  300.       }
  301.       _loc1_.a.gotoAndStop(sState);
  302.       if(_loc1_.nSpeed == 1)
  303.       {
  304.          _loc1_.a.a.blur._visible = true;
  305.       }
  306.       else
  307.       {
  308.          _loc1_.a.a.blur._visible = false;
  309.       }
  310.    }
  311.    function updateAir(nElapsed)
  312.    {
  313.    }
  314.    function setBuilding(o, c, g)
  315.    {
  316.       var _loc1_ = this;
  317.       _loc1_.oBuildings = o;
  318.       _loc1_.oCitizens = c;
  319.       _loc1_.oGerms = g;
  320.    }
  321.    function setShadow(mc)
  322.    {
  323.       var _loc1_ = this;
  324.       _loc1_.shadow = mc;
  325.       _loc1_.shadow._width = _loc1_.r * 2;
  326.       _loc1_.shadow._height = _loc1_.r * 2;
  327.    }
  328.    function setBundle(o)
  329.    {
  330.       this.oBundle = o;
  331.    }
  332.    function render()
  333.    {
  334.       var _loc2_ = this;
  335.       var _loc1_ = smashing.Viewport.getPos(_loc2_);
  336.       _loc2_._x = _loc1_.x + smashing.Viewport.centerX;
  337.       _loc2_._y = _loc1_.y + smashing.Viewport.centerY;
  338.    }
  339.    function hurt()
  340.    {
  341.       var _loc1_ = this;
  342.       _global.oSound.playSnd("hit");
  343.       var _loc2_ = new smashing.Point(-3,-2);
  344.       _loc2_.length = 700;
  345.       _loc1_.xm = _loc2_.x;
  346.       _loc1_.ym = _loc2_.y;
  347.       _loc1_.bHurt = true;
  348.       _loc1_.nStartHurt = getTimer();
  349.       _loc1_.onEnterFrame = function()
  350.       {
  351.          var _loc1_ = this;
  352.          _loc1_.cHurt = (getTimer() - _loc1_.nStartHurt) / 1000;
  353.          if(_loc1_.cHurt >= _loc1_.nHurtTime)
  354.          {
  355.             _loc1_.bHurt = false;
  356.             delete _loc1_.onEnterFrame;
  357.          }
  358.       };
  359.    }
  360.    function invulnerable()
  361.    {
  362.       this.bInvul = true;
  363.       this.mcInvul._visible = true;
  364.    }
  365.    function unInvulnerable()
  366.    {
  367.       var _loc1_ = this;
  368.       _loc1_.bInvul = false;
  369.       _loc1_.cInvul = 0;
  370.       _loc1_.mcInvul._visible = false;
  371.    }
  372.    function speed()
  373.    {
  374.       this.nSpeed = 1;
  375.    }
  376.    function unSpeed()
  377.    {
  378.       this.nSpeed = 0;
  379.       this.cSpeedTime = 0;
  380.    }
  381.    function getGerm(g)
  382.    {
  383.       var _loc1_ = this;
  384.       _global.oSound.playSnd("getGerm");
  385.       g.handler.removeClip(g);
  386.       _loc1_.cGerms = _loc1_.cGerms + 1;
  387.       _loc1_.oBundle.setGerms(_loc1_.cGerms);
  388.       MaggieWorld.addScore(_loc1_.germScore);
  389.       _loc1_.a.a.a.head.gotoAndPlay("gather");
  390.    }
  391.    function removeGerm()
  392.    {
  393.       var _loc1_ = this;
  394.       _loc1_.cGerms = _loc1_.cGerms - 1;
  395.       if(_loc1_.cGerms < 0)
  396.       {
  397.          _loc1_.cGerms = 0;
  398.       }
  399.       else
  400.       {
  401.          _global.oSound.playSnd("loseGerm");
  402.          _loc1_.oGerms.addGerm(_loc1_.oBundle.x,_loc1_.oBundle.y,"mcFlyAwayGerm");
  403.       }
  404.       _loc1_.oBundle.setGerms(_loc1_.cGerms);
  405.    }
  406.    function reset()
  407.    {
  408.       var _loc1_ = this;
  409.       _loc1_.a.gotoAndStop("stand");
  410.       _loc1_.x = smashing.Viewport.x - _loc1_.oWorld.playerOffset;
  411.       _loc1_.y = BuildingHandler.floor - _loc1_.r * 3;
  412.       _loc1_.cGerms = 0;
  413.       _loc1_.oBundle.setGerms(_loc1_.cGerms);
  414.       _loc1_.bHurt = false;
  415.       delete _loc1_.onEnterFrame;
  416.       _loc1_._visible = true;
  417.       _loc1_.xm = 0;
  418.       _loc1_.ym = 0;
  419.       _loc1_.faceRight();
  420.       _loc1_.render();
  421.    }
  422.    function faceLeft()
  423.    {
  424.       this.gotoAndStop("left");
  425.       this.facing = -1;
  426.    }
  427.    function faceRight()
  428.    {
  429.       this.gotoAndStop("right");
  430.       this.facing = 1;
  431.    }
  432. }
  433.