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

  1. class Enemy extends Entity
  2. {
  3.    var entity;
  4.    var attacktype;
  5.    var inputlocked;
  6.    var grounded;
  7.    var isboss;
  8.    var inp;
  9.    var animationtick;
  10.    var oldanim;
  11.    var newanim;
  12.    var AIstate;
  13.    var AItimer;
  14.    var HP;
  15.    var dead;
  16.    var isenemy;
  17.    var zv;
  18.    var x;
  19.    var y;
  20.    var AIdestx;
  21.    var AIdesty;
  22.    var xv;
  23.    var yv;
  24.    var z;
  25.    var myname;
  26.    var collide;
  27.    static var ACCELLERATION = 2;
  28.    static var MAX_SPEED = 3;
  29.    static var JUMP_VELOCITY = 10;
  30.    function Enemy(target, entityname, entityart, startx, starty, startz, startsize, collides)
  31.    {
  32.       super(target,entityname,startx,starty,startz,startsize,collides);
  33.       this.entity.gotoAndStop(entityart);
  34.       this.attacktype = entityart;
  35.       this.inputlocked = false;
  36.       this.grounded = true;
  37.       if(entityart == "enemy4")
  38.       {
  39.          this.isboss = true;
  40.       }
  41.       else
  42.       {
  43.          this.isboss = false;
  44.       }
  45.       this.entity.attacking = false;
  46.       this.entity.hurting = false;
  47.       this.inp = "neutral";
  48.       this.animationtick = 0;
  49.       this.oldanim = "null";
  50.       this.newanim = "idle";
  51.       this.AIstate = "idle";
  52.       this.AItimer = random(50);
  53.       this.HP = 100;
  54.       if(this.isboss)
  55.       {
  56.          this.HP = 500;
  57.       }
  58.       this.dead = false;
  59.       this.isenemy = true;
  60.    }
  61.    function update()
  62.    {
  63.       this.AItimer = this.AItimer - 1;
  64.       if(this.AIstate != "falling" && this.AItimer < 0)
  65.       {
  66.          this.AItimer = random(50) + 50;
  67.          var _loc3_ = random(10);
  68.          if(_loc3_ < 7)
  69.          {
  70.             this.AIstate = "attack";
  71.          }
  72.          if(_loc3_ >= 7)
  73.          {
  74.             this.AIstate = "idle";
  75.          }
  76.          if(this.isboss)
  77.          {
  78.             this.AIstate = "attack";
  79.          }
  80.       }
  81.       this.inp = "neutral";
  82.       if(this.AIstate == "stunned")
  83.       {
  84.          if(!this.entity.hurting)
  85.          {
  86.             this.newanim = "stunned";
  87.          }
  88.       }
  89.       if(this.AIstate == "falling")
  90.       {
  91.          if(this.grounded)
  92.          {
  93.             this.newanim = "fallen";
  94.          }
  95.          else if(this.zv >= 0)
  96.          {
  97.             this.newanim = "fallup";
  98.          }
  99.          else
  100.          {
  101.             this.newanim = "falldown";
  102.          }
  103.       }
  104.       var _loc2_ = 100;
  105.       if(this.attacktype == "enemy2")
  106.       {
  107.          _loc2_ = 200;
  108.       }
  109.       if(this.AIstate == "attack")
  110.       {
  111.          if(this.x > GameStage.STAGE.player.getX() && GameStage.STAGE.player.getX() > GameStage.RIGHT_LIMIT - _loc2_)
  112.          {
  113.             this.addinput("left");
  114.          }
  115.          else if(this.x < GameStage.STAGE.player.getX() && GameStage.STAGE.player.getX() < GameStage.LEFT_LIMIT + _loc2_)
  116.          {
  117.             this.addinput("right");
  118.          }
  119.          else if(Math.abs(this.y - GameStage.STAGE.player.getY()) > 50)
  120.          {
  121.             if(this.x - GameStage.STAGE.player.getX() < _loc2_ && this.x - GameStage.STAGE.player.getX() >= 0)
  122.             {
  123.                this.addinput("right");
  124.             }
  125.             if(this.x - GameStage.STAGE.player.getX() > - _loc2_ && this.x - GameStage.STAGE.player.getX() < 0)
  126.             {
  127.                this.addinput("left");
  128.             }
  129.             if(this.x - GameStage.STAGE.player.getX() >= _loc2_)
  130.             {
  131.                this.addinput("right");
  132.                if(this.y < GameStage.STAGE.player.getY())
  133.                {
  134.                   this.addinput("down");
  135.                }
  136.                else
  137.                {
  138.                   this.addinput("up");
  139.                }
  140.             }
  141.             if(this.x - GameStage.STAGE.player.getX() <= - _loc2_)
  142.             {
  143.                this.addinput("left");
  144.                if(this.y < GameStage.STAGE.player.getY())
  145.                {
  146.                   this.addinput("down");
  147.                }
  148.                else
  149.                {
  150.                   this.addinput("up");
  151.                }
  152.             }
  153.          }
  154.          else
  155.          {
  156.             if(Math.abs(this.x - GameStage.STAGE.player.getX() + _loc2_) < 20 && Math.abs(this.y - GameStage.STAGE.player.getY()) < 20)
  157.             {
  158.                this.entity._xscale = 100;
  159.                this.addinput("attack");
  160.                this.AIstate = "idle";
  161.             }
  162.             if(Math.abs(this.x - GameStage.STAGE.player.getX() - _loc2_) < 20 && Math.abs(this.y - GameStage.STAGE.player.getY()) < 20)
  163.             {
  164.                this.entity._xscale = -100;
  165.                this.addinput("attack");
  166.                this.AIstate = "idle";
  167.             }
  168.             if(this.x - GameStage.STAGE.player.getX() >= 0 && this.x - GameStage.STAGE.player.getX() < _loc2_)
  169.             {
  170.                this.addinput("right");
  171.             }
  172.             if(this.x - GameStage.STAGE.player.getX() >= _loc2_)
  173.             {
  174.                this.addinput("left");
  175.             }
  176.             if(this.x - GameStage.STAGE.player.getX() >= - _loc2_ && this.x - GameStage.STAGE.player.getX() < 0)
  177.             {
  178.                this.addinput("left");
  179.             }
  180.             if(this.x - GameStage.STAGE.player.getX() < - _loc2_)
  181.             {
  182.                this.addinput("right");
  183.             }
  184.             if(this.y - GameStage.STAGE.player.getY() > 0)
  185.             {
  186.                this.addinput("up");
  187.             }
  188.             else
  189.             {
  190.                this.addinput("down");
  191.             }
  192.          }
  193.       }
  194.       if(this.AIstate == "idle")
  195.       {
  196.          if(random(30) == 1)
  197.          {
  198.             this.AIdestx = this.x - 100 + random(200);
  199.             this.AIdesty = this.y - 100 + random(200);
  200.             if(this.AIdestx < GameStage.LEFT_LIMIT)
  201.             {
  202.                this.AIdestx = GameStage.LEFT_LIMIT;
  203.             }
  204.             if(this.AIdestx > GameStage.RIGHT_LIMIT)
  205.             {
  206.                this.AIdestx = GameStage.RIGHT_LIMIT;
  207.             }
  208.             if(this.AIdesty < 0)
  209.             {
  210.                this.AIdesty = 0;
  211.             }
  212.             if(this.AIdesty > 150)
  213.             {
  214.                this.AIdesty = 150;
  215.             }
  216.          }
  217.          if(this.AIdestx < this.x + 10)
  218.          {
  219.             this.addinput("left");
  220.          }
  221.          if(this.AIdestx > this.x - 10)
  222.          {
  223.             this.addinput("right");
  224.          }
  225.          if(this.AIdesty < this.y + 10)
  226.          {
  227.             this.addinput("up");
  228.          }
  229.          if(this.AIdesty > this.y - 10)
  230.          {
  231.             this.addinput("down");
  232.          }
  233.       }
  234.       this.takeaction();
  235.       this.updateanimation();
  236.       this.x += this.xv * GameStage.TIME_STEP;
  237.       this.y += this.yv * GameStage.TIME_STEP;
  238.       this.z += this.zv * GameStage.TIME_STEP;
  239.       this.entity._x = this.x;
  240.       this.entity._y = GameStage.GROUND_LEVEL + this.y / 3 - this.z;
  241.       this.animationtick += GameStage.TIME_STEP;
  242.       if(this.animationtick >= 1)
  243.       {
  244.          this.animationtick -= 1;
  245.          this.entity.anims.nextFrame();
  246.       }
  247.       if(!this.dead)
  248.       {
  249.          EntityManager.MANAGER.entityCollide(this.myname,this);
  250.       }
  251.       this.entity.x = this.x;
  252.       this.entity.y = this.y;
  253.       this.entity.z = this.z;
  254.    }
  255.    function addinput(newinp)
  256.    {
  257.       switch(this.inp)
  258.       {
  259.          case "default":
  260.             this.inp = newinp;
  261.             break;
  262.          case "upleft":
  263.             if(newinp == "right")
  264.             {
  265.                this.inp = "up";
  266.             }
  267.             if(newinp == "down")
  268.             {
  269.                this.inp = "left";
  270.             }
  271.             if(newinp == "jump")
  272.             {
  273.                this.inp = "jump";
  274.             }
  275.             if(newinp == "attack")
  276.             {
  277.                this.inp = "attack";
  278.             }
  279.             break;
  280.          case "up":
  281.             if(newinp == "left")
  282.             {
  283.                this.inp = "upleft";
  284.             }
  285.             if(newinp == "right")
  286.             {
  287.                this.inp = "upright";
  288.             }
  289.             if(newinp == "down")
  290.             {
  291.                this.inp = "neutral";
  292.             }
  293.             if(newinp == "jump")
  294.             {
  295.                this.inp = "jump";
  296.             }
  297.             if(newinp == "attack")
  298.             {
  299.                this.inp = "attack";
  300.             }
  301.             break;
  302.          case "upright":
  303.             if(newinp == "left")
  304.             {
  305.                this.inp = "up";
  306.             }
  307.             if(newinp == "down")
  308.             {
  309.                this.inp = "right";
  310.             }
  311.             if(newinp == "jump")
  312.             {
  313.                this.inp = "jump";
  314.             }
  315.             if(newinp == "attack")
  316.             {
  317.                this.inp = "attack";
  318.             }
  319.             break;
  320.          case "left":
  321.             if(newinp == "right")
  322.             {
  323.                this.inp = "neutral";
  324.             }
  325.             if(newinp == "up")
  326.             {
  327.                this.inp = "upleft";
  328.             }
  329.             if(newinp == "down")
  330.             {
  331.                this.inp = "downleft";
  332.             }
  333.             if(newinp == "jump")
  334.             {
  335.                this.inp = "jump";
  336.             }
  337.             if(newinp == "attack")
  338.             {
  339.                this.inp = "attack";
  340.             }
  341.             break;
  342.          case "neutral":
  343.             if(newinp == "left")
  344.             {
  345.                this.inp = "left";
  346.             }
  347.             if(newinp == "right")
  348.             {
  349.                this.inp = "right";
  350.             }
  351.             if(newinp == "up")
  352.             {
  353.                this.inp = "up";
  354.             }
  355.             if(newinp == "down")
  356.             {
  357.                this.inp = "down";
  358.             }
  359.             if(newinp == "jump")
  360.             {
  361.                this.inp = "jump";
  362.             }
  363.             if(newinp == "attack")
  364.             {
  365.                this.inp = "attack";
  366.             }
  367.             break;
  368.          case "right":
  369.             if(newinp == "left")
  370.             {
  371.                this.inp = "neutral";
  372.             }
  373.             if(newinp == "up")
  374.             {
  375.                this.inp = "upright";
  376.             }
  377.             if(newinp == "down")
  378.             {
  379.                this.inp = "downright";
  380.             }
  381.             if(newinp == "jump")
  382.             {
  383.                this.inp = "jump";
  384.             }
  385.             if(newinp == "attack")
  386.             {
  387.                this.inp = "attack";
  388.             }
  389.             break;
  390.          case "downleft":
  391.             if(newinp == "right")
  392.             {
  393.                this.inp = "down";
  394.             }
  395.             if(newinp == "up")
  396.             {
  397.                this.inp = "left";
  398.             }
  399.             if(newinp == "jump")
  400.             {
  401.                this.inp = "jump";
  402.             }
  403.             if(newinp == "attack")
  404.             {
  405.                this.inp = "attack";
  406.             }
  407.             break;
  408.          case "down":
  409.             if(newinp == "left")
  410.             {
  411.                this.inp = "downleft";
  412.             }
  413.             if(newinp == "right")
  414.             {
  415.                this.inp = "downright";
  416.             }
  417.             if(newinp == "up")
  418.             {
  419.                this.inp = "neutral";
  420.             }
  421.             if(newinp == "jump")
  422.             {
  423.                this.inp = "jump";
  424.             }
  425.             if(newinp == "attack")
  426.             {
  427.                this.inp = "attack";
  428.             }
  429.             break;
  430.          case "downright":
  431.             if(newinp == "left")
  432.             {
  433.                this.inp = "down";
  434.             }
  435.             if(newinp == "up")
  436.             {
  437.                this.inp = "right";
  438.             }
  439.             if(newinp == "jump")
  440.             {
  441.                this.inp = "jump";
  442.             }
  443.             if(newinp == "attack")
  444.             {
  445.                this.inp = "attack";
  446.             }
  447.             break;
  448.          case "attack":
  449.             if(newinp == "jump")
  450.             {
  451.                this.inp = "jumpattack";
  452.             }
  453.       }
  454.    }
  455.    function takeaction()
  456.    {
  457.       if(this.x < GameStage.LEFT_LIMIT)
  458.       {
  459.          this.inp = "right";
  460.       }
  461.       if(this.x > GameStage.RIGHT_LIMIT)
  462.       {
  463.          this.inp = "left";
  464.       }
  465.       if(this.y < 0)
  466.       {
  467.          this.inp = "down";
  468.       }
  469.       if(this.y > 150)
  470.       {
  471.          this.inp = "up";
  472.       }
  473.       if(!this.inputlocked)
  474.       {
  475.          if(this.inp == "left" || this.inp == "upleft" || this.inp == "downleft")
  476.          {
  477.             if(this.grounded)
  478.             {
  479.                this.xv -= Enemy.ACCELLERATION * GameStage.TIME_STEP;
  480.             }
  481.          }
  482.          if(this.inp == "right" || this.inp == "upright" || this.inp == "downright")
  483.          {
  484.             if(this.grounded)
  485.             {
  486.                this.xv += Enemy.ACCELLERATION * GameStage.TIME_STEP;
  487.             }
  488.          }
  489.          if(this.inp == "upleft" || this.inp == "up" || this.inp == "upright")
  490.          {
  491.             if(this.grounded)
  492.             {
  493.                this.yv -= Enemy.ACCELLERATION * GameStage.TIME_STEP;
  494.             }
  495.          }
  496.          if(this.inp == "downleft" || this.inp == "down" || this.inp == "downright")
  497.          {
  498.             if(this.grounded)
  499.             {
  500.                this.yv += Enemy.ACCELLERATION * GameStage.TIME_STEP;
  501.             }
  502.          }
  503.          else if(this.inp == "jump")
  504.          {
  505.             if(this.grounded)
  506.             {
  507.                this.xv *= 1.5;
  508.                this.grounded = false;
  509.                this.zv = Enemy.JUMP_VELOCITY;
  510.                this.newanim = "jump";
  511.             }
  512.          }
  513.          else if(this.inp == "attack")
  514.          {
  515.             if(this.grounded)
  516.             {
  517.                this.newanim = "attack";
  518.                this.entity.attacking = true;
  519.                this.inputlocked = true;
  520.             }
  521.          }
  522.       }
  523.       else if(!this.entity.attacking && !this.entity.hurting)
  524.       {
  525.          this.inputlocked = false;
  526.       }
  527.       if(this.grounded)
  528.       {
  529.          if(this.xv < - Enemy.MAX_SPEED)
  530.          {
  531.             this.xv = - Enemy.MAX_SPEED;
  532.          }
  533.          if(this.xv > Enemy.MAX_SPEED)
  534.          {
  535.             this.xv = Enemy.MAX_SPEED;
  536.          }
  537.          if(this.yv > Enemy.MAX_SPEED)
  538.          {
  539.             this.yv = Enemy.MAX_SPEED;
  540.          }
  541.          if(this.yv < - Enemy.MAX_SPEED)
  542.          {
  543.             this.yv = - Enemy.MAX_SPEED;
  544.          }
  545.          this.xv *= Math.pow(0.8,GameStage.TIME_STEP);
  546.          this.yv *= Math.pow(0.8,GameStage.TIME_STEP);
  547.          if(this.attacktype == "enemy2" && this.entity.attacking)
  548.          {
  549.             this.xv += Enemy.MAX_SPEED * this.entity._xscale / 70;
  550.          }
  551.       }
  552.       else
  553.       {
  554.          this.zv -= GameStage.GRAVITY * GameStage.TIME_STEP;
  555.          if(this.zv < -25)
  556.          {
  557.             this.zv = -20;
  558.          }
  559.          if(this.zv > 25)
  560.          {
  561.             this.zv = 20;
  562.          }
  563.          if(this.z + this.zv < 0)
  564.          {
  565.             if(this.AIstate == "falling")
  566.             {
  567.                new gamesound("fall",100);
  568.                this.zv = (- this.zv) * 0.6;
  569.                this.z = 0;
  570.                if(this.zv < 7)
  571.                {
  572.                   new gamesound("foof",100);
  573.                   var _loc3_ = EntityManager.MANAGER.addEntity(this.x,this.y,this.z,"explode","explode");
  574.                   _loc3_.entity._xscale = _parent._parent._xscale;
  575.                   this.entity.dead = true;
  576.                }
  577.             }
  578.             else
  579.             {
  580.                this.zv = 0;
  581.                this.z = 0;
  582.                this.grounded = true;
  583.                this.entity.anims.gotoAndStop("idle");
  584.                this.entity.anim = "idle";
  585.                this.inputlocked = false;
  586.             }
  587.             if(this.xv < - Enemy.MAX_SPEED)
  588.             {
  589.                this.xv = - Enemy.MAX_SPEED;
  590.             }
  591.             if(this.xv > Enemy.MAX_SPEED)
  592.             {
  593.                this.xv = Enemy.MAX_SPEED;
  594.             }
  595.             if(this.yv > Enemy.MAX_SPEED)
  596.             {
  597.                this.yv = Enemy.MAX_SPEED;
  598.             }
  599.             if(this.yv < - Enemy.MAX_SPEED)
  600.             {
  601.                this.yv = - Enemy.MAX_SPEED;
  602.             }
  603.          }
  604.       }
  605.    }
  606.    function updateanimation()
  607.    {
  608.       if(this.grounded && !this.entity.attacking && !this.entity.hurting && this.AIstate != "stunned" && this.AIstate != "falling")
  609.       {
  610.          if(this.xv < -2)
  611.          {
  612.             this.entity._xscale = -100;
  613.          }
  614.          if(this.xv > 2)
  615.          {
  616.             this.entity._xscale = 100;
  617.          }
  618.          if(Math.abs(this.xv) < 0.7 && Math.abs(this.yv) < 0.7)
  619.          {
  620.             this.xv = 0;
  621.             this.newanim = "idle";
  622.          }
  623.          else
  624.          {
  625.             this.newanim = "walk";
  626.          }
  627.       }
  628.       if(this.newanim != this.oldanim)
  629.       {
  630.          switch(this.newanim)
  631.          {
  632.             case "idle":
  633.                this.entity.anims.gotoAndStop("idle");
  634.                this.entity.anim = "idle";
  635.                break;
  636.             case "walk":
  637.                this.entity.anims.gotoAndStop("walk");
  638.                this.entity.anim = "walk";
  639.                break;
  640.             case "attack":
  641.                this.entity.anims.gotoAndStop("attack");
  642.                this.entity.anim = "attack";
  643.                this.xv = this.entity._xscale / 10;
  644.                break;
  645.             case "attack2":
  646.                this.entity.anims.gotoAndStop("attack2");
  647.                this.entity.anim = "attack2";
  648.                this.xv = this.entity._xscale / 25;
  649.                break;
  650.             case "jump":
  651.                this.entity.anims.gotoAndStop("jump");
  652.                this.entity.anim = "jump";
  653.                break;
  654.             case "hurt":
  655.                this.entity.anims.gotoAndStop("hurt");
  656.                this.entity.anim = "hurt";
  657.                break;
  658.             case "stunned":
  659.                this.entity.anims.gotoAndStop("stunned");
  660.                this.entity.anim = "stunned";
  661.                break;
  662.             case "fallup":
  663.                this.entity.anims.gotoAndStop("fallup");
  664.                this.entity.anim = "fallup";
  665.                break;
  666.             case "falldown":
  667.                this.entity.anims.gotoAndStop("falldown");
  668.                this.entity.anim = "falldown";
  669.                break;
  670.             case "fallen":
  671.                this.entity.anims.gotoAndStop("fallen");
  672.                this.entity.anim = "fallen";
  673.          }
  674.       }
  675.       this.oldanim = this.newanim;
  676.    }
  677.    function lift(amount)
  678.    {
  679.       this.zv += amount;
  680.       this.grounded = false;
  681.    }
  682.    function hurt(amount, stun)
  683.    {
  684.       var _loc3_ = this.HP;
  685.       this.HP -= amount;
  686.       if(stun)
  687.       {
  688.          this.AItimer = 50;
  689.          this.AIstate = "stunned";
  690.       }
  691.       if(this.HP < 0)
  692.       {
  693.          if(this.isboss)
  694.          {
  695.             _root.gotoAndStop("endcutscene");
  696.          }
  697.          this.AIstate = "falling";
  698.          this.collide = false;
  699.          if(this.grounded)
  700.          {
  701.             this.zv += 15;
  702.          }
  703.          this.dead = true;
  704.       }
  705.       else
  706.       {
  707.          this.newanim = "hurt";
  708.          this.oldanim = "idle";
  709.       }
  710.       this.entity.hurting = true;
  711.       this.inputlocked = true;
  712.    }
  713.    function remove()
  714.    {
  715.       this.entity.removeMovieClip();
  716.    }
  717. }
  718.