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

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