home *** CD-ROM | disk | FTP | other *** search
/ 600 Games / 600games.iso / Nave / rocket_rush.swf / scripts / __Packages / PlanetAI.as < prev    next >
Encoding:
Text File  |  2007-03-20  |  6.5 KB  |  257 lines

  1. class PlanetAI extends Position
  2. {
  3.    var orbit_clips;
  4.    var fire_count;
  5.    var color;
  6.    var target_clip;
  7.    var onEnterFrame;
  8.    var miss;
  9.    var laser_color;
  10.    var speed_x = 0;
  11.    var speed_y = 0;
  12.    var speed_z = 0;
  13.    var air_resistance_factor = 0.4;
  14.    var air_resistance_factor_xy = 0.05;
  15.    var keep_distance = 1000;
  16.    var follow_ratio = 600;
  17.    var fire_left = true;
  18.    var laser_frame = 0;
  19.    var laser_out = false;
  20.    var health = 100;
  21.    var redded_out = false;
  22.    var laser_health = 100;
  23.    var laser_redded_out = false;
  24.    var red = {ra:100,rb:255,ga:100,gb:0,ba:100,bb:0,aa:100,ab:255};
  25.    var normal = {ra:100,rb:0,ga:100,gb:0,ba:100,bb:0,aa:100,ab:0};
  26.    function PlanetAI()
  27.    {
  28.       super();
  29.       this.orbit_clips = [1,2,3,2,4,3,1,2];
  30.       this.fire_count = 0;
  31.       this.color = new Color(this);
  32.       this.target_clip = this._parent.player;
  33.       this.onEnterFrame = function()
  34.       {
  35.          if(!_global.game_paused)
  36.          {
  37.             this.play();
  38.             var _loc3_ = 0;
  39.             while(_loc3_ < 8)
  40.             {
  41.                this["orbit" + (_loc3_ + 1)].laser.play();
  42.                _loc3_ = _loc3_ + 1;
  43.             }
  44.             if(this.redded_out)
  45.             {
  46.                this.unRed();
  47.                this.redded_out = null;
  48.             }
  49.             else if(this.redded_out == null)
  50.             {
  51.                this.redded_out = false;
  52.             }
  53.             if(this.laser_redded_out)
  54.             {
  55.                this.lUnRed();
  56.                this.laser_redded_out = null;
  57.             }
  58.             else if(this.laser_redded_out == null)
  59.             {
  60.                this.laser_redded_out = false;
  61.             }
  62.             if(this.fire_count++ > 3 && this.render_z < 650)
  63.             {
  64.                this.fire_count = 0;
  65.                this.fireRocks();
  66.             }
  67.             this.followTarget();
  68.             this.moveX();
  69.             this.moveY();
  70.             this.moveZ();
  71.          }
  72.          else
  73.          {
  74.             this.stop();
  75.             _loc3_ = 0;
  76.             while(_loc3_ < 8)
  77.             {
  78.                this["orbit" + (_loc3_ + 1)].laser.stop();
  79.                _loc3_ = _loc3_ + 1;
  80.             }
  81.          }
  82.       };
  83.    }
  84.    function followTarget()
  85.    {
  86.       var _loc2_ = this.distance2D(this.target_clip);
  87.       var _loc3_ = this.posZ - (this.target_clip.posZ + this.keep_distance);
  88.       var _loc4_ = 1 - 2 * (this.posX > this.target_clip.posX);
  89.       var _loc5_ = 1 - 2 * (this.posY > this.target_clip.posY);
  90.       this.accelerateX(_loc4_ * _loc2_ / this.follow_ratio);
  91.       this.accelerateY(_loc5_ * _loc2_ / this.follow_ratio);
  92.       this.accelerateZ((- _loc3_) / 70);
  93.    }
  94.    function fireRocks()
  95.    {
  96.       this._parent.objects[this._parent.objects.length] = this._parent.attachMovie("rock","rock" + this._parent.objects.length,this._parent.getNextHighestDepth()).launch(this);
  97.    }
  98.    function getShot(p_hittest)
  99.    {
  100.       if(this.laser_frame > 54 && this.laser_out)
  101.       {
  102.          var _loc3_ = false;
  103.          var _loc2_ = 0;
  104.          while(_loc2_ < 8)
  105.          {
  106.             if(this["orbit" + (_loc2_ + 1)].laser.laser_target.hitTest(p_hittest))
  107.             {
  108.                _loc3_ = true;
  109.             }
  110.             _loc2_ = _loc2_ + 1;
  111.          }
  112.          if(_loc3_)
  113.          {
  114.             this.lRedOut();
  115.             this.laser_health -= 3;
  116.             if(this.laser_health <= 0)
  117.             {
  118.                this.getHurt();
  119.             }
  120.          }
  121.          else if(this.miss._currentframe == 1)
  122.          {
  123.             this.miss.play();
  124.          }
  125.       }
  126.       else if(this.miss._currentframe == 1)
  127.       {
  128.          this.miss.play();
  129.       }
  130.    }
  131.    function getHurt()
  132.    {
  133.       this.redOut();
  134.       this.health -= 20;
  135.       var _loc2_ = 0;
  136.       while(_loc2_ < 8)
  137.       {
  138.          this["orbit" + (_loc2_ + 1)].laser.gotoAndPlay(130);
  139.          _loc2_ = _loc2_ + 1;
  140.       }
  141.       if(this.health <= 0)
  142.       {
  143.          this.die();
  144.       }
  145.    }
  146.    function die()
  147.    {
  148.       this.gotoAndPlay(27);
  149.    }
  150.    function redOut()
  151.    {
  152.       if(this.redded_out == false)
  153.       {
  154.          this.color.setTransform(this.red);
  155.          this.redded_out = true;
  156.       }
  157.    }
  158.    function unRed()
  159.    {
  160.       this.color.setTransform(this.normal);
  161.       this.redded_out = false;
  162.    }
  163.    function lRedOut()
  164.    {
  165.       if(this.laser_redded_out == false)
  166.       {
  167.          this.laser_color.setTransform(this.red);
  168.          this.laser_redded_out = true;
  169.       }
  170.    }
  171.    function lUnRed()
  172.    {
  173.       this.laser_color.setTransform(this.normal);
  174.       this.laser_redded_out = false;
  175.    }
  176.    function updateOrbits()
  177.    {
  178.       this.lUnRed();
  179.       var _loc2_ = 0;
  180.       while(_loc2_ < 8)
  181.       {
  182.          this["orbit" + (_loc2_ + 1)].gotoAndStop(this.orbit_clips[_loc2_]);
  183.          if(this.orbit_clips[_loc2_] == 5)
  184.          {
  185.             this.laser_color = new Color(this["orbit" + (_loc2_ + 1)].laser);
  186.          }
  187.          _loc2_ = _loc2_ + 1;
  188.       }
  189.    }
  190.    function nextOrbit()
  191.    {
  192.       if(this.orbit_clips[7] == 5)
  193.       {
  194.          this.laser_out = false;
  195.       }
  196.       var _loc2_ = 7;
  197.       while(_loc2_ >= 0)
  198.       {
  199.          var _loc3_ = _loc2_ + 1;
  200.          this.orbit_clips[_loc3_] = this.orbit_clips[_loc2_];
  201.          _loc2_ = _loc2_ - 1;
  202.       }
  203.       this.orbit_clips[0] = Math.ceil(Math.random() * 4);
  204.       if(Math.random() < 0.3 && !this.laser_out)
  205.       {
  206.          this.laser_out = true;
  207.          this.laser_frame = 0;
  208.          this.laser_health = 100;
  209.          this.orbit_clips[0] = 5;
  210.       }
  211.       this.updateOrbits();
  212.    }
  213.    function moveX()
  214.    {
  215.       this.pos_x += this.speed_x;
  216.       if(this.speed_x > 20)
  217.       {
  218.          this.speed_x = 20;
  219.       }
  220.       else if(this.speed_x < -20)
  221.       {
  222.          this.speed_x = -20;
  223.       }
  224.    }
  225.    function moveY()
  226.    {
  227.       this.pos_y += this.speed_y;
  228.       if(this.speed_y > 20)
  229.       {
  230.          this.speed_y = 20;
  231.       }
  232.       else if(this.speed_y < -20)
  233.       {
  234.          this.speed_y = -20;
  235.       }
  236.    }
  237.    function moveZ()
  238.    {
  239.       this.pos_z += this.speed_z;
  240.    }
  241.    function accelerateX(p_a)
  242.    {
  243.       this.speed_x += p_a;
  244.       this.speed_x -= this.speed_x * this.air_resistance_factor_xy;
  245.    }
  246.    function accelerateY(p_a)
  247.    {
  248.       this.speed_y += p_a;
  249.       this.speed_y -= this.speed_y * this.air_resistance_factor_xy;
  250.    }
  251.    function accelerateZ(p_a)
  252.    {
  253.       this.speed_z += p_a;
  254.       this.speed_z -= this.speed_z * this.air_resistance_factor;
  255.    }
  256. }
  257.