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

  1. class EnemyAI extends TargetPosition
  2. {
  3.    var fire_count;
  4.    var extra_z;
  5.    var extra_x;
  6.    var extra_y;
  7.    var target_clip;
  8.    var onEnterFrame;
  9.    var blast_left;
  10.    var blast_right;
  11.    var color;
  12.    var speed_x = 0;
  13.    var speed_y = 0;
  14.    var speed_z = 0;
  15.    var air_resistance_factor = 0.4;
  16.    var air_resistance_factor_xy = 0.05;
  17.    var fire_left = true;
  18.    var has_missiles = false;
  19.    static var missiles_out = 0;
  20.    var health = 100;
  21.    function EnemyAI()
  22.    {
  23.       super();
  24.       this.fire_count = Math.random() * 5;
  25.       this.extra_z = Math.random() * 200;
  26.       this.extra_x = Math.random() * 600 - 300;
  27.       this.extra_y = Math.random() * 600 - 300;
  28.       this.target_clip = this._parent.player;
  29.       this.onEnterFrame = function()
  30.       {
  31.          if(!_global.game_paused && this._parent == _root.airspace)
  32.          {
  33.             if(this._x > 0 && this._x < 550)
  34.             {
  35.                if(this.whited_out)
  36.                {
  37.                   this.unWhite();
  38.                   this.whited_out = null;
  39.                }
  40.                else if(this.whited_out == null)
  41.                {
  42.                   this.whited_out = false;
  43.                }
  44.                this.fireCannon();
  45.                if(Math.random() < 0.015 && this.has_missiles)
  46.                {
  47.                   this.fireMissile();
  48.                }
  49.                this.followTarget();
  50.                this.keepApart();
  51.                this.moveX();
  52.                this.moveY();
  53.                this.moveZ();
  54.             }
  55.          }
  56.       };
  57.    }
  58.    function fireCannon()
  59.    {
  60.       if(this.fire_count++ > 5)
  61.       {
  62.          this.fire_count = 0;
  63.          if(this.fire_left)
  64.          {
  65.             this.blast_left._visible = true;
  66.             this.blast_left.gotoAndPlay(2);
  67.          }
  68.          else
  69.          {
  70.             this.blast_right._visible = true;
  71.             this.blast_right.gotoAndPlay(2);
  72.          }
  73.          this.fire_left = !this.fire_left;
  74.          this.shootCannon();
  75.       }
  76.       else
  77.       {
  78.          this.blast_left.gotoAndStop(1);
  79.          this.blast_right.gotoAndStop(1);
  80.       }
  81.    }
  82.    function shootCannon()
  83.    {
  84.       var _loc5_ = this.distance2D(this.target_clip);
  85.       var _loc2_ = this.posZ - this.target_clip.posZ;
  86.       var _loc3_ = 60 / _loc5_ * (600 / _loc2_);
  87.       var _loc4_ = Math.random() * _loc3_;
  88.       if(_loc4_ > 0.3)
  89.       {
  90.          this.target_clip.getShot();
  91.       }
  92.    }
  93.    function fireMissile()
  94.    {
  95.       if(EnemyAI.missiles_out < 4)
  96.       {
  97.          EnemyAI.missiles_out = EnemyAI.missiles_out + 1;
  98.          var _loc2_ = this._parent.attachMovie("enemy_missile","missile" + this._parent.objects.length,-1000 + this._parent.objects.length);
  99.          _loc2_.launch(this,this.target_clip,50,this.speed_z);
  100.          this._parent.addObject(_loc2_);
  101.       }
  102.    }
  103.    function followTarget()
  104.    {
  105.       var _loc2_ = this.distance2D(this.target_clip);
  106.       var _loc3_ = this.posZ - (this.target_clip.posZ + 800 + this.extra_z);
  107.       var _loc4_ = 1 - 2 * (this.posX > this.target_clip.posX);
  108.       var _loc5_ = 1 - 2 * (this.posY > this.target_clip.posY);
  109.       this.accelerateX(_loc4_ * (_loc2_ + this.extra_x) / 200);
  110.       this.accelerateY(_loc5_ * (_loc2_ + this.extra_y) / 200);
  111.       this.accelerateZ((- _loc3_) / 70);
  112.    }
  113.    function keepApart()
  114.    {
  115.       for(var _loc7_ in this._parent.enemies)
  116.       {
  117.          if(this._parent.enemies[_loc7_] != this && this._parent.enemies[_loc7_]._x > 0 && this._parent.enemies[_loc7_]._x < 550)
  118.          {
  119.             var _loc4_ = this.distance2D(this._parent.enemies[_loc7_]);
  120.             var _loc5_ = 1 - 2 * (this.posX < this._parent.enemies[_loc7_].posX);
  121.             var _loc6_ = 1 - 2 * (this.posY < this._parent.enemies[_loc7_].posY);
  122.             var _loc3_ = _loc5_ * 150 / _loc4_;
  123.             var _loc2_ = _loc6_ * 150 / _loc4_;
  124.             this.accelerateX(_loc3_ * _loc3_ * _loc3_);
  125.             this.accelerateY(_loc2_ * _loc2_ * _loc2_);
  126.          }
  127.       }
  128.    }
  129.    function getMissiled()
  130.    {
  131.       this.whiteOut();
  132.       this.reduceHealth(70);
  133.    }
  134.    function whiteOut()
  135.    {
  136.       if(this.whited_out == false)
  137.       {
  138.          this.color.setTransform(this.white);
  139.          this.whited_out = true;
  140.       }
  141.    }
  142.    function getShot()
  143.    {
  144.       this.whiteOut();
  145.       this.reduceHealth(5.5);
  146.    }
  147.    function reduceHealth(p_ammount)
  148.    {
  149.       this.health -= p_ammount;
  150.       if(this.health <= 0)
  151.       {
  152.          this.die();
  153.       }
  154.    }
  155.    function die()
  156.    {
  157.       this._parent.goodComment();
  158.       this._parent.enemies_killed = this._parent.enemies_killed + 1;
  159.       this.unWhite();
  160.       this.getShot = function()
  161.       {
  162.       };
  163.       this.getMissiled = function()
  164.       {
  165.       };
  166.       this.onEnterFrame = function()
  167.       {
  168.          if(!_global.game_paused)
  169.          {
  170.             if(this._x > 0 && this._x < 550)
  171.             {
  172.                this.moveX();
  173.                this.moveY();
  174.                this.moveZ();
  175.             }
  176.          }
  177.       };
  178.       this.gotoAndStop(2);
  179.    }
  180.    function moveX()
  181.    {
  182.       this.pos_x += this.speed_x;
  183.       if(this.speed_x > 20)
  184.       {
  185.          this.speed_x = 20;
  186.       }
  187.       else if(this.speed_x < -20)
  188.       {
  189.          this.speed_x = -20;
  190.       }
  191.    }
  192.    function moveY()
  193.    {
  194.       this.pos_y += this.speed_y;
  195.       if(this.speed_y > 20)
  196.       {
  197.          this.speed_y = 20;
  198.       }
  199.       else if(this.speed_y < -20)
  200.       {
  201.          this.speed_y = -20;
  202.       }
  203.    }
  204.    function moveZ()
  205.    {
  206.       this.pos_z += this.speed_z;
  207.    }
  208.    function accelerateX(p_a)
  209.    {
  210.       this.speed_x += p_a;
  211.       this.speed_x -= this.speed_x * this.air_resistance_factor_xy;
  212.    }
  213.    function accelerateY(p_a)
  214.    {
  215.       this.speed_y += p_a;
  216.       this.speed_y -= this.speed_y * this.air_resistance_factor_xy;
  217.    }
  218.    function accelerateZ(p_a)
  219.    {
  220.       this.speed_z += p_a;
  221.       this.speed_z -= this.speed_z * this.air_resistance_factor;
  222.    }
  223. }
  224.