From d588759e903821c81c0f9fef95639ec492316ce3 Mon Sep 17 00:00:00 2001 From: Fabrice Ecaille Date: Wed, 16 Oct 2013 18:14:39 +0200 Subject: [PATCH] * Optim: better way to manage waves * Feature: add mirror move and a beginnig of sinusoid move --- js/spaceinvaders-core.js | 45 +++++----------------- js/spaceinvaders-models.js | 78 ++++++++++++++++++++++++++++++++++++++ js/spaceinvaders-ui.js | 8 ++-- 3 files changed, 91 insertions(+), 40 deletions(-) diff --git a/js/spaceinvaders-core.js b/js/spaceinvaders-core.js index d3af83b..ebe548c 100644 --- a/js/spaceinvaders-core.js +++ b/js/spaceinvaders-core.js @@ -8,39 +8,6 @@ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -var WAVES = [ - { - wave : [ [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], - [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], - [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], - [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], - [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] ], - move : function() { - var offset = (PLAYGROUND_WIDTH - 16 * this.width) / 2; - if (Math.abs((this.getOriginX() - this.getX())) >= offset) { - this.directionX *= -1; - this.y = (this.y + this.height / 4); - } - }, - bonus : [40, 20] - }, - { - wave : [ [ 0, 0, 0, 0, 0, 0, 0 ], - [ 0, 0, 0, 0, 0, 0, 0, 0 ], - [ 0, 0, 0, 0, 0, 0, 0, 0, 0 ], - [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], - [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] ], - move : function() { - var offset = (PLAYGROUND_WIDTH - 16 * this.width) / 2; - if (Math.abs((this.getOriginX() - this.getX())) >= offset) { - this.directionX *= -1; - this.y = (this.y + this.height / 4); - } - }, - bonus : [30, 15] - } - ]; - Game = { running : false, wave_index : -1, @@ -66,7 +33,7 @@ Game = { var aliensRow = wave[row], type = aliensRow[0], offset = (PLAYGROUND_WIDTH - ((aliensRow.length - 1) * 0.5 + aliensRow.length) * ALIENS_WIDTH) / 2; for (col = 0; col < aliensRow.length; col = col + 1) { - Game.setAlien(col, row, offset, type, Game.wave.move); + Game.setAlien(col, row, offset, aliensRow[col], Game.wave.move); } } @@ -134,11 +101,17 @@ Game = { setAlien : function(x, y, offset, type, move) { "use strict"; + if( typeof type == "undefined" ) { + return; + } var id = x * ROWS + y; - var alien = new Alien("alien" + id, { + var alien = new type("alien" + id, { x : offset + x * ALIENS_WIDTH * 1.5, y : START_Y + (y * 1.25 * ALIENS_HEIGHT) - }, move); + }, move.move); + var directions = move.init( alien.x, alien.y ); + alien.directionX = directions.directionX; + alien.directionY = directions.directionY; $("#actors").addSprite("alien" + id, $.extend({posx : alien.x, posy : alien.y}, animations.alien)); alien.node = $("#alien" + id); alien.node.addClass("alien"); diff --git a/js/spaceinvaders-models.js b/js/spaceinvaders-models.js index f14a651..174bcca 100644 --- a/js/spaceinvaders-models.js +++ b/js/spaceinvaders-models.js @@ -21,6 +21,84 @@ WORLD.farm.bonus = [ } ]; +/*** Move ***/ + +var MOVE = { + translation : { + init : function (x, y) { + return {directionX : 1, directionY : 0}; + }, + move : function() { + var offset = (PLAYGROUND_WIDTH - 16 * this.width) / 2; + if (Math.abs((this.getOriginX() - this.getX())) >= offset) { + this.directionX *= -1; + this.y = (this.y + this.height / 4); + } + + }, + }, + mirror : { + init : function(x, y) { + if( x < PLAYGROUND_WIDTH / 2 ) { + return {directionX: -1, directionY: 0}; + } + return {directionX: 1, directionY: 0}; + }, + move : function() { + var offset = this.width / 2; + if (Math.abs((this.getOriginX() - this.getX())) >= offset) { + this.directionX *= -1; + this.y = (this.y + this.height / 4); + } + + }, + }, + sinusoid : { + init : function (x, y) { + return {directionX : 1, directionY : 0}; + }, + move : function () { + var offsetX = (PLAYGROUND_WIDTH - 16 * this.width) / 2; + if (Math.abs((this.getOriginX() - this.getX())) >= offsetX) { + this.directionX *= -1; + } + var offsetY = 5 * ALIENS_HEIGHT; + if (Math.abs((this.getOriginY() - this.getY())) >= offsetY) { + this.directionY *= -1; + } + } + } +}; + +/*** Move - end ***/ + +/*** Waves ***/ + +var WAVES = [ + { + wave : [ [ Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien ], + [ Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien ], + [ Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien ], + [ Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien ], + [ Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien, Alien ], ], + move : MOVE.sinusoid, + bonus : [40, 20] + }, + { + wave : [ [ Alien, Alien, Alien, undefined, Alien, Alien, Alien ], + [ Alien, Alien, Alien, Alien, undefined, Alien, Alien, Alien ], + [ Alien, Alien, Alien, Alien, undefined, Alien, Alien, Alien, Alien ], + [ Alien, Alien, Alien, Alien, undefined, Alien, Alien, Alien, Alien, Alien ], + [ Alien, Alien, Alien, Alien, Alien, undefined, Alien, Alien, Alien, Alien, Alien ] ], + move : MOVE.mirror, + bonus : [30, 15] + } + ]; + + +/*** Waves - end ***/ + + /*** Weapons ***/ function Weapon() { diff --git a/js/spaceinvaders-ui.js b/js/spaceinvaders-ui.js index e7f20fb..dc0f19a 100644 --- a/js/spaceinvaders-ui.js +++ b/js/spaceinvaders-ui.js @@ -46,15 +46,15 @@ function displayModal(data) { if( data.bonus ) { $("#modal_pane") - .addSprite("bonusLbl", {width: 180, height: 32, posx: 10, posy: data.end ? 90 : 50}) - .addSprite("bonusNbr", {width: 180, height: 32, posx: 200, posy: data.end ? 90 : 50}) .addSprite("totalLbl", {width: 180, height: 32, posx: 10, posy: data.end ? 130 : 90}) .addSprite("totalNbr", {width: 180, height: 32, posx: 200, posy: data.end ? 130 : 90}) + .addSprite("bonusLbl", {width: 180, height: 32, posx: 10, posy: data.end ? 90 : 50}) + .addSprite("bonusNbr", {width: 180, height: 32, posx: 200, posy: data.end ? 90 : 50}) ; + $("#totalLbl").append("Wave").lettering(); + $("#totalNbr").append(data.wave_score).lettering(); $("#bonusLbl").append("Bonus").lettering(); $("#bonusNbr").append(data.bonus).lettering(); - $("#totalLbl").append("Wave score").lettering(); - $("#totalNbr").append(data.wave_score).lettering(); } $("#modal_pane").addClass( "modal" );