From 045ae062419642388a05ed90d0c2c963834fc4a7 Mon Sep 17 00:00:00 2001 From: Fabrice Ecaille Date: Thu, 28 Nov 2013 18:23:24 +0100 Subject: [PATCH] Refactoring: better way to manage weapons --- index.html | 2 +- js/models/aliens.js | 32 ++-- js/models/models.js | 403 +-------------------------------------- js/models/weapons.js | 175 +++++++++-------- js/spaceinvaders-core.js | 2 +- 5 files changed, 107 insertions(+), 507 deletions(-) diff --git a/index.html b/index.html index 92ac6ce..a4417de 100644 --- a/index.html +++ b/index.html @@ -30,7 +30,7 @@ - + diff --git a/js/models/aliens.js b/js/models/aliens.js index 2ca23f6..76f3831 100644 --- a/js/models/aliens.js +++ b/js/models/aliens.js @@ -10,25 +10,25 @@ var ALIENS = { alien1 : { - health : 1, - weapon : AlienWeapon, - score : 5, - aggression : 0.0005, - animation : ALIENS_TYPE[0] + health : 1, + weapon : WEAPONS.ALIEN, + score : 5, + aggression : 0.0005, + animation : ALIENS_TYPE[0] }, alien2 : { - health : 1, - weapon : AlienWeapon, - score : 10, - aggression : 0.001, - animation : ALIENS_TYPE[1] + health : 1, + weapon : WEAPONS.ALIEN, + score : 10, + aggression : 0.001, + animation : ALIENS_TYPE[1] }, alien3 : { - health : 1, - weapon : AlienWeapon, - score : 20, - aggression : 0.0015, - animation : ALIENS_TYPE[2] + health : 1, + weapon : WEAPONS.ALIEN, + score : 20, + aggression : 0.0015, + animation : ALIENS_TYPE[2] } } @@ -42,7 +42,7 @@ function Alien(id, start, move, type) { this.y = start.y; this.moveFct = move; - this.weapon = new type.weapon(); + this.weapon = new Weapon(type.weapon); this.fireDirectionY = 1; this.originX = this.x; diff --git a/js/models/models.js b/js/models/models.js index d8ab6c8..ed2b9d3 100644 --- a/js/models/models.js +++ b/js/models/models.js @@ -24,355 +24,16 @@ function getAliensMidHeight() { WORLD.farm.bonus = [ { type: "weapon", - clazz: CarotWeapon, + clazz: WEAPONS.CAROT, animation: WORLD.farm.weapons.carot }, { type: "weapon", - clazz: CornWeapon, + clazz: WEAPONS.CORN, animation: WORLD.farm.weapons.corn } ]; -/*** 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); - } - }, - }, - half_part_rotation : { - init : function (x, y) { - return {directionX:0, directionY:0}; - }, - move : function () { - var _this = $(this)[0], - mid = PLAYGROUND_WIDTH / 2, - center = _this.center, - width = _this.width; - - if( this.directionX == 0 && this.directionY == 0 ) { - center = {x: ( this.getOriginX() < mid ? PLAYGROUND_WIDTH / 4 : 3 * PLAYGROUND_WIDTH / 4), y: getAliensMidHeight() }; - width = distance(center, {x: this.x, y: this.y}); - var xAxis = {x: width, y: 0}, - current = {x: center.x - this.getOriginX(), y: center.y - this.getOriginY()}, - alpha = angle( xAxis, current ); - this.directionX = 0.01; - this.directionY = alpha; - $(this)[0].center = center; - $(this)[0].width = width; - } - - if( this.getOriginX() < mid ) { - this.directionY = this.directionY + this.directionX; - } else { - this.directionY = this.directionY - this.directionX; - } - if( Math.abs(this.directionY) > 2 * Math.PI ) { - this.directionY = 0; - } - center.y = center.y + 0.1; - _this.center = center; - this.x = center.x + width * Math.cos(this.directionY); - this.y = center.y + width * Math.sin(this.directionY); - } - }, - - rotation : { - init : function (x, y) { - return {directionX:0, directionY:0}; - }, - move : function () { - var _this = $(this)[0], - mid = PLAYGROUND_WIDTH / 2, - center = _this.center, - width = _this.width; - - if( this.directionX == 0 && this.directionY == 0 ) { - center = {x: mid, y: getAliensMidHeight() }; - width = distance(center, {x: this.x, y: this.y}); - var xAxis = {x: width, y: 0}, - current = {x: center.x - this.getOriginX(), y: center.y - this.getOriginY()}, - alpha = angle( xAxis, current ); - this.directionX = 0.01; - this.directionY = alpha; - $(this)[0].center = center; - $(this)[0].width = width; - } - - this.directionY = this.directionY - this.directionX; - if( Math.abs(this.directionY) > 2 * Math.PI ) { - this.directionY = 0; - } - center.y = center.y + 0.1; - _this.center = center; - this.x = center.x + width * Math.cos(this.directionY); - this.y = center.y + width * Math.sin(this.directionY); - } - }, - - sinusoid : { - init : function (x, y) { - return {directionX : 1, directionY : 1}; - }, - move : function () { - var offset = this.width / 2; - if (Math.abs((this.getOriginX() - this.getX())) >= offset) { - this.directionX *= -1; - } - - if( Math.abs(this.getOriginY() - this.getY()) >= 3 * this.height ) { - this.directionY *= -1; - } - } - } -}; - -/*** Move - end ***/ - -var ALIENS = { - alien1 : { - health : 1, - weapon : AlienWeapon, - score : 5, - aggression : 0.0005, - animation : ALIENS_TYPE[0] - }, - alien2 : { - health : 1, - weapon : AlienWeapon, - score : 10, - aggression : 0.001, - animation : ALIENS_TYPE[1] - }, - alien3 : { - health : 1, - weapon : AlienWeapon, - score : 20, - aggression : 0.0015, - animation : ALIENS_TYPE[2] - } -} - - -/*** Waves ***/ - -var WAVES = [ - { - wave : [ - [ ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1 ], - [ ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1 ], - [ ALIENS.alien3, ALIENS.alien3, ALIENS.alien3, ALIENS.alien3, ALIENS.alien3, ALIENS.alien3, ALIENS.alien3, ALIENS.alien3, ALIENS.alien3, ALIENS.alien3, ALIENS.alien3 ], - [ ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1 ], - [ ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1, ALIENS.alien1 ] - ], - move : MOVE.translation, - 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] - }, - { - wave : [ [ undefined, undefined, Alien, undefined, undefined, undefined, undefined, undefined, Alien, undefined, undefined ], - [ undefined, Alien, Alien, Alien, undefined, undefined, undefined, Alien, Alien, Alien, undefined ], - [ Alien, Alien, undefined, Alien, Alien, undefined, Alien, Alien, undefined, Alien, Alien ], - [ undefined, Alien, Alien, Alien, Alien, undefined, undefined, Alien, Alien, Alien, undefined ], - [ undefined, undefined, Alien, undefined, undefined, undefined, undefined, undefined, Alien, undefined, undefined ] - ], - move : MOVE.half_part_rotation, - bonus : [20, 10] - }, - { - wave : [ - [ undefined, undefined, undefined, undefined, Alien, undefined, undefined, undefined, undefined ], - [ undefined, undefined, undefined, Alien, Alien, Alien, undefined, undefined, undefined ], - [ undefined, Alien, Alien, Alien, undefined, Alien, Alien, Alien, undefined ], - [ undefined, Alien, Alien, Alien, undefined, Alien, Alien, Alien, undefined ], - [ Alien, Alien, Alien, undefined, undefined, undefined, Alien, Alien, Alien ], - [ undefined, Alien, Alien, Alien, undefined, Alien, Alien, Alien, undefined ], - [ undefined, Alien, Alien, Alien, undefined, Alien, Alien, Alien, undefined ], - [ undefined, undefined, undefined, Alien, Alien, Alien, undefined, undefined, undefined ], - [ undefined, undefined, undefined, undefined, Alien, undefined, undefined, undefined, undefined ] - ], - move : MOVE.rotation, - bonus : [25, 12] - } - ]; - - -/*** Waves - end ***/ - - -/*** Weapons ***/ - -function Weapon() { - "use strict"; -} -Weapon.prototype = { - speed : 5, - strength : 10, - stock: Infinity, - rof : 300, - ror : 1500, - load : 1, - max_load : 1, - width : 5, - height : 5, - shot_timer : false, - reload_timer : false, - directionX : 0, - directionY : 1, - animation : null, - clazz : "default", - callback : undefined, - - fire : function() { - if (this.shot_timer || this.load <= 0) { - return false; - } - - var _this = this; - this.load = Math.max(0,this.load - 1); - this.shot_timer = setInterval(function() { - if (_this.load > 0) { - clearTimeout(_this.shot_timer); - _this.shot_timer = false; - } - }, this.rof); - - if( !this.reload_timer) { - this.reload_timer = setInterval( function() { - _this.load = Math.min(_this.load + 1, Math.min(_this.max_load, _this.stock)); - if( _this.load == _this.max_load ) { - clearInterval(_this.reload_timer); - _this.reload_timer = false; - } - }, this.ror); - } - return true; - } - -} - -function ShotgunWeapon() { - "use strict"; - this.directionY = -1; - this.rof = 200; - this.ror = 1500; - this.load = 2; - this.max_load = 2; - this.width = 3; - this.height = 3; - this.clazz = "Shotgun" -} -ShotgunWeapon.prototype = { -} -heriter(ShotgunWeapon.prototype, Weapon.prototype); - -function CarotWeapon() { - "use strict"; - this.directionY = -1; - this.stock = 10; - this.clazz = "carot"; - this.load = 5; - this.max_load = 5; - this.width = 5; - this.height = 10; -} -CarotWeapon.prototype = { - -} -heriter(CarotWeapon.prototype, Weapon.prototype); - -function CornWeapon() { - "use strict"; - this.directionY = -1; - this.stock = 3; - this.clazz = "corn"; - this.load = 1; - this.max_load = 1; - this.callback = function(shot) { - var mediumAlien = getAliensMidHeight(); - - if( shot.y() < mediumAlien ) { - var x = shot.x(), - y = shot.y(), - explosion = EXPLOSIONS.SMALL[0]; - $("#shipShots").addSprite("cornExplosion", {width: explosion.width, height: explosion.height, posx: x - explosion.width / 2, posy: y - explosion.height / 2}); - explosionSmall($("#cornExplosion"), function() {$("#cornExplosion").remove()}); - shot.remove(); - - var shipShots = $("#shipShots"); - for( var i = 0; i < 8; i++) { - var cos = Math.cos( (Math.PI / 4) * i ), - sin = Math.sin( (Math.PI / 4) * i); - if( Math.abs(cos) < 0.01 ) { - cos = 0; - } - if( Math.abs(sin) < 0.01) { - sin = 0; - } - shipShots.addSprite( "shotCorn" + i, { posx : x + 5 * cos, posy : y + 5 * sin, width: 2, height: 2}); - var shotCorn = $("#shotCorn" + i); - shotCorn.addClass("shipShot").addClass("shotCorn"); - $("#shotCorn" + i)[0].weapon = $.extend({ - directionX : cos < 0 ? -1 : cos > 0 ? 1 : 0, - directionY : sin < 0 ? -1 : sin > 0 ? 1 : 0, - speed : 1 - }, shot.weapon); - } - } - } -} -CornWeapon.prototype = { - -} -heriter(CornWeapon.prototype, Weapon.prototype); - - -function AlienWeapon() { - "use strict"; - this.directionY = 1; - this.width = 5; - this.height = 10; -} -AlienWeapon.prototype = { - -} -heriter(AlienWeapon.prototype, Weapon.prototype); -/*** Weapons -END ***/ - - /*** Actors ***/ function Actor() { "use strict"; @@ -469,62 +130,6 @@ Actor.prototype = { } }; -/*** Actors - Aliens ***/ -function Alien(id, start, move, type) { - "use strict"; - - this.id = id; - this.x = start.x; - this.y = start.y; - this.moveFct = move; - - this.weapon = new type.weapon(); - this.fireDirectionY = 1; - - this.originX = this.x; - this.originY = this.y; - this.directionX = -1; - this.speed = 0.5; - this.animation = type.animation; - this.width = type.animation.width; - this.height = type.animation.height; - this.health = type.health; - this.aggression = type.aggression; - this.score = type.score; -} - -Alien.prototype = { - moveFct : null, - width : 0, - height : 0, - aggression : 0, - animation : null, - score : 0, - - init : function() { - "use strict"; - this.speed = 0; - this.node.x(this.x); - this.node.y(this.y); - }, - - move : function() { - "use strict"; - this._super("move", arguments); - if (typeof this.moveFct !== undefined) { - this.moveFct(); - } - }, - - destroy : function() { - this._super("destroy", arguments); - Game.addToScore( this.score ); - } -}; - -heriter(Alien.prototype, Actor.prototype); - -/*** Actors - Aliens - END ***/ /*** Actors - Heroes - END ***/ function Ship(id, start, speed, animation) { @@ -533,7 +138,7 @@ function Ship(id, start, speed, animation) { this.x = start.x; this.y = start.y; - this.weapon = new ShotgunWeapon(); + this.weapon = new Weapon(WEAPONS.SHOTGUN); this.fireDirectionY = -1; this.originX = this.x; @@ -615,7 +220,7 @@ Ship.prototype = { Game.shots.total = Game.shots.total + 1; this.weapon.stock--; if( this.weapon.stock == 0 ) { - this.weapon = new ShotgunWeapon(); + this.weapon = new Weapon(WEAPONS.SHOTGUN); $("#current_weapon").setAnimation(this.weapon.animation); } } diff --git a/js/models/weapons.js b/js/models/weapons.js index f634bf0..1460b95 100644 --- a/js/models/weapons.js +++ b/js/models/weapons.js @@ -8,9 +8,91 @@ * 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. */ -function Weapon() { - "use strict"; +var WEAPONS = { + SHOTGUN : { + directionY : -1, + rof : 200, + ror : 1500, + load : 2, + max_load : 2, + width : 3, + height : 3, + clazz : "Shotgun" + }, + CAROT : { + directionY : -1, + stock : 10, + clazz : "carot", + load : 5, + max_load : 5, + width : 5, + height : 10 + }, + CORN : { + directionY : -1, + stock : 3, + clazz : "corn", + load : 1, + max_load : 1, + callback : function(shot) { + var mediumAlien = getAliensMidHeight(); + + if( shot.y() < mediumAlien ) { + var x = shot.x(), + y = shot.y(), + explosion = EXPLOSIONS.SMALL[0]; + $("#shipShots").addSprite("cornExplosion", {width: explosion.width, height: explosion.height, posx: x - explosion.width / 2, posy: y - explosion.height / 2}); + explosionSmall($("#cornExplosion"), function() {$("#cornExplosion").remove()}); + shot.remove(); + + var shipShots = $("#shipShots"); + for( var i = 0; i < 8; i++) { + var cos = Math.cos( (Math.PI / 4) * i ), + sin = Math.sin( (Math.PI / 4) * i); + if( Math.abs(cos) < 0.01 ) { + cos = 0; + } + if( Math.abs(sin) < 0.01) { + sin = 0; + } + shipShots.addSprite( "shotCorn" + i, { posx : x + 5 * cos, posy : y + 5 * sin, width: 2, height: 2}); + var shotCorn = $("#shotCorn" + i); + shotCorn.addClass("shipShot").addClass("shotCorn"); + $("#shotCorn" + i)[0].weapon = $.extend({ + directionX : cos < 0 ? -1 : cos > 0 ? 1 : 0, + directionY : sin < 0 ? -1 : sin > 0 ? 1 : 0, + speed : 1 + }, shot.weapon); + } + } + } + }, + ALIEN : { + directionY : 1, + width : 5, + height : 10 + } } + +function Weapon(type) { + "use strict"; + this.directionY = type.directionY || 1; + this.directionX = type.directionX || 0; + this.width = type.width; + this.height = type.height; + this.speed = type.speed || 5; + this.strenght = type.strength || 10; + this.stock = type.stock || Infinity; + this.rof = type.rof || 300; + this.ror = type.ror || 1500; + this.load = type.load || 1; + this.max_load = type.max_load || 1; + this.animation = type.animation; + this.clazz = type.clazz || "default"; + this.callback = type.callback; + +} + Weapon.prototype = { speed : 5, strength : 10, @@ -55,91 +137,4 @@ Weapon.prototype = { return true; } -} - -function ShotgunWeapon() { - "use strict"; - this.directionY = -1; - this.rof = 200; - this.ror = 1500; - this.load = 2; - this.max_load = 2; - this.width = 3; - this.height = 3; - this.clazz = "Shotgun" -} -ShotgunWeapon.prototype = { -} -heriter(ShotgunWeapon.prototype, Weapon.prototype); - -function CarotWeapon() { - "use strict"; - this.directionY = -1; - this.stock = 10; - this.clazz = "carot"; - this.load = 5; - this.max_load = 5; - this.width = 5; - this.height = 10; -} -CarotWeapon.prototype = { - -} -heriter(CarotWeapon.prototype, Weapon.prototype); - -function CornWeapon() { - "use strict"; - this.directionY = -1; - this.stock = 3; - this.clazz = "corn"; - this.load = 1; - this.max_load = 1; - this.callback = function(shot) { - var mediumAlien = getAliensMidHeight(); - - if( shot.y() < mediumAlien ) { - var x = shot.x(), - y = shot.y(), - explosion = EXPLOSIONS.SMALL[0]; - $("#shipShots").addSprite("cornExplosion", {width: explosion.width, height: explosion.height, posx: x - explosion.width / 2, posy: y - explosion.height / 2}); - explosionSmall($("#cornExplosion"), function() {$("#cornExplosion").remove()}); - shot.remove(); - - var shipShots = $("#shipShots"); - for( var i = 0; i < 8; i++) { - var cos = Math.cos( (Math.PI / 4) * i ), - sin = Math.sin( (Math.PI / 4) * i); - if( Math.abs(cos) < 0.01 ) { - cos = 0; - } - if( Math.abs(sin) < 0.01) { - sin = 0; - } - shipShots.addSprite( "shotCorn" + i, { posx : x + 5 * cos, posy : y + 5 * sin, width: 2, height: 2}); - var shotCorn = $("#shotCorn" + i); - shotCorn.addClass("shipShot").addClass("shotCorn"); - $("#shotCorn" + i)[0].weapon = $.extend({ - directionX : cos < 0 ? -1 : cos > 0 ? 1 : 0, - directionY : sin < 0 ? -1 : sin > 0 ? 1 : 0, - speed : 1 - }, shot.weapon); - } - } - } -} -CornWeapon.prototype = { - -} -heriter(CornWeapon.prototype, Weapon.prototype); - - -function AlienWeapon() { - "use strict"; - this.directionY = 1; - this.width = 5; - this.height = 10; -} -AlienWeapon.prototype = { - -} -heriter(AlienWeapon.prototype, Weapon.prototype); +} \ No newline at end of file diff --git a/js/spaceinvaders-core.js b/js/spaceinvaders-core.js index 669245f..7190972 100644 --- a/js/spaceinvaders-core.js +++ b/js/spaceinvaders-core.js @@ -256,7 +256,7 @@ Game = { var bonus = $(this)[0].bonus; if( collisions.length > 0 ) { if( bonus.type === "weapon" ) { - Game.ship.weapon = new bonus.clazz; + Game.ship.weapon = new Weapon(bonus.clazz); $("#current_weapon").setAnimation(bonus.animation.animation); this.remove(); }