Guest User

Untitled

a guest
Sep 9th, 2013
58
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /************************************************************************
  2.  *  @actor-enemyBoss.js
  3.  *  @version: 1.00
  4.  *  @author:  Dave Voyles
  5.  *  @date:    August 2013
  6.  *  @copyright (c) 2013 Dave Voyles, under The MIT License (see LICENSE)
  7.  *  @First level boss
  8.  ***********************************************************************/
  9. ig.module(
  10.     'game.entities.actor-enemyBoss'
  11. )
  12. .requires(
  13.     'game.entities.base-enemy'
  14.     )
  15. .defines(function () {
  16.  
  17. EntityActorEnemyBoss = EntityBaseEnemy.extend({
  18.  
  19.     /*********************
  20.     * Property Definitions
  21.     *********************/
  22.     animSheet: new ig.AnimationSheet('media/textures/animBossShipLg.png', 248, 362),
  23.     size: {
  24.         x: 248,
  25.         y: 362
  26.     },
  27.     offset: {
  28.         x: 0,
  29.         y: 0
  30.     },
  31.     vel: {
  32.         x: -85,
  33.         y: 0
  34.     },
  35.     health:            400,
  36.     bWPExposed:        false,
  37.     canTakeDmg:        false,
  38.     shootTimer:        null,
  39.     moveTimer:         null,
  40.     initTimer:         null,
  41.     animTimer:         null,
  42.     animTime:          4,
  43.     newSpawnTimer:     null,
  44.     xModifier:         100,
  45.     initTime:          2,
  46.     shootSpeed:        0.09,
  47.     rotation:          1.4,
  48.     rotTime:           1,
  49.     spawnTime:         3,
  50.     rotateTime:        1,
  51.     statTurretsArr:    [],
  52.     rotTurretsArr:     [],
  53.     WPArr:             [],
  54.     bSpawnWeakPt:      true,
  55.     bSpawnRot:         true,
  56.     type:              ig.Entity.TYPE.A,
  57.     checkAgainst:      ig.Entity.TYPE.A,
  58.  
  59.  
  60.     init: function (x, y, settings) {
  61.         this.parent(x, y, settings);
  62.         this.addAnim('idle',  0.1, [0]), true;
  63.         this.initTimer      = new ig.Timer(this.initTime);
  64.         this.animTimer      = new ig.Timer(this.animTime);
  65.         this.shootTimer     = new ig.Timer(this.randomFromTo(3, 5));
  66.         this.moveTimer      = new ig.Timer(this.randomFromTo(2, 4));
  67.         this.newSpawnTimer  = new ig.Timer(this.spawnTime);
  68.         this.lastShootTimer = new ig.Timer(0);
  69.         this.rotateTimer    = new ig.Timer(this.rotateTime);
  70.         ig.game.boss        = this;
  71.         this.pos.y          = ig.system.height / 3;
  72.        
  73.         /* vars for gun */
  74.         this.startAngle    = this.ownAngle;
  75.         this._angle        = -500;
  76.         this._increase     = 60;
  77.         this.bullets       = 4  
  78.         this.radius        = 10;
  79.         this._entityBullet = EntityObjectEnemyBulletGreen;
  80.     },
  81.     update: function () {
  82.         this.parent();
  83.         this.parental = ig.game.enemySpawner;
  84.  
  85.         // Perform different manuevers based on current HP
  86.         this.checkHealth();
  87.  
  88.         // Stop when on screen
  89.         if (this.pos.x + 300  <= this.parental.pos.x) {
  90.             this.stop()
  91.             this.checkAnims();
  92.         }
  93.        
  94.         // Boss can take damage if weak points are killed
  95.         this.checkCanTakeDmg();
  96.     },
  97.     /* change behaviour based on current health */      
  98.     checkHealth: function(){
  99.         switch (this.health) {
  100.             case 400:
  101.                 /*          this.spawnRotTurrets(); */
  102.                 this.spawnWeakPoints(); // Keep this here, so that it draws on TOP of boss
  103.                 break;
  104.             case 300:
  105.                 break;
  106.             case 200:
  107.                 this.spawnEnemy();
  108.                 break;
  109.             case 100:
  110.                 this.spray();
  111.                 break;
  112.             default:
  113.                 break;
  114.         }
  115.     },
  116.     receiveDamage: function (value) {
  117.         if (!this.canTakeDmg) {
  118.             return; // If weak points are alive, don't take dmg
  119.         }
  120.     },
  121.     spawnStatTurrets: function(){
  122.         if (statTurrets.length === 0) {
  123.             var statTurrets = ig.game.getEntitiesByType(EntityBossTurret);
  124.             this.statTurretsArr.push(ig.game.spawnEntity(EntityBossTurret, this.pos.x - 50, this.pos.y - 50));
  125.             this.statTurretsArr.push(ig.game.spawnEntity(EntityBossTurret, this.pos.x - 50, this.pos.y + 50,
  126.                 {shotDir: 'down'}));
  127.         }
  128.     },
  129.     spawnRotTurrets: function () {
  130.         if (this.bSpawnRot) {
  131.             var rotTurrets = ig.game.getEntitiesByType(EntityRotTurret);
  132.             if (rotTurrets.length === 0) {
  133.                 this.rotTurretsArr.push(ig.game.spawnEntity(EntityRotTurret, this.pos.x - 50, this.pos.y + 50,
  134.                     { shotDir: 'down' }));
  135.             }
  136.         this.bSpawnRot = false;
  137.         }
  138.     },
  139.     spawnWeakPoints: function () {
  140.         if (this.bSpawnWeakPt) {
  141.             var EntityWeakPoints = ig.game.getEntitiesByType(EntityWeakPoint);
  142.             if (EntityWeakPoints.length === 0) {
  143. /*              var topWP = this.WPArr.push(ig.game.spawnEntity(    EntityWeakPoint, this.pos.x,                        this.pos.y, { xOffset: +140, yOffset: -6 })); */
  144.              var centerWP = this.WPArr.push(ig.game.spawnEntity(EntityWeakPoint, this.pos.x,                        this.pos.y, { xOffset: +75, yOffset:  +130 }));
  145.              var bottomWP = this.WPArr.push(ig.game.spawnEntity(EntityWeakPoint, this.pos.x,                        this.pos.y, { xOffset: +140, yOffset: +246 }));
  146.            }
  147.         }
  148.         this.bSpawnWeakPt = false;
  149.     },  
  150.     /* Show weak points based on animTimer. If visible, then can take DMG */
  151.     checkAnims: function(){
  152.         this.checkWPDmg();
  153.         if (!this.bWPExposed && this.animTimer.delta() > 0 ){
  154.               this.currentAnim = this.addAnim('opening',  0.3, [0, 1, 2, 3, 4], true);
  155.               this.bWPExposed = true;
  156.               this.animTimer.set(4);
  157.         }
  158.         if (this.bWPExposed && this.animTimer.delta() > 0 ){
  159.               this.currentAnim = this.addAnim('closing',  0.3, [4, 3, 2, 1, 0], true);
  160.               this.bWPExposed = false;
  161.               this.animTimer.set(4);
  162.         }    
  163.     },
  164.     /* check if weak points are killed -- if so, take DMG */
  165.     checkCanTakeDmg: function() {
  166.         var EntityWeakPoints = ig.game.getEntitiesByType(EntityWeakPoint);
  167.         if (EntityWeakPoints.length === 0) {
  168.             this.canTakeDmg == true;
  169.         }
  170.     },
  171.     checkWPDmg: function () {
  172.         var EntityWP = ig.game.getEntitiesByType(EntityWeakPoint);
  173.         for (var i = 0; i < EntityWP.length; i++); {
  174.             if (this.bWPExposed === true) {
  175.                 EntityWP[i].canTakeDmg = true;
  176.             } else {
  177.                 EntityWP[i].canTakeDmg = false;
  178.             }
  179.         }
  180.     },
  181. });
  182.  
  183.  
  184. /*************************
  185.  * @EntityBossTurret  
  186.  * @Turret attachd to boss
  187.  ************************/
  188. EntityBossTurret = EntityBaseEnemy.extend({
  189.  
  190.     /*********************
  191.     * Property Definitions
  192.     *********************/
  193.     animSheet:  new ig.AnimationSheet('media/textures/actor-playerShip_R.png', 24, 24),
  194.     soundShoot: new ig.Sound('media/sfx/Explode01.ogg'                               ),
  195.     font:       new ig.Font('media/fonts/xfinfont.png'                               ),
  196.     size: {
  197.         x: 20,
  198.         y: 20
  199.     },
  200.     offset: {
  201.         x: 0,
  202.         y: 1
  203.     },
  204.     friction: {
  205.         x: 800,
  206.         y: 800
  207.     },
  208.     maxVel: {
  209.         x: 300,
  210.         y: 300
  211.     },
  212.     angle:        -Math.PI / 2,
  213.     speed:        200,
  214.     health:       20,
  215.     respawnDelay: 2,
  216.     shootSpeed:   0.25,
  217.     rotation:     0.5,
  218.     rotDist:      1.2,
  219.     rotSpeed:     2.5,
  220.     lifeTimer:    null,
  221.     shotDir:      'up',
  222.     type:         ig.Entity.TYPE.B,
  223.     checkAgainst: ig.Entity.TYPE.C,
  224.    
  225.    
  226.     init: function (x, y, settings) {
  227.         this.parent(x, y, settings);
  228.         this.addAnim('idle', 60, [0]);
  229.         this.addAnim('shoot', 0.05, [3, 2, 1, 0], true);
  230.         this.lastShootTimer = new ig.Timer(0);
  231.         this.shotDir        = settings.shotDir || 'up';
  232.     },
  233.     update: function () {
  234.         if (this.currentAnim.loopCount > 0) {
  235.             this.currentAnim = this.anims.idle;
  236.         }
  237.         this.parent();
  238.         this.parental = ig.game.boss;
  239.         this.pos.x    = this.parental.pos. x + 30
  240.  
  241.         if ((0 | (ig.Timer.time / this.rotSpeed * this.rotDist)) & 1) {
  242.             this.currentAnim.angle += this.rotation * ig.system.tick;
  243.             if (this.shotDir === 'up'){
  244.                 this.checkShoot();
  245.             }
  246.         }
  247.         else {
  248.             this.currentAnim.angle -= this.rotation * ig.system.tick;
  249.             if (this.shotDir === 'down') {
  250.                 this.checkShoot();
  251.             }
  252.         }
  253.     },
  254.     checkShoot: function () {
  255.         var isShooting = true;
  256.         if (isShooting && this.lastShootTimer.delta() > 0) {
  257.             this.shoot();
  258.             this.lastShootTimer.set(this.shootSpeed);
  259.         }
  260.     },
  261.     shoot: function () {
  262.         ig.game.spawnEntity(EntityEnemyTurretBullet, this.pos.x, this.pos.y + 9, {
  263.             angle: -this.currentAnim.angle,
  264.         });
  265.     },
  266.     kill: function () {
  267.         ig.game.spawnEntity(EntityExplosionParticleGrey, this.pos.x, this.pos.y, {
  268.             count: 50,
  269.         });
  270.         ig.game.spawnEntity(EntityExplosionParticleRed,  this.pos.x, this.pos.y, {
  271.             count: 50,
  272.         });
  273.         this.MiniShipKill_sfx.play(); //TODO: Replace with new SFX
  274.         this.parent();
  275.     }
  276. });
  277.  
  278.  
  279. /*****************************
  280.  * @EntityRotTurret  
  281.  * @Turret rotates around boss
  282.  ****************************/
  283. EntityRotTurret = EntityBaseEnemy.extend({
  284.  
  285.     /*********************
  286.     * Property Definitions
  287.     *********************/
  288.     animSheet:  new ig.AnimationSheet('media/textures/actor-playerShip_R.png', 24, 24),
  289.     soundShoot: new ig.Sound('media/sfx/Explode01.ogg'                               ),
  290.     font:       new ig.Font('media/fonts/xfinfont.png'                               ),
  291.     size: {
  292.         x: 20,
  293.         y: 20
  294.     },
  295.     offset: {
  296.         x: 0,
  297.         y: 1
  298.     },
  299.     friction: {
  300.         x: 800,
  301.         y: 800
  302.     },
  303.     maxVel: {
  304.         x: 300,
  305.         y: 300
  306.     },
  307.     angle:        -Math.PI / 2,
  308.     speed:        200,
  309.     health:       5,
  310.     respawnDelay: 2,
  311.     shootSpeed:   0.8,
  312.     rotation:     0.5,
  313.     rotDist:      1.2,
  314.     rotSpeed:     .1,
  315.     lifeTimer:    null,
  316.     radius:       200,               // How far away from center should hit area be positioned
  317.     shotDir:      'up',
  318.     type:         ig.Entity.TYPE.B,
  319.     checkAgainst: ig.Entity.TYPE.C,
  320.    
  321.    
  322.     init: function (x, y, settings) {
  323.         this.parent(x, y, settings);
  324.         this.addAnim('idle', 60, [0]);
  325.         this.addAnim('shoot', 0.05, [3, 2, 1, 0], true);
  326.         this.lastShootTimer = new ig.Timer(0);
  327.         this.shotDir        = settings.shotDir || 'up';
  328.     },
  329.     update: function () {
  330.         if (this.currentAnim.loopCount > 0) {
  331.             this.currentAnim = this.anims.idle;
  332.         }
  333.         this.parent();
  334.         this.parental = ig.game.boss;
  335.         this.checkShoot();
  336.  
  337.    
  338.         // Set position based on parental position
  339.         var x = (this.parental.pos.x + this.parental.size.x / 2) + Math.cos(this.angle) * 70;
  340.         var y = (this.parental.pos.y + this.parental.size.y / 2) + Math.sin(this.angle) * 70;
  341.  
  342.         // Rotate around boss
  343.         this.pos.x = x;
  344.         this.pos.y = y;
  345.         this.angle += this.rotSpeed
  346.     },
  347.     checkShoot: function () {
  348.         var isShooting = true;
  349.         if (isShooting && this.lastShootTimer.delta() > 0) {
  350.             this.shoot();
  351.             this.lastShootTimer.set(this.shootSpeed);
  352.         }
  353.     },
  354.     shoot: function () {
  355.         ig.game.spawnEntity(EntityEnemyTurretBullet, this.pos.x, this.pos.y +9, {
  356.             angle: -this.currentAnim.angle,
  357.         });
  358.     },
  359.     kill: function () {
  360.         ig.game.spawnEntity(EntityExplosionParticleGrey, this.pos.x, this.pos.y, {
  361.             count: 50,
  362.         });
  363.         ig.game.spawnEntity(EntityExplosionParticleRed,  this.pos.x, this.pos.y, {
  364.             count: 50,
  365.         });
  366.         this.MiniShipKill_sfx.play(); //TODO: Replace with new SFX
  367.         this.parent();
  368.     }
  369. });
  370.  
  371.  
  372.  
  373. /*********************************************************************************
  374.  * @EntityWeakPoint  
  375.  * @Vulnerable spot on the boss for players to attack
  376.  * @Params: accepts int to determine the X and Y pos offset based on boss location
  377.  ********************************************************************************/
  378. EntityWeakPoint = EntityBaseEnemy.extend({
  379.         animSheet:  new ig.AnimationSheet('media/textures/hitBox.png', 40, 100),
  380.         size:{
  381.             x: 40,
  382.             y: 100
  383.         },
  384.         xOffset:      null,
  385.         yOffset:      null,
  386.         health:       10,
  387.         canTakeDmg:   true,
  388.         type:         ig.Entity.TYPE.B,
  389.         checkAgainst: ig.Entity.TYPE.C,
  390.         collides:     ig.Entity.COLLIDES.NEVER,
  391.  
  392.         init: function(x,y, settings) {
  393.             this.parent(x,y, settings);
  394.             this.addAnim('idle', 60, [0]);
  395.             this.xOffset = settings.xOffset || this.xOffset;
  396.             this.yOffset = settings.yOffset || this.yOffset;
  397.         },
  398.         update: function() {
  399.             this.parent();
  400.             this.parental = ig.game.boss;
  401.  
  402.             // Set position based on boss location + offset param passed in during init
  403.             this.pos.x = this.parental.pos.x + this.xOffset;
  404.             this.pos.y = this.parental.pos.y + this.yOffset;
  405.             this.angle += this.rotSpeed
  406.         },        
  407.         receiveDamage: function (value) {
  408.             this.parent();
  409.             if (!this.canTakeDmg) {
  410.                 return;
  411.             }
  412.         },
  413. });
  414.  
  415.  
  416.  
  417.  /**********************************
  418.   * @window.EntityEnemyTurretBullet
  419.   * @Shoots from enemy turret ships
  420.   *********************************/
  421. EntityEnemyTurretBullet = ig.Entity.extend({
  422.         image: new ig.Image('media/textures/plasma_b_R.png'),
  423.         speed: {
  424.             x: -30,
  425.             y: 150
  426.         },
  427.         /* This looks pretty awesome */
  428.         //speed: {
  429.         //    x: 0,
  430.         //    y: 100
  431.         //},
  432.         size: {
  433.             x: 8,
  434.             y: 6
  435.         },
  436.         offset: {
  437.             x: 20,
  438.             y: 16
  439.         },
  440.         friction: {
  441.             x: 0,
  442.             y: 15
  443.         },
  444.         speedModifier: 5,
  445.         type:          ig.Entity.TYPE.C,
  446.         checkAgainst:  ig.Entity.TYPE.A,
  447.         collides:      ig.Entity.COLLIDES.PASSIVE,
  448.  
  449.  
  450.         init: function (x, y, settings) {
  451.             this.parent(x, y, settings);
  452.             this.vel.x = Math.cos(this.angle) * this.speed.y;
  453.             this.vel.y = Math.sin(this.angle) * this.speed.y;
  454.         },
  455.         draw: function () {
  456.             ig.system.context.save();
  457.             ig.system.context.translate(this.pos.x - ig.game._rscreen.x, this.pos.y - ig.game._rscreen.y);
  458.             ig.system.context.rotate(-this.angle);
  459.             ig.system.context.drawImage(this.image.data, -this.offset.x, -this.offset.y);
  460.             ig.system.context.restore();
  461.         },
  462.         update: function () {
  463.             this.parent();
  464.             this.pos.x -= this.vel.x * ig.system.tick * this.speedModifier;
  465.             this.pos.y += this.vel.y * ig.system.tick / 2;
  466.             if (this.pos.x > ig.system.width  + 50 ||
  467.                 this.pos.y > ig.system.height + 50 ||
  468.                 this.pos.x < -50 || this.pos.y < -50) {
  469.                 this.kill();
  470.             }
  471.         },
  472.         check: function (other) {
  473.             if (other instanceof EntityActorPlayer) {
  474.                 other.receiveDamage(1, this);
  475.                 this.kill();
  476.             }
  477.         }
  478.     });
  479. });
RAW Paste Data