Advertisement
Guest User

player.js

a guest
Mar 9th, 2014
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ig.module(
  2.     'game.entities.player'
  3. )
  4. .requires(
  5.     'impact.entity'
  6. )
  7. .defines(function(){
  8.     EntityPlayer = ig.Entity.extend({
  9.         animSheet: new ig.AnimationSheet( 'media/player.png', 16, 16 ),
  10.  
  11.         size: {x: 16, y: 16},
  12.         offset: {x: 0, y: 0},
  13.         flip: false,
  14.  
  15.         maxVel: {x: 100, y: 150},
  16.         friction: {x: 600, y: 0},
  17.         accelGround: 400,
  18.         accelAir: 200,
  19.         jump: 200,
  20.  
  21.     init: function( x, y, settings ) {
  22.         this.parent( x, y, settings );
  23.        
  24.         this.addAnim( 'idle', 1, [0, 4] );
  25.         this.addAnim( 'walk', 1, [1] );
  26.         this.addAnim( 'jump', 1, [2] );
  27.         this.addAnim( 'fall', 1, [3] );
  28.  
  29.         //Camera
  30.         ig.game.player = this;
  31.     },
  32.  
  33.     update: function() {
  34.         var accel = this.standing ? this.accelGround : this.accelAir;
  35.             if( ig.input.state('left') ) {
  36.                 this.accel.x = -accel;
  37.                 this.flip = true;
  38.             }else if( ig.input.state('right') ) {
  39.                 this.accel.x = accel;
  40.                 this.flip = false;
  41.             }else{
  42.                 this.accel.x = 0;
  43.             }
  44.             if( this.standing && ig.input.pressed('jump') ) {
  45.                 this.vel.y = -this.jump;                
  46.             }
  47.  
  48.             if( ig.input.pressed('shoot') ) {
  49.                 var mx = (ig.input.mouse.x + ig.game.screen.x);
  50.                 var my = (ig.input.mouse.y + ig.game.screen.y);
  51.                 var r = Math.atan2(my-this.pos.y, mx-this.pos.x);
  52.                 ig.game.spawnEntity( EntityBullet, this.pos.x, this.pos.y, {flip:this.flip} );
  53.             }
  54.  
  55.             //Animations
  56.             if( this.vel.y < 0) {
  57.                 this.currentAnim = this.anims.jump;
  58.             }else if( this.vel.y > 0 ) {
  59.                 this.currentAnim = this.anims.fall;
  60.             }else if( this.vel.x != 0 ) {
  61.                 this.currentAnim = this.anims.walk;
  62.             }else{
  63.                 this.currentAnim = this.anims.idle;
  64.             }
  65.  
  66.         this.parent();
  67.     },
  68. });
  69.  
  70.  
  71. EntityBullet = ig.Entity.extend({
  72.         animSheet: new ig.AnimationSheet( 'media/bullet.png', 5, 3 ),
  73.         size: {x: 5, y: 3},
  74.         maxVel: {x: 200, y: 0},
  75.  
  76.         //Collision type.
  77.         type: ig.Entity.TYPE.NONE,
  78.         checkAgainst: ig.Entity.TYPE.B,
  79.         collides: ig.Entity.COLLIDES.PASSIVE,
  80.  
  81.     init: function( x, y, settings ) {
  82.         this.parent( x + (settings.flip ? -4 : 8) , y+8, settings );
  83.    
  84.     var vely = Math.sin(this.angle) * this.desiredVel; //.desiredVel is just the velocity I would want if we were going in a straight line directly out of the right of the player. I just put it as a property of the entity since I refer to it in multiple locations
  85.     var velx =  Math.cos(this.angle) * this.desiredVel;
  86.     this.maxVel.x = this.vel.x = this.accel.x = 100;
  87.     this.maxVel.y = this.vel.y = this.accel.y = 100;
  88.  
  89.         this.vel.x = this.accel.x = (settings.flip ? -this.maxVel.x : this.maxVel.x);
  90.         this.addAnim( 'idle', 0.2, [0] );
  91.     },
  92.  
  93.     handleMovementTrace: function( res ) {
  94.         this.parent( res );
  95.         if( res.collision.x || res.collision.y ){
  96.             this.kill();
  97.         }
  98.     },
  99.  
  100.     check: function( other ) {
  101.         other.recieveDamage( 3, this );
  102.         this.kill();
  103.     }
  104.     });
  105. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement