Guest User

Bullet Manager

a guest
Mar 15th, 2013
42
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*********************************************************8
  2.  * EnemyBullet.js & EnemyGun.js
  3.  * Creates bullet patterns for enemies, along with a bullet manager
  4.  * Basis of this was found at: http://phoboslab.org/xtype/xtype.js
  5.  * @copyright (c) 2013 Dave Voyles, under The MIT License (see LICENSE)
  6.  *********************************************************/
  7.  
  8. ig.module(
  9.     'game.entities.EnemyBullet'
  10. )
  11.     .requires(
  12.         'game.entities.BaseWeapon'
  13.  
  14. )
  15.     .defines(function () {
  16.      /******************************************8
  17.      * Enemy Bullet
  18.      ******************************************/
  19.     EntityEnemyBullet = EntityBaseWeapon.extend({
  20.     size: { x: 16, y: 2 },
  21.     offset: { x: 1, y: 8 },
  22.     animSheet: new ig.AnimationSheet('media/bullet_neon.png', 16, 16),
  23.     maxVel: {x: 100, y: 70},
  24.     health: 10,
  25.     speed: 10,  
  26.     angle:60,
  27.     maxSpeed: 80,
  28.     type: ig.Entity.TYPE.B,
  29.     checkAgainst: ig.Entity.TYPE.A,
  30.     collides: ig.Entity.COLLIDES.PASSIVE,
  31.    
  32.     /******************************************8
  33.     * Initialization
  34.     ******************************************/
  35.     init: function(x,y,settings){
  36.         this.parent(x,y,settings);
  37.         this.addAnim('idle', 1, [0]);
  38.         this.angle = this.angle;
  39.     },
  40.     update: function () {
  41.         this.parent();
  42.         this.speed = Math.min(this.maxSpeed, this.speed + ig.system.tick * 100);
  43.         this.vel.x = Math.cos(this.angle) * this.speed;
  44.         this.vel.y = Math.sin(this.angle) * this.speed;
  45.     },
  46.     handleMovementTrace: function( res ) {
  47.         this.parent( res );
  48.         if( res.collision.y || res.collision.x ) {
  49.  
  50.             this.kill();
  51.         }
  52.     }
  53.     });
  54.        
  55.        
  56.     /******************************************8
  57.     * Enemy Gun (Bullet Manager)
  58.     ******************************************/
  59.     EntityEnemyGun = ig.Entity.extend({
  60.  
  61.     bullets: 6,
  62.     firingTimer: null,
  63.     reloadTime: 2,
  64.  
  65.     /******************************************8
  66.     * Initialization
  67.     ******************************************/
  68.     init: function (x, y, settings) {
  69.         this.parent(x, y, settings);
  70.         this.firingTimer = new ig.Timer(Math.random() * this.reloadTime * 2);
  71.         this.shootTimer = new ig.Timer(3);
  72.     },
  73.  
  74.     /******************************************8
  75.     * Update
  76.     ******************************************/
  77.     update: function() {
  78.         this.parent();
  79.         if (this.shootTimer.delta() > 0) {
  80.             var inc = 120 / (this.bullets - 1);
  81.            
  82.             // The angle which the bullet leaves the weapon/enemy
  83.             var a2 = 110;
  84.             var radius = 5;
  85.             for (var i = 0; i < this.bullets; i++) {
  86.                 var angle = a2 * Math.PI / 180;
  87.                 var x = this.pos.x + 24 + Math.cos(angle) * radius;
  88.                 var y = this.pos.y + 44 + Math.sin(angle) * radius;
  89.                
  90.                 // Attaches bullet to weapon
  91.                 ig.game.spawnEntity(EntityEnemyBullet, x-25, y -40, {
  92.                     angle: angle
  93.                 });
  94.                 a2 += inc;
  95.             }      
  96.             this.shootTimer.reset();
  97.         }
  98.     }
  99.     });
  100.  
  101. });
RAW Paste Data