wyrian/js/layouts/PlayerLayer.js

167 lines
3.7 KiB
JavaScript
Raw Normal View History

2011-02-12 14:55:38 +00:00
// -- Init the Background DIV
Layouts.Player = new Layout({
// -- Define elements to draw with options
el: [{
2011-02-12 17:54:57 +00:00
id: 'ship',
2011-02-12 23:15:01 +00:00
name: 'ship',
2011-02-12 17:54:57 +00:00
width: 160,
2011-02-12 21:24:14 +00:00
height: 160,
type: 'human',
2011-02-12 21:24:14 +00:00
sprites: [0,1],
2011-02-12 17:54:57 +00:00
origin: {
x: Game.width/2 - 80,
y: Game.height-220
2011-02-12 17:54:57 +00:00
},
2011-02-12 14:55:38 +00:00
fireInterval: 300,
2011-02-13 05:19:58 +00:00
explode: function(obj) {
if ( ! obj ) return false ;
obj.explosing = true ;
Layouts.Ennemies.createExplosion(obj) ;
Layouts.Player.running = false ;
},
2011-02-12 14:55:38 +00:00
animate: function(obj) {
2011-02-12 17:54:57 +00:00
2011-02-12 14:55:38 +00:00
// -- KEY up /down
if ( Game.input.keyboard.up ) {
2011-02-12 21:24:14 +00:00
if ( obj.y > 0 ) obj.y -= obj.parent.settings.speed ;
2011-02-12 14:55:38 +00:00
}
if ( Game.input.keyboard.down && (obj.y < (Game.height-obj.height) ) ) {
2011-02-12 14:55:38 +00:00
obj.y += obj.parent.settings.speed;
}
// -- Left/Right move : choose sprite sequence to display
if ( Game.input.keyboard.left && (obj.x > 0)) {
2011-02-12 14:55:38 +00:00
obj.x -= obj.parent.settings.speed;
}
if ( Game.input.keyboard.right && (obj.x < Game.width-obj.width) ) {
2011-02-12 14:55:38 +00:00
obj.x += obj.parent.settings.speed;
}
// -- Press Space : fire
if ( Game.input.keyboard.space ) {
2011-02-12 14:55:38 +00:00
obj.parent.fire(obj) ;
}
}
2011-02-12 21:24:14 +00:00
}, {
id: 'ship_reactor',
width: 44,
height: 68,
imageSrc: 'images/sprites/reactor_fire_sprite.png',
2011-02-12 21:24:14 +00:00
sprites: [0,1],
nomove: true,
animate: function(obj) {
2011-02-13 05:19:58 +00:00
obj.settings.spriteMod = 3 ;
2011-02-12 21:24:14 +00:00
obj.settings.sprites = [0,1] ;
if ( Game.input.keyboard.up ) {
2011-02-13 05:19:58 +00:00
obj.settings.sprites = [2,3,4] ;
2011-02-12 21:24:14 +00:00
}
}
2011-02-12 14:55:38 +00:00
}],
// -- Define current Speed
speed: 20,
direction: 1,
// -- Define canvas parent
dom: $('div#game')
}) ;
// -- Bullets types
Layouts.Player.bulletLib = function(obj, bulletType) {
var bulletConf = {
power: 40,
imageSrc: 'images/bullet-electric-sprite.png',
2011-02-12 14:55:38 +00:00
sprites: [0,1,2],
speed: 40,
direction: -1,
type: 'human',
2011-02-12 23:15:01 +00:00
name: 'bullet',
2011-02-13 05:19:58 +00:00
explode: function(obj) {
if ( ! obj ) return false ;
obj.explosing = true ;
obj.deleteAfter = true ;
},
2011-02-12 14:55:38 +00:00
origin: {x:0, y:0}
} ;
// -- Default Left
2011-02-12 23:15:01 +00:00
if ( bulletType == 'weapon_pilot' ) {
2011-02-12 14:55:38 +00:00
bulletConf.width = 16 ;
bulletConf.height = 64 ;
bulletConf.sprites = false ;
bulletConf.imageSrc = 'images/12px-long-blue.png' ;
2011-02-12 21:24:14 +00:00
bulletConf.origin.x = obj.x-6+obj.width/2 ;
2011-02-12 14:55:38 +00:00
bulletConf.origin.y = obj.y - bulletConf.height ;
}
// -- Big Left
if ( bulletType == 'big_left' ) {
bulletConf.width = 60 ;
bulletConf.height = 60 ;
bulletConf.speed = 20 ;
2011-02-12 22:03:47 +00:00
bulletConf.origin.x = obj.x+18+obj.width/2 ;
2011-02-12 21:24:14 +00:00
bulletConf.origin.y = obj.y + 50 - bulletConf.height ;
2011-02-12 14:55:38 +00:00
}
// -- Big Left
if ( bulletType == 'big_right' ) {
bulletConf.width = 60 ;
bulletConf.height = 60 ;
bulletConf.speed = 20 ;
2011-02-12 21:24:14 +00:00
bulletConf.origin.x = obj.x-75+obj.width/2 ;
bulletConf.origin.y = obj.y + 50 - bulletConf.height ;
2011-02-12 14:55:38 +00:00
}
// -- The animate function
bulletConf.animate = function(obj) {
if ( obj.deleteAfter ) return false;
2011-02-13 05:19:58 +00:00
if ( obj.explosing ) {
Game.log(obj) ;
2011-02-13 05:19:58 +00:00
return false ;
}
obj.y += obj.settings.speed*obj.settings.direction ;
if ( obj.y < -(obj.height+300) ) {
obj.deleteObj() ;
}
2011-02-12 14:55:38 +00:00
} ;
// Add the bullet to display
return this.createObj(bulletConf) ;
} ;
// -- Fire bullets on space bar
Layouts.Player.fire = function(obj) {
// Get now time and lastfired
this.now = (new Date().getTime()) ;
this.lastFired = this.lastFired || this.now ;
// If firing, wait for next interval
if ( (this.now-this.lastFired) < (obj.settings.fireInterval) ) return false;
// Store now for last fired event
this.lastFired = this.now ;
// Create new bullets
var bullets = [] ;
2011-02-12 23:15:01 +00:00
bullets.push(this.bulletLib(obj, 'weapon_pilot')) ;
2011-02-12 14:55:38 +00:00
bullets.push(this.bulletLib(obj, 'big_left')) ;
bullets.push(this.bulletLib(obj, 'big_right')) ;
for ( var i in bullets ) {
this.els.push(bullets[i]) ;
}
2011-02-13 13:28:05 +00:00
// Play sound
soundManager.play('shoot') ;
2011-02-12 14:55:38 +00:00
}