const flyimages = require( './images.js' );
const emitter = require( '../utils/emitter.js' );
//游戏配置
var config = {
"gameSpeed": 8, //游戏速度
"cartridgeSpeed": 10 //子弹速度
};
function flyGame( opts ) {
var c_width = this.c_width = opts.width;
var c_height = this.c_height = opts.height; //画布的高和宽
var cxt = this.cxt = opts.ctx;
var id = this.id = opts.id;
this.cxt.setFontSize( 30 );
this.cxt.setFillStyle( "#333" );
//等待时间
var loadingTime = 0;
//等待动画刷新事件
var refresh = function() {
drawBg();
drawLogo();
load();
loadingTime++;
wx.drawCanvas( {
canvasId: id,
actions: cxt.getActions()
})
}
//设置背景
function drawBg() {
var bg_img = flyimages[ "bg" ];
var bg_img_width = bg_img.width;
var bg_img_height = bg_img.height;
cxt.drawImage( bg_img.src, 0, 0, bg_img_width, bg_img_height );
}
//构造logo
function drawLogo() {
var logo_img = flyimages[ "logo" ];
var logo_width = logo_img.width;
var logo_height = logo_img.height;
var y = 100;
cxt.drawImage( logo_img.src, 0, y, logo_width, logo_height );
}
//等待动画
function load() {
if( loadingTime == 600 ) {
loadingTime = 0;
clearInterval( loadingClock );
game.start();
}
//loadingTime每隔200换一张图, 实现等待动画
var pic = flyimages[ "loading" + ( parseInt( loadingTime / 200 ) + 1 ) + "" ];
var pic_width = pic.width;
var pic_height = pic.height;
var x = ( c_width - pic_width ) / 3;
cxt.drawImage( pic.src, x, 220, pic_width, pic_height );
}
//开始动画
var loadingClock = setInterval( refresh, 1 );
var player = this.player = {};
player.x;
player.y;
player.lastX;
player.lastY;
player.bomb = 0;
player.status = true;
player.model = flyimages[ "me" ];
player.model2 = flyimages[ "me_2" ];
player.width = c_width / 375 * player.model.width;
player.height = player.width / player.model.width * player.model.height;
player.move = function( x, y ) {
player.lastX = player.x;
player.lastY = player.y;
player.x = x - player.width / 2;
player.y = y - player.height / 2;
player.x = player.x > c_width - player.width ? c_width - player.width : player.x;
player.x = player.x < 0 ? 0 : player.x;
player.y = player.y > c_height - player.height ? c_height - player.height : player.y;
player.y = player.y < 0 ? 0 : player.y;
}
player.moveing = function() {
if( !player.status ) {
return;
}
cxt.drawImage( game.time % 30 > 15 ? player.model.src : player.model2.src, player.x, player.y, player.width, player.height );
player.attacking();
}
player.cartridges = [];
player.attackTime = 0;
player.attackPower = false;
player.attack = function() {
if( !player.status ) {
return;
}
player.attackTime++;
if( ( player.attackTime * game.refreshInterval ) % ( game.refreshInterval * 20 ) != 0 ) {
return;
}
player.attackTime = 0;
//playAudio( "fire_bullet.mp3" );
var cartridges;
if( player.attackPower ) {
cartridges = [ ( new cartridge( player.x - ( player.width / 5 ), player.y, 2, player ) ), ( new cartridge( player.x + ( player.width / 5 ), player.y, 2, player ) ) ];
} else {
cartridges = [ ( new cartridge( player.x, player.y, 1, player ) ) ];
}
Array.prototype.push.apply( player.cartridges, cartridges );
}
player.attacking = function() {
player.attack();
var cartridgeSpeed = config.cartridgeSpeed;
var cartridges_length = player.cartridges.length;
firstloop: for( var i = cartridges_length;i--; ) {
var cartridge = player.cartridges[ i ];
cxt.drawImage( cartridge.model.src, cartridge.x, cartridge.y, cartridge.width, cartridge.height );
if( cartridge.y <= 0 ) {
player.cartridges.splice( i, 1 );
continue firstloop;
}
var plain_length = game.plains.length;
secondloop: for( var j = plain_length;j--; ) {
var plain = game.plains[ j ];
var X = cartridge.x;
var Y = cartridge.y;
var nextY = Y - cartridgeSpeed;
if(
X > plain.x
&& X < ( plain.x + plain.width )
&& nextY < ( plain.y + plain.height + plain.speed )
&& Y >= ( plain.y + plain.height )
) {
plain.byAttack();
player.cartridges.splice( i, 1 );
continue firstloop;
}
}
cartridge.y = cartridge.y - cartridgeSpeed; //子弹向上移动
}
}
player.useBomb = function() {
if( game.player.bomb <= 0 ) {
return;
}
game.player.bomb--;
//playAudio( "use_bomb.mp3" );
var plains_length = game.plains.length;
for( var i = plains_length;i--; ) {
var plain = game.plains[ i ];
plain.die();
}
}
player.die = function() {
if( !player.status ) {
return;
}
player.status = false;
//playAudio( "game_over.mp3" );
var dieSpeed = 20;
var x = player.x;
var y = player.y;
var h = player.height;
var w = player.width;
game.plainsDies.push(( new playerDie() ) );
function playerDie() {
var dieTime = 4 * dieSpeed;
this.animationTime = 4 * dieSpeed;
this.call = function() {
if( this.animationTime == 1 ) {
game.over();
}
var dieModel = flyimages[ "me_die" + ( parseInt(( dieTime - this.animationTime ) / dieSpeed ) + 1 ) + "" ];
cxt.drawImage( dieModel.src, x, y, w, h );
this.animationTime--;
}
}
}
var game = this.game = {};
game.fire = this.__proto__.fire;
game.score = 0;
game.time = 0;
game.player = player;
game.bgImg = flyimages[ "bg" ];
game.refreshInterval = config.gameSpeed;
game.refresh = function() {
game.time++;
game.bgScroll();
game.plainsScroll();
game.plainsDying();
game.player.moveing();
game.propShow();
game.refreshMessage();
wx.drawCanvas( {
canvasId: id,
actions: cxt.getActions()
})
}
game.bgScrollTime = 0;
game.bgScroll = function() {
var bg_img_height = game.bgImg.height;
var bg_img_width = game.bgImg.width;
game.bgScrollTime += 0.5;
if( game.bgScrollTime > bg_img_height ) {
game.bgScrollTime = 0;
}
cxt.drawImage( game.bgImg.src, 0, game.bgScrollTime - bg_img_height, bg_img_width, bg_img_height );
cxt.drawImage( game.bgImg.src, 0, game.bgScrollTime, bg_img_width, bg_img_height );
}
game.props = [];
game.addProp = function() {
var interval = 10;
if( ( game.time * game.refreshInterval ) % ( interval * 1000 ) == 0 ) {
game.props.push(( new prop( parseInt( Math.random() * 1.8 + 1.1 ) ) ) );
//playAudio( "show_prop.mp3" );
}
}
game.propShow = function() {
game.addProp();
var props_length = game.props.length;
for( var i = props_length;i--; ) {
var prop = game.props[ i ];
if( prop.isDeleted == true ) {
game.props.splice( i, 1 );
continue;
}